1 /*
2 * Copyright (c) 1997, 2014, 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.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
70 * of all JavaBeans™
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 * @since 1.2
81 */
82 @SuppressWarnings("serial") // Same-version serialization only
83 public class JScrollBar extends JComponent implements Adjustable, Accessible
84 {
85 /**
86 * @see #getUIClassID
87 * @see #readObject
88 */
89 private static final String uiClassID = "ScrollBarUI";
90
91 /**
92 * All changes from the model are treated as though the user moved
93 * the scrollbar knob.
94 */
95 private ChangeListener fwdAdjustmentEvents = new ModelListener();
96
97
98 /**
99 * The model that represents the scrollbar's minimum, maximum, extent
100 * (aka "visibleAmount") and current value.
101 * @see #setModel
190 /**
191 * Creates a vertical scrollbar with the following initial values:
192 * <pre>
193 * minimum = 0
194 * maximum = 100
195 * value = 0
196 * extent = 10
197 * </pre>
198 */
199 public JScrollBar() {
200 this(VERTICAL);
201 }
202
203
204 /**
205 * Sets the {@literal L&F} object that renders this component.
206 *
207 * @param ui the <code>ScrollBarUI</code> {@literal L&F} object
208 * @see UIDefaults#getUI
209 * @since 1.4
210 * @beaninfo
211 * bound: true
212 * hidden: true
213 * attribute: visualUpdate true
214 * description: The UI object that implements the Component's LookAndFeel
215 */
216 public void setUI(ScrollBarUI ui) {
217 super.setUI(ui);
218 }
219
220
221 /**
222 * Returns the delegate that implements the look and feel for
223 * this component.
224 *
225 * @return the scroll bar's current UI.
226 * @see JComponent#setUI
227 */
228 public ScrollBarUI getUI() {
229 return (ScrollBarUI)ui;
230 }
231
232
233 /**
234 * Overrides <code>JComponent.updateUI</code>.
235 * @see JComponent#updateUI
236 */
237 public void updateUI() {
238 setUI((ScrollBarUI)UIManager.getUI(this));
239 }
240
241
242 /**
243 * Returns the name of the LookAndFeel class for this component.
244 *
245 * @return "ScrollBarUI"
246 * @see JComponent#getUIClassID
247 * @see UIDefaults#getUI
248 */
249 public String getUIClassID() {
250 return uiClassID;
251 }
252
253
254
255 /**
256 * Returns the component's orientation (horizontal or vertical).
257 *
258 * @return VERTICAL or HORIZONTAL
259 * @see #setOrientation
260 * @see java.awt.Adjustable#getOrientation
261 */
262 public int getOrientation() {
263 return orientation;
264 }
265
266
267 /**
268 * Set the scrollbar's orientation to either VERTICAL or
269 * HORIZONTAL.
270 *
271 * @param orientation an orientation of the {@code JScrollBar}
272 * @exception IllegalArgumentException if orientation is not one of VERTICAL, HORIZONTAL
273 * @see #getOrientation
274 * @beaninfo
275 * preferred: true
276 * bound: true
277 * attribute: visualUpdate true
278 * description: The scrollbar's orientation.
279 * enum: VERTICAL JScrollBar.VERTICAL
280 * HORIZONTAL JScrollBar.HORIZONTAL
281 */
282 public void setOrientation(int orientation)
283 {
284 checkOrientation(orientation);
285 int oldValue = this.orientation;
286 this.orientation = orientation;
287 firePropertyChange("orientation", oldValue, orientation);
288
289 if ((oldValue != orientation) && (accessibleContext != null)) {
290 accessibleContext.firePropertyChange(
291 AccessibleContext.ACCESSIBLE_STATE_PROPERTY,
292 ((oldValue == VERTICAL)
293 ? AccessibleState.VERTICAL : AccessibleState.HORIZONTAL),
294 ((orientation == VERTICAL)
295 ? AccessibleState.VERTICAL : AccessibleState.HORIZONTAL));
296 }
297 if (orientation != oldValue) {
298 revalidate();
299 }
300 }
301
302
303 /**
304 * Returns data model that handles the scrollbar's four
305 * fundamental properties: minimum, maximum, value, extent.
306 *
307 * @return the data model
308 *
309 * @see #setModel
310 */
311 public BoundedRangeModel getModel() {
312 return model;
313 }
314
315
316 /**
317 * Sets the model that handles the scrollbar's four
318 * fundamental properties: minimum, maximum, value, extent.
319 *
320 * @param newModel a new model
321 * @see #getModel
322 * @beaninfo
323 * bound: true
324 * expert: true
325 * description: The scrollbar's BoundedRangeModel.
326 */
327 public void setModel(BoundedRangeModel newModel) {
328 Integer oldValue = null;
329 BoundedRangeModel oldModel = model;
330 if (model != null) {
331 model.removeChangeListener(fwdAdjustmentEvents);
332 oldValue = Integer.valueOf(model.getValue());
333 }
334 model = newModel;
335 if (model != null) {
336 model.addChangeListener(fwdAdjustmentEvents);
337 }
338
339 firePropertyChange("model", oldModel, model);
340
341 if (accessibleContext != null) {
342 accessibleContext.firePropertyChange(
343 AccessibleContext.ACCESSIBLE_VALUE_PROPERTY,
344 oldValue, model.getValue());
345 }
346 }
367 * @return the value of the unitIncrement property
368 * @see #setUnitIncrement
369 * @see #setValue
370 * @see Scrollable#getScrollableUnitIncrement
371 */
372 public int getUnitIncrement(int direction) {
373 return unitIncrement;
374 }
375
376
377 /**
378 * Sets the unitIncrement property.
379 * <p>
380 * Note, that if the argument is equal to the value of Integer.MIN_VALUE,
381 * the most look and feels will not provide the scrolling to the right/down.
382 * <p>
383 * Some look and feels implement custom scrolling behavior
384 * and ignore this property.
385 *
386 * @see #getUnitIncrement
387 * @beaninfo
388 * preferred: true
389 * bound: true
390 * description: The scrollbar's unit increment.
391 */
392 public void setUnitIncrement(int unitIncrement) {
393 int oldValue = this.unitIncrement;
394 this.unitIncrement = unitIncrement;
395 firePropertyChange("unitIncrement", oldValue, unitIncrement);
396 }
397
398
399 /**
400 * Returns the amount to change the scrollbar's value by,
401 * given a block (usually "page") up/down request. A ScrollBarUI
402 * implementation typically calls this method when the user clicks
403 * above or below the scrollbar "knob" to change the value
404 * up or down by large amount. Subclasses my override this
405 * method to compute a value, e.g. the change required to scroll
406 * up or down one paragraph in a text document.
407 * <p>
408 * The JScrollPane component creates scrollbars (by default)
409 * that override this method and delegate to the viewports
410 * Scrollable view, if it has one. The Scrollable interface
411 * provides a more specialized version of this method.
417 * @return the value of the blockIncrement property
418 * @see #setBlockIncrement
419 * @see #setValue
420 * @see Scrollable#getScrollableBlockIncrement
421 */
422 public int getBlockIncrement(int direction) {
423 return blockIncrement;
424 }
425
426
427 /**
428 * Sets the blockIncrement property.
429 * <p>
430 * Note, that if the argument is equal to the value of Integer.MIN_VALUE,
431 * the most look and feels will not provide the scrolling to the right/down.
432 * <p>
433 * Some look and feels implement custom scrolling behavior
434 * and ignore this property.
435 *
436 * @see #getBlockIncrement()
437 * @beaninfo
438 * preferred: true
439 * bound: true
440 * description: The scrollbar's block increment.
441 */
442 public void setBlockIncrement(int blockIncrement) {
443 int oldValue = this.blockIncrement;
444 this.blockIncrement = blockIncrement;
445 firePropertyChange("blockIncrement", oldValue, blockIncrement);
446 }
447
448
449 /**
450 * For backwards compatibility with java.awt.Scrollbar.
451 * @see Adjustable#getUnitIncrement
452 * @see #getUnitIncrement(int)
453 */
454 public int getUnitIncrement() {
455 return unitIncrement;
456 }
457
458
459 /**
460 * For backwards compatibility with java.awt.Scrollbar.
461 * @see Adjustable#getBlockIncrement
465 return blockIncrement;
466 }
467
468
469 /**
470 * Returns the scrollbar's value.
471 * @return the model's value property
472 * @see #setValue
473 */
474 public int getValue() {
475 return getModel().getValue();
476 }
477
478
479 /**
480 * Sets the scrollbar's value. This method just forwards the value
481 * to the model.
482 *
483 * @see #getValue
484 * @see BoundedRangeModel#setValue
485 * @beaninfo
486 * preferred: true
487 * description: The scrollbar's current value.
488 */
489 public void setValue(int value) {
490 BoundedRangeModel m = getModel();
491 int oldValue = m.getValue();
492 m.setValue(value);
493
494 if (accessibleContext != null) {
495 accessibleContext.firePropertyChange(
496 AccessibleContext.ACCESSIBLE_VALUE_PROPERTY,
497 Integer.valueOf(oldValue),
498 Integer.valueOf(m.getValue()));
499 }
500 }
501
502
503 /**
504 * Returns the scrollbar's extent, aka its "visibleAmount". In many
505 * scrollbar look and feel implementations the size of the
506 * scrollbar "knob" or "thumb" is proportional to the extent.
507 *
508 * @return the value of the model's extent property
509 * @see #setVisibleAmount
510 */
511 public int getVisibleAmount() {
512 return getModel().getExtent();
513 }
514
515
516 /**
517 * Set the model's extent property.
518 *
519 * @see #getVisibleAmount
520 * @see BoundedRangeModel#setExtent
521 * @beaninfo
522 * preferred: true
523 * description: The amount of the view that is currently visible.
524 */
525 public void setVisibleAmount(int extent) {
526 getModel().setExtent(extent);
527 }
528
529
530 /**
531 * Returns the minimum value supported by the scrollbar
532 * (usually zero).
533 *
534 * @return the value of the model's minimum property
535 * @see #setMinimum
536 */
537 public int getMinimum() {
538 return getModel().getMinimum();
539 }
540
541
542 /**
543 * Sets the model's minimum property.
544 *
545 * @see #getMinimum
546 * @see BoundedRangeModel#setMinimum
547 * @beaninfo
548 * preferred: true
549 * description: The scrollbar's minimum value.
550 */
551 public void setMinimum(int minimum) {
552 getModel().setMinimum(minimum);
553 }
554
555
556 /**
557 * The maximum value of the scrollbar is maximum - extent.
558 *
559 * @return the value of the model's maximum property
560 * @see #setMaximum
561 */
562 public int getMaximum() {
563 return getModel().getMaximum();
564 }
565
566
567 /**
568 * Sets the model's maximum property. Note that the scrollbar's value
569 * can only be set to maximum - extent.
570 *
571 * @see #getMaximum
572 * @see BoundedRangeModel#setMaximum
573 * @beaninfo
574 * preferred: true
575 * description: The scrollbar's maximum value.
576 */
577 public void setMaximum(int maximum) {
578 getModel().setMaximum(maximum);
579 }
580
581
582 /**
583 * True if the scrollbar knob is being dragged.
584 *
585 * @return the value of the model's valueIsAdjusting property
586 * @see #setValueIsAdjusting
587 */
588 public boolean getValueIsAdjusting() {
589 return getModel().getValueIsAdjusting();
590 }
591
592
593 /**
594 * Sets the model's valueIsAdjusting property. Scrollbar look and
595 * feel implementations should set this property to true when
596 * a knob drag begins, and to false when the drag ends. The
597 * scrollbar model will not generate ChangeEvents while
598 * valueIsAdjusting is true.
599 *
600 * @param b {@code true} if the upcoming changes to the value property are part of a series
601 *
602 * @see #getValueIsAdjusting
603 * @see BoundedRangeModel#setValueIsAdjusting
604 * @beaninfo
605 * expert: true
606 * description: True if the scrollbar thumb is being dragged.
607 */
608 public void setValueIsAdjusting(boolean b) {
609 BoundedRangeModel m = getModel();
610 boolean oldValue = m.getValueIsAdjusting();
611 m.setValueIsAdjusting(b);
612
613 if ((oldValue != b) && (accessibleContext != null)) {
614 accessibleContext.firePropertyChange(
615 AccessibleContext.ACCESSIBLE_STATE_PROPERTY,
616 ((oldValue) ? AccessibleState.BUSY : null),
617 ((b) ? AccessibleState.BUSY : null));
618 }
619 }
620
621
622 /**
623 * Sets the four BoundedRangeModel properties after forcing
624 * the arguments to obey the usual constraints:
625 * <pre>
626 * minimum ≤ value ≤ value+extent ≤ maximum
627 * </pre>
676
677 /**
678 * Removes an AdjustmentEvent listener.
679 *
680 * @param l the AdjustmentLister to remove
681 * @see #addAdjustmentListener
682 */
683 public void removeAdjustmentListener(AdjustmentListener l) {
684 listenerList.remove(AdjustmentListener.class, l);
685 }
686
687
688 /**
689 * Returns an array of all the <code>AdjustmentListener</code>s added
690 * to this JScrollBar with addAdjustmentListener().
691 *
692 * @return all of the <code>AdjustmentListener</code>s added or an empty
693 * array if no listeners have been added
694 * @since 1.4
695 */
696 public AdjustmentListener[] getAdjustmentListeners() {
697 return listenerList.getListeners(AdjustmentListener.class);
698 }
699
700
701 /**
702 * Notify listeners that the scrollbar's model has changed.
703 *
704 * @param id an integer indicating the type of event.
705 * @param type an integer indicating the adjustment type.
706 * @param value the current value of the adjustment
707 *
708 * @see #addAdjustmentListener
709 * @see EventListenerList
710 */
711 protected void fireAdjustmentValueChanged(int id, int type, int value) {
712 fireAdjustmentValueChanged(id, type, value, getValueIsAdjusting());
713 }
714
715 /**
828
829 return super.paramString() +
830 ",blockIncrement=" + blockIncrement +
831 ",orientation=" + orientationString +
832 ",unitIncrement=" + unitIncrement;
833 }
834
835 /////////////////
836 // Accessibility support
837 ////////////////
838
839 /**
840 * Gets the AccessibleContext associated with this JScrollBar.
841 * For JScrollBar, the AccessibleContext takes the form of an
842 * AccessibleJScrollBar.
843 * A new AccessibleJScrollBar instance is created if necessary.
844 *
845 * @return an AccessibleJScrollBar that serves as the
846 * AccessibleContext of this JScrollBar
847 */
848 public AccessibleContext getAccessibleContext() {
849 if (accessibleContext == null) {
850 accessibleContext = new AccessibleJScrollBar();
851 }
852 return accessibleContext;
853 }
854
855 /**
856 * This class implements accessibility support for the
857 * <code>JScrollBar</code> class. It provides an implementation of the
858 * Java Accessibility API appropriate to scroll bar user-interface
859 * elements.
860 * <p>
861 * <strong>Warning:</strong>
862 * Serialized objects of this class will not be compatible with
863 * future Swing releases. The current serialization support is
864 * appropriate for short term storage or RMI between applications running
865 * the same version of Swing. As of 1.4, support for long term storage
866 * of all JavaBeans™
867 * has been added to the <code>java.beans</code> package.
|
1 /*
2 * Copyright (c) 1997, 2015, 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 package javax.swing;
26
27 import java.io.Serializable;
28 import java.awt.Component;
29 import java.awt.Adjustable;
30 import java.awt.Dimension;
31 import java.awt.event.AdjustmentListener;
32 import java.awt.event.AdjustmentEvent;
33 import java.beans.JavaBean;
34 import java.beans.BeanProperty;
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.IOException;
42
43 /**
44 * An implementation of a scrollbar. The user positions the knob in the
45 * scrollbar to determine the contents of the viewing area. The
46 * program typically adjusts the display so that the end of the
47 * scrollbar represents the end of the displayable contents, or 100%
48 * of the contents. The start of the scrollbar is the beginning of the
49 * displayable contents, or 0%. The position of the knob within
50 * those bounds then translates to the corresponding percentage of
51 * the displayable contents.
52 * <p>
53 * Typically, as the position of the knob in the scrollbar changes
54 * a corresponding change is made to the position of the JViewport on
55 * the underlying view, changing the contents of the JViewport.
56 * <p>
57 * <strong>Warning:</strong> Swing is not thread safe. For more
58 * information see <a
59 * href="package-summary.html#threading">Swing's Threading
60 * Policy</a>.
61 * <p>
62 * <strong>Warning:</strong>
63 * Serialized objects of this class will not be compatible with
64 * future Swing releases. The current serialization support is
65 * appropriate for short term storage or RMI between applications running
66 * the same version of Swing. As of 1.4, support for long term storage
67 * of all JavaBeans™
68 * has been added to the <code>java.beans</code> package.
69 * Please see {@link java.beans.XMLEncoder}.
70 *
71 * @see JScrollPane
72 *
73 * @author David Kloba
74 * @since 1.2
75 */
76 @JavaBean(defaultProperty = "UI", description = "A component that helps determine the visible content range of an area.")
77 @SwingContainer(false)
78 @SuppressWarnings("serial") // Same-version serialization only
79 public class JScrollBar extends JComponent implements Adjustable, Accessible
80 {
81 /**
82 * @see #getUIClassID
83 * @see #readObject
84 */
85 private static final String uiClassID = "ScrollBarUI";
86
87 /**
88 * All changes from the model are treated as though the user moved
89 * the scrollbar knob.
90 */
91 private ChangeListener fwdAdjustmentEvents = new ModelListener();
92
93
94 /**
95 * The model that represents the scrollbar's minimum, maximum, extent
96 * (aka "visibleAmount") and current value.
97 * @see #setModel
186 /**
187 * Creates a vertical scrollbar with the following initial values:
188 * <pre>
189 * minimum = 0
190 * maximum = 100
191 * value = 0
192 * extent = 10
193 * </pre>
194 */
195 public JScrollBar() {
196 this(VERTICAL);
197 }
198
199
200 /**
201 * Sets the {@literal L&F} object that renders this component.
202 *
203 * @param ui the <code>ScrollBarUI</code> {@literal L&F} object
204 * @see UIDefaults#getUI
205 * @since 1.4
206 */
207 @BeanProperty(hidden = true, visualUpdate = true, description
208 = "The UI object that implements the Component's LookAndFeel")
209 public void setUI(ScrollBarUI ui) {
210 super.setUI(ui);
211 }
212
213
214 /**
215 * Returns the delegate that implements the look and feel for
216 * this component.
217 *
218 * @return the scroll bar's current UI.
219 * @see JComponent#setUI
220 */
221 public ScrollBarUI getUI() {
222 return (ScrollBarUI)ui;
223 }
224
225
226 /**
227 * Overrides <code>JComponent.updateUI</code>.
228 * @see JComponent#updateUI
229 */
230 public void updateUI() {
231 setUI((ScrollBarUI)UIManager.getUI(this));
232 }
233
234
235 /**
236 * Returns the name of the LookAndFeel class for this component.
237 *
238 * @return "ScrollBarUI"
239 * @see JComponent#getUIClassID
240 * @see UIDefaults#getUI
241 */
242 @BeanProperty(bound = false)
243 public String getUIClassID() {
244 return uiClassID;
245 }
246
247
248
249 /**
250 * Returns the component's orientation (horizontal or vertical).
251 *
252 * @return VERTICAL or HORIZONTAL
253 * @see #setOrientation
254 * @see java.awt.Adjustable#getOrientation
255 */
256 public int getOrientation() {
257 return orientation;
258 }
259
260
261 /**
262 * Set the scrollbar's orientation to either VERTICAL or
263 * HORIZONTAL.
264 *
265 * @param orientation an orientation of the {@code JScrollBar}
266 * @exception IllegalArgumentException if orientation is not one of VERTICAL, HORIZONTAL
267 * @see #getOrientation
268 */
269 @BeanProperty(preferred = true, visualUpdate = true, enumerationValues = {
270 "JScrollBar.VERTICAL",
271 "JScrollBar.HORIZONTAL"}, description
272 = "The scrollbar's orientation.")
273 public void setOrientation(int orientation)
274 {
275 checkOrientation(orientation);
276 int oldValue = this.orientation;
277 this.orientation = orientation;
278 firePropertyChange("orientation", oldValue, orientation);
279
280 if ((oldValue != orientation) && (accessibleContext != null)) {
281 accessibleContext.firePropertyChange(
282 AccessibleContext.ACCESSIBLE_STATE_PROPERTY,
283 ((oldValue == VERTICAL)
284 ? AccessibleState.VERTICAL : AccessibleState.HORIZONTAL),
285 ((orientation == VERTICAL)
286 ? AccessibleState.VERTICAL : AccessibleState.HORIZONTAL));
287 }
288 if (orientation != oldValue) {
289 revalidate();
290 }
291 }
292
293
294 /**
295 * Returns data model that handles the scrollbar's four
296 * fundamental properties: minimum, maximum, value, extent.
297 *
298 * @return the data model
299 *
300 * @see #setModel
301 */
302 public BoundedRangeModel getModel() {
303 return model;
304 }
305
306
307 /**
308 * Sets the model that handles the scrollbar's four
309 * fundamental properties: minimum, maximum, value, extent.
310 *
311 * @param newModel a new model
312 * @see #getModel
313 */
314 @BeanProperty(expert = true, description
315 = "The scrollbar's BoundedRangeModel.")
316 public void setModel(BoundedRangeModel newModel) {
317 Integer oldValue = null;
318 BoundedRangeModel oldModel = model;
319 if (model != null) {
320 model.removeChangeListener(fwdAdjustmentEvents);
321 oldValue = Integer.valueOf(model.getValue());
322 }
323 model = newModel;
324 if (model != null) {
325 model.addChangeListener(fwdAdjustmentEvents);
326 }
327
328 firePropertyChange("model", oldModel, model);
329
330 if (accessibleContext != null) {
331 accessibleContext.firePropertyChange(
332 AccessibleContext.ACCESSIBLE_VALUE_PROPERTY,
333 oldValue, model.getValue());
334 }
335 }
356 * @return the value of the unitIncrement property
357 * @see #setUnitIncrement
358 * @see #setValue
359 * @see Scrollable#getScrollableUnitIncrement
360 */
361 public int getUnitIncrement(int direction) {
362 return unitIncrement;
363 }
364
365
366 /**
367 * Sets the unitIncrement property.
368 * <p>
369 * Note, that if the argument is equal to the value of Integer.MIN_VALUE,
370 * the most look and feels will not provide the scrolling to the right/down.
371 * <p>
372 * Some look and feels implement custom scrolling behavior
373 * and ignore this property.
374 *
375 * @see #getUnitIncrement
376 */
377 @BeanProperty(preferred = true, description
378 = "The scrollbar's unit increment.")
379 public void setUnitIncrement(int unitIncrement) {
380 int oldValue = this.unitIncrement;
381 this.unitIncrement = unitIncrement;
382 firePropertyChange("unitIncrement", oldValue, unitIncrement);
383 }
384
385
386 /**
387 * Returns the amount to change the scrollbar's value by,
388 * given a block (usually "page") up/down request. A ScrollBarUI
389 * implementation typically calls this method when the user clicks
390 * above or below the scrollbar "knob" to change the value
391 * up or down by large amount. Subclasses my override this
392 * method to compute a value, e.g. the change required to scroll
393 * up or down one paragraph in a text document.
394 * <p>
395 * The JScrollPane component creates scrollbars (by default)
396 * that override this method and delegate to the viewports
397 * Scrollable view, if it has one. The Scrollable interface
398 * provides a more specialized version of this method.
404 * @return the value of the blockIncrement property
405 * @see #setBlockIncrement
406 * @see #setValue
407 * @see Scrollable#getScrollableBlockIncrement
408 */
409 public int getBlockIncrement(int direction) {
410 return blockIncrement;
411 }
412
413
414 /**
415 * Sets the blockIncrement property.
416 * <p>
417 * Note, that if the argument is equal to the value of Integer.MIN_VALUE,
418 * the most look and feels will not provide the scrolling to the right/down.
419 * <p>
420 * Some look and feels implement custom scrolling behavior
421 * and ignore this property.
422 *
423 * @see #getBlockIncrement()
424 */
425 @BeanProperty(preferred = true, description
426 = "The scrollbar's block increment.")
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
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 */
471 @BeanProperty(bound = false, preferred = true, description
472 = "The scrollbar's current value.")
473 public void setValue(int value) {
474 BoundedRangeModel m = getModel();
475 int oldValue = m.getValue();
476 m.setValue(value);
477
478 if (accessibleContext != null) {
479 accessibleContext.firePropertyChange(
480 AccessibleContext.ACCESSIBLE_VALUE_PROPERTY,
481 Integer.valueOf(oldValue),
482 Integer.valueOf(m.getValue()));
483 }
484 }
485
486
487 /**
488 * Returns the scrollbar's extent, aka its "visibleAmount". In many
489 * scrollbar look and feel implementations the size of the
490 * scrollbar "knob" or "thumb" is proportional to the extent.
491 *
492 * @return the value of the model's extent property
493 * @see #setVisibleAmount
494 */
495 public int getVisibleAmount() {
496 return getModel().getExtent();
497 }
498
499
500 /**
501 * Set the model's extent property.
502 *
503 * @see #getVisibleAmount
504 * @see BoundedRangeModel#setExtent
505 */
506 @BeanProperty(bound = false, preferred = true, description
507 = "The amount of the view that is currently visible.")
508 public void setVisibleAmount(int extent) {
509 getModel().setExtent(extent);
510 }
511
512
513 /**
514 * Returns the minimum value supported by the scrollbar
515 * (usually zero).
516 *
517 * @return the value of the model's minimum property
518 * @see #setMinimum
519 */
520 public int getMinimum() {
521 return getModel().getMinimum();
522 }
523
524
525 /**
526 * Sets the model's minimum property.
527 *
528 * @see #getMinimum
529 * @see BoundedRangeModel#setMinimum
530 */
531 @BeanProperty(bound = false, preferred = true, description
532 = "The scrollbar's minimum value.")
533 public void setMinimum(int minimum) {
534 getModel().setMinimum(minimum);
535 }
536
537
538 /**
539 * The maximum value of the scrollbar is maximum - extent.
540 *
541 * @return the value of the model's maximum property
542 * @see #setMaximum
543 */
544 public int getMaximum() {
545 return getModel().getMaximum();
546 }
547
548
549 /**
550 * Sets the model's maximum property. Note that the scrollbar's value
551 * can only be set to maximum - extent.
552 *
553 * @see #getMaximum
554 * @see BoundedRangeModel#setMaximum
555 */
556 @BeanProperty(bound = false, preferred = true, description
557 = "The scrollbar's maximum value.")
558 public void setMaximum(int maximum) {
559 getModel().setMaximum(maximum);
560 }
561
562
563 /**
564 * True if the scrollbar knob is being dragged.
565 *
566 * @return the value of the model's valueIsAdjusting property
567 * @see #setValueIsAdjusting
568 */
569 public boolean getValueIsAdjusting() {
570 return getModel().getValueIsAdjusting();
571 }
572
573
574 /**
575 * Sets the model's valueIsAdjusting property. Scrollbar look and
576 * feel implementations should set this property to true when
577 * a knob drag begins, and to false when the drag ends. The
578 * scrollbar model will not generate ChangeEvents while
579 * valueIsAdjusting is true.
580 *
581 * @param b {@code true} if the upcoming changes to the value property are part of a series
582 *
583 * @see #getValueIsAdjusting
584 * @see BoundedRangeModel#setValueIsAdjusting
585 */
586 @BeanProperty(bound = false, expert = true, description
587 = "True if the scrollbar thumb is being dragged.")
588 public void setValueIsAdjusting(boolean b) {
589 BoundedRangeModel m = getModel();
590 boolean oldValue = m.getValueIsAdjusting();
591 m.setValueIsAdjusting(b);
592
593 if ((oldValue != b) && (accessibleContext != null)) {
594 accessibleContext.firePropertyChange(
595 AccessibleContext.ACCESSIBLE_STATE_PROPERTY,
596 ((oldValue) ? AccessibleState.BUSY : null),
597 ((b) ? AccessibleState.BUSY : null));
598 }
599 }
600
601
602 /**
603 * Sets the four BoundedRangeModel properties after forcing
604 * the arguments to obey the usual constraints:
605 * <pre>
606 * minimum ≤ value ≤ value+extent ≤ maximum
607 * </pre>
656
657 /**
658 * Removes an AdjustmentEvent listener.
659 *
660 * @param l the AdjustmentLister to remove
661 * @see #addAdjustmentListener
662 */
663 public void removeAdjustmentListener(AdjustmentListener l) {
664 listenerList.remove(AdjustmentListener.class, l);
665 }
666
667
668 /**
669 * Returns an array of all the <code>AdjustmentListener</code>s added
670 * to this JScrollBar with addAdjustmentListener().
671 *
672 * @return all of the <code>AdjustmentListener</code>s added or an empty
673 * array if no listeners have been added
674 * @since 1.4
675 */
676 @BeanProperty(bound = false)
677 public AdjustmentListener[] getAdjustmentListeners() {
678 return listenerList.getListeners(AdjustmentListener.class);
679 }
680
681
682 /**
683 * Notify listeners that the scrollbar's model has changed.
684 *
685 * @param id an integer indicating the type of event.
686 * @param type an integer indicating the adjustment type.
687 * @param value the current value of the adjustment
688 *
689 * @see #addAdjustmentListener
690 * @see EventListenerList
691 */
692 protected void fireAdjustmentValueChanged(int id, int type, int value) {
693 fireAdjustmentValueChanged(id, type, value, getValueIsAdjusting());
694 }
695
696 /**
809
810 return super.paramString() +
811 ",blockIncrement=" + blockIncrement +
812 ",orientation=" + orientationString +
813 ",unitIncrement=" + unitIncrement;
814 }
815
816 /////////////////
817 // Accessibility support
818 ////////////////
819
820 /**
821 * Gets the AccessibleContext associated with this JScrollBar.
822 * For JScrollBar, the AccessibleContext takes the form of an
823 * AccessibleJScrollBar.
824 * A new AccessibleJScrollBar instance is created if necessary.
825 *
826 * @return an AccessibleJScrollBar that serves as the
827 * AccessibleContext of this JScrollBar
828 */
829 @BeanProperty(bound = false)
830 public AccessibleContext getAccessibleContext() {
831 if (accessibleContext == null) {
832 accessibleContext = new AccessibleJScrollBar();
833 }
834 return accessibleContext;
835 }
836
837 /**
838 * This class implements accessibility support for the
839 * <code>JScrollBar</code> class. It provides an implementation of the
840 * Java Accessibility API appropriate to scroll bar user-interface
841 * elements.
842 * <p>
843 * <strong>Warning:</strong>
844 * Serialized objects of this class will not be compatible with
845 * future Swing releases. The current serialization support is
846 * appropriate for short term storage or RMI between applications running
847 * the same version of Swing. As of 1.4, support for long term storage
848 * of all JavaBeans™
849 * has been added to the <code>java.beans</code> package.
|