Path: blob/master/src/java.desktop/share/classes/javax/swing/Autoscroller.java
41153 views
/*1* Copyright (c) 1997, 2006, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package javax.swing;2627import java.awt.*;28import java.awt.event.*;2930import sun.awt.AWTAccessor;31import sun.awt.AWTAccessor.MouseEventAccessor;3233/**34* Autoscroller is responsible for generating synthetic mouse dragged35* events. It is the responsibility of the Component (or its MouseListeners)36* that receive the events to do the actual scrolling in response to the37* mouse dragged events.38*39* @author Dave Moore40* @author Scott Violet41*/42class Autoscroller implements ActionListener {43/**44* Global Autoscroller.45*/46private static Autoscroller sharedInstance = new Autoscroller();4748// As there can only ever be one autoscroller active these fields are49// static. The Timer is recreated as necessary to target the appropriate50// Autoscroller instance.51private static MouseEvent event;52private static Timer timer;53private static JComponent component;5455//56// The public API, all methods are cover methods for an instance method57//58/**59* Stops autoscroll events from happening on the specified component.60*/61public static void stop(JComponent c) {62sharedInstance._stop(c);63}6465/**66* Stops autoscroll events from happening on the specified component.67*/68public static boolean isRunning(JComponent c) {69return sharedInstance._isRunning(c);70}7172/**73* Invoked when a mouse dragged event occurs, will start the autoscroller74* if necessary.75*/76public static void processMouseDragged(MouseEvent e) {77sharedInstance._processMouseDragged(e);78}798081Autoscroller() {82}8384/**85* Starts the timer targeting the passed in component.86*/87@SuppressWarnings("deprecation")88private void start(JComponent c, MouseEvent e) {89Point screenLocation = c.getLocationOnScreen();9091if (component != c) {92_stop(component);93}94component = c;95event = new MouseEvent(component, e.getID(), e.getWhen(),96e.getModifiers(), e.getX() + screenLocation.x,97e.getY() + screenLocation.y,98e.getXOnScreen(),99e.getYOnScreen(),100e.getClickCount(), e.isPopupTrigger(),101MouseEvent.NOBUTTON);102MouseEventAccessor meAccessor = AWTAccessor.getMouseEventAccessor();103meAccessor.setCausedByTouchEvent(event,104meAccessor.isCausedByTouchEvent(e));105106if (timer == null) {107timer = new Timer(100, this);108}109110if (!timer.isRunning()) {111timer.start();112}113}114115//116// Methods mirror the public static API117//118119/**120* Stops scrolling for the passed in widget.121*/122private void _stop(JComponent c) {123if (component == c) {124if (timer != null) {125timer.stop();126}127timer = null;128event = null;129component = null;130}131}132133/**134* Returns true if autoscrolling is currently running for the specified135* widget.136*/137private boolean _isRunning(JComponent c) {138return (c == component && timer != null && timer.isRunning());139}140141/**142* MouseListener method, invokes start/stop as necessary.143*/144private void _processMouseDragged(MouseEvent e) {145JComponent component = (JComponent)e.getComponent();146boolean stop = true;147if (component.isShowing()) {148Rectangle visibleRect = component.getVisibleRect();149stop = visibleRect.contains(e.getX(), e.getY());150}151if (stop) {152_stop(component);153} else {154start(component, e);155}156}157158//159// ActionListener160//161/**162* ActionListener method. Invoked when the Timer fires. This will scroll163* if necessary.164*/165@SuppressWarnings("deprecation")166public void actionPerformed(ActionEvent x) {167JComponent component = Autoscroller.component;168169if (component == null || !component.isShowing() || (event == null)) {170_stop(component);171return;172}173Point screenLocation = component.getLocationOnScreen();174MouseEvent e = new MouseEvent(component, event.getID(),175event.getWhen(), event.getModifiers(),176event.getX() - screenLocation.x,177event.getY() - screenLocation.y,178event.getXOnScreen(),179event.getYOnScreen(),180event.getClickCount(),181event.isPopupTrigger(),182MouseEvent.NOBUTTON);183MouseEventAccessor meAccessor = AWTAccessor.getMouseEventAccessor();184meAccessor.setCausedByTouchEvent(e,185meAccessor.isCausedByTouchEvent(event));186component.superProcessMouseMotionEvent(e);187}188189}190191192