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