1 7 8 package javax.swing; 9 10 import java.awt.*; 11 import java.awt.event.*; 12 13 23 class Autoscroller implements ActionListener { 24 27 private static Autoscroller sharedInstance = new Autoscroller (); 28 29 private static MouseEvent event; 33 private static Timer timer; 34 private static JComponent component; 35 36 42 public static void stop(JComponent c) { 43 sharedInstance._stop(c); 44 } 45 46 49 public static boolean isRunning(JComponent c) { 50 return sharedInstance._isRunning(c); 51 } 52 53 57 public static void processMouseDragged(MouseEvent e) { 58 sharedInstance._processMouseDragged(e); 59 } 60 61 62 Autoscroller() { 63 } 64 65 68 private void start(JComponent c, MouseEvent e) { 69 Point screenLocation = c.getLocationOnScreen(); 70 71 if (component != c) { 72 _stop(component); 73 } 74 component = c; 75 event = new MouseEvent(component, e.getID(), e.getWhen(), 76 e.getModifiers(), e.getX() + screenLocation.x, 77 e.getY() + screenLocation.y, 78 e.getClickCount(), e.isPopupTrigger()); 79 80 if (timer == null) { 81 timer = new Timer (100, this); 82 } 83 84 if (!timer.isRunning()) { 85 timer.start(); 86 } 87 } 88 89 93 96 private void _stop(JComponent c) { 97 if (component == c) { 98 if (timer != null) { 99 timer.stop(); 100 } 101 timer = null; 102 event = null; 103 component = null; 104 } 105 } 106 107 111 private boolean _isRunning(JComponent c) { 112 return (c == component && timer != null && timer.isRunning()); 113 } 114 115 118 private void _processMouseDragged(MouseEvent e) { 119 JComponent component = (JComponent )e.getComponent(); 120 Rectangle visibleRect = component.getVisibleRect(); 121 boolean contains = visibleRect.contains(e.getX(), e.getY()); 122 123 if (contains) { 124 _stop(component); 125 } else { 126 start(component, e); 127 } 128 } 129 130 137 public void actionPerformed(ActionEvent x) { 138 JComponent component = Autoscroller.component; 139 140 if (component == null || !component.isShowing() || (event == null)) { 141 _stop(component); 142 return; 143 } 144 Point screenLocation = component.getLocationOnScreen(); 145 MouseEvent e = new MouseEvent(component, event.getID(), 146 event.getWhen(), event.getModifiers(), 147 event.getX() - screenLocation.x, 148 event.getY() - screenLocation.y, 149 event.getClickCount(), 150 event.isPopupTrigger()); 151 component.superProcessMouseMotionEvent(e); 152 } 153 154 } 155 | Popular Tags |