2 Copyright (C) 2002, 2004, 2005 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
41 import java
.awt
.Adjustable
;
42 import java
.awt
.Dimension
;
43 import java
.awt
.event
.AdjustmentEvent
;
44 import java
.awt
.event
.AdjustmentListener
;
46 import javax
.accessibility
.Accessible
;
47 import javax
.accessibility
.AccessibleContext
;
48 import javax
.accessibility
.AccessibleRole
;
49 import javax
.accessibility
.AccessibleStateSet
;
50 import javax
.accessibility
.AccessibleValue
;
51 import javax
.swing
.plaf
.ScrollBarUI
;
54 * The JScrollBar. Two buttons control how the values that the
55 * scroll bar can take. You can also drag the thumb or click the track
56 * to move the scroll bar. Typically, the JScrollBar is used with
57 * other components to translate the value of the bar to the viewable
58 * contents of the other components.
60 public class JScrollBar
extends JComponent
implements Adjustable
, Accessible
65 protected class AccessibleJScrollBar
extends JComponent
.AccessibleJComponent
66 implements AccessibleValue
68 private static final long serialVersionUID
= -7758162392045586663L;
71 * Creates a new AccessibleJSlider object.
73 protected AccessibleJScrollBar()
81 * @return DOCUMENT ME!
83 public AccessibleStateSet
getAccessibleStateSet()
91 * @return DOCUMENT ME!
93 public AccessibleRole
getAccessibleRole()
101 * @return DOCUMENT ME!
103 public AccessibleValue
getAccessibleValue()
111 * @return DOCUMENT ME!
113 public Number
getCurrentAccessibleValue()
119 * setCurrentAccessibleValue
125 public boolean setCurrentAccessibleValue(Number value0
)
131 * getMinimumAccessibleValue
135 public Number
getMinimumAccessibleValue()
141 * getMaximumAccessibleValue
145 public Number
getMaximumAccessibleValue()
151 private static final long serialVersionUID
= -8195169869225066566L;
153 /** How much the thumb moves when moving in a block. */
154 protected int blockIncrement
= 10;
156 /** The model that holds the scroll bar's data. */
157 protected BoundedRangeModel model
;
159 /** The orientation of the scroll bar. */
160 protected int orientation
= SwingConstants
.VERTICAL
;
162 /** How much the thumb moves when moving in a unit. */
163 protected int unitIncrement
= 1;
166 * Creates a new horizontal JScrollBar object with a minimum
167 * of 0, a maxmium of 100, a value of 0 and an extent of 10.
171 this(SwingConstants
.VERTICAL
, 0, 10, 0, 100);
175 * Creates a new JScrollBar object with a minimum of 0, a
176 * maximum of 100, a value of 0, an extent of 10 and the given
179 * @param orientation The orientation of the JScrollBar.
181 public JScrollBar(int orientation
)
183 this(orientation
, 0, 10, 0, 100);
187 * Creates a new JScrollBar object with the given orientation,
188 * value, min, max, and extent.
190 * @param orientation The orientation to use.
191 * @param value The value to use.
192 * @param extent The extent to use.
193 * @param min The minimum value of the scrollbar.
194 * @param max The maximum value of the scrollbar.
196 public JScrollBar(int orientation
, int value
, int extent
, int min
, int max
)
198 model
= new DefaultBoundedRangeModel(value
, extent
, min
, max
);
199 if (orientation
!= SwingConstants
.HORIZONTAL
200 && orientation
!= SwingConstants
.VERTICAL
)
201 throw new IllegalArgumentException(orientation
202 + " is not a legal orientation");
203 this.orientation
= orientation
;
208 * This method sets the UI of this scrollbar to
211 * @param ui The UI to use with this scrollbar.
213 public void setUI(ScrollBarUI ui
)
219 * This method returns the UI that is being used
220 * with this scrollbar.
222 * @return The scrollbar's current UI.
224 public ScrollBarUI
getUI()
226 return (ScrollBarUI
) ui
;
230 * This method changes the UI to be the
231 * default for the current look and feel.
233 public void updateUI()
235 setUI((ScrollBarUI
) UIManager
.getUI(this));
241 * This method returns an identifier to
242 * choose the correct UI delegate for the
245 * @return The identifer to choose the UI delegate; "ScrollBarUI"
247 public String
getUIClassID()
249 return "ScrollBarUI";
253 * This method returns the orientation of the scrollbar.
255 * @return The orientation of the scrollbar.
257 public int getOrientation()
263 * This method sets the orientation of the scrollbar.
265 * @param orientation The orientation of the scrollbar.
267 public void setOrientation(int orientation
)
269 if (orientation
!= SwingConstants
.HORIZONTAL
270 && orientation
!= SwingConstants
.VERTICAL
)
271 throw new IllegalArgumentException("orientation must be one of HORIZONTAL or VERTICAL");
272 if (orientation
!= this.orientation
)
274 int oldOrientation
= this.orientation
;
275 this.orientation
= orientation
;
276 firePropertyChange("orientation", oldOrientation
,
282 * This method returns the model being used with
285 * @return The scrollbar's model.
287 public BoundedRangeModel
getModel()
293 * This method sets the model to use with
296 * @param newModel The new model to use with the scrollbar.
298 public void setModel(BoundedRangeModel newModel
)
300 if (model
!= newModel
)
302 BoundedRangeModel oldModel
= model
;
304 firePropertyChange("model", oldModel
, model
);
309 * This method returns how much the scrollbar's value
310 * should change for a unit increment depending on the
313 * @param direction The direction to scroll in.
315 * @return The amount the scrollbar's value will change given the direction.
317 public int getUnitIncrement(int direction
)
319 return direction
* unitIncrement
;
323 * This method sets the unitIncrement property.
325 * @param unitIncrement The new unitIncrement.
327 public void setUnitIncrement(int unitIncrement
)
329 if (unitIncrement
!= this.unitIncrement
)
331 int oldInc
= this.unitIncrement
;
332 this.unitIncrement
= unitIncrement
;
333 firePropertyChange("unitIncrement", oldInc
,
339 * The method returns how much the scrollbar's value
340 * should change for a block increment depending on
341 * the given direction.
343 * @param direction The direction to scroll in.
345 * @return The amount the scrollbar's value will change given the direction.
347 public int getBlockIncrement(int direction
)
349 return direction
* blockIncrement
;
353 * This method sets the blockIncrement property.
355 * @param blockIncrement The new blockIncrement.
357 public void setBlockIncrement(int blockIncrement
)
359 if (blockIncrement
!= this.blockIncrement
)
361 int oldInc
= this.blockIncrement
;
362 this.blockIncrement
= blockIncrement
;
363 firePropertyChange("blockIncrement", oldInc
,
364 this.blockIncrement
);
369 * This method returns the unitIncrement.
371 * @return The unitIncrement.
373 public int getUnitIncrement()
375 return unitIncrement
;
379 * This method returns the blockIncrement.
381 * @return The blockIncrement.
383 public int getBlockIncrement()
385 return blockIncrement
;
389 * This method returns the value of the scrollbar.
391 * @return The value of the scrollbar.
393 public int getValue()
395 return model
.getValue();
399 * This method changes the value of the scrollbar.
401 * @param value The new value of the scrollbar.
403 public void setValue(int value
)
405 if (isEnabled() && value
!= getValue())
407 model
.setValue(value
);
408 fireAdjustmentValueChanged(AdjustmentEvent
.ADJUSTMENT_VALUE_CHANGED
,
409 AdjustmentEvent
.TRACK
, value
);
414 * This method returns the visible amount (AKA extent).
415 * The visible amount can be used by UI delegates to
416 * determine the size of the thumb.
418 * @return The visible amount (AKA extent).
420 public int getVisibleAmount()
422 return model
.getExtent();
426 * This method sets the visible amount (AKA extent).
428 * @param extent The visible amount (AKA extent).
430 public void setVisibleAmount(int extent
)
432 if (extent
!= getVisibleAmount())
434 model
.setExtent(extent
);
435 fireAdjustmentValueChanged(AdjustmentEvent
.ADJUSTMENT_VALUE_CHANGED
,
436 AdjustmentEvent
.TRACK
, extent
);
441 * This method returns the minimum value of the scrollbar.
443 * @return The minimum value of the scrollbar.
445 public int getMinimum()
447 return model
.getMinimum();
451 * This method sets the minimum value of the scrollbar.
453 * @param minimum The minimum value of the scrollbar.
455 public void setMinimum(int minimum
)
457 if (minimum
!= getMinimum())
459 model
.setMinimum(minimum
);
460 fireAdjustmentValueChanged(AdjustmentEvent
.ADJUSTMENT_VALUE_CHANGED
,
461 AdjustmentEvent
.TRACK
, minimum
);
466 * This method returns the maximum value of the scrollbar.
468 * @return The maximum value of the scrollbar.
470 public int getMaximum()
472 return model
.getMaximum();
476 * This method sets the maximum value of the scrollbar.
478 * @param maximum The maximum value of the scrollbar.
480 public void setMaximum(int maximum
)
482 if (maximum
!= getMaximum())
484 model
.setMaximum(maximum
);
485 fireAdjustmentValueChanged(AdjustmentEvent
.ADJUSTMENT_VALUE_CHANGED
,
486 AdjustmentEvent
.TRACK
, maximum
);
491 * This method returns the model's isAjusting value.
493 * @return The model's isAdjusting value.
495 public boolean getValueIsAdjusting()
497 return model
.getValueIsAdjusting();
501 * This method sets the model's isAdjusting value.
503 * @param b The new isAdjusting value.
505 public void setValueIsAdjusting(boolean b
)
507 model
.setValueIsAdjusting(b
);
511 * This method sets the value, extent, minimum and
514 * @param newValue The new value.
515 * @param newExtent The new extent.
516 * @param newMin The new minimum.
517 * @param newMax The new maximum.
519 public void setValues(int newValue
, int newExtent
, int newMin
, int newMax
)
522 newValue
= model
.getValue();
523 // It seems to be that on any change the value is fired.
524 if (newValue
!= getValue() || newExtent
!= getVisibleAmount() ||
525 newMin
!= getMinimum() || newMax
!= getMaximum())
527 model
.setRangeProperties(newValue
, newExtent
, newMin
, newMax
,
528 model
.getValueIsAdjusting());
529 fireAdjustmentValueChanged(AdjustmentEvent
.ADJUSTMENT_VALUE_CHANGED
,
530 AdjustmentEvent
.TRACK
, newValue
);
535 * This method adds an AdjustmentListener to the scroll bar.
537 * @param listener The listener to add.
539 public void addAdjustmentListener(AdjustmentListener listener
)
541 listenerList
.add(AdjustmentListener
.class, listener
);
545 * This method removes an AdjustmentListener from the scroll bar.
547 * @param listener The listener to remove.
549 public void removeAdjustmentListener(AdjustmentListener listener
)
551 listenerList
.remove(AdjustmentListener
.class, listener
);
555 * This method returns an arry of all AdjustmentListeners listening to
558 * @return An array of AdjustmentListeners listening to this scroll bar.
560 public AdjustmentListener
[] getAdjustmentListeners()
562 return (AdjustmentListener
[]) listenerList
.getListeners(AdjustmentListener
.class);
566 * This method is called to fired AdjustmentEvents to the listeners
567 * of this scroll bar. All AdjustmentEvents that are fired
568 * will have an ID of ADJUSTMENT_VALUE_CHANGED and a type of
571 * @param id The ID of the adjustment event.
572 * @param type The Type of change.
573 * @param value The new value for the property that was changed..
575 protected void fireAdjustmentValueChanged(int id
, int type
, int value
)
577 Object
[] adjustmentListeners
= listenerList
.getListenerList();
578 AdjustmentEvent adjustmentEvent
= new AdjustmentEvent(this,
579 AdjustmentEvent
.ADJUSTMENT_VALUE_CHANGED
,
580 AdjustmentEvent
.TRACK
,
582 for (int i
= adjustmentListeners
.length
- 2; i
>= 0; i
-= 2)
584 if (adjustmentListeners
[i
] == AdjustmentListener
.class)
585 ((AdjustmentListener
) adjustmentListeners
[i
+ 1]).adjustmentValueChanged(adjustmentEvent
);
590 * This method returns the minimum size for this scroll bar.
592 * @return The minimum size.
594 public Dimension
getMinimumSize()
596 return ui
.getMinimumSize(this);
600 * This method returns the maximum size for this scroll bar.
602 * @return The maximum size.
604 public Dimension
getMaximumSize()
606 return ui
.getMaximumSize(this);
610 * This method overrides the setEnabled in JComponent.
611 * When the scroll bar is disabled, the knob cannot
614 * @param x Whether the scrollbar is enabled.
616 public void setEnabled(boolean x
)
618 // nothing special needs to be done here since we
619 // just check the enabled setting before changing the value.
624 * A string that describes this JScrollBar. Normally only used
627 * @return A string describing this JScrollBar.
629 protected String
paramString()
637 * @return DOCUMENT ME!
639 public AccessibleContext
getAccessibleContext()
641 if (accessibleContext
== null)
642 accessibleContext
= new AccessibleJScrollBar();
643 return accessibleContext
;