Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/javax/swing/Autoscroller.java
38829 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*/87private void start(JComponent c, MouseEvent e) {88Point screenLocation = c.getLocationOnScreen();8990if (component != c) {91_stop(component);92}93component = c;94event = new MouseEvent(component, e.getID(), e.getWhen(),95e.getModifiers(), e.getX() + screenLocation.x,96e.getY() + screenLocation.y,97e.getXOnScreen(),98e.getYOnScreen(),99e.getClickCount(), e.isPopupTrigger(),100MouseEvent.NOBUTTON);101MouseEventAccessor meAccessor = AWTAccessor.getMouseEventAccessor();102meAccessor.setCausedByTouchEvent(event,103meAccessor.isCausedByTouchEvent(e));104105if (timer == null) {106timer = new Timer(100, this);107}108109if (!timer.isRunning()) {110timer.start();111}112}113114//115// Methods mirror the public static API116//117118/**119* Stops scrolling for the passed in widget.120*/121private void _stop(JComponent c) {122if (component == c) {123if (timer != null) {124timer.stop();125}126timer = null;127event = null;128component = null;129}130}131132/**133* Returns true if autoscrolling is currently running for the specified134* widget.135*/136private boolean _isRunning(JComponent c) {137return (c == component && timer != null && timer.isRunning());138}139140/**141* MouseListener method, invokes start/stop as necessary.142*/143private void _processMouseDragged(MouseEvent e) {144JComponent component = (JComponent)e.getComponent();145boolean stop = true;146if (component.isShowing()) {147Rectangle visibleRect = component.getVisibleRect();148stop = visibleRect.contains(e.getX(), e.getY());149}150if (stop) {151_stop(component);152} else {153start(component, e);154}155}156157//158// ActionListener159//160/**161* ActionListener method. Invoked when the Timer fires. This will scroll162* if necessary.163*/164public void actionPerformed(ActionEvent x) {165JComponent component = Autoscroller.component;166167if (component == null || !component.isShowing() || (event == null)) {168_stop(component);169return;170}171Point screenLocation = component.getLocationOnScreen();172MouseEvent e = new MouseEvent(component, event.getID(),173event.getWhen(), event.getModifiers(),174event.getX() - screenLocation.x,175event.getY() - screenLocation.y,176event.getXOnScreen(),177event.getYOnScreen(),178event.getClickCount(),179event.isPopupTrigger(),180MouseEvent.NOBUTTON);181MouseEventAccessor meAccessor = AWTAccessor.getMouseEventAccessor();182meAccessor.setCausedByTouchEvent(e,183meAccessor.isCausedByTouchEvent(event));184component.superProcessMouseMotionEvent(e);185}186187}188189190