2 Copyright (C) 2002, 2004 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., 59 Temple Place, Suite 330, 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. */
40 import java.awt.Adjustable;
41 import java.awt.Dimension;
42 import java.awt.event.AdjustmentEvent;
43 import java.awt.event.AdjustmentListener;
45 import javax.accessibility.Accessible;
46 import javax.accessibility.AccessibleContext;
47 import javax.accessibility.AccessibleRole;
48 import javax.accessibility.AccessibleStateSet;
49 import javax.accessibility.AccessibleValue;
50 import javax.swing.event.ChangeEvent;
51 import javax.swing.event.ChangeListener;
52 import javax.swing.plaf.ScrollBarUI;
56 * The JScrollBar. Two buttons control how the values that the
57 * scroll bar can take. You can also drag the thumb or click the track
58 * to move the scroll bar. Typically, the JScrollBar is used with
59 * other components to translate the value of the bar to the viewable
60 * contents of the other components.
62 public class JScrollBar extends JComponent implements Adjustable, Accessible
67 protected class AccessibleJScrollBar extends JComponent.AccessibleJComponent
68 implements AccessibleValue
70 private static final long serialVersionUID = -7758162392045586663L;
73 * Creates a new AccessibleJSlider object.
75 * @param value0 DOCUMENT ME!
77 protected AccessibleJScrollBar()
85 * @return DOCUMENT ME!
87 public AccessibleStateSet getAccessibleStateSet()
95 * @return DOCUMENT ME!
97 public AccessibleRole getAccessibleRole()
105 * @return DOCUMENT ME!
107 public AccessibleValue getAccessibleValue()
115 * @return DOCUMENT ME!
117 public Number getCurrentAccessibleValue()
123 * setCurrentAccessibleValue
129 public boolean setCurrentAccessibleValue(Number value0)
135 * getMinimumAccessibleValue
139 public Number getMinimumAccessibleValue()
145 * getMaximumAccessibleValue
149 public Number getMaximumAccessibleValue()
155 private static final long serialVersionUID = -8195169869225066566L;
157 /** Fired in a PropertyChangeEvent when the "blockIncrement" changes. */
158 public static final String BLOCK_INCREMENT_CHANGED_PROPERTY = "blockIncrement";
160 /** Fired in a PropertyChangeEvent when the "model" changes. */
161 public static final String MODEL_CHANGED_PROPERTY = "model";
163 /** Fired in a PropertyChangeEvent when the "orientation" changes. */
164 public static final String ORIENTATION_CHANGED_PROPERTY = "orientation";
166 /** Fired in a PropertyChangeEvent when the "unitIncrement" changes. */
167 public static final String UNIT_INCREMENT_CHANGED_PROPERTY = "unitIncrement";
169 /** How much the thumb moves when moving in a block. */
170 protected int blockIncrement = 10;
172 /** The model that holds the scroll bar's data. */
173 protected BoundedRangeModel model;
175 /** The orientation of the scroll bar. */
176 protected int orientation = SwingConstants.VERTICAL;
178 /** How much the thumb moves when moving in a unit. */
179 protected int unitIncrement = 1;
181 /** The ChangeListener that listens to the model. */
182 private transient ChangeListener changeListener;
184 /** The ChangeEvent that's fired. */
185 private transient ChangeEvent changeEvent;
188 * Creates a new horizontal JScrollBar object with a minimum
189 * of 0, a maxmium of 100, a value of 0 and an extent of 10.
193 this(SwingConstants.VERTICAL, 0, 10, 0, 100);
197 * Creates a new JScrollBar object with a minimum of 0, a
198 * maximum of 100, a value of 0, an extent of 10 and the given
201 * @param orientation The orientation of the JScrollBar.
203 public JScrollBar(int orientation)
205 this(orientation, 0, 10, 0, 100);
209 * Creates a new JScrollBar object with the given orientation,
210 * value, min, max, and extent.
212 * @param orientation The orientation to use.
213 * @param value The value to use.
214 * @param extent The extent to use.
215 * @param min The minimum value of the scrollbar.
216 * @param max The maximum value of the scrollbar.
218 public JScrollBar(int orientation, int value, int extent, int min, int max)
220 model = new DefaultBoundedRangeModel(value, extent, min, max);
221 if (orientation != SwingConstants.HORIZONTAL
222 && orientation != SwingConstants.VERTICAL)
223 throw new IllegalArgumentException(orientation
224 + " is not a legal orientation");
225 this.orientation = orientation;
226 changeListener = createChangeListener();
227 model.addChangeListener(changeListener);
232 * This method sets the UI of this scrollbar to
235 * @param ui The UI to use with this scrollbar.
237 public void setUI(ScrollBarUI ui)
243 * This method returns the UI that is being used
244 * with this scrollbar.
246 * @return The scrollbar's current UI.
248 public ScrollBarUI getUI()
250 return (ScrollBarUI) ui;
254 * This method changes the UI to be the
255 * default for the current look and feel.
257 public void updateUI()
259 setUI((ScrollBarUI) UIManager.getUI(this));
265 * This method returns an identifier to
266 * choose the correct UI delegate for the
269 * @return The identifer to choose the UI delegate; "ScrollBarUI"
271 public String getUIClassID()
273 return "ScrollBarUI";
277 * This method returns the orientation of the scrollbar.
279 * @return The orientation of the scrollbar.
281 public int getOrientation()
287 * This method sets the orientation of the scrollbar.
289 * @param orientation The orientation of the scrollbar.
291 public void setOrientation(int orientation)
293 if (orientation != SwingConstants.HORIZONTAL
294 && orientation != SwingConstants.VERTICAL)
295 throw new IllegalArgumentException("orientation must be one of HORIZONTAL or VERTICAL");
296 if (orientation != this.orientation)
298 int oldOrientation = this.orientation;
299 this.orientation = orientation;
300 firePropertyChange(ORIENTATION_CHANGED_PROPERTY, oldOrientation,
306 * This method returns the model being used with
309 * @return The scrollbar's model.
311 public BoundedRangeModel getModel()
317 * This method sets the model to use with
320 * @param newModel The new model to use with the scrollbar.
322 public void setModel(BoundedRangeModel newModel)
324 if (model != newModel)
326 BoundedRangeModel oldModel = model;
328 oldModel.removeChangeListener(changeListener);
329 model.addChangeListener(changeListener);
330 firePropertyChange(MODEL_CHANGED_PROPERTY, oldModel, model);
335 * This method returns how much the scrollbar's value
336 * should change for a unit increment depending on the
339 * @param direction The direction to scroll in.
341 * @return The amount the scrollbar's value will change given the direction.
343 public int getUnitIncrement(int direction)
345 return direction * unitIncrement;
349 * This method sets the unitIncrement property.
351 * @param unitIncrement The new unitIncrement.
353 public void setUnitIncrement(int unitIncrement)
355 if (unitIncrement != this.unitIncrement)
357 int oldInc = this.unitIncrement;
358 this.unitIncrement = unitIncrement;
359 firePropertyChange(UNIT_INCREMENT_CHANGED_PROPERTY, oldInc,
365 * The method returns how much the scrollbar's value
366 * should change for a block increment depending on
367 * the given direction.
369 * @param direction The direction to scroll in.
371 * @return The amount the scrollbar's value will change given the direction.
373 public int getBlockIncrement(int direction)
375 return direction * blockIncrement;
379 * This method sets the blockIncrement property.
381 * @param blockIncrement The new blockIncrement.
383 public void setBlockIncrement(int blockIncrement)
385 if (blockIncrement != this.blockIncrement)
387 int oldInc = this.blockIncrement;
388 this.blockIncrement = blockIncrement;
389 firePropertyChange(BLOCK_INCREMENT_CHANGED_PROPERTY, oldInc,
390 this.blockIncrement);
395 * This method returns the unitIncrement.
397 * @return The unitIncrement.
399 public int getUnitIncrement()
401 return unitIncrement;
405 * This method returns the blockIncrement.
407 * @return The blockIncrement.
409 public int getBlockIncrement()
411 return blockIncrement;
415 * This method returns the value of the scrollbar.
417 * @return The value of the scrollbar.
419 public int getValue()
421 return model.getValue();
425 * This method changes the value of the scrollbar.
427 * @param value The new value of the scrollbar.
429 public void setValue(int value)
431 if (isEnabled() && value != getValue())
433 model.setValue(value);
434 fireAdjustmentValueChanged(AdjustmentEvent.ADJUSTMENT_VALUE_CHANGED,
435 AdjustmentEvent.TRACK, value);
440 * This method returns the visible amount (AKA extent).
441 * The visible amount can be used by UI delegates to
442 * determine the size of the thumb.
444 * @return The visible amount (AKA extent).
446 public int getVisibleAmount()
448 return model.getExtent();
452 * This method sets the visible amount (AKA extent).
454 * @param extent The visible amount (AKA extent).
456 public void setVisibleAmount(int extent)
458 if (extent != getVisibleAmount())
460 model.setExtent(extent);
461 fireAdjustmentValueChanged(AdjustmentEvent.ADJUSTMENT_VALUE_CHANGED,
462 AdjustmentEvent.TRACK, extent);
467 * This method returns the minimum value of the scrollbar.
469 * @return The minimum value of the scrollbar.
471 public int getMinimum()
473 return model.getMinimum();
477 * This method sets the minimum value of the scrollbar.
479 * @param minimum The minimum value of the scrollbar.
481 public void setMinimum(int minimum)
483 if (minimum != getMinimum())
485 model.setMinimum(minimum);
486 fireAdjustmentValueChanged(AdjustmentEvent.ADJUSTMENT_VALUE_CHANGED,
487 AdjustmentEvent.TRACK, minimum);
492 * This method returns the maximum value of the scrollbar.
494 * @return The maximum value of the scrollbar.
496 public int getMaximum()
498 return model.getMaximum();
502 * This method sets the maximum value of the scrollbar.
504 * @param maximum The maximum value of the scrollbar.
506 public void setMaximum(int maximum)
508 if (maximum != getMaximum())
510 model.setMaximum(maximum);
511 fireAdjustmentValueChanged(AdjustmentEvent.ADJUSTMENT_VALUE_CHANGED,
512 AdjustmentEvent.TRACK, maximum);
517 * This method returns the model's isAjusting value.
519 * @return The model's isAdjusting value.
521 public boolean getValueIsAdjusting()
523 return model.getValueIsAdjusting();
527 * This method sets the model's isAdjusting value.
529 * @param b The new isAdjusting value.
531 public void setValueIsAdjusting(boolean b)
533 model.setValueIsAdjusting(b);
537 * This method sets the value, extent, minimum and
540 * @param newValue The new value.
541 * @param newExtent The new extent.
542 * @param newMin The new minimum.
543 * @param newMax The new maximum.
545 public void setValue(int newValue, int newExtent, int newMin, int newMax)
548 newValue = model.getValue();
549 // It seems to be that on any change the value is fired.
550 if (newValue != getValue() || newExtent != getVisibleAmount() ||
551 newMin != getMinimum() || newMax != getMaximum())
553 model.setRangeProperties(newValue, newExtent, newMin, newMax,
554 model.getValueIsAdjusting());
555 fireAdjustmentValueChanged(AdjustmentEvent.ADJUSTMENT_VALUE_CHANGED,
556 AdjustmentEvent.TRACK, newValue);
561 * This method creates a new ChangeListener.
563 * @return A new ChangeListener.
565 private ChangeListener createChangeListener()
567 return new ChangeListener()
569 public void stateChanged(ChangeEvent e)
577 * This method is called whenever the model fires a ChangeEvent. It should
578 * propagate the ChangeEvent to its listeners with a new ChangeEvent that
579 * identifies the scroll bar as the source.
581 private void fireStateChanged()
583 Object[] changeListeners = listenerList.getListenerList();
584 if (changeEvent == null)
585 changeEvent = new ChangeEvent(this);
586 for (int i = changeListeners.length - 2; i >= 0; i -= 2)
588 if (changeListeners[i] == ChangeListener.class)
589 ((ChangeListener) changeListeners[i + 1]).stateChanged(changeEvent);
594 * This method adds a ChangeListener to the scroll bar.
596 * @param listener The listener to add.
598 public void addChangeListener(ChangeListener listener)
600 listenerList.add(ChangeListener.class, listener);
604 * This method removes a ChangeListener from the scroll bar.
606 * @param listener The listener to remove.
608 public void removeChangeListener(ChangeListener listener)
610 listenerList.remove(ChangeListener.class, listener);
614 * This method returns an array of all ChangeListeners listening to this
617 * @return An array of ChangeListeners listening to this scroll bar.
619 public ChangeListener[] getChangeListeners()
621 return (ChangeListener[]) listenerList.getListeners(ChangeListener.class);
625 * This method adds an AdjustmentListener to the scroll bar.
627 * @param listener The listener to add.
629 public void addAdjustmentListener(AdjustmentListener listener)
631 listenerList.add(AdjustmentListener.class, listener);
635 * This method removes an AdjustmentListener from the scroll bar.
637 * @param listener The listener to remove.
639 public void removeAdjustmentListener(AdjustmentListener listener)
641 listenerList.remove(AdjustmentListener.class, listener);
645 * This method returns an arry of all AdjustmentListeners listening to
648 * @return An array of AdjustmentListeners listening to this scroll bar.
650 public AdjustmentListener[] getAdjustmentListeners()
652 return (AdjustmentListener[]) listenerList.getListeners(AdjustmentListener.class);
656 * This method is called to fired AdjustmentEvents to the listeners
657 * of this scroll bar. All AdjustmentEvents that are fired
658 * will have an ID of ADJUSTMENT_VALUE_CHANGED and a type of
661 * @param id The ID of the adjustment event.
662 * @param type The Type of change.
663 * @param value The new value for the property that was changed..
665 protected void fireAdjustmentValueChanged(int id, int type, int value)
667 Object[] adjustmentListeners = listenerList.getListenerList();
668 AdjustmentEvent adjustmentEvent = new AdjustmentEvent(this,
669 AdjustmentEvent.ADJUSTMENT_VALUE_CHANGED,
670 AdjustmentEvent.TRACK,
672 for (int i = adjustmentListeners.length - 2; i >= 0; i -= 2)
674 if (adjustmentListeners[i] == AdjustmentListener.class)
675 ((AdjustmentListener) adjustmentListeners[i + 1]).adjustmentValueChanged(adjustmentEvent);
680 * This method returns the minimum size for this scroll bar.
682 * @return The minimum size.
684 public Dimension getMinimumSize()
686 return ui.getMinimumSize(this);
690 * This method returns the maximum size for this scroll bar.
692 * @return The maximum size.
694 public Dimension getMaximumSize()
696 return ui.getMaximumSize(this);
700 * This method overrides the setEnabled in JComponent.
701 * When the scroll bar is disabled, the knob cannot
704 * @param x Whether the scrollbar is enabled.
706 public void setEnabled(boolean x)
708 // nothing special needs to be done here since we
709 // just check the enabled setting before changing the value.
714 * A string that describes this JScrollBar. Normally only used
717 * @return A string describing this JScrollBar.
719 protected String paramString()
727 * @return DOCUMENT ME!
729 public AccessibleContext getAccessibleContext()
731 if (accessibleContext == null)
732 accessibleContext = new AccessibleJScrollBar();
733 return accessibleContext;