Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/javax/swing/Autoscroller.java
38829 views
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
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;
54
private static JComponent component;
55
56
//
57
// The public API, all methods are cover methods for an instance method
58
//
59
/**
60
* Stops autoscroll events from happening on the specified component.
61
*/
62
public static void stop(JComponent c) {
63
sharedInstance._stop(c);
64
}
65
66
/**
67
* Stops autoscroll events from happening on the specified component.
68
*/
69
public static boolean isRunning(JComponent c) {
70
return sharedInstance._isRunning(c);
71
}
72
73
/**
74
* Invoked when a mouse dragged event occurs, will start the autoscroller
75
* if necessary.
76
*/
77
public static void processMouseDragged(MouseEvent e) {
78
sharedInstance._processMouseDragged(e);
79
}
80
81
82
Autoscroller() {
83
}
84
85
/**
86
* Starts the timer targeting the passed in component.
87
*/
88
private void start(JComponent c, MouseEvent e) {
89
Point screenLocation = c.getLocationOnScreen();
90
91
if (component != c) {
92
_stop(component);
93
}
94
component = c;
95
event = new MouseEvent(component, e.getID(), e.getWhen(),
96
e.getModifiers(), e.getX() + screenLocation.x,
97
e.getY() + screenLocation.y,
98
e.getXOnScreen(),
99
e.getYOnScreen(),
100
e.getClickCount(), e.isPopupTrigger(),
101
MouseEvent.NOBUTTON);
102
MouseEventAccessor meAccessor = AWTAccessor.getMouseEventAccessor();
103
meAccessor.setCausedByTouchEvent(event,
104
meAccessor.isCausedByTouchEvent(e));
105
106
if (timer == null) {
107
timer = new Timer(100, this);
108
}
109
110
if (!timer.isRunning()) {
111
timer.start();
112
}
113
}
114
115
//
116
// Methods mirror the public static API
117
//
118
119
/**
120
* Stops scrolling for the passed in widget.
121
*/
122
private void _stop(JComponent c) {
123
if (component == c) {
124
if (timer != null) {
125
timer.stop();
126
}
127
timer = null;
128
event = null;
129
component = null;
130
}
131
}
132
133
/**
134
* Returns true if autoscrolling is currently running for the specified
135
* widget.
136
*/
137
private boolean _isRunning(JComponent c) {
138
return (c == component && timer != null && timer.isRunning());
139
}
140
141
/**
142
* MouseListener method, invokes start/stop as necessary.
143
*/
144
private void _processMouseDragged(MouseEvent e) {
145
JComponent component = (JComponent)e.getComponent();
146
boolean stop = true;
147
if (component.isShowing()) {
148
Rectangle visibleRect = component.getVisibleRect();
149
stop = visibleRect.contains(e.getX(), e.getY());
150
}
151
if (stop) {
152
_stop(component);
153
} else {
154
start(component, e);
155
}
156
}
157
158
//
159
// ActionListener
160
//
161
/**
162
* ActionListener method. Invoked when the Timer fires. This will scroll
163
* if necessary.
164
*/
165
public void actionPerformed(ActionEvent x) {
166
JComponent component = Autoscroller.component;
167
168
if (component == null || !component.isShowing() || (event == null)) {
169
_stop(component);
170
return;
171
}
172
Point screenLocation = component.getLocationOnScreen();
173
MouseEvent e = new MouseEvent(component, event.getID(),
174
event.getWhen(), event.getModifiers(),
175
event.getX() - screenLocation.x,
176
event.getY() - screenLocation.y,
177
event.getXOnScreen(),
178
event.getYOnScreen(),
179
event.getClickCount(),
180
event.isPopupTrigger(),
181
MouseEvent.NOBUTTON);
182
MouseEventAccessor meAccessor = AWTAccessor.getMouseEventAccessor();
183
meAccessor.setCausedByTouchEvent(e,
184
meAccessor.isCausedByTouchEvent(event));
185
component.superProcessMouseMotionEvent(e);
186
}
187
188
}
189
190