KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > Autoscroller


1 /*
2  * @(#)Autoscroller.java 1.14 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package javax.swing;
9
10 import java.awt.*;
11 import java.awt.event.*;
12
13 /**
14  * Autoscroller is responsible for generating synthetic mouse dragged
15  * events. It is the responsibility of the Component (or its MouseListeners)
16  * that receive the events to do the actual scrolling in response to the
17  * mouse dragged events.
18  *
19  * @version 1.14 12/19/03
20  * @author Dave Moore
21  * @author Scott Violet
22  */

23 class Autoscroller implements ActionListener {
24     /**
25      * Global Autoscroller.
26      */

27     private static Autoscroller JavaDoc sharedInstance = new Autoscroller JavaDoc();
28
29     // As there can only ever be one autoscroller active these fields are
30
// static. The Timer is recreated as necessary to target the appropriate
31
// Autoscroller instance.
32
private static MouseEvent event;
33     private static Timer JavaDoc timer;
34     private static JComponent JavaDoc component;
35
36     //
37
// The public API, all methods are cover methods for an instance method
38
//
39
/**
40      * Stops autoscroll events from happening on the specified component.
41      */

42     public static void stop(JComponent JavaDoc c) {
43         sharedInstance._stop(c);
44     }
45
46     /**
47      * Stops autoscroll events from happening on the specified component.
48      */

49     public static boolean isRunning(JComponent JavaDoc c) {
50         return sharedInstance._isRunning(c);
51     }
52
53     /**
54      * Invoked when a mouse dragged event occurs, will start the autoscroller
55      * if necessary.
56      */

57     public static void processMouseDragged(MouseEvent e) {
58         sharedInstance._processMouseDragged(e);
59     }
60
61
62     Autoscroller() {
63     }
64
65     /**
66      * Starts the timer targeting the passed in component.
67      */

68     private void start(JComponent JavaDoc c, MouseEvent e) {
69         Point screenLocation = c.getLocationOnScreen();
70
71         if (component != c) {
72             _stop(component);
73         }
74         component = c;
75         event = new MouseEvent(component, e.getID(), e.getWhen(),
76                                e.getModifiers(), e.getX() + screenLocation.x,
77                                e.getY() + screenLocation.y,
78                                e.getClickCount(), e.isPopupTrigger());
79
80         if (timer == null) {
81             timer = new Timer JavaDoc(100, this);
82         }
83
84         if (!timer.isRunning()) {
85             timer.start();
86         }
87     }
88
89     //
90
// Methods mirror the public static API
91
//
92

93     /**
94      * Stops scrolling for the passed in widget.
95      */

96     private void _stop(JComponent JavaDoc c) {
97         if (component == c) {
98             if (timer != null) {
99                 timer.stop();
100             }
101             timer = null;
102             event = null;
103             component = null;
104         }
105     }
106
107     /**
108      * Returns true if autoscrolling is currently running for the specified
109      * widget.
110      */

111     private boolean _isRunning(JComponent JavaDoc c) {
112         return (c == component && timer != null && timer.isRunning());
113     }
114
115     /**
116      * MouseListener method, invokes start/stop as necessary.
117      */

118     private void _processMouseDragged(MouseEvent e) {
119         JComponent JavaDoc component = (JComponent JavaDoc)e.getComponent();
120     Rectangle visibleRect = component.getVisibleRect();
121     boolean contains = visibleRect.contains(e.getX(), e.getY());
122
123     if (contains) {
124             _stop(component);
125     } else {
126             start(component, e);
127     }
128     }
129
130     //
131
// ActionListener
132
//
133
/**
134      * ActionListener method. Invoked when the Timer fires. This will scroll
135      * if necessary.
136      */

137     public void actionPerformed(ActionEvent x) {
138         JComponent JavaDoc component = Autoscroller.component;
139
140         if (component == null || !component.isShowing() || (event == null)) {
141             _stop(component);
142             return;
143         }
144         Point screenLocation = component.getLocationOnScreen();
145         MouseEvent e = new MouseEvent(component, event.getID(),
146                                       event.getWhen(), event.getModifiers(),
147                                       event.getX() - screenLocation.x,
148                                       event.getY() - screenLocation.y,
149                                       event.getClickCount(),
150                                       event.isPopupTrigger());
151         component.superProcessMouseMotionEvent(e);
152     }
153
154 }
155
Popular Tags