/*
* "limit" gives the row and column of the view component that must
* appear just inside the JViewport after scrolling.
*/
Point limit = e_.getLimit();
/*
* Determine the value of "limit" relative to the top left corner of
* the JScrollPane.
*/
Point viewportLocation = getViewport().getLocation();
limit.translate(viewportLocation.x, viewportLocation.y);
limit.translate(scrollable.getLocation());
/*
* Get the bounding rectangle of the child viewport, relative to the
* top left corner of the JScrollPane.
*/
Rectangle viewport = _childViewport.getBounds();
//Dimension viewportExtent = _childViewport.getExtentSize();
Point viewPosition = _childViewport.getViewPosition();
Point headerPosition = null;
if (_headerViewport != null)
headerPosition = _headerViewport.getViewPosition();
/*
* If the limit is inside the viewport, the component doesn't need to
* be scrolled. First do the left/right scrolling.
*/
if (limit.x > viewport.getRight()) {
if ((direction == ScrollEvent.LEFT
|| direction == ScrollEvent.UP_LEFT || direction == ScrollEvent.DOWN_LEFT)) {
viewPosition.x -= (limit.x - viewport.getRight());
if (_headerViewport != null)
headerPosition.x -= (limit.x - viewport.getRight());
} else if (direction == ScrollEvent.RIGHT
|| direction == ScrollEvent.UP_RIGHT
|| direction == ScrollEvent.DOWN_RIGHT) {
viewPosition.x += (viewport.getLeft() - limit.x);
if (_headerViewport != null)
headerPosition.x += (viewport.getLeft() - limit.x);
}
} else if (limit.x < viewport.getLeft()) {
if (direction == ScrollEvent.RIGHT
|| direction == ScrollEvent.UP_RIGHT
|| direction == ScrollEvent.DOWN_RIGHT) {
viewPosition.x += (viewport.getLeft() - limit.x);
if (_headerViewport != null)
headerPosition.x += (viewport.getLeft() - limit.x);
} else if (direction == ScrollEvent.LEFT
|| direction == ScrollEvent.UP_LEFT
|| direction == ScrollEvent.DOWN_LEFT) {
viewPosition.x -= (limit.x - viewport.getRight());
if (_headerViewport != null)
headerPosition.x -= (limit.x - viewport.getRight());
}
}
// Now do the up/down scrolling
if (limit.y < viewport.getTop()
&& (direction == ScrollEvent.DOWN
|| direction == ScrollEvent.DOWN_LEFT || direction == ScrollEvent.DOWN_RIGHT)) {
viewPosition.y += (viewport.getTop() - limit.y);
} else if (limit.y > viewport.getBottom()
&& (direction == ScrollEvent.UP
|| direction == ScrollEvent.UP_LEFT || direction == ScrollEvent.UP_RIGHT)) {
viewPosition.y -= (limit.y - viewport.getBottom());
}
_childViewport.setViewPosition(viewPosition);
if (_headerViewport != null)
_headerViewport.setViewPosition(headerPosition);
draw(Toolkit.getDefaultToolkit());
/*
* Ensure the cursor is within the viewport (if the component contained
* within the viewport is offset a long way to the left, the cursor
* position can get scrambled).
*/
Toolkit toolkit = Toolkit.getDefaultToolkit();
Point cursor = toolkit.getCursor();
Point viewportOrigin = _childViewport.getLocationOnScreen();
if (cursor.x < viewportOrigin.x || cursor.y < viewportOrigin.y) {
if (cursor.x < viewportOrigin.x) cursor.x = viewportOrigin.x;
if (cursor.y < viewportOrigin.y) cursor.y = viewportOrigin.y;
toolkit.setCursor(cursor);
}