1 /* 2 * Copyright (c) 1997, 2006, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package javax.swing; 27 28 import java.awt.*; 29 import java.awt.event.*; 30 31 import sun.awt.AWTAccessor; 32 import sun.awt.AWTAccessor.MouseEventAccessor; 33 34 /** 35 * Autoscroller is responsible for generating synthetic mouse dragged 36 * events. It is the responsibility of the Component (or its MouseListeners) 37 * that receive the events to do the actual scrolling in response to the 38 * mouse dragged events. 39 * 40 * @author Dave Moore 41 * @author Scott Violet 42 */ 43 class Autoscroller implements ActionListener { 44 /** 45 * Global Autoscroller. 46 */ 47 private static Autoscroller sharedInstance = new Autoscroller(); 48 49 // As there can only ever be one autoscroller active these fields are 50 // static. The Timer is recreated as necessary to target the appropriate 51 // Autoscroller instance. 52 private static MouseEvent event; 53 private static Timer timer; 54 private static JComponent component; 55 56 // 57 // The public API, all methods are cover methods for an instance method 58 // 59 /** 60 * Stops autoscroll events from happening on the specified component. 61 */ 62 public static void stop(JComponent c) { 63 sharedInstance._stop(c); 64 } 65 66 /** 67 * Stops autoscroll events from happening on the specified component. 68 */ 69 public static boolean isRunning(JComponent c) { 70 return sharedInstance._isRunning(c); 71 } 72 73 /** 74 * Invoked when a mouse dragged event occurs, will start the autoscroller 75 * if necessary. 76 */ 77 public static void processMouseDragged(MouseEvent e) { 78 sharedInstance._processMouseDragged(e); 79 } 80 81 82 Autoscroller() { 83 } 84 85 /** 86 * Starts the timer targeting the passed in component. 87 */ 88 @SuppressWarnings("deprecation") 89 private void start(JComponent c, MouseEvent e) { 90 Point screenLocation = c.getLocationOnScreen(); 91 92 if (component != c) { 93 _stop(component); 94 } 95 component = c; 96 event = new MouseEvent(component, e.getID(), e.getWhen(), 97 e.getModifiers(), e.getX() + screenLocation.x, 98 e.getY() + screenLocation.y, 99 e.getXOnScreen(), 100 e.getYOnScreen(), 101 e.getClickCount(), e.isPopupTrigger(), 102 MouseEvent.NOBUTTON); 103 MouseEventAccessor meAccessor = AWTAccessor.getMouseEventAccessor(); 104 meAccessor.setCausedByTouchEvent(event, 105 meAccessor.isCausedByTouchEvent(e)); 106 107 if (timer == null) { 108 timer = new Timer(100, this); 109 } 110 111 if (!timer.isRunning()) { 112 timer.start(); 113 } 114 } 115 116 // 117 // Methods mirror the public static API 118 // 119 120 /** 121 * Stops scrolling for the passed in widget. 122 */ 123 private void _stop(JComponent c) { 124 if (component == c) { 125 if (timer != null) { 126 timer.stop(); 127 } 128 timer = null; 129 event = null; 130 component = null; 131 } 132 } 133 134 /** 135 * Returns true if autoscrolling is currently running for the specified 136 * widget. 137 */ 138 private boolean _isRunning(JComponent c) { 139 return (c == component && timer != null && timer.isRunning()); 140 } 141 142 /** 143 * MouseListener method, invokes start/stop as necessary. 144 */ 145 private void _processMouseDragged(MouseEvent e) { 146 JComponent component = (JComponent)e.getComponent(); 147 boolean stop = true; 148 if (component.isShowing()) { 149 Rectangle visibleRect = component.getVisibleRect(); 150 stop = visibleRect.contains(e.getX(), e.getY()); 151 } 152 if (stop) { 153 _stop(component); 154 } else { 155 start(component, e); 156 } 157 } 158 159 // 160 // ActionListener 161 // 162 /** 163 * ActionListener method. Invoked when the Timer fires. This will scroll 164 * if necessary. 165 */ 166 @SuppressWarnings("deprecation") 167 public void actionPerformed(ActionEvent x) { 168 JComponent component = Autoscroller.component; 169 170 if (component == null || !component.isShowing() || (event == null)) { 171 _stop(component); 172 return; 173 } 174 Point screenLocation = component.getLocationOnScreen(); 175 MouseEvent e = new MouseEvent(component, event.getID(), 176 event.getWhen(), event.getModifiers(), 177 event.getX() - screenLocation.x, 178 event.getY() - screenLocation.y, 179 event.getXOnScreen(), 180 event.getYOnScreen(), 181 event.getClickCount(), 182 event.isPopupTrigger(), 183 MouseEvent.NOBUTTON); 184 MouseEventAccessor meAccessor = AWTAccessor.getMouseEventAccessor(); 185 meAccessor.setCausedByTouchEvent(e, 186 meAccessor.isCausedByTouchEvent(event)); 187 component.superProcessMouseMotionEvent(e); 188 } 189 190 }