summaryrefslogtreecommitdiffstats
path: root/libjava/classpath/javax/swing/JViewport.java
diff options
context:
space:
mode:
Diffstat (limited to 'libjava/classpath/javax/swing/JViewport.java')
-rw-r--r--libjava/classpath/javax/swing/JViewport.java43
1 files changed, 42 insertions, 1 deletions
diff --git a/libjava/classpath/javax/swing/JViewport.java b/libjava/classpath/javax/swing/JViewport.java
index 397cb31acdf..d750bad29cc 100644
--- a/libjava/classpath/javax/swing/JViewport.java
+++ b/libjava/classpath/javax/swing/JViewport.java
@@ -89,7 +89,7 @@ import javax.swing.plaf.ViewportUI;
*
* <p>But in terms of drawing its child, the viewport thinks of itself as
* covering a particular position <em>of the view's coordinate space</em>.
- * For example, the {@link javax.JViewPort.getViewPosition} method returns
+ * For example, the {@link #getViewPosition} method returns
* the position <code>(VX,VY)</code> shown above, which is an position in
* "view space", even though this is <em>implemented</em> by positioning
* the underlying child at position <code>(-VX,-VY)</code></p>
@@ -487,4 +487,45 @@ public class JViewport extends JComponent
{
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)
+ 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.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));
+ }
}
OpenPOWER on IntegriCloud