2 Copyright (C) 2002, 2004, 2005, 2006, 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;
45 import java.beans.PropertyChangeEvent;
47 import javax.accessibility.Accessible;
48 import javax.accessibility.AccessibleContext;
49 import javax.accessibility.AccessibleRole;
50 import javax.accessibility.AccessibleState;
51 import javax.accessibility.AccessibleStateSet;
52 import javax.accessibility.AccessibleValue;
53 import javax.swing.event.ChangeEvent;
54 import javax.swing.event.ChangeListener;
55 import javax.swing.plaf.ScrollBarUI;
58 * The JScrollBar. Two buttons control how the values that the
59 * scroll bar can take. You can also drag the thumb or click the track
60 * to move the scroll bar. Typically, the JScrollBar is used with
61 * other components to translate the value of the bar to the viewable
62 * contents of the other components.
64 public class JScrollBar extends JComponent implements Adjustable, Accessible
67 * Provides the accessibility features for the <code>JScrollBar</code>
70 protected class AccessibleJScrollBar extends JComponent.AccessibleJComponent
71 implements AccessibleValue
73 private static final long serialVersionUID = -7758162392045586663L;
76 * Creates a new <code>AccessibleJScrollBar</code> instance.
78 protected AccessibleJScrollBar()
84 * Returns a set containing the current state of the {@link JScrollBar}
87 * @return The accessible state set.
89 public AccessibleStateSet getAccessibleStateSet()
91 AccessibleStateSet result = super.getAccessibleStateSet();
92 if (orientation == JScrollBar.HORIZONTAL)
93 result.add(AccessibleState.HORIZONTAL);
94 else if (orientation == JScrollBar.VERTICAL)
95 result.add(AccessibleState.VERTICAL);
100 * Returns the accessible role for the <code>JScrollBar</code> component.
102 * @return {@link AccessibleRole#SCROLL_BAR}.
104 public AccessibleRole getAccessibleRole()
106 return AccessibleRole.SCROLL_BAR;
110 * Returns an object that provides access to the current, minimum and
113 * @return The accessible value.
115 public AccessibleValue getAccessibleValue()
121 * Returns the current value of the {@link JScrollBar} component, as an
124 * @return The current value of the {@link JScrollBar} component.
126 public Number getCurrentAccessibleValue()
128 return new Integer(getValue());
132 * Sets the current value of the {@link JScrollBar} component and sends a
133 * {@link PropertyChangeEvent} (with the property name
134 * {@link AccessibleContext#ACCESSIBLE_VALUE_PROPERTY}) to all registered
135 * listeners. If the supplied value is <code>null</code>, this method
136 * does nothing and returns <code>false</code>.
138 * @param value the new slider value (<code>null</code> permitted).
140 * @return <code>true</code> if the slider value is updated, and
141 * <code>false</code> otherwise.
143 public boolean setCurrentAccessibleValue(Number value)
147 Number oldValue = getCurrentAccessibleValue();
148 setValue(value.intValue());
149 firePropertyChange(AccessibleContext.ACCESSIBLE_VALUE_PROPERTY, oldValue,
150 new Integer(getValue()));
155 * Returns the minimum value of the {@link JScrollBar} component, as an
158 * @return The minimum value of the {@link JScrollBar} component.
160 public Number getMinimumAccessibleValue()
162 return new Integer(getMinimum());
166 * Returns the maximum value of the {@link JScrollBar} component, as an
169 * @return The maximum value of the {@link JScrollBar} component.
171 public Number getMaximumAccessibleValue()
173 return new Integer(getMaximum() - model.getExtent());
178 * Listens for changes on the model and fires them to interested
179 * listeners on the JScrollBar, after re-sourcing them.
181 private class ScrollBarChangeListener
182 implements ChangeListener
185 public void stateChanged(ChangeEvent event)
187 Object o = event.getSource();
188 if (o instanceof BoundedRangeModel)
190 BoundedRangeModel m = (BoundedRangeModel) o;
191 fireAdjustmentValueChanged(AdjustmentEvent.ADJUSTMENT_VALUE_CHANGED,
192 AdjustmentEvent.TRACK, m.getValue(),
193 m.getValueIsAdjusting());
199 private static final long serialVersionUID = -8195169869225066566L;
201 /** How much the thumb moves when moving in a block. */
202 protected int blockIncrement = 10;
204 /** The model that holds the scroll bar's data. */
205 protected BoundedRangeModel model;
207 /** The orientation of the scroll bar. */
208 protected int orientation = SwingConstants.VERTICAL;
210 /** How much the thumb moves when moving in a unit. */
211 protected int unitIncrement = 1;
214 * This ChangeListener forwards events fired from the model and re-sources
215 * them to originate from this JScrollBar.
217 private ChangeListener sbChangeListener;
220 * Creates a new horizontal JScrollBar object with a minimum
221 * of 0, a maxmium of 100, a value of 0 and an extent of 10.
225 this(SwingConstants.VERTICAL, 0, 10, 0, 100);
229 * Creates a new JScrollBar object with a minimum of 0, a
230 * maximum of 100, a value of 0, an extent of 10 and the given
233 * @param orientation The orientation of the JScrollBar.
235 public JScrollBar(int orientation)
237 this(orientation, 0, 10, 0, 100);
241 * Creates a new JScrollBar object with the given orientation,
242 * value, min, max, and extent.
244 * @param orientation The orientation to use.
245 * @param value The value to use.
246 * @param extent The extent to use.
247 * @param min The minimum value of the scrollbar.
248 * @param max The maximum value of the scrollbar.
250 public JScrollBar(int orientation, int value, int extent, int min, int max)
252 model = new DefaultBoundedRangeModel(value, extent, min, max);
253 sbChangeListener = new ScrollBarChangeListener();
254 model.addChangeListener(sbChangeListener);
255 if (orientation != SwingConstants.HORIZONTAL
256 && orientation != SwingConstants.VERTICAL)
257 throw new IllegalArgumentException(orientation
258 + " is not a legal orientation");
259 this.orientation = orientation;
264 * This method sets the UI of this scrollbar to
267 * @param ui The UI to use with this scrollbar.
269 public void setUI(ScrollBarUI ui)
275 * This method returns the UI that is being used
276 * with this scrollbar.
278 * @return The scrollbar's current UI.
280 public ScrollBarUI getUI()
282 return (ScrollBarUI) ui;
286 * This method changes the UI to be the
287 * default for the current look and feel.
289 public void updateUI()
291 setUI((ScrollBarUI) UIManager.getUI(this));
295 * This method returns an identifier to
296 * choose the correct UI delegate for the
299 * @return The identifer to choose the UI delegate; "ScrollBarUI"
301 public String getUIClassID()
303 return "ScrollBarUI";
307 * This method returns the orientation of the scrollbar.
309 * @return The orientation of the scrollbar.
311 public int getOrientation()
317 * This method sets the orientation of the scrollbar.
319 * @param orientation The orientation of the scrollbar.
321 public void setOrientation(int orientation)
323 if (orientation != SwingConstants.HORIZONTAL
324 && orientation != SwingConstants.VERTICAL)
325 throw new IllegalArgumentException("orientation must be one of HORIZONTAL or VERTICAL");
326 if (orientation != this.orientation)
328 int oldOrientation = this.orientation;
329 this.orientation = orientation;
330 firePropertyChange("orientation", oldOrientation,
336 * This method returns the model being used with
339 * @return The scrollbar's model.
341 public BoundedRangeModel getModel()
347 * This method sets the model to use with
350 * @param newModel The new model to use with the scrollbar.
352 public void setModel(BoundedRangeModel newModel)
354 BoundedRangeModel oldModel = model;
355 if (oldModel != null)
356 oldModel.removeChangeListener(sbChangeListener);
359 model.addChangeListener(sbChangeListener);
360 firePropertyChange("model", oldModel, model);
364 * This method returns how much the scrollbar's value
365 * should change for a unit increment depending on the
368 * @param direction The direction to scroll in.
370 * @return The amount the scrollbar's value will change given the direction.
372 public int getUnitIncrement(int direction)
374 return direction * unitIncrement;
378 * This method sets the unitIncrement property.
380 * @param unitIncrement The new unitIncrement.
382 public void setUnitIncrement(int unitIncrement)
384 if (unitIncrement != this.unitIncrement)
386 int oldInc = this.unitIncrement;
387 this.unitIncrement = unitIncrement;
388 firePropertyChange("unitIncrement", oldInc,
394 * The method returns how much the scrollbar's value
395 * should change for a block increment depending on
396 * the given direction.
398 * @param direction The direction to scroll in.
400 * @return The amount the scrollbar's value will change given the direction.
402 public int getBlockIncrement(int direction)
404 return direction * blockIncrement;
408 * This method sets the blockIncrement property.
410 * @param blockIncrement The new blockIncrement.
412 public void setBlockIncrement(int blockIncrement)
414 if (blockIncrement != this.blockIncrement)
416 int oldInc = this.blockIncrement;
417 this.blockIncrement = blockIncrement;
418 firePropertyChange("blockIncrement", oldInc,
419 this.blockIncrement);
424 * This method returns the unitIncrement.
426 * @return The unitIncrement.
428 public int getUnitIncrement()
430 return unitIncrement;
434 * This method returns the blockIncrement.
436 * @return The blockIncrement.
438 public int getBlockIncrement()
440 return blockIncrement;
444 * This method returns the value of the scrollbar.
446 * @return The value of the scrollbar.
448 public int getValue()
450 return model.getValue();
454 * This method changes the value of the scrollbar.
456 * @param value The new value of the scrollbar.
458 public void setValue(int value)
460 model.setValue(value);
464 * This method returns the visible amount (AKA extent).
465 * The visible amount can be used by UI delegates to
466 * determine the size of the thumb.
468 * @return The visible amount (AKA extent).
470 public int getVisibleAmount()
472 return model.getExtent();
476 * This method sets the visible amount (AKA extent).
478 * @param extent The visible amount (AKA extent).
480 public void setVisibleAmount(int extent)
482 model.setExtent(extent);
486 * This method returns the minimum value of the scrollbar.
488 * @return The minimum value of the scrollbar.
490 public int getMinimum()
492 return model.getMinimum();
496 * This method sets the minimum value of the scrollbar.
498 * @param minimum The minimum value of the scrollbar.
500 public void setMinimum(int minimum)
502 model.setMinimum(minimum);
506 * This method returns the maximum value of the scrollbar.
508 * @return The maximum value of the scrollbar.
510 public int getMaximum()
512 return model.getMaximum();
516 * This method sets the maximum value of the scrollbar.
518 * @param maximum The maximum value of the scrollbar.
520 public void setMaximum(int maximum)
522 model.setMaximum(maximum);
526 * This method returns the model's isAjusting value.
528 * @return The model's isAdjusting value.
530 public boolean getValueIsAdjusting()
532 return model.getValueIsAdjusting();
536 * This method sets the model's isAdjusting value.
538 * @param b The new isAdjusting value.
540 public void setValueIsAdjusting(boolean b)
542 model.setValueIsAdjusting(b);
546 * This method sets the value, extent, minimum and
549 * @param newValue The new value.
550 * @param newExtent The new extent.
551 * @param newMin The new minimum.
552 * @param newMax The new maximum.
554 public void setValues(int newValue, int newExtent, int newMin, int newMax)
556 model.setRangeProperties(newValue, newExtent, newMin, newMax,
557 model.getValueIsAdjusting());
561 * This method adds an AdjustmentListener to the scroll bar.
563 * @param listener The listener to add.
565 public void addAdjustmentListener(AdjustmentListener listener)
567 listenerList.add(AdjustmentListener.class, listener);
571 * This method removes an AdjustmentListener from the scroll bar.
573 * @param listener The listener to remove.
575 public void removeAdjustmentListener(AdjustmentListener listener)
577 listenerList.remove(AdjustmentListener.class, listener);
581 * This method returns an arry of all AdjustmentListeners listening to
584 * @return An array of AdjustmentListeners listening to this scroll bar.
586 public AdjustmentListener[] getAdjustmentListeners()
588 return (AdjustmentListener[]) listenerList.getListeners(AdjustmentListener.class);
592 * This method is called to fired AdjustmentEvents to the listeners
593 * of this scroll bar. All AdjustmentEvents that are fired
594 * will have an ID of ADJUSTMENT_VALUE_CHANGED and a type of
597 * @param id The ID of the adjustment event.
598 * @param type The Type of change.
599 * @param value The new value for the property that was changed..
601 protected void fireAdjustmentValueChanged(int id, int type, int value)
603 fireAdjustmentValueChanged(id, type, value, getValueIsAdjusting());
607 * Helper method for firing adjustment events that can have their
608 * isAdjusting field modified.
610 * This is package private to avoid an accessor method.
612 * @param id the ID of the event
613 * @param type the type of the event
614 * @param value the value
615 * @param isAdjusting if the scrollbar is adjusting or not
617 void fireAdjustmentValueChanged(int id, int type, int value,
620 Object[] adjustmentListeners = listenerList.getListenerList();
621 AdjustmentEvent adjustmentEvent = new AdjustmentEvent(this, id, type,
623 for (int i = adjustmentListeners.length - 2; i >= 0; i -= 2)
625 if (adjustmentListeners[i] == AdjustmentListener.class)
626 ((AdjustmentListener) adjustmentListeners[i + 1]).adjustmentValueChanged(adjustmentEvent);
631 * This method returns the minimum size for this scroll bar.
633 * @return The minimum size.
635 public Dimension getMinimumSize()
637 return ui.getMinimumSize(this);
641 * This method returns the maximum size for this scroll bar.
643 * @return The maximum size.
645 public Dimension getMaximumSize()
647 return ui.getMaximumSize(this);
651 * This method overrides the setEnabled in JComponent.
652 * When the scroll bar is disabled, the knob cannot
655 * @param x Whether the scrollbar is enabled.
657 public void setEnabled(boolean x)
659 // nothing special needs to be done here since we
660 // just check the enabled setting before changing the value.
665 * Returns a string describing the attributes for the <code>JScrollBar</code>
666 * component, for use in debugging. The return value is guaranteed to be
667 * non-<code>null</code>, but the format of the string may vary between
670 * @return A string describing the attributes of the <code>JScrollBar</code>.
672 protected String paramString()
674 StringBuffer sb = new StringBuffer(super.paramString());
675 sb.append(",blockIncrement=").append(blockIncrement);
676 sb.append(",orientation=");
677 if (this.orientation == JScrollBar.HORIZONTAL)
678 sb.append("HORIZONTAL");
680 sb.append("VERTICAL");
681 sb.append(",unitIncrement=").append(unitIncrement);
682 return sb.toString();
686 * Returns the object that provides accessibility features for this
687 * <code>JScrollBar</code> component.
689 * @return The accessible context (an instance of
690 * {@link AccessibleJScrollBar}).
692 public AccessibleContext getAccessibleContext()
694 if (accessibleContext == null)
695 accessibleContext = new AccessibleJScrollBar();
696 return accessibleContext;