KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > JScrollBar


1 /*
2  * @(#)JScrollBar.java 1.78 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.io.Serializable JavaDoc;
11 import java.awt.Component JavaDoc;
12 import java.awt.Adjustable JavaDoc;
13 import java.awt.Dimension JavaDoc;
14 import java.awt.event.AdjustmentListener JavaDoc;
15 import java.awt.event.AdjustmentEvent JavaDoc;
16 import java.awt.Graphics JavaDoc;
17
18 import javax.swing.event.*;
19 import javax.swing.plaf.*;
20 import javax.accessibility.*;
21
22 import java.io.ObjectOutputStream JavaDoc;
23 import java.io.ObjectInputStream JavaDoc;
24 import java.io.IOException JavaDoc;
25
26
27
28 /**
29  * An implementation of a scrollbar. The user positions the knob in the
30  * scrollbar to determine the contents of the viewing area. The
31  * program typically adjusts the display so that the end of the
32  * scrollbar represents the end of the displayable contents, or 100%
33  * of the contents. The start of the scrollbar is the beginning of the
34  * displayable contents, or 0%. The position of the knob within
35  * those bounds then translates to the corresponding percentage of
36  * the displayable contents.
37  * <p>
38  * Typically, as the position of the knob in the scrollbar changes
39  * a corresponding change is made to the position of the JViewport on
40  * the underlying view, changing the contents of the JViewport.
41  * <p>
42  * <strong>Warning:</strong>
43  * Serialized objects of this class will not be compatible with
44  * future Swing releases. The current serialization support is
45  * appropriate for short term storage or RMI between applications running
46  * the same version of Swing. As of 1.4, support for long term storage
47  * of all JavaBeans<sup><font size="-2">TM</font></sup>
48  * has been added to the <code>java.beans</code> package.
49  * Please see {@link java.beans.XMLEncoder}.
50  *
51  * @see JScrollPane
52  * @beaninfo
53  * attribute: isContainer false
54  * description: A component that helps determine the visible content range of an area.
55  *
56  * @version 1.78 12/19/03
57  * @author David Kloba
58  */

59 public class JScrollBar extends JComponent JavaDoc implements Adjustable JavaDoc, Accessible
60 {
61     /**
62      * @see #getUIClassID
63      * @see #readObject
64      */

65     private static final String JavaDoc uiClassID = "ScrollBarUI";
66
67     /**
68      * All changes from the model are treated as though the user moved
69      * the scrollbar knob.
70      */

71     private ChangeListener fwdAdjustmentEvents = new ModelListener();
72
73
74     /**
75      * The model that represents the scrollbar's minimum, maximum, extent
76      * (aka "visibleAmount") and current value.
77      * @see #setModel
78      */

79     protected BoundedRangeModel JavaDoc model;
80
81
82     /**
83      * @see #setOrientation
84      */

85     protected int orientation;
86
87
88     /**
89      * @see #setUnitIncrement
90      */

91     protected int unitIncrement;
92
93
94     /**
95      * @see #setBlockIncrement
96      */

97     protected int blockIncrement;
98
99     
100     private void checkOrientation(int orientation) {
101         switch (orientation) {
102         case VERTICAL:
103         case HORIZONTAL:
104             break;
105         default:
106             throw new IllegalArgumentException JavaDoc("orientation must be one of: VERTICAL, HORIZONTAL");
107         }
108     }
109
110
111     /**
112      * Creates a scrollbar with the specified orientation,
113      * value, extent, minimum, and maximum.
114      * The "extent" is the size of the viewable area. It is also known
115      * as the "visible amount".
116      * <p>
117      * Note: Use <code>setBlockIncrement</code> to set the block
118      * increment to a size slightly smaller than the view's extent.
119      * That way, when the user jumps the knob to an adjacent position,
120      * one or two lines of the original contents remain in view.
121      *
122      * @exception IllegalArgumentException if orientation is not one of VERTICAL, HORIZONTAL
123      *
124      * @see #setOrientation
125      * @see #setValue
126      * @see #setVisibleAmount
127      * @see #setMinimum
128      * @see #setMaximum
129      */

130     public JScrollBar(int orientation, int value, int extent, int min, int max)
131     {
132         checkOrientation(orientation);
133         this.unitIncrement = 1;
134         this.blockIncrement = (extent == 0) ? 1 : extent;
135         this.orientation = orientation;
136         this.model = new DefaultBoundedRangeModel JavaDoc(value, extent, min, max);
137         this.model.addChangeListener(fwdAdjustmentEvents);
138     setRequestFocusEnabled(false);
139         updateUI();
140     }
141
142
143     /**
144      * Creates a scrollbar with the specified orientation
145      * and the following initial values:
146      * <pre>
147      * minimum = 0
148      * maximum = 100
149      * value = 0
150      * extent = 10
151      * </pre>
152      */

153     public JScrollBar(int orientation) {
154         this(orientation, 0, 10, 0, 100);
155     }
156
157
158     /**
159      * Creates a vertical scrollbar with the following initial values:
160      * <pre>
161      * minimum = 0
162      * maximum = 100
163      * value = 0
164      * extent = 10
165      * </pre>
166      */

167     public JScrollBar() {
168         this(VERTICAL);
169     }
170
171
172     /**
173      * Sets the L&F object that renders this component.
174      *
175      * @param ui the <code>ScrollBarUI</code> L&F object
176      * @see UIDefaults#getUI
177      * @since 1.4
178      * @beaninfo
179      * bound: true
180      * hidden: true
181      * attribute: visualUpdate true
182      * description: The UI object that implements the Component's LookAndFeel
183      */

184     public void setUI(ScrollBarUI ui) {
185         super.setUI(ui);
186     }
187
188
189     /**
190      * Returns the delegate that implements the look and feel for
191      * this component.
192      *
193      * @see JComponent#setUI
194      */

195     public ScrollBarUI getUI() {
196         return (ScrollBarUI)ui;
197     }
198
199
200     /**
201      * Overrides <code>JComponent.updateUI</code>.
202      * @see JComponent#updateUI
203      */

204     public void updateUI() {
205         setUI((ScrollBarUI)UIManager.getUI(this));
206     }
207
208
209     /**
210      * Returns the name of the LookAndFeel class for this component.
211      *
212      * @return "ScrollBarUI"
213      * @see JComponent#getUIClassID
214      * @see UIDefaults#getUI
215      */

216     public String JavaDoc getUIClassID() {
217         return uiClassID;
218     }
219
220
221
222     /**
223      * Returns the component's orientation (horizontal or vertical).
224      *
225      * @return VERTICAL or HORIZONTAL
226      * @see #setOrientation
227      * @see java.awt.Adjustable#getOrientation
228      */

229     public int getOrientation() {
230         return orientation;
231     }
232
233
234     /**
235      * Set the scrollbar's orientation to either VERTICAL or
236      * HORIZONTAL.
237      *
238      * @exception IllegalArgumentException if orientation is not one of VERTICAL, HORIZONTAL
239      * @see #getOrientation
240      * @beaninfo
241      * preferred: true
242      * bound: true
243      * attribute: visualUpdate true
244      * description: The scrollbar's orientation.
245      * enum: VERTICAL JScrollBar.VERTICAL
246      * HORIZONTAL JScrollBar.HORIZONTAL
247      */

248     public void setOrientation(int orientation)
249     {
250         checkOrientation(orientation);
251         int oldValue = this.orientation;
252         this.orientation = orientation;
253         firePropertyChange("orientation", oldValue, orientation);
254
255         if ((oldValue != orientation) && (accessibleContext != null)) {
256             accessibleContext.firePropertyChange(
257                     AccessibleContext.ACCESSIBLE_STATE_PROPERTY,
258                     ((oldValue == VERTICAL)
259                      ? AccessibleState.VERTICAL : AccessibleState.HORIZONTAL),
260                     ((orientation == VERTICAL)
261                      ? AccessibleState.VERTICAL : AccessibleState.HORIZONTAL));
262         }
263         if (orientation != oldValue) {
264             revalidate();
265         }
266     }
267
268
269     /**
270      * Returns data model that handles the scrollbar's four
271      * fundamental properties: minimum, maximum, value, extent.
272      *
273      * @see #setModel
274      */

275     public BoundedRangeModel JavaDoc getModel() {
276         return model;
277     }
278
279
280     /**
281      * Sets the model that handles the scrollbar's four
282      * fundamental properties: minimum, maximum, value, extent.
283      *
284      * @see #getModel
285      * @beaninfo
286      * bound: true
287      * expert: true
288      * description: The scrollbar's BoundedRangeModel.
289      */

290     public void setModel(BoundedRangeModel JavaDoc newModel) {
291         Integer JavaDoc oldValue = null;
292         BoundedRangeModel JavaDoc oldModel = model;
293         if (model != null) {
294             model.removeChangeListener(fwdAdjustmentEvents);
295             oldValue = new Integer JavaDoc(model.getValue());
296         }
297         model = newModel;
298         if (model != null) {
299             model.addChangeListener(fwdAdjustmentEvents);
300         }
301
302         firePropertyChange("model", oldModel, model);
303
304         if (accessibleContext != null) {
305             accessibleContext.firePropertyChange(
306                     AccessibleContext.ACCESSIBLE_VALUE_PROPERTY,
307                     oldValue, new Integer JavaDoc(model.getValue()));
308         }
309     }
310
311
312     /**
313      * Returns the amount to change the scrollbar's value by,
314      * given a unit up/down request. A ScrollBarUI implementation
315      * typically calls this method when the user clicks on a scrollbar
316      * up/down arrow and uses the result to update the scrollbar's
317      * value. Subclasses my override this method to compute
318      * a value, e.g. the change required to scroll up or down one
319      * (variable height) line text or one row in a table.
320      * <p>
321      * The JScrollPane component creates scrollbars (by default)
322      * that override this method and delegate to the viewports
323      * Scrollable view, if it has one. The Scrollable interface
324      * provides a more specialized version of this method.
325      *
326      * @param direction is -1 or 1 for up/down respectively
327      * @return the value of the unitIncrement property
328      * @see #setUnitIncrement
329      * @see #setValue
330      * @see Scrollable#getScrollableUnitIncrement
331      */

332     public int getUnitIncrement(int direction) {
333         return unitIncrement;
334     }
335
336
337     /**
338      * Sets the unitIncrement property.
339      * <p>
340      * Note, that if the argument is equal to the value of Integer.MIN_VALUE,
341      * the most look and feels will not provide the scrolling to the right/down.
342      * @see #getUnitIncrement
343      * @beaninfo
344      * preferred: true
345      * bound: true
346      * description: The scrollbar's unit increment.
347      */

348     public void setUnitIncrement(int unitIncrement) {
349         int oldValue = this.unitIncrement;
350         this.unitIncrement = unitIncrement;
351         firePropertyChange("unitIncrement", oldValue, unitIncrement);
352     }
353
354
355     /**
356      * Returns the amount to change the scrollbar's value by,
357      * given a block (usually "page") up/down request. A ScrollBarUI
358      * implementation typically calls this method when the user clicks
359      * above or below the scrollbar "knob" to change the value
360      * up or down by large amount. Subclasses my override this
361      * method to compute a value, e.g. the change required to scroll
362      * up or down one paragraph in a text document.
363      * <p>
364      * The JScrollPane component creates scrollbars (by default)
365      * that override this method and delegate to the viewports
366      * Scrollable view, if it has one. The Scrollable interface
367      * provides a more specialized version of this method.
368      *
369      * @param direction is -1 or 1 for up/down respectively
370      * @return the value of the blockIncrement property
371      * @see #setBlockIncrement
372      * @see #setValue
373      * @see Scrollable#getScrollableBlockIncrement
374      */

375     public int getBlockIncrement(int direction) {
376         return blockIncrement;
377     }
378
379
380     /**
381      * Sets the blockIncrement property.
382      * <p>
383      * Note, that if the argument is equal to the value of Integer.MIN_VALUE,
384      * the most look and feels will not provide the scrolling to the right/down.
385      * @see #getBlockIncrement()
386      * @beaninfo
387      * preferred: true
388      * bound: true
389      * description: The scrollbar's block increment.
390      */

391     public void setBlockIncrement(int blockIncrement) {
392         int oldValue = this.blockIncrement;
393         this.blockIncrement = blockIncrement;
394         firePropertyChange("blockIncrement", oldValue, blockIncrement);
395     }
396
397
398     /**
399      * For backwards compatibility with java.awt.Scrollbar.
400      * @see Adjustable#getUnitIncrement
401      * @see #getUnitIncrement(int)
402      */

403     public int getUnitIncrement() {
404         return unitIncrement;
405     }
406
407
408     /**
409      * For backwards compatibility with java.awt.Scrollbar.
410      * @see Adjustable#getBlockIncrement
411      * @see #getBlockIncrement(int)
412      */

413     public int getBlockIncrement() {
414         return blockIncrement;
415     }
416
417
418     /**
419      * Returns the scrollbar's value.
420      * @return the model's value property
421      * @see #setValue
422      */

423     public int getValue() {
424         return getModel().getValue();
425     }
426
427
428     /**
429      * Sets the scrollbar's value. This method just forwards the value
430      * to the model.
431      *
432      * @see #getValue
433      * @see BoundedRangeModel#setValue
434      * @beaninfo
435      * preferred: true
436      * description: The scrollbar's current value.
437      */

438     public void setValue(int value) {
439         BoundedRangeModel JavaDoc m = getModel();
440         int oldValue = m.getValue();
441         m.setValue(value);
442
443         if (accessibleContext != null) {
444             accessibleContext.firePropertyChange(
445                     AccessibleContext.ACCESSIBLE_VALUE_PROPERTY,
446                     new Integer JavaDoc(oldValue),
447                     new Integer JavaDoc(m.getValue()));
448         }
449     }
450
451
452     /**
453      * Returns the scrollbar's extent, aka its "visibleAmount". In many
454      * scrollbar look and feel implementations the size of the
455      * scrollbar "knob" or "thumb" is proportional to the extent.
456      *
457      * @return the value of the model's extent property
458      * @see #setVisibleAmount
459      */

460     public int getVisibleAmount() {
461         return getModel().getExtent();
462     }
463
464
465     /**
466      * Set the model's extent property.
467      *
468      * @see #getVisibleAmount
469      * @see BoundedRangeModel#setExtent
470      * @beaninfo
471      * preferred: true
472      * description: The amount of the view that is currently visible.
473      */

474     public void setVisibleAmount(int extent) {
475         getModel().setExtent(extent);
476     }
477
478
479     /**
480      * Returns the minimum value supported by the scrollbar
481      * (usually zero).
482      *
483      * @return the value of the model's minimum property
484      * @see #setMinimum
485      */

486     public int getMinimum() {
487         return getModel().getMinimum();
488     }
489
490
491     /**
492      * Sets the model's minimum property.
493      *
494      * @see #getMinimum
495      * @see BoundedRangeModel#setMinimum
496      * @beaninfo
497      * preferred: true
498      * description: The scrollbar's minimum value.
499      */

500     public void setMinimum(int minimum) {
501         getModel().setMinimum(minimum);
502     }
503
504
505     /**
506      * The maximum value of the scrollbar is maximum - extent.
507      *
508      * @return the value of the model's maximum property
509      * @see #setMaximum
510      */

511     public int getMaximum() {
512         return getModel().getMaximum();
513     }
514
515
516     /**
517      * Sets the model's maximum property. Note that the scrollbar's value
518      * can only be set to maximum - extent.
519      *
520      * @see #getMaximum
521      * @see BoundedRangeModel#setMaximum
522      * @beaninfo
523      * preferred: true
524      * description: The scrollbar's maximum value.
525      */

526     public void setMaximum(int maximum) {
527         getModel().setMaximum(maximum);
528     }
529
530
531     /**
532      * True if the scrollbar knob is being dragged.
533      *
534      * @return the value of the model's valueIsAdjusting property
535      * @see #setValueIsAdjusting
536      */

537     public boolean getValueIsAdjusting() {
538         return getModel().getValueIsAdjusting();
539     }
540
541
542     /**
543      * Sets the model's valueIsAdjusting property. Scrollbar look and
544      * feel implementations should set this property to true when
545      * a knob drag begins, and to false when the drag ends. The
546      * scrollbar model will not generate ChangeEvents while
547      * valueIsAdjusting is true.
548      *
549      * @see #getValueIsAdjusting
550      * @see BoundedRangeModel#setValueIsAdjusting
551      * @beaninfo
552      * expert: true
553      * description: True if the scrollbar thumb is being dragged.
554      */

555     public void setValueIsAdjusting(boolean b) {
556         BoundedRangeModel JavaDoc m = getModel();
557         boolean oldValue = m.getValueIsAdjusting();
558         m.setValueIsAdjusting(b);
559    
560         if ((oldValue != b) && (accessibleContext != null)) {
561             accessibleContext.firePropertyChange(
562                     AccessibleContext.ACCESSIBLE_STATE_PROPERTY,
563                     ((oldValue) ? AccessibleState.BUSY : null),
564                     ((b) ? AccessibleState.BUSY : null));
565         }
566     }
567
568
569     /**
570      * Sets the four BoundedRangeModel properties after forcing
571      * the arguments to obey the usual constraints:
572      * <pre>
573      * minimum <= value <= value+extent <= maximum
574      * </pre>
575      * <p>
576      *
577      * @see BoundedRangeModel#setRangeProperties
578      * @see #setValue
579      * @see #setVisibleAmount
580      * @see #setMinimum
581      * @see #setMaximum
582      */

583     public void setValues(int newValue, int newExtent, int newMin, int newMax)
584     {
585         BoundedRangeModel JavaDoc m = getModel();
586         int oldValue = m.getValue();
587         m.setRangeProperties(newValue, newExtent, newMin, newMax, m.getValueIsAdjusting());
588
589         if (accessibleContext != null) {
590             accessibleContext.firePropertyChange(
591                     AccessibleContext.ACCESSIBLE_VALUE_PROPERTY,
592                     new Integer JavaDoc(oldValue),
593                     new Integer JavaDoc(m.getValue()));
594         }
595     }
596
597
598     /**
599      * Adds an AdjustmentListener. Adjustment listeners are notified
600      * each time the scrollbar's model changes. Adjustment events are
601      * provided for backwards compatibility with java.awt.Scrollbar.
602      * <p>
603      * Note that the AdjustmentEvents type property will always have a
604      * placeholder value of AdjustmentEvent.TRACK because all changes
605      * to a BoundedRangeModels value are considered equivalent. To change
606      * the value of a BoundedRangeModel one just sets its value property,
607      * i.e. model.setValue(123). No information about the origin of the
608      * change, e.g. it's a block decrement, is provided. We don't try
609      * fabricate the origin of the change here.
610      *
611      * @param l the AdjustmentLister to add
612      * @see #removeAdjustmentListener
613      * @see BoundedRangeModel#addChangeListener
614      */

615     public void addAdjustmentListener(AdjustmentListener JavaDoc l) {
616         listenerList.add(AdjustmentListener JavaDoc.class, l);
617     }
618
619
620     /**
621      * Removes an AdjustmentEvent listener.
622      *
623      * @param l the AdjustmentLister to remove
624      * @see #addAdjustmentListener
625      */

626     public void removeAdjustmentListener(AdjustmentListener JavaDoc l) {
627         listenerList.remove(AdjustmentListener JavaDoc.class, l);
628     }
629
630
631     /**
632      * Returns an array of all the <code>AdjustmentListener</code>s added
633      * to this JScrollBar with addAdjustmentListener().
634      *
635      * @return all of the <code>AdjustmentListener</code>s added or an empty
636      * array if no listeners have been added
637      * @since 1.4
638      */

639     public AdjustmentListener JavaDoc[] getAdjustmentListeners() {
640         return (AdjustmentListener JavaDoc[])listenerList.getListeners(
641                 AdjustmentListener JavaDoc.class);
642     }
643
644
645     /*
646      * Notify listeners that the scrollbar's model has changed.
647      *
648      * @see #addAdjustmentListener
649      * @see EventListenerList
650      */

651     protected void fireAdjustmentValueChanged(int id, int type, int value) {
652     fireAdjustmentValueChanged(id, type, value, getValueIsAdjusting());
653     }
654     
655     /*
656      * Notify listeners that the scrollbar's model has changed.
657      *
658      * @see #addAdjustmentListener
659      * @see EventListenerList
660      */

661     private void fireAdjustmentValueChanged(int id, int type, int value,
662                         boolean isAdjusting) {
663         Object JavaDoc[] listeners = listenerList.getListenerList();
664         AdjustmentEvent JavaDoc e = null;
665         for (int i = listeners.length - 2; i >= 0; i -= 2) {
666             if (listeners[i]==AdjustmentListener JavaDoc.class) {
667                 if (e == null) {
668                     e = new AdjustmentEvent JavaDoc(this, id, type, value, isAdjusting);
669                 }
670                 ((AdjustmentListener JavaDoc)listeners[i+1]).adjustmentValueChanged(e);
671             }
672         }
673     }
674
675
676     /**
677      * This class listens to ChangeEvents on the model and forwards
678      * AdjustmentEvents for the sake of backwards compatibility.
679      * Unfortunately there's no way to determine the proper
680      * type of the AdjustmentEvent as all updates to the model's
681      * value are considered equivalent.
682      */

683     private class ModelListener implements ChangeListener, Serializable JavaDoc {
684         public void stateChanged(ChangeEvent e) {
685         Object JavaDoc obj = e.getSource();
686         if (obj instanceof BoundedRangeModel JavaDoc) {
687         int id = AdjustmentEvent.ADJUSTMENT_VALUE_CHANGED;
688         int type = AdjustmentEvent.TRACK;
689         BoundedRangeModel JavaDoc model = (BoundedRangeModel JavaDoc)obj;
690         int value = model.getValue();
691         boolean isAdjusting = model.getValueIsAdjusting();
692         fireAdjustmentValueChanged(id, type, value, isAdjusting);
693         }
694         }
695     }
696
697     // PENDING(hmuller) - the next three methods should be removed
698

699     /**
700      * The scrollbar is flexible along it's scrolling axis and
701      * rigid along the other axis.
702      */

703     public Dimension JavaDoc getMinimumSize() {
704         Dimension JavaDoc pref = getPreferredSize();
705         if (orientation == VERTICAL) {
706             return new Dimension JavaDoc(pref.width, 5);
707         } else {
708             return new Dimension JavaDoc(5, pref.height);
709         }
710     }
711
712     /**
713      * The scrollbar is flexible along it's scrolling axis and
714      * rigid along the other axis.
715      */

716     public Dimension JavaDoc getMaximumSize() {
717         Dimension JavaDoc pref = getPreferredSize();
718         if (getOrientation() == VERTICAL) {
719             return new Dimension JavaDoc(pref.width, Short.MAX_VALUE);
720         } else {
721             return new Dimension JavaDoc(Short.MAX_VALUE, pref.height);
722         }
723     }
724
725     /**
726      * Enables the component so that the knob position can be changed.
727      * When the disabled, the knob position cannot be changed.
728      *
729      * @param x a boolean value, where true enables the component and
730      * false disables it
731      */

732     public void setEnabled(boolean x) {
733         super.setEnabled(x);
734         Component JavaDoc[] children = getComponents();
735         for(int i = 0; i < children.length; i++) {
736             children[i].setEnabled(x);
737         }
738     }
739
740     /**
741      * See readObject() and writeObject() in JComponent for more
742      * information about serialization in Swing.
743      */

744     private void writeObject(ObjectOutputStream JavaDoc s) throws IOException JavaDoc {
745         s.defaultWriteObject();
746         if (getUIClassID().equals(uiClassID)) {
747             byte count = JComponent.getWriteObjCounter(this);
748             JComponent.setWriteObjCounter(this, --count);
749             if (count == 0 && ui != null) {
750                 ui.installUI(this);
751             }
752         }
753     }
754
755
756     /**
757      * Returns a string representation of this JScrollBar. This method
758      * is intended to be used only for debugging purposes, and the
759      * content and format of the returned string may vary between
760      * implementations. The returned string may be empty but may not
761      * be <code>null</code>.
762      *
763      * @return a string representation of this JScrollBar.
764      */

765     protected String JavaDoc paramString() {
766     String JavaDoc orientationString = (orientation == HORIZONTAL ?
767                     "HORIZONTAL" : "VERTICAL");
768
769     return super.paramString() +
770     ",blockIncrement=" + blockIncrement +
771     ",orientation=" + orientationString +
772     ",unitIncrement=" + unitIncrement;
773     }
774
775 /////////////////
776
// Accessibility support
777
////////////////
778

779     /**
780      * Gets the AccessibleContext associated with this JScrollBar.
781      * For JScrollBar, the AccessibleContext takes the form of an
782      * AccessibleJScrollBar.
783      * A new AccessibleJScrollBar instance is created if necessary.
784      *
785      * @return an AccessibleJScrollBar that serves as the
786      * AccessibleContext of this JScrollBar
787      */

788     public AccessibleContext getAccessibleContext() {
789         if (accessibleContext == null) {
790             accessibleContext = new AccessibleJScrollBar();
791         }
792         return accessibleContext;
793     }
794
795     /**
796      * This class implements accessibility support for the
797      * <code>JScrollBar</code> class. It provides an implementation of the
798      * Java Accessibility API appropriate to scroll bar user-interface
799      * elements.
800      * <p>
801      * <strong>Warning:</strong>
802      * Serialized objects of this class will not be compatible with
803      * future Swing releases. The current serialization support is
804      * appropriate for short term storage or RMI between applications running
805      * the same version of Swing. As of 1.4, support for long term storage
806      * of all JavaBeans<sup><font size="-2">TM</font></sup>
807      * has been added to the <code>java.beans</code> package.
808      * Please see {@link java.beans.XMLEncoder}.
809      */

810     protected class AccessibleJScrollBar extends AccessibleJComponent
811         implements AccessibleValue {
812
813         /**
814          * Get the state set of this object.
815          *
816          * @return an instance of AccessibleState containing the current state
817          * of the object
818          * @see AccessibleState
819          */

820         public AccessibleStateSet getAccessibleStateSet() {
821             AccessibleStateSet states = super.getAccessibleStateSet();
822             if (getValueIsAdjusting()) {
823                 states.add(AccessibleState.BUSY);
824             }
825             if (getOrientation() == VERTICAL) {
826                 states.add(AccessibleState.VERTICAL);
827             } else {
828                 states.add(AccessibleState.HORIZONTAL);
829             }
830             return states;
831         }
832
833         /**
834          * Get the role of this object.
835          *
836          * @return an instance of AccessibleRole describing the role of the
837          * object
838          */

839         public AccessibleRole getAccessibleRole() {
840             return AccessibleRole.SCROLL_BAR;
841         }
842
843         /**
844          * Get the AccessibleValue associated with this object. In the
845          * implementation of the Java Accessibility API for this class,
846      * return this object, which is responsible for implementing the
847          * AccessibleValue interface on behalf of itself.
848      *
849      * @return this object
850          */

851         public AccessibleValue getAccessibleValue() {
852             return this;
853         }
854
855         /**
856          * Get the accessible value of this object.
857          *
858          * @return The current value of this object.
859          */

860         public Number JavaDoc getCurrentAccessibleValue() {
861             return new Integer JavaDoc(getValue());
862         }
863
864         /**
865          * Set the value of this object as a Number.
866          *
867          * @return True if the value was set.
868          */

869         public boolean setCurrentAccessibleValue(Number JavaDoc n) {
870         // TIGER - 4422535
871
if (n == null) {
872         return false;
873         }
874         setValue(n.intValue());
875         return true;
876         }
877
878         /**
879          * Get the minimum accessible value of this object.
880          *
881          * @return The minimum value of this object.
882          */

883         public Number JavaDoc getMinimumAccessibleValue() {
884             return new Integer JavaDoc(getMinimum());
885         }
886
887         /**
888          * Get the maximum accessible value of this object.
889          *
890          * @return The maximum value of this object.
891          */

892         public Number JavaDoc getMaximumAccessibleValue() {
893         // TIGER - 4422362
894
return new Integer JavaDoc(model.getMaximum() - model.getExtent());
895         }
896
897     } // AccessibleJScrollBar
898
}
899
Popular Tags