* javax/swing/JToggleButton.java (ToggleButtonModel):
[official-gcc.git] / libjava / javax / swing / JSlider.java
blobf677f8fb4cb68c47a3ac233fc383ec4f64505356
1 /* JSlider.java --
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)
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. */
38 package javax.swing;
40 import java.awt.ComponentOrientation;
41 import java.awt.MenuContainer;
42 import java.awt.Dimension;
43 import java.awt.image.ImageObserver;
44 import java.io.IOException;
45 import java.io.ObjectOutputStream;
46 import java.io.Serializable;
47 import java.util.Dictionary;
48 import java.util.Enumeration;
49 import java.util.Hashtable;
50 import javax.accessibility.Accessible;
51 import javax.accessibility.AccessibleContext;
52 import javax.accessibility.AccessibleRole;
53 import javax.accessibility.AccessibleStateSet;
54 import javax.accessibility.AccessibleValue;
55 import javax.swing.event.ChangeEvent;
56 import javax.swing.event.ChangeListener;
57 import javax.swing.event.EventListenerList;
58 import javax.swing.plaf.SliderUI;
61 /**
62 * <p>
63 * The JSlider is a Swing component that allows selection of a value within a
64 * range by adjusting a thumb in a track. The values for the minimum,
65 * maximum, extent and value are stored in a {@link
66 * DefaultBoundedRangeModel}.
67 * </p>
69 * <p>
70 * JSliders have the following properties:
71 * </p>
73 * <table>
74 * <tr><th> Property </td><th> Stored in </td><th> Bound? </td></tr>
75 * <tr><td> extent </td><td> model </td><td> no </td></tr>
76 * <tr><td> inverted </td><td> slider </td><td> yes </td></tr>
77 * <tr><td> labelTable </td><td> slider </td><td> yes </td></tr>
78 * <tr><td> majorTickSpacing </td><td> slider </td><td> yes </td></tr>
79 * <tr><td> maximum </td><td> model </td><td> no </td></tr>
80 * <tr><td> minimum </td><td> model </td><td> no </td></tr>
81 * <tr><td> minorTickSpacing </td><td> slider </td><td> yes </td></tr>
82 * <tr><td> model </td><td> slider </td><td> yes </td></tr>
83 * <tr><td> orientation </td><td> slider </td><td> yes </td></tr>
84 * <tr><td> paintLabels </td><td> slider </td><td> yes </td></tr>
85 * <tr><td> paintTicks </td><td> slider </td><td> yes </td></tr>
86 * <tr><td> snapToTicks </td><td> slider </td><td> no </td></tr>
87 * <tr><td> value </td><td> model </td><td> no </td></tr>
88 * <tr><td> valueIsAdjusting </td><td> model </td><td> no </td></tr>
89 * </table>
91 * <p>
92 * The various behavioral aspects of these properties follows:
93 * </p>
95 * <ul>
96 * <li>
97 * When non-bound properties stored in the slider change, the slider fires
98 * ChangeEvents to its ChangeListeners.
99 * </li>
100 * <li>
101 * When bound properties stored in the slider change, the slider fires
102 * PropertyChangeEvents to its PropertyChangeListeners
103 * </li>
104 * <li>
105 * If any of the model's properties change, it fires a ChangeEvent to its
106 * ChangeListeners, which include the slider.
107 * </li>
108 * <li>
109 * If the slider receives a ChangeEvent from its model, it will propagate the
110 * ChangeEvent to its ChangeListeners, with the ChangeEvent's "source"
111 * property set to refer to the slider, rather than the model.
112 * </li>
113 * </ul>
115 public class JSlider extends JComponent implements SwingConstants, Accessible,
116 ImageObserver,
117 MenuContainer, Serializable
119 /** DOCUMENT ME! */
120 static final long serialVersionUID = -1441275936141218479L;
123 * DOCUMENT ME!
125 protected class AccessibleJSlider extends JComponent.AccessibleJComponent
126 implements AccessibleValue
129 * Creates a new AccessibleJSlider object.
131 * @param value0 DOCUMENT ME!
133 protected AccessibleJSlider(JSlider value0)
135 super(value0);
139 * DOCUMENT ME!
141 * @return DOCUMENT ME!
143 public AccessibleStateSet getAccessibleStateSet()
145 return null;
149 * DOCUMENT ME!
151 * @return DOCUMENT ME!
153 public AccessibleRole getAccessibleRole()
155 return null;
159 * DOCUMENT ME!
161 * @return DOCUMENT ME!
163 public AccessibleValue getAccessibleValue()
165 return null;
169 * DOCUMENT ME!
171 * @return DOCUMENT ME!
173 public Number getCurrentAccessibleValue()
175 return null;
179 * setCurrentAccessibleValue
181 * @param value0 TODO
183 * @return boolean
185 public boolean setCurrentAccessibleValue(Number value0)
187 return false;
191 * getMinimumAccessibleValue
193 * @return Number
195 public Number getMinimumAccessibleValue()
197 return null;
201 * getMaximumAccessibleValue
203 * @return Number
205 public Number getMaximumAccessibleValue()
207 return null;
211 /** Fired in a PropertyChangeEvent when the "inverted" property changes. */
212 public static final String INVERTED_CHANGED_PROPERTY = "inverted";
214 /** Fired in a PropertyChangeEvent when the "labelTable" property changes. */
215 public static final String LABEL_TABLE_CHANGED_PROPERTY = "labelTable";
218 * Fired in a PropertyChangeEvent when the "majorTickSpacing" property
219 * changes.
221 public static final String MAJOR_TICK_SPACING_CHANGED_PROPERTY = "majorTickSpacing";
224 * Fired in a PropertyChangeEvent when the "minorTickSpacing" property
225 * changes.
227 public static final String MINOR_TICK_SPACING_CHANGED_PROPERTY = "minorTickSpacing";
229 /** Fired in a PropertyChangeEvent when the "model" property changes. */
230 public static final String MODEL_CHANGED_PROPERTY = "model";
232 /** Fired in a PropertyChangeEvent when the "orientation" property changes. */
233 public static final String ORIENTATION_CHANGED_PROPERTY = "orientation";
235 /** Fired in a PropertyChangeEvent when the "paintLabels" property changes. */
236 public static final String PAINT_LABELS_CHANGED_PROPERTY = "paintLabels";
238 /** Fired in a PropertyChangeEvent when the "paintTicks" property changes. */
239 public static final String PAINT_TICKS_CHANGED_PROPERTY = "paintTicks";
241 /** Whether or not this slider paints its ticks. */
242 private transient boolean paintTicks = false;
244 /** Whether or not this slider paints its track. */
245 private transient boolean paintTrack = true;
247 /** Whether or not this slider paints its labels. */
248 private transient boolean paintLabels = false;
251 * A dictionary of (Integer, Component) pairs where each Component is a
252 * JLabel and the Integer determines where the label will be painted.
254 private transient Dictionary labelTable;
256 /** The model used to describe the slider. */
257 protected BoundedRangeModel sliderModel;
259 /** The space between major ticks. */
260 protected int majorTickSpacing;
262 /** The space between minor ticks. */
263 protected int minorTickSpacing;
265 /** Whether the slider snaps its values to ticks. */
266 protected boolean snapToTicks = true;
268 /** The orientation of the slider. */
269 protected int orientation = HORIZONTAL;
271 /** Whether the slider is inverted. */
272 private transient boolean isInverted;
274 /** The ChangeListener that listens to the model. */
275 protected ChangeListener changeListener;
277 /** The ChangeEvent that is passed to all listeners of this slider. */
278 protected transient ChangeEvent changeEvent;
281 * Creates a new horizontal JSlider object with a minimum of 0, a maximum of
282 * 100, and a value of 50.
284 public JSlider()
286 this(HORIZONTAL, 0, 100, 50);
290 * Creates a new JSlider object with the given orientation and a minimum of
291 * 0, a maximum of 100, and a value of 50.
293 * @param orientation The orientation of the slider.
295 public JSlider(int orientation)
297 this(orientation, 0, 100, 50);
301 * Creates a new horizontal JSlider object with the given maximum and
302 * minimum and a value that is halfway between the minimum and the
303 * maximum.
305 * @param minimum The minimum value of the JSlider.
306 * @param maximum The maximum value of the JSlider.
308 public JSlider(int minimum, int maximum)
310 this(HORIZONTAL, minimum, maximum, (maximum - minimum) / 2);
314 * Creates a new horizontal JSlider object with the given minimum, maximum,
315 * and value.
317 * @param minimum The minimum value of the JSlider.
318 * @param maximum The maximum value of the JSlider.
319 * @param value The initial value of the JSlider.
321 public JSlider(int minimum, int maximum, int value)
323 this(HORIZONTAL, minimum, maximum, value);
327 * Creates a new JSlider object with the given orientation, minimum,
328 * maximum, and value.
330 * @param orientation The orientation of the JSlider.
331 * @param minimum The minimum value of the JSlider.
332 * @param maximum The maximum value of the JSlider.
333 * @param value The initial value of the JSlider.
335 public JSlider(int orientation, int minimum, int maximum, int value)
337 sliderModel = new DefaultBoundedRangeModel(value, 0, minimum, maximum);
338 if (orientation != HORIZONTAL && orientation != VERTICAL)
339 throw new IllegalArgumentException(orientation + " is not a legal orientation");
340 this.orientation = orientation;
341 changeListener = createChangeListener();
342 sliderModel.addChangeListener(changeListener);
343 updateUI();
347 * Creates a new horizontal JSlider object with the given model.
349 * @param model The model the slider will be created with.
351 public JSlider(BoundedRangeModel model)
353 if (model == null)
354 sliderModel = new DefaultBoundedRangeModel(50, 0, 0, 100);
355 else
356 sliderModel = model;
357 changeListener = createChangeListener();
358 sliderModel.addChangeListener(changeListener);
359 updateUI();
363 * This method returns the current value of the slider.
365 * @return The value of the slider stored in the model.
367 public int getValue()
369 return sliderModel.getValue();
373 * This method sets the value of the slider.
375 * @param value The slider's new value.
377 public void setValue(int value)
379 sliderModel.setValue(value);
383 * This method returns the slider's UI delegate.
385 * @return The slider's UI delegate.
387 public SliderUI getUI()
389 return (SliderUI) ui;
393 * This method sets the slider's UI delegate.
395 * @param ui A SliderUI object to use with this slider.
397 public void setUI(SliderUI ui)
399 super.setUI(ui);
403 * This method sets this slider's UI to the UIManager's default for the
404 * current look and feel.
406 public void updateUI()
408 setUI((SliderUI) UIManager.getUI(this));
409 invalidate();
410 repaint();
414 * This method returns a name to identify which look and feel class will be
415 * the UI delegate for the slider.
417 * @return The L&F classID. "SliderUI"
419 public String getUIClassID()
421 return "SliderUI";
425 * Creates a ChangeListener for this Slider.
427 * @return A new ChangeListener.
429 protected ChangeListener createChangeListener()
431 return new ChangeListener()
433 public void stateChanged(ChangeEvent ce)
435 // No need to trigger a repaint since the UI listens to the model
436 // as well. All we need to do is pass on the stateChanged event
437 // to our listeners.
438 fireStateChanged();
444 * This method registers a listener to this slider. The listener will be
445 * informed of new ChangeEvents.
447 * @param listener The listener to register.
449 public void addChangeListener(ChangeListener listener)
451 listenerList.add(ChangeListener.class, listener);
455 * This method removes a listener from this slider.
457 * @param listener The listener to remove.
459 public void removeChangeListener(ChangeListener listener)
461 listenerList.remove(ChangeListener.class, listener);
465 * This method is called whenever the model fires a ChangeEvent. It should
466 * propagate the ChangeEvent to its listeners with a new ChangeEvent that
467 * identifies the slider as the source.
469 protected void fireStateChanged()
471 Object[] changeListeners = listenerList.getListenerList();
472 if (changeEvent == null)
473 changeEvent = new ChangeEvent(this);
474 for (int i = changeListeners.length - 2; i >= 0; i -= 2)
476 if (changeListeners[i] == ChangeListener.class)
477 ((ChangeListener) changeListeners[i + 1]).stateChanged(changeEvent);
482 * This method returns an array of all ChangeListeners listening to this
483 * slider.
485 * @return An array of ChangeListeners listening to this slider.
487 public ChangeListener[] getChangeListeners()
489 return (ChangeListener[]) listenerList.getListeners(ChangeListener.class);
493 * This method returns the model of the slider.
495 * @return The slider's model.
497 public BoundedRangeModel getModel()
499 return sliderModel;
503 * This method changes the "model" property. It also needs to unregister
504 * any listeners to the old model and register any listeners to the new
505 * model.
507 * @param model The model to use with the slider.
509 public void setModel(BoundedRangeModel model)
511 // I didn't do the null pointer check on purpose.
512 // If you try it with Sun's, it'll go ahead and set it to null
513 // and bork the next time it tries to access the model.
514 if (model != sliderModel)
516 BoundedRangeModel oldModel = sliderModel;
517 sliderModel = model;
518 oldModel.removeChangeListener(changeListener);
519 sliderModel.addChangeListener(changeListener);
520 firePropertyChange(MODEL_CHANGED_PROPERTY, oldModel, sliderModel);
525 * This method returns the minimum value of the slider.
527 * @return The minimum value of the slider.
529 public int getMinimum()
531 return sliderModel.getMinimum();
535 * This method sets the minimum value of the slider.
537 * @param minimum The minimum value of the slider.
539 public void setMinimum(int minimum)
541 sliderModel.setMinimum(minimum);
545 * This method returns the maximum value of the slider.
547 * @return The maximum value of the slider.
549 public int getMaximum()
551 return sliderModel.getMaximum();
555 * This method sets the maximum value of the slider.
557 * @param maximum The maximum value of the slider.
559 public void setMaximum(int maximum)
561 sliderModel.setMaximum(maximum);
565 * This method returns this slider's isAdjusting value which is true if the
566 * thumb is being dragged.
568 * @return The slider's isAdjusting value.
570 public boolean getValueIsAdjusting()
572 return sliderModel.getValueIsAdjusting();
576 * This method sets the isAdjusting value for the slider.
578 * @param adjusting The slider's isAdjusting value.
580 public void setValueIsAdjusting(boolean adjusting)
582 sliderModel.setValueIsAdjusting(adjusting);
586 * This method returns the extent value for this slider.
588 * @return The extent value for this slider.
590 public int getExtent()
592 return sliderModel.getExtent();
596 * This method sets the extent value for this slider.
598 * @param extent The extent value for this slider.
600 public void setExtent(int extent)
602 sliderModel.setExtent(extent);
606 * This method returns the slider orientation.
608 * @return The orientation of the slider.
610 public int getOrientation()
612 return orientation;
616 * This method changes the "orientation" property of this slider. If the
617 * orientation is not VERTICAL or HORIZONTAL, this method does nothing.
619 * @param orientation The orientation of this slider.
621 public void setOrientation(int orientation)
623 if (orientation != VERTICAL && orientation != HORIZONTAL)
624 throw new IllegalArgumentException("orientation must be one of: VERTICAL, HORIZONTAL");
625 if (orientation != this.orientation)
627 int oldOrientation = this.orientation;
628 this.orientation = orientation;
629 firePropertyChange(ORIENTATION_CHANGED_PROPERTY, oldOrientation,
630 this.orientation);
635 * This method returns the label table for this slider.
637 * @return The label table for this slider.
639 public Dictionary getLabelTable()
641 return labelTable;
645 * This method changes the "labelTable" property of this slider.
647 * @param table The label table for this slider.
649 public void setLabelTable(Dictionary table)
651 if (table != labelTable)
653 Dictionary oldTable = labelTable;
654 labelTable = table;
655 firePropertyChange(LABEL_TABLE_CHANGED_PROPERTY, oldTable, labelTable);
660 * This method is called to reset UI delegates for the labels in the
661 * labelTable to a default for the current look and feel.
663 protected void updateLabelUIs()
665 if (labelTable == null)
666 return;
667 for (Enumeration list = labelTable.elements(); list.hasMoreElements();)
669 JLabel label = (JLabel) list.nextElement();
670 label.updateUI();
675 * Creates a hashtable of (Integer, JLabel) pairs that can be used as a
676 * label table for this slider. The labels will start from the sliders
677 * minimum and increase by the increment. Each label will have a text
678 * string indicating their integer value.
680 * @param increment The increment to between labels.
682 * @return A hashtable with the labels and their keys.
684 public Hashtable createStandardLabels(int increment)
686 return createStandardLabels(increment, sliderModel.getMinimum());
690 * Creates a hashtable of (Integer, JLabel) pairs that can be used as a
691 * label table for this slider. The labels will start from the given start
692 * value and increase by the increment. Each label will have a text string
693 * indicating their integer value.
695 * @param increment The increment to between labels.
696 * @param start The value to start from.
698 * @return A hashtable with the labels and their keys.
700 public Hashtable createStandardLabels(int increment, int start)
702 Hashtable table = new Hashtable();
703 JLabel label;
704 Dimension dim;
706 int max = sliderModel.getMaximum();
708 for (int i = start; i <= max; i += increment)
710 label = new JLabel(String.valueOf(i));
711 label.setVerticalAlignment(CENTER);
712 label.setHorizontalAlignment(CENTER);
714 // Make sure these labels have the width and height
715 // they want.
716 dim = label.getPreferredSize();
717 label.setBounds(label.getX(), label.getY(),
718 (int) dim.getWidth(),
719 (int) dim.getHeight());
720 table.put(new Integer(i), label);
722 return table;
726 * This method returns whether the slider is inverted. Horizontal sliders
727 * that are not inverted will have the minimums on the left. If they are
728 * inverted, the minimums will be on the right. Vertical sliders that are
729 * not inverted will have the minimums at the bottom. If they are inverted,
730 * the minimums will be at the top.
732 * @return Whether this slider is inverted.
734 public boolean getInverted()
736 return isInverted;
740 * This method changes the "inverted" property for this slider.Horizontal
741 * sliders that are not inverted will have the minimums on the left. If
742 * they are inverted, the minimums will be on the right. Vertical sliders
743 * that are not inverted will have the minimums at the bottom. If they are
744 * inverted, the minimums will be at the top. However, if the slider's
745 * componentOrientation is set to RIGHT_TO_LEFT, then everything gets
746 * reversed again.
748 * @param inverted Whether the slider should be inverted.
750 public void setInverted(boolean inverted)
752 if (isInverted != inverted)
754 boolean oldInverted = isInverted;
755 isInverted = inverted;
756 firePropertyChange(INVERTED_CHANGED_PROPERTY, oldInverted, isInverted);
761 * This method returns the amount of units between each major tick mark.
763 * @return The amount of units between each major tick mark.
765 public int getMajorTickSpacing()
767 return majorTickSpacing;
771 * This method changes the "majorTickSpacing" property for this slider. The
772 * major tick spacing is the amount of units between each major tick mark.
774 * @param spacing The amount of units between each major tick mark.
776 public void setMajorTickSpacing(int spacing)
778 if (majorTickSpacing != spacing)
780 int oldSpacing = majorTickSpacing;
781 majorTickSpacing = spacing;
782 firePropertyChange(MAJOR_TICK_SPACING_CHANGED_PROPERTY, oldSpacing,
783 majorTickSpacing);
788 * This method returns the amount of units between each minor tick mark.
790 * @return The amount of units between each minor tick mark.
792 public int getMinorTickSpacing()
794 return minorTickSpacing;
798 * This method changes the "minorTickSpacing" property for this slider. The
799 * minor tick spacing is the amount of units between each minor tick mark.
801 * @param spacing The amount of units between each minor tick mark.
803 public void setMinorTickSpacing(int spacing)
805 if (minorTickSpacing != spacing)
807 int oldSpacing = minorTickSpacing;
808 minorTickSpacing = spacing;
809 firePropertyChange(MINOR_TICK_SPACING_CHANGED_PROPERTY, oldSpacing,
810 minorTickSpacing);
815 * This method returns whether this slider is snapping to ticks. Sliders
816 * that snap to ticks will automatically move the thumb to the nearest tick
817 * mark.
819 * @return Whether this slider snaps to ticks.
821 public boolean getSnapToTicks()
823 return snapToTicks;
827 * This method sets whether this slider will snap to ticks. Sliders that
828 * snap to ticks will automatically move the thumb to the nearest tick
829 * mark.
831 * @param snap Whether this slider snaps to ticks.
833 public void setSnapToTicks(boolean snap)
835 if (snap != snapToTicks)
837 snapToTicks = snap;
838 fireStateChanged();
843 * This method returns whether the slider will paint its tick marks. In
844 * addition to setting this property to true, one of minor tick spacing or
845 * major tick spacing must be set to a value greater than 0 in order for
846 * ticks to be painted.
848 * @return Whether ticks will be painted.
850 public boolean getPaintTicks()
852 return paintTicks;
856 * This method changes the "paintTicks" property for this slider. In
857 * addition to setting this property to true, one of minor tick spacing or
858 * major tick spacing must be set to a value greater than 0 in order for
859 * ticks to be painted.
861 * @param paint Whether ticks will be painted.
863 public void setPaintTicks(boolean paint)
865 if (paint != paintTicks)
867 boolean oldPaintTicks = paintTicks;
868 paintTicks = paint;
869 firePropertyChange(PAINT_TICKS_CHANGED_PROPERTY, oldPaintTicks,
870 paintTicks);
875 * This method returns whether the track will be painted.
877 * @return Whether the track will be painted.
879 public boolean getPaintTrack()
881 return paintTrack;
885 * This method sets whether the track will be painted.
887 * @param paint Whether the track will be painted.
889 public void setPaintTrack(boolean paint)
891 paintTrack = paint;
895 * This method returns whether labels will be painted.
897 * @return Whether labels will be painted.
899 public boolean getPaintLabels()
901 return paintLabels;
905 * This method changes the "paintLabels" property.
907 * @param paint Whether labels will be painted.
909 public void setPaintLabels(boolean paint)
911 if (paint != paintLabels)
913 boolean oldPaintLabels = paintLabels;
914 paintLabels = paint;
915 firePropertyChange(PAINT_LABELS_CHANGED_PROPERTY, oldPaintLabels,
916 paintLabels);
921 * This method is used primarily for debugging purposes and returns a string
922 * that can be used to represent this slider.
924 * @return A string representing this slider.
926 protected String paramString()
928 return "JSlider";
932 * DOCUMENT ME!
934 * @return DOCUMENT ME!
936 public AccessibleContext getAccessibleContext()
938 if (accessibleContext == null)
939 accessibleContext = new AccessibleJSlider(this);
940 return accessibleContext;