[cp-patches] FYI: Some JViewport fixes

2005-10-18 Thread Roman Kennke
Hi,

This fixes some issues with JViewport.

2005-10-18  Roman Kennke  [EMAIL PROTECTED]

* javax/swing/JViewport.java
(setView): Added repaint() call.
(revalidate): Removed unnecessary and unspecified method.
(reshape): Fire stateChanged if only the size changes. Do not fire
stateChanged if only the location changes.
(scrollRectToVisible): Reworked most of this method to correctly
determine the scroll offsets. Added validation of view as
specified.

/Roman
Index: javax/swing/JViewport.java
===
RCS file: /cvsroot/classpath/classpath/javax/swing/JViewport.java,v
retrieving revision 1.32
diff -u -r1.32 JViewport.java
--- javax/swing/JViewport.java	17 Oct 2005 19:08:22 -	1.32
+++ javax/swing/JViewport.java	18 Oct 2005 10:02:28 -
@@ -411,35 +411,24 @@
 fireStateChanged();
   }
 revalidate();
-  }
-
-  public void revalidate()
-  {
-damaged = true;
-fireStateChanged();
-super.revalidate();
+repaint();
   }
 
   public void reshape(int x, int y, int w, int h)
   {
-boolean changed = 
-  (x != getX()) 
-  || (y != getY()) 
-  || (w != getWidth())
-  || (h != getHeight());
 if (w != getWidth() || h != getHeight())
   sizeChanged = true;
 super.reshape(x, y, w, h);
-if (changed)
+if (sizeChanged)
   {
 damaged = true;
 fireStateChanged();
   }
   }
-
-  public final Insets getInsets() 
+
+  public final Insets getInsets()
   {
-return new Insets(0,0,0,0);
+return new Insets(0, 0, 0, 0);
   }
 
   public final Insets getInsets(Insets insets)
@@ -574,36 +563,26 @@
 Rectangle viewBounds = getView().getBounds();
 Rectangle portBounds = getBounds();
 
-// FIXME: should validate the view if it is not valid, however
-// this may cause excessive validation when the containment
-// hierarchy is being created.
-
-// Y-DIRECTION
-
-// if contentRect is larger than the portBounds, center the view
-if (contentRect.height  portBounds.height)
-  setViewPosition(new Point(pos.x, contentRect.y));
-
-if (contentRect.y  -viewBounds.y)
-  setViewPosition(new Point(pos.x, contentRect.y));
-else if (contentRect.y + contentRect.height  
- -viewBounds.y + portBounds.height) 
-  setViewPosition (new Point(pos.x, contentRect.y - 
- (portBounds.height - contentRect.height)));
-
-// X-DIRECTION
-pos = getViewPosition();
-
-//  if contentRect is larger than the portBounds, center the view
-if (contentRect.width portBounds.width)
-setViewPosition(new Point(contentRect.x, pos.y));
-
-if (contentRect.x  -viewBounds.x)
-  setViewPosition(new Point(contentRect.x, pos.y));
-else if (contentRect.x + contentRect.width  
- -viewBounds.x + portBounds.width)
-  setViewPosition (new Point(contentRect.x - 
- (portBounds.width - contentRect.width), pos.y));
+if (isShowing())
+  getView().validate();
+
+// If the bottom boundary of contentRect is below the port
+// boundaries, scroll up as necessary.
+if (contentRect.y + contentRect.height + viewBounds.y  portBounds.height)
+  pos.y = contentRect.y + contentRect.height - viewBounds.height;
+// If contentRect.y is above the port boundaries, scroll down to
+// contentRect.y.
+if (contentRect.y + viewBounds.y  0)
+  pos.y = contentRect.y;
+// If the right boundary of contentRect is right from the port
+// boundaries, scroll left as necessary.
+if (contentRect.x + contentRect.width + viewBounds.x  portBounds.width)
+  pos.x = contentRect.x + contentRect.width - viewBounds.width;
+// If contentRect.x is left from the port boundaries, scroll right to
+// contentRect.x.
+if (contentRect.x + viewBounds.x  0)
+  pos.x = contentRect.x;
+setViewPosition(pos);
   }
 
   /**
@@ -617,6 +596,25 @@
 if (accessibleContext == null)
   accessibleContext = new AccessibleJViewport();
 return accessibleContext;
+  }
+
+  /**
+   * Forward repaint to parent to make sure only one paint is performed by the
+   * RepaintManager.
+   *
+   * @param tm number of milliseconds to defer the repaint request
+   * @param x the X coordinate of the upper left corner of the dirty area
+   * @param y the Y coordinate of the upper left corner of the dirty area
+   * @param w the width of the dirty area
+   * @param h the height of the dirty area
+   */
+  public void repaint(long tm, int x, int y, int w, int h)
+  {
+Component parent = getParent();
+if (parent != null)
+  {
+parent.repaint(tm, x + getX(), y + getY(), w, h);
+  }
   }
 
   protected void addImpl(Component comp, Object constraints, int index)
___
Classpath-patches mailing list

[cp-patches] FYI: Some JViewport fixes

2005-10-12 Thread Roman Kennke
I investigated the JViewport stuff a little more and fixed the following
issues, which should bring JViewport a little closer to the reference
impl's behaviour:

2005-10-12  Roman Kennke  [EMAIL PROTECTED]

* javax/swing/JViewport.java
(ViewListener.componentResized): Only call revalidate instead of
going through weird reverting code.
(JViewport): First call updateUI, then set layout.
(setViewPosition): Don't do anything if there is no real
change.
(setView): Don't remove the old component. This is handled by
addImpl. Call revalidate().
* javax/swing/ViewportLayout.java
(layoutContainer): Preferably set view size to it's preferredSize
instead of its minimumSize.

/Roman
Index: javax/swing/JViewport.java
===
RCS file: /cvsroot/classpath/classpath/javax/swing/JViewport.java,v
retrieving revision 1.28
diff -u -r1.28 JViewport.java
--- javax/swing/JViewport.java	22 Sep 2005 14:35:30 -	1.28
+++ javax/swing/JViewport.java	12 Oct 2005 12:30:59 -
@@ -130,12 +130,10 @@
 
   /**
* A [EMAIL PROTECTED] java.awt.event.ComponentListener} that listens for
-   * changes of the view's size. This class forbids changes of the view
-   * component's size that would exceed the viewport's size.
+   * changes of the view's size. This triggers a revalidate() call on the
+   * viewport.
*/
-  protected class ViewListener
-extends ComponentAdapter
-implements Serializable
+  protected class ViewListener extends ComponentAdapter implements Serializable
   {
 private static final long serialVersionUID = -2812489404285958070L;
 
@@ -148,37 +146,14 @@
 
 /**
  * Receives notification when a component (in this case: the view
- * component) changes it's size.
+ * component) changes it's size. This simply triggers a revalidate() on the
+ * viewport.
  *
  * @param ev the ComponentEvent describing the change
  */
 public void componentResized(ComponentEvent ev)
 {
-  // According to some tests that I did with Sun's implementation
-  // this class is supposed to make sure that the view component
-  // is not resized to a larger size than the viewport.
-  // This is not documented anywhere. What I did is: I subclassed JViewport
-  // and ViewListener and 'disabled' the componentResized method by
-  // overriding it and not calling super.componentResized().
-  // When this method is disabled I can set the size on the view component
-  // normally, when it is enabled, it gets immediatly resized back,
-  // after a resize attempt that would exceed the Viewport's size.
-  Component comp = ev.getComponent();
-  Dimension newSize = comp.getSize();
-  Dimension viewportSize = getSize();
-  boolean revert = false;
-  if (newSize.width  viewportSize.width)
-{
-  newSize.width = viewportSize.width;
-  revert = true;
-}
-  if (newSize.height  viewportSize.height)
-{
-  newSize.height = viewportSize.height;
-  revert = true;
-}
-  if (revert == true)
-comp.setSize(newSize);
+  revalidate();
 }
   }
 
@@ -264,8 +239,8 @@
   {
 setOpaque(true);
 setScrollMode(BLIT_SCROLL_MODE);
-setLayout(createLayoutManager());
 updateUI();
+setLayout(createLayoutManager());
 lastPaintPosition = new Point();
 cachedBlitFrom = new Point();
 cachedBlitTo = new Point();
@@ -356,6 +331,8 @@
 
   public void setViewPosition(Point p)
   {
+if (getViewPosition().equals(p))
+  return;
 Component view = getView();
 if (view != null)
   {
@@ -418,7 +395,7 @@
   {
 if (viewListener != null)
   getView().removeComponentListener(viewListener);
-remove(0);
+//remove(0);
   }
 
 if (v != null)
@@ -429,6 +406,7 @@
 add(v);
 fireStateChanged();
   }
+revalidate();
   }
 
   public void revalidate()
Index: javax/swing/ViewportLayout.java
===
RCS file: /cvsroot/classpath/classpath/javax/swing/ViewportLayout.java,v
retrieving revision 1.15
diff -u -r1.15 ViewportLayout.java
--- javax/swing/ViewportLayout.java	13 Sep 2005 09:17:21 -	1.15
+++ javax/swing/ViewportLayout.java	12 Oct 2005 12:30:59 -
@@ -142,7 +142,7 @@
  portBounds.y + portBounds.height);
 
 // vertical implementation of the above rules
-if (portBounds.height = viewMinimum.height)
+if (portBounds.height = viewPref.height)
   {
 portBounds.y = 0;
 if ( !(view instanceof Scrollable) || ((Scrollable)view).getScrollableTracksViewportHeight())
@@ -150,14 +150,13 @@
   }
 else
   {
-viewPref.height = viewMinimum.height;
 int overextension = portLowerRight.y - viewPref.height;