JComponent's scrollRectToVisible method simply passes the call up to the
Component's parent.  Components that can handle the call should override
scrollRectToVisible.  This patch implements the method for JViewport.

Patch is pending approval.  Patch is attached.


2005-07-27  Anthony Balkissoon  <[EMAIL PROTECTED]>

* javax/swing/JViewport.java:
(scrollRectToVisible): New method, overrides JComponent method as 
intended.


- Tony
Index: javax/swing/JViewport.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/JViewport.java,v
retrieving revision 1.22
diff -u -r1.22 JViewport.java
--- javax/swing/JViewport.java	23 Jul 2005 19:47:14 -0000	1.22
+++ javax/swing/JViewport.java	27 Jul 2005 19:25:39 -0000
@@ -487,4 +487,45 @@
   {
     return new ViewportLayout();
   }
+
+  /**
+   * Scrolls the view so that contentRect becomes visible.
+   *
+   * @param contentRect the rectangle to make visible within the view
+   */
+  public void scrollRectToVisible(Rectangle contentRect)
+  {
+    Point pos = getViewPosition();
+    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.
+
+    // if contentRect is larger than the portBounds, center the view
+    if (contentRect.height > portBounds.height || 
+        contentRect.width > portBounds.width)
+      {
+        setViewPosition(new Point(contentRect.x, contentRect.y));
+        return;
+      }
+
+    // Y-DIRECTION
+    if (contentRect.y + viewBounds.y < portBounds.y)
+      setViewPosition(new Point(pos.x, contentRect.y));
+    else if (contentRect.y + viewBounds.y + contentRect.height > 
+             (portBounds.y+portBounds.height))
+      setViewPosition (new Point(pos.x, contentRect.y - 
+                                 (portBounds.height - contentRect.height)));
+
+    // X-DIRECTION
+    pos = getViewPosition();
+    if (contentRect.x + viewBounds.x < portBounds.x)
+      setViewPosition(new Point(contentRect.x, pos.y));
+    else if (contentRect.x + viewBounds.x + contentRect.width > 
+             (portBounds.x + portBounds.height))
+      setViewPosition (new Point(contentRect.x - 
+                                 (portBounds.width - contentRect.width), pos.y));
+  }
 }
_______________________________________________
Classpath-patches mailing list
[email protected]
http://lists.gnu.org/mailman/listinfo/classpath-patches

Reply via email to