Merge from mainline (gomp-merge-2005-02-26).
[official-gcc.git] / libjava / javax / swing / JScrollBar.java
blob0ed7679e77fbed744ba9253db72c45e336acf2ec
1 /* JScrollBar.java --
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)
9 any later version.
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
19 02111-1307 USA.
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
24 combination.
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. */
39 package javax.swing;
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;
53 /**
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
62 /**
63 * DOCUMENT ME!
65 protected class AccessibleJScrollBar extends JComponent.AccessibleJComponent
66 implements AccessibleValue
68 private static final long serialVersionUID = -7758162392045586663L;
70 /**
71 * Creates a new AccessibleJSlider object.
73 * @param value0 DOCUMENT ME!
75 protected AccessibleJScrollBar()
77 super();
80 /**
81 * DOCUMENT ME!
83 * @return DOCUMENT ME!
85 public AccessibleStateSet getAccessibleStateSet()
87 return null;
90 /**
91 * DOCUMENT ME!
93 * @return DOCUMENT ME!
95 public AccessibleRole getAccessibleRole()
97 return null;
101 * DOCUMENT ME!
103 * @return DOCUMENT ME!
105 public AccessibleValue getAccessibleValue()
107 return null;
111 * DOCUMENT ME!
113 * @return DOCUMENT ME!
115 public Number getCurrentAccessibleValue()
117 return null;
121 * setCurrentAccessibleValue
123 * @param value0 TODO
125 * @return boolean
127 public boolean setCurrentAccessibleValue(Number value0)
129 return false;
133 * getMinimumAccessibleValue
135 * @return Number
137 public Number getMinimumAccessibleValue()
139 return null;
143 * getMaximumAccessibleValue
145 * @return Number
147 public Number getMaximumAccessibleValue()
149 return null;
153 private static final long serialVersionUID = -8195169869225066566L;
155 /** How much the thumb moves when moving in a block. */
156 protected int blockIncrement = 10;
158 /** The model that holds the scroll bar's data. */
159 protected BoundedRangeModel model;
161 /** The orientation of the scroll bar. */
162 protected int orientation = SwingConstants.VERTICAL;
164 /** How much the thumb moves when moving in a unit. */
165 protected int unitIncrement = 1;
167 /**
168 * Creates a new horizontal JScrollBar object with a minimum
169 * of 0, a maxmium of 100, a value of 0 and an extent of 10.
171 public JScrollBar()
173 this(SwingConstants.VERTICAL, 0, 10, 0, 100);
177 * Creates a new JScrollBar object with a minimum of 0, a
178 * maximum of 100, a value of 0, an extent of 10 and the given
179 * orientation.
181 * @param orientation The orientation of the JScrollBar.
183 public JScrollBar(int orientation)
185 this(orientation, 0, 10, 0, 100);
189 * Creates a new JScrollBar object with the given orientation,
190 * value, min, max, and extent.
192 * @param orientation The orientation to use.
193 * @param value The value to use.
194 * @param extent The extent to use.
195 * @param min The minimum value of the scrollbar.
196 * @param max The maximum value of the scrollbar.
198 public JScrollBar(int orientation, int value, int extent, int min, int max)
200 model = new DefaultBoundedRangeModel(value, extent, min, max);
201 if (orientation != SwingConstants.HORIZONTAL
202 && orientation != SwingConstants.VERTICAL)
203 throw new IllegalArgumentException(orientation
204 + " is not a legal orientation");
205 this.orientation = orientation;
206 updateUI();
210 * This method sets the UI of this scrollbar to
211 * the given UI.
213 * @param ui The UI to use with this scrollbar.
215 public void setUI(ScrollBarUI ui)
217 super.setUI(ui);
221 * This method returns the UI that is being used
222 * with this scrollbar.
224 * @return The scrollbar's current UI.
226 public ScrollBarUI getUI()
228 return (ScrollBarUI) ui;
232 * This method changes the UI to be the
233 * default for the current look and feel.
235 public void updateUI()
237 setUI((ScrollBarUI) UIManager.getUI(this));
238 invalidate();
239 repaint();
243 * This method returns an identifier to
244 * choose the correct UI delegate for the
245 * scrollbar.
247 * @return The identifer to choose the UI delegate; "ScrollBarUI"
249 public String getUIClassID()
251 return "ScrollBarUI";
255 * This method returns the orientation of the scrollbar.
257 * @return The orientation of the scrollbar.
259 public int getOrientation()
261 return orientation;
265 * This method sets the orientation of the scrollbar.
267 * @param orientation The orientation of the scrollbar.
269 public void setOrientation(int orientation)
271 if (orientation != SwingConstants.HORIZONTAL
272 && orientation != SwingConstants.VERTICAL)
273 throw new IllegalArgumentException("orientation must be one of HORIZONTAL or VERTICAL");
274 if (orientation != this.orientation)
276 int oldOrientation = this.orientation;
277 this.orientation = orientation;
278 firePropertyChange("orientation", oldOrientation,
279 this.orientation);
284 * This method returns the model being used with
285 * the scrollbar.
287 * @return The scrollbar's model.
289 public BoundedRangeModel getModel()
291 return model;
295 * This method sets the model to use with
296 * the scrollbar.
298 * @param newModel The new model to use with the scrollbar.
300 public void setModel(BoundedRangeModel newModel)
302 if (model != newModel)
304 BoundedRangeModel oldModel = model;
305 model = newModel;
306 firePropertyChange("model", oldModel, model);
311 * This method returns how much the scrollbar's value
312 * should change for a unit increment depending on the
313 * given direction.
315 * @param direction The direction to scroll in.
317 * @return The amount the scrollbar's value will change given the direction.
319 public int getUnitIncrement(int direction)
321 return direction * unitIncrement;
325 * This method sets the unitIncrement property.
327 * @param unitIncrement The new unitIncrement.
329 public void setUnitIncrement(int unitIncrement)
331 if (unitIncrement != this.unitIncrement)
333 int oldInc = this.unitIncrement;
334 this.unitIncrement = unitIncrement;
335 firePropertyChange("unitIncrement", oldInc,
336 this.unitIncrement);
341 * The method returns how much the scrollbar's value
342 * should change for a block increment depending on
343 * the given direction.
345 * @param direction The direction to scroll in.
347 * @return The amount the scrollbar's value will change given the direction.
349 public int getBlockIncrement(int direction)
351 return direction * blockIncrement;
355 * This method sets the blockIncrement property.
357 * @param blockIncrement The new blockIncrement.
359 public void setBlockIncrement(int blockIncrement)
361 if (blockIncrement != this.blockIncrement)
363 int oldInc = this.blockIncrement;
364 this.blockIncrement = blockIncrement;
365 firePropertyChange("blockIncrement", oldInc,
366 this.blockIncrement);
371 * This method returns the unitIncrement.
373 * @return The unitIncrement.
375 public int getUnitIncrement()
377 return unitIncrement;
381 * This method returns the blockIncrement.
383 * @return The blockIncrement.
385 public int getBlockIncrement()
387 return blockIncrement;
391 * This method returns the value of the scrollbar.
393 * @return The value of the scrollbar.
395 public int getValue()
397 return model.getValue();
401 * This method changes the value of the scrollbar.
403 * @param value The new value of the scrollbar.
405 public void setValue(int value)
407 if (isEnabled() && value != getValue())
409 model.setValue(value);
410 fireAdjustmentValueChanged(AdjustmentEvent.ADJUSTMENT_VALUE_CHANGED,
411 AdjustmentEvent.TRACK, value);
416 * This method returns the visible amount (AKA extent).
417 * The visible amount can be used by UI delegates to
418 * determine the size of the thumb.
420 * @return The visible amount (AKA extent).
422 public int getVisibleAmount()
424 return model.getExtent();
428 * This method sets the visible amount (AKA extent).
430 * @param extent The visible amount (AKA extent).
432 public void setVisibleAmount(int extent)
434 if (extent != getVisibleAmount())
436 model.setExtent(extent);
437 fireAdjustmentValueChanged(AdjustmentEvent.ADJUSTMENT_VALUE_CHANGED,
438 AdjustmentEvent.TRACK, extent);
443 * This method returns the minimum value of the scrollbar.
445 * @return The minimum value of the scrollbar.
447 public int getMinimum()
449 return model.getMinimum();
453 * This method sets the minimum value of the scrollbar.
455 * @param minimum The minimum value of the scrollbar.
457 public void setMinimum(int minimum)
459 if (minimum != getMinimum())
461 model.setMinimum(minimum);
462 fireAdjustmentValueChanged(AdjustmentEvent.ADJUSTMENT_VALUE_CHANGED,
463 AdjustmentEvent.TRACK, minimum);
468 * This method returns the maximum value of the scrollbar.
470 * @return The maximum value of the scrollbar.
472 public int getMaximum()
474 return model.getMaximum();
478 * This method sets the maximum value of the scrollbar.
480 * @param maximum The maximum value of the scrollbar.
482 public void setMaximum(int maximum)
484 if (maximum != getMaximum())
486 model.setMaximum(maximum);
487 fireAdjustmentValueChanged(AdjustmentEvent.ADJUSTMENT_VALUE_CHANGED,
488 AdjustmentEvent.TRACK, maximum);
493 * This method returns the model's isAjusting value.
495 * @return The model's isAdjusting value.
497 public boolean getValueIsAdjusting()
499 return model.getValueIsAdjusting();
503 * This method sets the model's isAdjusting value.
505 * @param b The new isAdjusting value.
507 public void setValueIsAdjusting(boolean b)
509 model.setValueIsAdjusting(b);
513 * This method sets the value, extent, minimum and
514 * maximum.
516 * @param newValue The new value.
517 * @param newExtent The new extent.
518 * @param newMin The new minimum.
519 * @param newMax The new maximum.
521 public void setValues(int newValue, int newExtent, int newMin, int newMax)
523 if (!isEnabled())
524 newValue = model.getValue();
525 // It seems to be that on any change the value is fired.
526 if (newValue != getValue() || newExtent != getVisibleAmount() ||
527 newMin != getMinimum() || newMax != getMaximum())
529 model.setRangeProperties(newValue, newExtent, newMin, newMax,
530 model.getValueIsAdjusting());
531 fireAdjustmentValueChanged(AdjustmentEvent.ADJUSTMENT_VALUE_CHANGED,
532 AdjustmentEvent.TRACK, newValue);
537 * This method adds an AdjustmentListener to the scroll bar.
539 * @param listener The listener to add.
541 public void addAdjustmentListener(AdjustmentListener listener)
543 listenerList.add(AdjustmentListener.class, listener);
547 * This method removes an AdjustmentListener from the scroll bar.
549 * @param listener The listener to remove.
551 public void removeAdjustmentListener(AdjustmentListener listener)
553 listenerList.remove(AdjustmentListener.class, listener);
557 * This method returns an arry of all AdjustmentListeners listening to
558 * this scroll bar.
560 * @return An array of AdjustmentListeners listening to this scroll bar.
562 public AdjustmentListener[] getAdjustmentListeners()
564 return (AdjustmentListener[]) listenerList.getListeners(AdjustmentListener.class);
568 * This method is called to fired AdjustmentEvents to the listeners
569 * of this scroll bar. All AdjustmentEvents that are fired
570 * will have an ID of ADJUSTMENT_VALUE_CHANGED and a type of
571 * TRACK.
573 * @param id The ID of the adjustment event.
574 * @param type The Type of change.
575 * @param value The new value for the property that was changed..
577 protected void fireAdjustmentValueChanged(int id, int type, int value)
579 Object[] adjustmentListeners = listenerList.getListenerList();
580 AdjustmentEvent adjustmentEvent = new AdjustmentEvent(this,
581 AdjustmentEvent.ADJUSTMENT_VALUE_CHANGED,
582 AdjustmentEvent.TRACK,
583 value);
584 for (int i = adjustmentListeners.length - 2; i >= 0; i -= 2)
586 if (adjustmentListeners[i] == AdjustmentListener.class)
587 ((AdjustmentListener) adjustmentListeners[i + 1]).adjustmentValueChanged(adjustmentEvent);
592 * This method returns the minimum size for this scroll bar.
594 * @return The minimum size.
596 public Dimension getMinimumSize()
598 return ui.getMinimumSize(this);
602 * This method returns the maximum size for this scroll bar.
604 * @return The maximum size.
606 public Dimension getMaximumSize()
608 return ui.getMaximumSize(this);
612 * This method overrides the setEnabled in JComponent.
613 * When the scroll bar is disabled, the knob cannot
614 * be moved.
616 * @param x Whether the scrollbar is enabled.
618 public void setEnabled(boolean x)
620 // nothing special needs to be done here since we
621 // just check the enabled setting before changing the value.
622 super.setEnabled(x);
626 * A string that describes this JScrollBar. Normally only used
627 * for debugging.
629 * @return A string describing this JScrollBar.
631 protected String paramString()
633 return "JScrollBar";
637 * DOCUMENT ME!
639 * @return DOCUMENT ME!
641 public AccessibleContext getAccessibleContext()
643 if (accessibleContext == null)
644 accessibleContext = new AccessibleJScrollBar();
645 return accessibleContext;