/Users/lyon/j4p/src/gui/tree/AutoScrollingEditorPane.java

1    package gui.tree; 
2     
3    import javax.swing.*; 
4    import java.awt.*; 
5    import java.awt.dnd.Autoscroll; 
6     
7    public class AutoScrollingEditorPane extends JEditorPane implements Autoscroll { 
8        public static final Insets defaultScrollInsets = new Insets(8, 8, 8, 8); 
9        protected Insets scrollInsets = defaultScrollInsets; 
10    
11       public AutoScrollingEditorPane() { 
12       } 
13    
14       public void setScrollInsets(Insets insets) { 
15           this.scrollInsets = insets; 
16       } 
17    
18       public Insets getScrollInsets() { 
19           return scrollInsets; 
20       } 
21    
22       // Implementation of Autoscroll interface 
23       public Insets getAutoscrollInsets() { 
24           Rectangle r = getVisibleRect(); 
25           Dimension size = getSize(); 
26           Insets i = new Insets(r.y + scrollInsets.top, r.x + scrollInsets.left, 
27                   size.height - r.y - r.height + scrollInsets.bottom, 
28                   size.width - r.x - r.width + scrollInsets.right); 
29           return i; 
30       } 
31    
32       public void autoscroll(Point location) { 
33           JScrollPane scroller = 
34                   (JScrollPane) SwingUtilities.getAncestorOfClass(JScrollPane.class, this); 
35           if (scroller != null) { 
36               JScrollBar hBar = scroller.getHorizontalScrollBar(); 
37               JScrollBar vBar = scroller.getVerticalScrollBar(); 
38               Rectangle r = getVisibleRect(); 
39               if (location.x <= r.x + scrollInsets.left) { 
40                   // Need to scroll left 
41                   hBar.setValue(hBar.getValue() - hBar.getUnitIncrement(-1)); 
42               } 
43               if (location.y <= r.y + scrollInsets.top) { 
44                   // Need to scroll up 
45                   vBar.setValue(vBar.getValue() - vBar.getUnitIncrement(-1)); 
46               } 
47               if (location.x >= r.x + r.width - scrollInsets.right) { 
48                   // Need to scroll right 
49                   hBar.setValue(hBar.getValue() + hBar.getUnitIncrement(1)); 
50               } 
51               if (location.y >= r.y + r.height - scrollInsets.bottom) { 
52                   // Need to scroll down 
53                   vBar.setValue(vBar.getValue() + vBar.getUnitIncrement(1)); 
54               } 
55           } 
56       } 
57   } 
58