< prev index next >

src/java.desktop/share/classes/javax/swing/Autoscroller.java

Print this page




  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;


  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) {


 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 }


  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;


  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) {


 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 }
< prev index next >