01: package com.eviware.soapui.support.swing;
02:
03: import java.awt.Component;
04: import java.awt.Insets;
05: import java.awt.Point;
06: import java.awt.Rectangle;
07: import java.awt.dnd.Autoscroll;
08:
09: import javax.swing.JViewport;
10: import javax.swing.SwingUtilities;
11:
12: public class AutoscrollSupport implements Autoscroll {
13: private static final int AUTOSCROLL_MARGIN = 12;
14:
15: Component comp;
16: Insets insets;
17: Insets scrollUnits;
18:
19: public AutoscrollSupport(Component comp, Insets insets) {
20: this (comp, insets, insets);
21: }
22:
23: public AutoscrollSupport(Component comp, Insets insets,
24: Insets scrollUnits) {
25: this .comp = comp;
26: this .insets = insets;
27: this .scrollUnits = scrollUnits;
28: }
29:
30: public AutoscrollSupport(Component comp) {
31: this (comp, new Insets(AUTOSCROLL_MARGIN, AUTOSCROLL_MARGIN,
32: AUTOSCROLL_MARGIN, AUTOSCROLL_MARGIN));
33: }
34:
35: public void autoscroll(Point cursorLoc) {
36: JViewport viewport = getViewport();
37: if (viewport == null)
38: return;
39: Point viewPos = viewport.getViewPosition();
40: int viewHeight = viewport.getExtentSize().height;
41: int viewWidth = viewport.getExtentSize().width;
42:
43: // perform scrolling
44: if ((cursorLoc.y - viewPos.y) < insets.top) { // scroll up
45: viewport.setViewPosition(new Point(viewPos.x, Math.max(
46: viewPos.y - scrollUnits.top, 0)));
47: } else if ((viewPos.y + viewHeight - cursorLoc.y) < insets.bottom) { // scroll down
48: viewport.setViewPosition(new Point(viewPos.x, Math.min(
49: viewPos.y + scrollUnits.bottom, comp.getHeight()
50: - viewHeight)));
51: } else if ((cursorLoc.x - viewPos.x) < insets.left) { // scroll left
52: viewport.setViewPosition(new Point(Math.max(viewPos.x
53: - scrollUnits.left, 0), viewPos.y));
54: } else if ((viewPos.x + viewWidth - cursorLoc.x) < insets.right) { // scroll right
55: viewport.setViewPosition(new Point(Math.min(viewPos.x
56: + scrollUnits.right, comp.getWidth() - viewWidth),
57: viewPos.y));
58: }
59: }
60:
61: public Insets getAutoscrollInsets() {
62: Rectangle raOuter = comp.getBounds();
63: Rectangle raInner = comp.getParent().getBounds();
64: return new Insets(raInner.y - raOuter.y + AUTOSCROLL_MARGIN,
65: raInner.x - raOuter.x + comp.getWidth(), raOuter.height
66: - raInner.height - raInner.y + raOuter.y
67: + AUTOSCROLL_MARGIN, raOuter.width
68: - raInner.width - raInner.x + raOuter.x
69: + AUTOSCROLL_MARGIN);
70: }
71:
72: JViewport getViewport() {
73: return (JViewport) SwingUtilities.getAncestorOfClass(
74: JViewport.class, comp);
75: }
76: }
|