author | cl |
Mon, 07 Oct 2013 11:34:44 -0700 | |
changeset 20458 | f2423fb3fd19 |
parent 20095 | 619a080ff304 |
child 21982 | fd6e5fe509df |
permissions | -rw-r--r-- |
2 | 1 |
/* |
5506 | 2 |
* Copyright (c) 1997, 2008, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 24 |
*/ |
25 |
||
26 |
package javax.swing; |
|
27 |
||
28 |
import java.io.Serializable; |
|
29 |
import java.awt.Component; |
|
30 |
import java.awt.Adjustable; |
|
31 |
import java.awt.Dimension; |
|
32 |
import java.awt.event.AdjustmentListener; |
|
33 |
import java.awt.event.AdjustmentEvent; |
|
34 |
import java.awt.Graphics; |
|
35 |
||
36 |
import javax.swing.event.*; |
|
37 |
import javax.swing.plaf.*; |
|
38 |
import javax.accessibility.*; |
|
39 |
||
40 |
import java.io.ObjectOutputStream; |
|
41 |
import java.io.ObjectInputStream; |
|
42 |
import java.io.IOException; |
|
43 |
||
44 |
||
45 |
||
46 |
/** |
|
47 |
* An implementation of a scrollbar. The user positions the knob in the |
|
48 |
* scrollbar to determine the contents of the viewing area. The |
|
49 |
* program typically adjusts the display so that the end of the |
|
50 |
* scrollbar represents the end of the displayable contents, or 100% |
|
51 |
* of the contents. The start of the scrollbar is the beginning of the |
|
52 |
* displayable contents, or 0%. The position of the knob within |
|
53 |
* those bounds then translates to the corresponding percentage of |
|
54 |
* the displayable contents. |
|
55 |
* <p> |
|
56 |
* Typically, as the position of the knob in the scrollbar changes |
|
57 |
* a corresponding change is made to the position of the JViewport on |
|
58 |
* the underlying view, changing the contents of the JViewport. |
|
59 |
* <p> |
|
60 |
* <strong>Warning:</strong> Swing is not thread safe. For more |
|
61 |
* information see <a |
|
62 |
* href="package-summary.html#threading">Swing's Threading |
|
63 |
* Policy</a>. |
|
64 |
* <p> |
|
65 |
* <strong>Warning:</strong> |
|
66 |
* Serialized objects of this class will not be compatible with |
|
67 |
* future Swing releases. The current serialization support is |
|
68 |
* appropriate for short term storage or RMI between applications running |
|
69 |
* the same version of Swing. As of 1.4, support for long term storage |
|
20458 | 70 |
* of all JavaBeans™ |
2 | 71 |
* has been added to the <code>java.beans</code> package. |
72 |
* Please see {@link java.beans.XMLEncoder}. |
|
73 |
* |
|
74 |
* @see JScrollPane |
|
75 |
* @beaninfo |
|
76 |
* attribute: isContainer false |
|
77 |
* description: A component that helps determine the visible content range of an area. |
|
78 |
* |
|
79 |
* @author David Kloba |
|
80 |
*/ |
|
81 |
public class JScrollBar extends JComponent implements Adjustable, Accessible |
|
82 |
{ |
|
83 |
/** |
|
84 |
* @see #getUIClassID |
|
85 |
* @see #readObject |
|
86 |
*/ |
|
87 |
private static final String uiClassID = "ScrollBarUI"; |
|
88 |
||
89 |
/** |
|
90 |
* All changes from the model are treated as though the user moved |
|
91 |
* the scrollbar knob. |
|
92 |
*/ |
|
93 |
private ChangeListener fwdAdjustmentEvents = new ModelListener(); |
|
94 |
||
95 |
||
96 |
/** |
|
97 |
* The model that represents the scrollbar's minimum, maximum, extent |
|
98 |
* (aka "visibleAmount") and current value. |
|
99 |
* @see #setModel |
|
100 |
*/ |
|
101 |
protected BoundedRangeModel model; |
|
102 |
||
103 |
||
104 |
/** |
|
105 |
* @see #setOrientation |
|
106 |
*/ |
|
107 |
protected int orientation; |
|
108 |
||
109 |
||
110 |
/** |
|
111 |
* @see #setUnitIncrement |
|
112 |
*/ |
|
113 |
protected int unitIncrement; |
|
114 |
||
115 |
||
116 |
/** |
|
117 |
* @see #setBlockIncrement |
|
118 |
*/ |
|
119 |
protected int blockIncrement; |
|
120 |
||
121 |
||
122 |
private void checkOrientation(int orientation) { |
|
123 |
switch (orientation) { |
|
124 |
case VERTICAL: |
|
125 |
case HORIZONTAL: |
|
126 |
break; |
|
127 |
default: |
|
128 |
throw new IllegalArgumentException("orientation must be one of: VERTICAL, HORIZONTAL"); |
|
129 |
} |
|
130 |
} |
|
131 |
||
132 |
||
133 |
/** |
|
134 |
* Creates a scrollbar with the specified orientation, |
|
135 |
* value, extent, minimum, and maximum. |
|
136 |
* The "extent" is the size of the viewable area. It is also known |
|
137 |
* as the "visible amount". |
|
138 |
* <p> |
|
139 |
* Note: Use <code>setBlockIncrement</code> to set the block |
|
140 |
* increment to a size slightly smaller than the view's extent. |
|
141 |
* That way, when the user jumps the knob to an adjacent position, |
|
142 |
* one or two lines of the original contents remain in view. |
|
143 |
* |
|
144 |
* @exception IllegalArgumentException if orientation is not one of VERTICAL, HORIZONTAL |
|
145 |
* |
|
146 |
* @see #setOrientation |
|
147 |
* @see #setValue |
|
148 |
* @see #setVisibleAmount |
|
149 |
* @see #setMinimum |
|
150 |
* @see #setMaximum |
|
151 |
*/ |
|
152 |
public JScrollBar(int orientation, int value, int extent, int min, int max) |
|
153 |
{ |
|
154 |
checkOrientation(orientation); |
|
155 |
this.unitIncrement = 1; |
|
156 |
this.blockIncrement = (extent == 0) ? 1 : extent; |
|
157 |
this.orientation = orientation; |
|
158 |
this.model = new DefaultBoundedRangeModel(value, extent, min, max); |
|
159 |
this.model.addChangeListener(fwdAdjustmentEvents); |
|
160 |
setRequestFocusEnabled(false); |
|
161 |
updateUI(); |
|
162 |
} |
|
163 |
||
164 |
||
165 |
/** |
|
166 |
* Creates a scrollbar with the specified orientation |
|
167 |
* and the following initial values: |
|
168 |
* <pre> |
|
169 |
* minimum = 0 |
|
170 |
* maximum = 100 |
|
171 |
* value = 0 |
|
172 |
* extent = 10 |
|
173 |
* </pre> |
|
174 |
*/ |
|
175 |
public JScrollBar(int orientation) { |
|
176 |
this(orientation, 0, 10, 0, 100); |
|
177 |
} |
|
178 |
||
179 |
||
180 |
/** |
|
181 |
* Creates a vertical scrollbar with the following initial values: |
|
182 |
* <pre> |
|
183 |
* minimum = 0 |
|
184 |
* maximum = 100 |
|
185 |
* value = 0 |
|
186 |
* extent = 10 |
|
187 |
* </pre> |
|
188 |
*/ |
|
189 |
public JScrollBar() { |
|
190 |
this(VERTICAL); |
|
191 |
} |
|
192 |
||
193 |
||
194 |
/** |
|
195 |
* Sets the L&F object that renders this component. |
|
196 |
* |
|
197 |
* @param ui the <code>ScrollBarUI</code> L&F object |
|
198 |
* @see UIDefaults#getUI |
|
199 |
* @since 1.4 |
|
200 |
* @beaninfo |
|
201 |
* bound: true |
|
202 |
* hidden: true |
|
203 |
* attribute: visualUpdate true |
|
204 |
* description: The UI object that implements the Component's LookAndFeel |
|
205 |
*/ |
|
206 |
public void setUI(ScrollBarUI ui) { |
|
207 |
super.setUI(ui); |
|
208 |
} |
|
209 |
||
210 |
||
211 |
/** |
|
212 |
* Returns the delegate that implements the look and feel for |
|
213 |
* this component. |
|
214 |
* |
|
215 |
* @see JComponent#setUI |
|
216 |
*/ |
|
217 |
public ScrollBarUI getUI() { |
|
218 |
return (ScrollBarUI)ui; |
|
219 |
} |
|
220 |
||
221 |
||
222 |
/** |
|
223 |
* Overrides <code>JComponent.updateUI</code>. |
|
224 |
* @see JComponent#updateUI |
|
225 |
*/ |
|
226 |
public void updateUI() { |
|
227 |
setUI((ScrollBarUI)UIManager.getUI(this)); |
|
228 |
} |
|
229 |
||
230 |
||
231 |
/** |
|
232 |
* Returns the name of the LookAndFeel class for this component. |
|
233 |
* |
|
234 |
* @return "ScrollBarUI" |
|
235 |
* @see JComponent#getUIClassID |
|
236 |
* @see UIDefaults#getUI |
|
237 |
*/ |
|
238 |
public String getUIClassID() { |
|
239 |
return uiClassID; |
|
240 |
} |
|
241 |
||
242 |
||
243 |
||
244 |
/** |
|
245 |
* Returns the component's orientation (horizontal or vertical). |
|
246 |
* |
|
247 |
* @return VERTICAL or HORIZONTAL |
|
248 |
* @see #setOrientation |
|
249 |
* @see java.awt.Adjustable#getOrientation |
|
250 |
*/ |
|
251 |
public int getOrientation() { |
|
252 |
return orientation; |
|
253 |
} |
|
254 |
||
255 |
||
256 |
/** |
|
257 |
* Set the scrollbar's orientation to either VERTICAL or |
|
258 |
* HORIZONTAL. |
|
259 |
* |
|
260 |
* @exception IllegalArgumentException if orientation is not one of VERTICAL, HORIZONTAL |
|
261 |
* @see #getOrientation |
|
262 |
* @beaninfo |
|
263 |
* preferred: true |
|
264 |
* bound: true |
|
265 |
* attribute: visualUpdate true |
|
266 |
* description: The scrollbar's orientation. |
|
267 |
* enum: VERTICAL JScrollBar.VERTICAL |
|
268 |
* HORIZONTAL JScrollBar.HORIZONTAL |
|
269 |
*/ |
|
270 |
public void setOrientation(int orientation) |
|
271 |
{ |
|
272 |
checkOrientation(orientation); |
|
273 |
int oldValue = this.orientation; |
|
274 |
this.orientation = orientation; |
|
275 |
firePropertyChange("orientation", oldValue, orientation); |
|
276 |
||
277 |
if ((oldValue != orientation) && (accessibleContext != null)) { |
|
278 |
accessibleContext.firePropertyChange( |
|
279 |
AccessibleContext.ACCESSIBLE_STATE_PROPERTY, |
|
280 |
((oldValue == VERTICAL) |
|
281 |
? AccessibleState.VERTICAL : AccessibleState.HORIZONTAL), |
|
282 |
((orientation == VERTICAL) |
|
283 |
? AccessibleState.VERTICAL : AccessibleState.HORIZONTAL)); |
|
284 |
} |
|
285 |
if (orientation != oldValue) { |
|
286 |
revalidate(); |
|
287 |
} |
|
288 |
} |
|
289 |
||
290 |
||
291 |
/** |
|
292 |
* Returns data model that handles the scrollbar's four |
|
293 |
* fundamental properties: minimum, maximum, value, extent. |
|
294 |
* |
|
295 |
* @see #setModel |
|
296 |
*/ |
|
297 |
public BoundedRangeModel getModel() { |
|
298 |
return model; |
|
299 |
} |
|
300 |
||
301 |
||
302 |
/** |
|
303 |
* Sets the model that handles the scrollbar's four |
|
304 |
* fundamental properties: minimum, maximum, value, extent. |
|
305 |
* |
|
306 |
* @see #getModel |
|
307 |
* @beaninfo |
|
308 |
* bound: true |
|
309 |
* expert: true |
|
310 |
* description: The scrollbar's BoundedRangeModel. |
|
311 |
*/ |
|
312 |
public void setModel(BoundedRangeModel newModel) { |
|
313 |
Integer oldValue = null; |
|
314 |
BoundedRangeModel oldModel = model; |
|
315 |
if (model != null) { |
|
316 |
model.removeChangeListener(fwdAdjustmentEvents); |
|
438
2ae294e4518c
6613529: Avoid duplicate object creation within JDK packages
dav
parents:
2
diff
changeset
|
317 |
oldValue = Integer.valueOf(model.getValue()); |
2 | 318 |
} |
319 |
model = newModel; |
|
320 |
if (model != null) { |
|
321 |
model.addChangeListener(fwdAdjustmentEvents); |
|
322 |
} |
|
323 |
||
324 |
firePropertyChange("model", oldModel, model); |
|
325 |
||
326 |
if (accessibleContext != null) { |
|
327 |
accessibleContext.firePropertyChange( |
|
328 |
AccessibleContext.ACCESSIBLE_VALUE_PROPERTY, |
|
329 |
oldValue, new Integer(model.getValue())); |
|
330 |
} |
|
331 |
} |
|
332 |
||
333 |
||
334 |
/** |
|
335 |
* Returns the amount to change the scrollbar's value by, |
|
336 |
* given a unit up/down request. A ScrollBarUI implementation |
|
337 |
* typically calls this method when the user clicks on a scrollbar |
|
338 |
* up/down arrow and uses the result to update the scrollbar's |
|
339 |
* value. Subclasses my override this method to compute |
|
340 |
* a value, e.g. the change required to scroll up or down one |
|
341 |
* (variable height) line text or one row in a table. |
|
342 |
* <p> |
|
343 |
* The JScrollPane component creates scrollbars (by default) |
|
344 |
* that override this method and delegate to the viewports |
|
345 |
* Scrollable view, if it has one. The Scrollable interface |
|
346 |
* provides a more specialized version of this method. |
|
20095
619a080ff304
7057769: JScrollBar spec should specify that unit increment & decrement functionality may not be present
malenkov
parents:
5506
diff
changeset
|
347 |
* <p> |
619a080ff304
7057769: JScrollBar spec should specify that unit increment & decrement functionality may not be present
malenkov
parents:
5506
diff
changeset
|
348 |
* Some look and feels implement custom scrolling behavior |
619a080ff304
7057769: JScrollBar spec should specify that unit increment & decrement functionality may not be present
malenkov
parents:
5506
diff
changeset
|
349 |
* and ignore this property. |
2 | 350 |
* |
351 |
* @param direction is -1 or 1 for up/down respectively |
|
352 |
* @return the value of the unitIncrement property |
|
353 |
* @see #setUnitIncrement |
|
354 |
* @see #setValue |
|
355 |
* @see Scrollable#getScrollableUnitIncrement |
|
356 |
*/ |
|
357 |
public int getUnitIncrement(int direction) { |
|
358 |
return unitIncrement; |
|
359 |
} |
|
360 |
||
361 |
||
362 |
/** |
|
363 |
* Sets the unitIncrement property. |
|
364 |
* <p> |
|
365 |
* Note, that if the argument is equal to the value of Integer.MIN_VALUE, |
|
366 |
* the most look and feels will not provide the scrolling to the right/down. |
|
20095
619a080ff304
7057769: JScrollBar spec should specify that unit increment & decrement functionality may not be present
malenkov
parents:
5506
diff
changeset
|
367 |
* <p> |
619a080ff304
7057769: JScrollBar spec should specify that unit increment & decrement functionality may not be present
malenkov
parents:
5506
diff
changeset
|
368 |
* Some look and feels implement custom scrolling behavior |
619a080ff304
7057769: JScrollBar spec should specify that unit increment & decrement functionality may not be present
malenkov
parents:
5506
diff
changeset
|
369 |
* and ignore this property. |
619a080ff304
7057769: JScrollBar spec should specify that unit increment & decrement functionality may not be present
malenkov
parents:
5506
diff
changeset
|
370 |
* |
2 | 371 |
* @see #getUnitIncrement |
372 |
* @beaninfo |
|
373 |
* preferred: true |
|
374 |
* bound: true |
|
375 |
* description: The scrollbar's unit increment. |
|
376 |
*/ |
|
377 |
public void setUnitIncrement(int unitIncrement) { |
|
378 |
int oldValue = this.unitIncrement; |
|
379 |
this.unitIncrement = unitIncrement; |
|
380 |
firePropertyChange("unitIncrement", oldValue, unitIncrement); |
|
381 |
} |
|
382 |
||
383 |
||
384 |
/** |
|
385 |
* Returns the amount to change the scrollbar's value by, |
|
386 |
* given a block (usually "page") up/down request. A ScrollBarUI |
|
387 |
* implementation typically calls this method when the user clicks |
|
388 |
* above or below the scrollbar "knob" to change the value |
|
389 |
* up or down by large amount. Subclasses my override this |
|
390 |
* method to compute a value, e.g. the change required to scroll |
|
391 |
* up or down one paragraph in a text document. |
|
392 |
* <p> |
|
393 |
* The JScrollPane component creates scrollbars (by default) |
|
394 |
* that override this method and delegate to the viewports |
|
395 |
* Scrollable view, if it has one. The Scrollable interface |
|
396 |
* provides a more specialized version of this method. |
|
20095
619a080ff304
7057769: JScrollBar spec should specify that unit increment & decrement functionality may not be present
malenkov
parents:
5506
diff
changeset
|
397 |
* <p> |
619a080ff304
7057769: JScrollBar spec should specify that unit increment & decrement functionality may not be present
malenkov
parents:
5506
diff
changeset
|
398 |
* Some look and feels implement custom scrolling behavior |
619a080ff304
7057769: JScrollBar spec should specify that unit increment & decrement functionality may not be present
malenkov
parents:
5506
diff
changeset
|
399 |
* and ignore this property. |
2 | 400 |
* |
401 |
* @param direction is -1 or 1 for up/down respectively |
|
402 |
* @return the value of the blockIncrement property |
|
403 |
* @see #setBlockIncrement |
|
404 |
* @see #setValue |
|
405 |
* @see Scrollable#getScrollableBlockIncrement |
|
406 |
*/ |
|
407 |
public int getBlockIncrement(int direction) { |
|
408 |
return blockIncrement; |
|
409 |
} |
|
410 |
||
411 |
||
412 |
/** |
|
413 |
* Sets the blockIncrement property. |
|
414 |
* <p> |
|
415 |
* Note, that if the argument is equal to the value of Integer.MIN_VALUE, |
|
416 |
* the most look and feels will not provide the scrolling to the right/down. |
|
20095
619a080ff304
7057769: JScrollBar spec should specify that unit increment & decrement functionality may not be present
malenkov
parents:
5506
diff
changeset
|
417 |
* <p> |
619a080ff304
7057769: JScrollBar spec should specify that unit increment & decrement functionality may not be present
malenkov
parents:
5506
diff
changeset
|
418 |
* Some look and feels implement custom scrolling behavior |
619a080ff304
7057769: JScrollBar spec should specify that unit increment & decrement functionality may not be present
malenkov
parents:
5506
diff
changeset
|
419 |
* and ignore this property. |
619a080ff304
7057769: JScrollBar spec should specify that unit increment & decrement functionality may not be present
malenkov
parents:
5506
diff
changeset
|
420 |
* |
2 | 421 |
* @see #getBlockIncrement() |
422 |
* @beaninfo |
|
423 |
* preferred: true |
|
424 |
* bound: true |
|
425 |
* description: The scrollbar's block increment. |
|
426 |
*/ |
|
427 |
public void setBlockIncrement(int blockIncrement) { |
|
428 |
int oldValue = this.blockIncrement; |
|
429 |
this.blockIncrement = blockIncrement; |
|
430 |
firePropertyChange("blockIncrement", oldValue, blockIncrement); |
|
431 |
} |
|
432 |
||
433 |
||
434 |
/** |
|
435 |
* For backwards compatibility with java.awt.Scrollbar. |
|
436 |
* @see Adjustable#getUnitIncrement |
|
437 |
* @see #getUnitIncrement(int) |
|
438 |
*/ |
|
439 |
public int getUnitIncrement() { |
|
440 |
return unitIncrement; |
|
441 |
} |
|
442 |
||
443 |
||
444 |
/** |
|
445 |
* For backwards compatibility with java.awt.Scrollbar. |
|
446 |
* @see Adjustable#getBlockIncrement |
|
447 |
* @see #getBlockIncrement(int) |
|
448 |
*/ |
|
449 |
public int getBlockIncrement() { |
|
450 |
return blockIncrement; |
|
451 |
} |
|
452 |
||
453 |
||
454 |
/** |
|
455 |
* Returns the scrollbar's value. |
|
456 |
* @return the model's value property |
|
457 |
* @see #setValue |
|
458 |
*/ |
|
459 |
public int getValue() { |
|
460 |
return getModel().getValue(); |
|
461 |
} |
|
462 |
||
463 |
||
464 |
/** |
|
465 |
* Sets the scrollbar's value. This method just forwards the value |
|
466 |
* to the model. |
|
467 |
* |
|
468 |
* @see #getValue |
|
469 |
* @see BoundedRangeModel#setValue |
|
470 |
* @beaninfo |
|
471 |
* preferred: true |
|
472 |
* description: The scrollbar's current value. |
|
473 |
*/ |
|
474 |
public void setValue(int value) { |
|
475 |
BoundedRangeModel m = getModel(); |
|
476 |
int oldValue = m.getValue(); |
|
477 |
m.setValue(value); |
|
478 |
||
479 |
if (accessibleContext != null) { |
|
480 |
accessibleContext.firePropertyChange( |
|
481 |
AccessibleContext.ACCESSIBLE_VALUE_PROPERTY, |
|
438
2ae294e4518c
6613529: Avoid duplicate object creation within JDK packages
dav
parents:
2
diff
changeset
|
482 |
Integer.valueOf(oldValue), |
2ae294e4518c
6613529: Avoid duplicate object creation within JDK packages
dav
parents:
2
diff
changeset
|
483 |
Integer.valueOf(m.getValue())); |
2 | 484 |
} |
485 |
} |
|
486 |
||
487 |
||
488 |
/** |
|
489 |
* Returns the scrollbar's extent, aka its "visibleAmount". In many |
|
490 |
* scrollbar look and feel implementations the size of the |
|
491 |
* scrollbar "knob" or "thumb" is proportional to the extent. |
|
492 |
* |
|
493 |
* @return the value of the model's extent property |
|
494 |
* @see #setVisibleAmount |
|
495 |
*/ |
|
496 |
public int getVisibleAmount() { |
|
497 |
return getModel().getExtent(); |
|
498 |
} |
|
499 |
||
500 |
||
501 |
/** |
|
502 |
* Set the model's extent property. |
|
503 |
* |
|
504 |
* @see #getVisibleAmount |
|
505 |
* @see BoundedRangeModel#setExtent |
|
506 |
* @beaninfo |
|
507 |
* preferred: true |
|
508 |
* description: The amount of the view that is currently visible. |
|
509 |
*/ |
|
510 |
public void setVisibleAmount(int extent) { |
|
511 |
getModel().setExtent(extent); |
|
512 |
} |
|
513 |
||
514 |
||
515 |
/** |
|
516 |
* Returns the minimum value supported by the scrollbar |
|
517 |
* (usually zero). |
|
518 |
* |
|
519 |
* @return the value of the model's minimum property |
|
520 |
* @see #setMinimum |
|
521 |
*/ |
|
522 |
public int getMinimum() { |
|
523 |
return getModel().getMinimum(); |
|
524 |
} |
|
525 |
||
526 |
||
527 |
/** |
|
528 |
* Sets the model's minimum property. |
|
529 |
* |
|
530 |
* @see #getMinimum |
|
531 |
* @see BoundedRangeModel#setMinimum |
|
532 |
* @beaninfo |
|
533 |
* preferred: true |
|
534 |
* description: The scrollbar's minimum value. |
|
535 |
*/ |
|
536 |
public void setMinimum(int minimum) { |
|
537 |
getModel().setMinimum(minimum); |
|
538 |
} |
|
539 |
||
540 |
||
541 |
/** |
|
542 |
* The maximum value of the scrollbar is maximum - extent. |
|
543 |
* |
|
544 |
* @return the value of the model's maximum property |
|
545 |
* @see #setMaximum |
|
546 |
*/ |
|
547 |
public int getMaximum() { |
|
548 |
return getModel().getMaximum(); |
|
549 |
} |
|
550 |
||
551 |
||
552 |
/** |
|
553 |
* Sets the model's maximum property. Note that the scrollbar's value |
|
554 |
* can only be set to maximum - extent. |
|
555 |
* |
|
556 |
* @see #getMaximum |
|
557 |
* @see BoundedRangeModel#setMaximum |
|
558 |
* @beaninfo |
|
559 |
* preferred: true |
|
560 |
* description: The scrollbar's maximum value. |
|
561 |
*/ |
|
562 |
public void setMaximum(int maximum) { |
|
563 |
getModel().setMaximum(maximum); |
|
564 |
} |
|
565 |
||
566 |
||
567 |
/** |
|
568 |
* True if the scrollbar knob is being dragged. |
|
569 |
* |
|
570 |
* @return the value of the model's valueIsAdjusting property |
|
571 |
* @see #setValueIsAdjusting |
|
572 |
*/ |
|
573 |
public boolean getValueIsAdjusting() { |
|
574 |
return getModel().getValueIsAdjusting(); |
|
575 |
} |
|
576 |
||
577 |
||
578 |
/** |
|
579 |
* Sets the model's valueIsAdjusting property. Scrollbar look and |
|
580 |
* feel implementations should set this property to true when |
|
581 |
* a knob drag begins, and to false when the drag ends. The |
|
582 |
* scrollbar model will not generate ChangeEvents while |
|
583 |
* valueIsAdjusting is true. |
|
584 |
* |
|
585 |
* @see #getValueIsAdjusting |
|
586 |
* @see BoundedRangeModel#setValueIsAdjusting |
|
587 |
* @beaninfo |
|
588 |
* expert: true |
|
589 |
* description: True if the scrollbar thumb is being dragged. |
|
590 |
*/ |
|
591 |
public void setValueIsAdjusting(boolean b) { |
|
592 |
BoundedRangeModel m = getModel(); |
|
593 |
boolean oldValue = m.getValueIsAdjusting(); |
|
594 |
m.setValueIsAdjusting(b); |
|
595 |
||
596 |
if ((oldValue != b) && (accessibleContext != null)) { |
|
597 |
accessibleContext.firePropertyChange( |
|
598 |
AccessibleContext.ACCESSIBLE_STATE_PROPERTY, |
|
599 |
((oldValue) ? AccessibleState.BUSY : null), |
|
600 |
((b) ? AccessibleState.BUSY : null)); |
|
601 |
} |
|
602 |
} |
|
603 |
||
604 |
||
605 |
/** |
|
606 |
* Sets the four BoundedRangeModel properties after forcing |
|
607 |
* the arguments to obey the usual constraints: |
|
608 |
* <pre> |
|
609 |
* minimum <= value <= value+extent <= maximum |
|
610 |
* </pre> |
|
611 |
* <p> |
|
612 |
* |
|
613 |
* @see BoundedRangeModel#setRangeProperties |
|
614 |
* @see #setValue |
|
615 |
* @see #setVisibleAmount |
|
616 |
* @see #setMinimum |
|
617 |
* @see #setMaximum |
|
618 |
*/ |
|
619 |
public void setValues(int newValue, int newExtent, int newMin, int newMax) |
|
620 |
{ |
|
621 |
BoundedRangeModel m = getModel(); |
|
622 |
int oldValue = m.getValue(); |
|
623 |
m.setRangeProperties(newValue, newExtent, newMin, newMax, m.getValueIsAdjusting()); |
|
624 |
||
625 |
if (accessibleContext != null) { |
|
626 |
accessibleContext.firePropertyChange( |
|
627 |
AccessibleContext.ACCESSIBLE_VALUE_PROPERTY, |
|
438
2ae294e4518c
6613529: Avoid duplicate object creation within JDK packages
dav
parents:
2
diff
changeset
|
628 |
Integer.valueOf(oldValue), |
2ae294e4518c
6613529: Avoid duplicate object creation within JDK packages
dav
parents:
2
diff
changeset
|
629 |
Integer.valueOf(m.getValue())); |
2 | 630 |
} |
631 |
} |
|
632 |
||
633 |
||
634 |
/** |
|
635 |
* Adds an AdjustmentListener. Adjustment listeners are notified |
|
636 |
* each time the scrollbar's model changes. Adjustment events are |
|
637 |
* provided for backwards compatibility with java.awt.Scrollbar. |
|
638 |
* <p> |
|
639 |
* Note that the AdjustmentEvents type property will always have a |
|
640 |
* placeholder value of AdjustmentEvent.TRACK because all changes |
|
641 |
* to a BoundedRangeModels value are considered equivalent. To change |
|
642 |
* the value of a BoundedRangeModel one just sets its value property, |
|
643 |
* i.e. model.setValue(123). No information about the origin of the |
|
644 |
* change, e.g. it's a block decrement, is provided. We don't try |
|
645 |
* fabricate the origin of the change here. |
|
646 |
* |
|
647 |
* @param l the AdjustmentLister to add |
|
648 |
* @see #removeAdjustmentListener |
|
649 |
* @see BoundedRangeModel#addChangeListener |
|
650 |
*/ |
|
651 |
public void addAdjustmentListener(AdjustmentListener l) { |
|
652 |
listenerList.add(AdjustmentListener.class, l); |
|
653 |
} |
|
654 |
||
655 |
||
656 |
/** |
|
657 |
* Removes an AdjustmentEvent listener. |
|
658 |
* |
|
659 |
* @param l the AdjustmentLister to remove |
|
660 |
* @see #addAdjustmentListener |
|
661 |
*/ |
|
662 |
public void removeAdjustmentListener(AdjustmentListener l) { |
|
663 |
listenerList.remove(AdjustmentListener.class, l); |
|
664 |
} |
|
665 |
||
666 |
||
667 |
/** |
|
668 |
* Returns an array of all the <code>AdjustmentListener</code>s added |
|
669 |
* to this JScrollBar with addAdjustmentListener(). |
|
670 |
* |
|
671 |
* @return all of the <code>AdjustmentListener</code>s added or an empty |
|
672 |
* array if no listeners have been added |
|
673 |
* @since 1.4 |
|
674 |
*/ |
|
675 |
public AdjustmentListener[] getAdjustmentListeners() { |
|
1301
15e81207e1f2
6727662: Code improvement and warnings removing from swing packages
rupashka
parents:
715
diff
changeset
|
676 |
return listenerList.getListeners(AdjustmentListener.class); |
2 | 677 |
} |
678 |
||
679 |
||
680 |
/** |
|
681 |
* Notify listeners that the scrollbar's model has changed. |
|
682 |
* |
|
683 |
* @see #addAdjustmentListener |
|
684 |
* @see EventListenerList |
|
685 |
*/ |
|
686 |
protected void fireAdjustmentValueChanged(int id, int type, int value) { |
|
687 |
fireAdjustmentValueChanged(id, type, value, getValueIsAdjusting()); |
|
688 |
} |
|
689 |
||
690 |
/** |
|
691 |
* Notify listeners that the scrollbar's model has changed. |
|
692 |
* |
|
693 |
* @see #addAdjustmentListener |
|
694 |
* @see EventListenerList |
|
695 |
*/ |
|
696 |
private void fireAdjustmentValueChanged(int id, int type, int value, |
|
697 |
boolean isAdjusting) { |
|
698 |
Object[] listeners = listenerList.getListenerList(); |
|
699 |
AdjustmentEvent e = null; |
|
700 |
for (int i = listeners.length - 2; i >= 0; i -= 2) { |
|
701 |
if (listeners[i]==AdjustmentListener.class) { |
|
702 |
if (e == null) { |
|
703 |
e = new AdjustmentEvent(this, id, type, value, isAdjusting); |
|
704 |
} |
|
705 |
((AdjustmentListener)listeners[i+1]).adjustmentValueChanged(e); |
|
706 |
} |
|
707 |
} |
|
708 |
} |
|
709 |
||
710 |
||
711 |
/** |
|
712 |
* This class listens to ChangeEvents on the model and forwards |
|
713 |
* AdjustmentEvents for the sake of backwards compatibility. |
|
714 |
* Unfortunately there's no way to determine the proper |
|
715 |
* type of the AdjustmentEvent as all updates to the model's |
|
716 |
* value are considered equivalent. |
|
717 |
*/ |
|
718 |
private class ModelListener implements ChangeListener, Serializable { |
|
719 |
public void stateChanged(ChangeEvent e) { |
|
720 |
Object obj = e.getSource(); |
|
721 |
if (obj instanceof BoundedRangeModel) { |
|
722 |
int id = AdjustmentEvent.ADJUSTMENT_VALUE_CHANGED; |
|
723 |
int type = AdjustmentEvent.TRACK; |
|
724 |
BoundedRangeModel model = (BoundedRangeModel)obj; |
|
725 |
int value = model.getValue(); |
|
726 |
boolean isAdjusting = model.getValueIsAdjusting(); |
|
727 |
fireAdjustmentValueChanged(id, type, value, isAdjusting); |
|
728 |
} |
|
729 |
} |
|
730 |
} |
|
731 |
||
732 |
// PENDING(hmuller) - the next three methods should be removed |
|
733 |
||
734 |
/** |
|
735 |
* The scrollbar is flexible along it's scrolling axis and |
|
736 |
* rigid along the other axis. |
|
737 |
*/ |
|
738 |
public Dimension getMinimumSize() { |
|
739 |
Dimension pref = getPreferredSize(); |
|
740 |
if (orientation == VERTICAL) { |
|
741 |
return new Dimension(pref.width, 5); |
|
742 |
} else { |
|
743 |
return new Dimension(5, pref.height); |
|
744 |
} |
|
745 |
} |
|
746 |
||
747 |
/** |
|
748 |
* The scrollbar is flexible along it's scrolling axis and |
|
749 |
* rigid along the other axis. |
|
750 |
*/ |
|
751 |
public Dimension getMaximumSize() { |
|
752 |
Dimension pref = getPreferredSize(); |
|
753 |
if (getOrientation() == VERTICAL) { |
|
754 |
return new Dimension(pref.width, Short.MAX_VALUE); |
|
755 |
} else { |
|
756 |
return new Dimension(Short.MAX_VALUE, pref.height); |
|
757 |
} |
|
758 |
} |
|
759 |
||
760 |
/** |
|
761 |
* Enables the component so that the knob position can be changed. |
|
762 |
* When the disabled, the knob position cannot be changed. |
|
763 |
* |
|
764 |
* @param x a boolean value, where true enables the component and |
|
765 |
* false disables it |
|
766 |
*/ |
|
767 |
public void setEnabled(boolean x) { |
|
768 |
super.setEnabled(x); |
|
769 |
Component[] children = getComponents(); |
|
1301
15e81207e1f2
6727662: Code improvement and warnings removing from swing packages
rupashka
parents:
715
diff
changeset
|
770 |
for (Component child : children) { |
15e81207e1f2
6727662: Code improvement and warnings removing from swing packages
rupashka
parents:
715
diff
changeset
|
771 |
child.setEnabled(x); |
2 | 772 |
} |
773 |
} |
|
774 |
||
775 |
/** |
|
776 |
* See readObject() and writeObject() in JComponent for more |
|
777 |
* information about serialization in Swing. |
|
778 |
*/ |
|
779 |
private void writeObject(ObjectOutputStream s) throws IOException { |
|
780 |
s.defaultWriteObject(); |
|
781 |
if (getUIClassID().equals(uiClassID)) { |
|
782 |
byte count = JComponent.getWriteObjCounter(this); |
|
783 |
JComponent.setWriteObjCounter(this, --count); |
|
784 |
if (count == 0 && ui != null) { |
|
785 |
ui.installUI(this); |
|
786 |
} |
|
787 |
} |
|
788 |
} |
|
789 |
||
790 |
||
791 |
/** |
|
792 |
* Returns a string representation of this JScrollBar. This method |
|
793 |
* is intended to be used only for debugging purposes, and the |
|
794 |
* content and format of the returned string may vary between |
|
795 |
* implementations. The returned string may be empty but may not |
|
796 |
* be <code>null</code>. |
|
797 |
* |
|
798 |
* @return a string representation of this JScrollBar. |
|
799 |
*/ |
|
800 |
protected String paramString() { |
|
801 |
String orientationString = (orientation == HORIZONTAL ? |
|
802 |
"HORIZONTAL" : "VERTICAL"); |
|
803 |
||
804 |
return super.paramString() + |
|
805 |
",blockIncrement=" + blockIncrement + |
|
806 |
",orientation=" + orientationString + |
|
807 |
",unitIncrement=" + unitIncrement; |
|
808 |
} |
|
809 |
||
810 |
///////////////// |
|
811 |
// Accessibility support |
|
812 |
//////////////// |
|
813 |
||
814 |
/** |
|
815 |
* Gets the AccessibleContext associated with this JScrollBar. |
|
816 |
* For JScrollBar, the AccessibleContext takes the form of an |
|
817 |
* AccessibleJScrollBar. |
|
818 |
* A new AccessibleJScrollBar instance is created if necessary. |
|
819 |
* |
|
820 |
* @return an AccessibleJScrollBar that serves as the |
|
821 |
* AccessibleContext of this JScrollBar |
|
822 |
*/ |
|
823 |
public AccessibleContext getAccessibleContext() { |
|
824 |
if (accessibleContext == null) { |
|
825 |
accessibleContext = new AccessibleJScrollBar(); |
|
826 |
} |
|
827 |
return accessibleContext; |
|
828 |
} |
|
829 |
||
830 |
/** |
|
831 |
* This class implements accessibility support for the |
|
832 |
* <code>JScrollBar</code> class. It provides an implementation of the |
|
833 |
* Java Accessibility API appropriate to scroll bar user-interface |
|
834 |
* elements. |
|
835 |
* <p> |
|
836 |
* <strong>Warning:</strong> |
|
837 |
* Serialized objects of this class will not be compatible with |
|
838 |
* future Swing releases. The current serialization support is |
|
839 |
* appropriate for short term storage or RMI between applications running |
|
840 |
* the same version of Swing. As of 1.4, support for long term storage |
|
20458 | 841 |
* of all JavaBeans™ |
2 | 842 |
* has been added to the <code>java.beans</code> package. |
843 |
* Please see {@link java.beans.XMLEncoder}. |
|
844 |
*/ |
|
845 |
protected class AccessibleJScrollBar extends AccessibleJComponent |
|
846 |
implements AccessibleValue { |
|
847 |
||
848 |
/** |
|
849 |
* Get the state set of this object. |
|
850 |
* |
|
851 |
* @return an instance of AccessibleState containing the current state |
|
852 |
* of the object |
|
853 |
* @see AccessibleState |
|
854 |
*/ |
|
855 |
public AccessibleStateSet getAccessibleStateSet() { |
|
856 |
AccessibleStateSet states = super.getAccessibleStateSet(); |
|
857 |
if (getValueIsAdjusting()) { |
|
858 |
states.add(AccessibleState.BUSY); |
|
859 |
} |
|
860 |
if (getOrientation() == VERTICAL) { |
|
861 |
states.add(AccessibleState.VERTICAL); |
|
862 |
} else { |
|
863 |
states.add(AccessibleState.HORIZONTAL); |
|
864 |
} |
|
865 |
return states; |
|
866 |
} |
|
867 |
||
868 |
/** |
|
869 |
* Get the role of this object. |
|
870 |
* |
|
871 |
* @return an instance of AccessibleRole describing the role of the |
|
872 |
* object |
|
873 |
*/ |
|
874 |
public AccessibleRole getAccessibleRole() { |
|
875 |
return AccessibleRole.SCROLL_BAR; |
|
876 |
} |
|
877 |
||
878 |
/** |
|
879 |
* Get the AccessibleValue associated with this object. In the |
|
880 |
* implementation of the Java Accessibility API for this class, |
|
881 |
* return this object, which is responsible for implementing the |
|
882 |
* AccessibleValue interface on behalf of itself. |
|
883 |
* |
|
884 |
* @return this object |
|
885 |
*/ |
|
886 |
public AccessibleValue getAccessibleValue() { |
|
887 |
return this; |
|
888 |
} |
|
889 |
||
890 |
/** |
|
891 |
* Get the accessible value of this object. |
|
892 |
* |
|
893 |
* @return The current value of this object. |
|
894 |
*/ |
|
895 |
public Number getCurrentAccessibleValue() { |
|
438
2ae294e4518c
6613529: Avoid duplicate object creation within JDK packages
dav
parents:
2
diff
changeset
|
896 |
return Integer.valueOf(getValue()); |
2 | 897 |
} |
898 |
||
899 |
/** |
|
900 |
* Set the value of this object as a Number. |
|
901 |
* |
|
902 |
* @return True if the value was set. |
|
903 |
*/ |
|
904 |
public boolean setCurrentAccessibleValue(Number n) { |
|
905 |
// TIGER - 4422535 |
|
906 |
if (n == null) { |
|
907 |
return false; |
|
908 |
} |
|
909 |
setValue(n.intValue()); |
|
910 |
return true; |
|
911 |
} |
|
912 |
||
913 |
/** |
|
914 |
* Get the minimum accessible value of this object. |
|
915 |
* |
|
916 |
* @return The minimum value of this object. |
|
917 |
*/ |
|
918 |
public Number getMinimumAccessibleValue() { |
|
438
2ae294e4518c
6613529: Avoid duplicate object creation within JDK packages
dav
parents:
2
diff
changeset
|
919 |
return Integer.valueOf(getMinimum()); |
2 | 920 |
} |
921 |
||
922 |
/** |
|
923 |
* Get the maximum accessible value of this object. |
|
924 |
* |
|
925 |
* @return The maximum value of this object. |
|
926 |
*/ |
|
927 |
public Number getMaximumAccessibleValue() { |
|
928 |
// TIGER - 4422362 |
|
929 |
return new Integer(model.getMaximum() - model.getExtent()); |
|
930 |
} |
|
931 |
||
932 |
} // AccessibleJScrollBar |
|
933 |
} |