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 /**
  32  * Autoscroller is responsible for generating synthetic mouse dragged
  33  * events. It is the responsibility of the Component (or its MouseListeners)
  34  * that receive the events to do the actual scrolling in response to the
  35  * mouse dragged events.
  36  *
  37  * @author Dave Moore
  38  * @author Scott Violet
  39  */
  40 class Autoscroller implements ActionListener {
  41     /**
  42      * Global Autoscroller.
  43      */
  44     private static Autoscroller sharedInstance = new Autoscroller();
  45 
  46     // As there can only ever be one autoscroller active these fields are
  47     // static. The Timer is recreated as necessary to target the appropriate
  48     // Autoscroller instance.
  49     private static MouseEvent event;
  50     private static Timer timer;
  51     private static JComponent component;
  52 
  53     //
  54     // The public API, all methods are cover methods for an instance method
  55     //
  56     /**
  57      * Stops autoscroll events from happening on the specified component.
  58      */
  59     public static void stop(JComponent c) {
  60         sharedInstance._stop(c);
  61     }
  62 
  63     /**
  64      * Stops autoscroll events from happening on the specified component.
  65      */
  66     public static boolean isRunning(JComponent c) {
  67         return sharedInstance._isRunning(c);
  68     }
  69 
  70     /**
  71      * Invoked when a mouse dragged event occurs, will start the autoscroller
  72      * if necessary.
  73      */
  74     public static void processMouseDragged(MouseEvent e) {
  75         sharedInstance._processMouseDragged(e);
  76     }
  77 
  78 
  79     Autoscroller() {
  80     }
  81 
  82     /**
  83      * Starts the timer targeting the passed in component.
  84      */
  85     @SuppressWarnings("deprecation")
  86     private void start(JComponent c, MouseEvent e) {
  87         Point screenLocation = c.getLocationOnScreen();
  88 
  89         if (component != c) {
  90             _stop(component);
  91         }
  92         component = c;
  93         event = new MouseEvent(component, e.getID(), e.getWhen(),
  94                                e.getModifiers(), e.getX() + screenLocation.x,
  95                                e.getY() + screenLocation.y,
  96                                e.getXOnScreen(),
  97                                e.getYOnScreen(),
  98                                e.getClickCount(), e.isPopupTrigger(),
  99                                MouseEvent.NOBUTTON);
 100 
 101         if (timer == null) {
 102             timer = new Timer(100, this);
 103         }
 104 
 105         if (!timer.isRunning()) {
 106             timer.start();
 107         }
 108     }
 109 
 110     //
 111     // Methods mirror the public static API
 112     //
 113 
 114     /**
 115      * Stops scrolling for the passed in widget.
 116      */
 117     private void _stop(JComponent c) {
 118         if (component == c) {
 119             if (timer != null) {
 120                 timer.stop();
 121             }
 122             timer = null;
 123             event = null;
 124             component = null;
 125         }
 126     }
 127 
 128     /**
 129      * Returns true if autoscrolling is currently running for the specified
 130      * widget.
 131      */
 132     private boolean _isRunning(JComponent c) {
 133         return (c == component && timer != null && timer.isRunning());
 134     }
 135 
 136     /**
 137      * MouseListener method, invokes start/stop as necessary.
 138      */
 139     private void _processMouseDragged(MouseEvent e) {
 140         JComponent component = (JComponent)e.getComponent();
 141         boolean stop = true;
 142         if (component.isShowing()) {
 143             Rectangle visibleRect = component.getVisibleRect();
 144             stop = visibleRect.contains(e.getX(), e.getY());
 145         }
 146         if (stop) {
 147             _stop(component);
 148         } else {
 149             start(component, e);
 150         }
 151     }
 152 
 153     //
 154     // ActionListener
 155     //
 156     /**
 157      * ActionListener method. Invoked when the Timer fires. This will scroll
 158      * if necessary.
 159      */
 160     @SuppressWarnings("deprecation")
 161     public void actionPerformed(ActionEvent x) {
 162         JComponent component = Autoscroller.component;
 163 
 164         if (component == null || !component.isShowing() || (event == null)) {
 165             _stop(component);
 166             return;
 167         }
 168         Point screenLocation = component.getLocationOnScreen();
 169         MouseEvent e = new MouseEvent(component, event.getID(),
 170                                       event.getWhen(), event.getModifiers(),
 171                                       event.getX() - screenLocation.x,
 172                                       event.getY() - screenLocation.y,
 173                                       event.getXOnScreen(),
 174                                       event.getYOnScreen(),
 175                                       event.getClickCount(),
 176                                       event.isPopupTrigger(),
 177                                       MouseEvent.NOBUTTON);
 178         component.superProcessMouseMotionEvent(e);
 179     }
 180 
 181 }