-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathJumpScrollBarModel.java
More file actions
268 lines (248 loc) · 8.64 KB
/
JumpScrollBarModel.java
File metadata and controls
268 lines (248 loc) · 8.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/*
* @(#)JumpScrollBarModel.java 1.0.0 04/07/09
*/
package darrylbu.model;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BoundedRangeModel;
import javax.swing.DefaultBoundedRangeModel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.SwingConstants;
import javax.swing.Timer;
/**
* This class is used to create a scroll bar model which continues to return the
* initial value as long as the scroll thumb is being dragged, and returns the
* updated value when it is released, or optionally after a specified delay.
* <P>
* This can be useful in two perceived situations:
* <UL>
* <LI>When the rendering of the scrollable component is time consuming, so as
* to render the component once at the end of the scrolling instead of
* several times over.</LI>
* <LI>When detail to be displayed in different areas of the scrollable
* component is dynamically loaded at runtime, and it is desirable to load
* only the data for the final visible rectangle.</LI>
* </UL>
* <P>
* If the <code>JumpScrollBarModel</code> is constructed with a reference to
* the scroll bar or scroll pane to which it is attached, the original model
* can be restored by invoking the class's dispose() method.
* @see #dispose
* <P>
* The model can be set to jump when the scroll bar's slider is paused for
* a specified period by setting a positive jump interval.
* @see #setJumpInterval(int)
* <P>
* Extends {@link javax.swing.DefaultBoundedRangeModel}.
* @author Darryl
*/
public class JumpScrollBarModel extends DefaultBoundedRangeModel
implements SwingConstants {
private JScrollBar scrollBar;
private BoundedRangeModel oldModel;
private int oldValue;
private Timer timer = new Timer(1000, new ActionListener() {
int lastValue = getTrueValue();
public void actionPerformed(ActionEvent e) {
if (lastValue != getTrueValue()) {
lastValue = getTrueValue();
timer.restart();
return;
}
int newValue = getTrueValue();
if (oldValue != newValue) {
oldValue = newValue;
fireStateChanged();
}
}
});
/**
* Creates a <code>JumpScrollBarModel</code> with all of the properties
* set to default values. Those values are:
* <UL>
* <LI>value = 0</LI>
* <LI>extent = 0</LI>
* <LI>minimum = 0</LI>
* <LI>maximum = 100</LI>
* <LI>adjusting = false</LI>
* </UL>
*/
public JumpScrollBarModel() {
super();
}
/**
* Creates a <code>JumpScrollBarModel</code> with the specified value,
* extent, minimum and maximum. Adjusting is false.
* <P>
* Throws an IllegalArgumentException if the following constraints aren't
* satisfied:
* <code>min <= value <= value+extent <= max</code>
*
* @param value the value
* @param extent the extent
* @param min the minimum of the range
* @param max the maximum of the range
*/
public JumpScrollBarModel(int value, int extent, int min, int max) {
super(value, extent, min, max);
}
/**
* Creates a <code>JumpScrollBarModel</code> for the vertical scroll bar
* of a JScrollPane. The properties are initialized from the scroll bar's
* original model.
*
* @param scrollPane the JScrollPane
*/
public JumpScrollBarModel(JScrollPane scrollPane) {
this(scrollPane, VERTICAL);
}
/**
* Creates a <code>JumpScrollBarModel</code> for the specified scroll bar
* of a JScrollPane. The properties are initialized from the scroll bar's
* original model.
*
* @param scrollPane the JScrollPane
* @param scrollBarType should be either of
* <UL>
* <LI>SwingConstants.HORIZONTAL</LI>
* <LI>SwingConstants.VERTICAL</LI>
* </UL>
* <P>
* In case of an illegal value, the vertical scroll bar will be selected
* by default.
*/
public JumpScrollBarModel(JScrollPane scrollPane, int scrollBarType) {
this(scrollBarType == HORIZONTAL
? scrollPane.getHorizontalScrollBar()
: scrollPane.getVerticalScrollBar());
}
/**
* Creates a <code>JumpScrollBarModel</code> for a JScrollBar. The
* properties are initialized from the scroll bar's original model.
*
* @param scrollBar the JScrollBar
*/
public JumpScrollBarModel(JScrollBar scrollBar) {
this.scrollBar = scrollBar;
oldModel = scrollBar.getModel();
setValue(oldModel.getValue());
setExtent(oldModel.getExtent());
setMinimum(oldModel.getMinimum());
setMaximum(oldModel.getMaximum());
}
/**
* Returns a <code>JumpScrollBarModel</code> for the vertical scroll bar
* of a JScrollPane. The properties are initialized from the scroll bar's
* original model.
*
* @param scrollPane the JScrollPane
* @return the <code>JumpScrollBarModel</code>
*/
public static JumpScrollBarModel setJumpScrollBarModel(JScrollPane scrollPane) {
return setJumpScrollBarModel(scrollPane, VERTICAL);
}
/**
* Returns a <code>JumpScrollBarModel</code> for the specified scroll bar
* of a JScrollPane. The properties are initialized from the scroll bar's
* original model.
*
* @param scrollPane the JScrollPane
* @param scrollBarType should be either of
* <UL>
* <LI>SwingConstants.HORIZONTAL</LI>
* <LI>SwingConstants.VERTICAL</LI>
* </UL>
* <P>
* In case of an illegal value, the vertical scroll bar will be selected
* by default.
*
* @return the <code>JumpScrollBarModel</code>
*/
public static JumpScrollBarModel setJumpScrollBarModel(JScrollPane scrollPane,
int scrollBarType) {
return setJumpScrollBarModel(scrollBarType == HORIZONTAL
? scrollPane.getHorizontalScrollBar()
: scrollPane.getVerticalScrollBar());
}
/**
* Returns a <code>JumpScrollBarModel</code> for a JScrollBar. The
* properties are initialized from the scroll bar's original model.
*
* @param scrollBar the JScrollBar
* @return the <code>JumpScrollBarModel</code>
*/
public static JumpScrollBarModel setJumpScrollBarModel(JScrollBar scrollBar) {
JumpScrollBarModel model = new JumpScrollBarModel(scrollBar);
scrollBar.setModel(model);
return model;
}
/**
* Overridden to return the previous value while the scroll bar's value is
* adjusting, and to return the current value only when not adjusting.
*
* @return the model's value, adjusted as noted.
*/
@Override
public int getValue() {
int newValue = getTrueValue();
if (!getValueIsAdjusting() && (oldValue != newValue)) {
oldValue = newValue;
fireStateChanged();
}
return oldValue;
}
/**
* Returns the true value of the scroll bar, as retrieved from the
* superclass. This method is provided as a convenience for consumers
* that require the true value, for example to post a tool tip or status
* message while adjusting.
*
* @return the true value
*/
public int getTrueValue() {
return super.getValue();
}
/**
* Returns the jump interval in milliseconds, or -1 if the model does not
* auto jump.
*
* @return the jump interval, or -1 if the model does not auto jump.
*/
public int getJumpInterval() {
if (timer.isRunning()) {
return timer.getDelay();
} else {
return -1;
}
}
/**
* Sets the interval in milliseconds after which the scrollbar will
* jump if paused at one point. A value of -1 will stop the auto jump.
* <P>
* Any other negative value or 0 will do nothing.
*
* @param interval the jump interval, or -1 to stop auto jump.
*/
public void setJumpInterval(int interval) {
if (interval == -1 && timer.isRunning()) {
timer.stop();
} else if (interval > 0 && interval != getJumpInterval()) {
timer.setDelay(interval);
timer.setInitialDelay(interval);
timer.start();
}
}
/**
* Invoking this method will restore the scroll bar's original model. In
* case this <code>JumpScrollBarModel</code> was constructed without a
* reference to a scroll bar or scroll pane, the method will do nothing.
* <P>
*
*/
public void dispose() {
if (scrollBar != null) {
scrollBar.setModel(oldModel);
}
}
}