jdk/src/share/classes/javax/swing/JSlider.java
author ohair
Wed, 06 Apr 2011 22:06:11 -0700
changeset 9035 1255eb81cc2f
parent 7785 23c1995f9bf7
child 20157 cafca01a8e28
permissions -rw-r--r--
7033660: Update copyright year to 2011 on any files changed in 2011 Reviewed-by: dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
9035
1255eb81cc2f 7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents: 7785
diff changeset
     2
 * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1309
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1309
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1309
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1309
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1309
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package javax.swing;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import javax.swing.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import javax.swing.plaf.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import javax.accessibility.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.Serializable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.io.ObjectOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
677
20718977427b 4987336: JSlider doesn't show label's animated icon
rupashka
parents: 676
diff changeset
    36
import java.awt.*;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.beans.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * A component that lets the user graphically select a value by sliding
7785
23c1995f9bf7 6973773: JCK manual case JSliderTests.html#JSlider fails in jdk7 b100
rupashka
parents: 5506
diff changeset
    43
 * a knob within a bounded interval. The knob is always positioned
23c1995f9bf7 6973773: JCK manual case JSliderTests.html#JSlider fails in jdk7 b100
rupashka
parents: 5506
diff changeset
    44
 * at the points that match integer values within the specified interval.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * The slider can show both
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * major tick marks, and minor tick marks between the major ones.  The number of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * values between the tick marks is controlled with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * <code>setMajorTickSpacing</code> and <code>setMinorTickSpacing</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * Painting of tick marks is controlled by {@code setPaintTicks}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * Sliders can also print text labels at regular intervals (or at
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * arbitrary locations) along the slider track.  Painting of labels is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * controlled by {@code setLabelTable} and {@code setPaintLabels}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * For further information and examples see
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 href="http://java.sun.com/docs/books/tutorial/uiswing/components/slider.html">How to Use Sliders</a>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * a section in <em>The Java Tutorial.</em>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * <strong>Warning:</strong> Swing is not thread safe. For more
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * information see <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * href="package-summary.html#threading">Swing's Threading
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * Policy</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * <strong>Warning:</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * Serialized objects of this class will not be compatible with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * future Swing releases. The current serialization support is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * appropriate for short term storage or RMI between applications running
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * the same version of Swing.  As of 1.4, support for long term storage
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * of all JavaBeans<sup><font size="-2">TM</font></sup>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * has been added to the <code>java.beans</code> package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * Please see {@link java.beans.XMLEncoder}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 *      attribute: isContainer false
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 *    description: A component that supports selecting a integer value from a range.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * @author David Kloba
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
public class JSlider extends JComponent implements SwingConstants, Accessible {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * @see #getUIClassID
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * @see #readObject
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    private static final String uiClassID = "SliderUI";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    private boolean paintTicks = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    private boolean paintTrack = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    private boolean paintLabels = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    private boolean isInverted = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * The data model that handles the numeric maximum value,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * minimum value, and current-position value for the slider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    protected BoundedRangeModel sliderModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * The number of values between the major tick marks -- the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * larger marks that break up the minor tick marks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    protected int majorTickSpacing;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * The number of values between the minor tick marks -- the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * smaller marks that occur between the major tick marks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * @see #setMinorTickSpacing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    protected int minorTickSpacing;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * If true, the knob (and the data value it represents)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * resolve to the closest tick mark next to where the user
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * positioned the knob.  The default is false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * @see #setSnapToTicks
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    protected boolean snapToTicks = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * If true, the knob (and the data value it represents)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * resolve to the closest slider value next to where the user
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * positioned the knob.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    boolean snapToValue = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * Whether the slider is horizontal or vertical
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * The default is horizontal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * @see #setOrientation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    protected int orientation;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * {@code Dictionary} of what labels to draw at which values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    private Dictionary labelTable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * The changeListener (no suffix) is the listener we add to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * slider's model.  This listener is initialized to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * {@code ChangeListener} returned from {@code createChangeListener},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * which by default just forwards events
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * to {@code ChangeListener}s (if any) added directly to the slider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * @see #addChangeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * @see #createChangeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    protected ChangeListener changeListener = createChangeListener();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * Only one <code>ChangeEvent</code> is needed per slider instance since the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * event's only (read-only) state is the source property.  The source
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * of events generated here is always "this". The event is lazily
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * created the first time that an event notification is fired.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * @see #fireStateChanged
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    protected transient ChangeEvent changeEvent = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    private void checkOrientation(int orientation) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        switch (orientation) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        case VERTICAL:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        case HORIZONTAL:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            throw new IllegalArgumentException("orientation must be one of: VERTICAL, HORIZONTAL");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * Creates a horizontal slider with the range 0 to 100 and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * an initial value of 50.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    public JSlider() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        this(HORIZONTAL, 0, 100, 50);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * Creates a slider using the specified orientation with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * range {@code 0} to {@code 100} and an initial value of {@code 50}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * The orientation can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * either <code>SwingConstants.VERTICAL</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * <code>SwingConstants.HORIZONTAL</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * @param  orientation  the orientation of the slider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * @throws IllegalArgumentException if orientation is not one of {@code VERTICAL}, {@code HORIZONTAL}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * @see #setOrientation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    public JSlider(int orientation) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        this(orientation, 0, 100, 50);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * Creates a horizontal slider using the specified min and max
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * with an initial value equal to the average of the min plus max.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * The <code>BoundedRangeModel</code> that holds the slider's data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * handles any issues that may arise from improperly setting the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * minimum and maximum values on the slider.  See the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * {@code BoundedRangeModel} documentation for details.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * @param min  the minimum value of the slider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * @param max  the maximum value of the slider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * @see BoundedRangeModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * @see #setMinimum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * @see #setMaximum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    public JSlider(int min, int max) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        this(HORIZONTAL, min, max, (min + max) / 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * Creates a horizontal slider using the specified min, max and value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * The <code>BoundedRangeModel</code> that holds the slider's data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * handles any issues that may arise from improperly setting the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * minimum, initial, and maximum values on the slider.  See the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * {@code BoundedRangeModel} documentation for details.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * @param min  the minimum value of the slider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * @param max  the maximum value of the slider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * @param value  the initial value of the slider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * @see BoundedRangeModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * @see #setMinimum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * @see #setMaximum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * @see #setValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    public JSlider(int min, int max, int value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        this(HORIZONTAL, min, max, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * Creates a slider with the specified orientation and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * specified minimum, maximum, and initial values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * The orientation can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * either <code>SwingConstants.VERTICAL</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * <code>SwingConstants.HORIZONTAL</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * The <code>BoundedRangeModel</code> that holds the slider's data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * handles any issues that may arise from improperly setting the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * minimum, initial, and maximum values on the slider.  See the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * {@code BoundedRangeModel} documentation for details.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * @param orientation  the orientation of the slider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * @param min  the minimum value of the slider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * @param max  the maximum value of the slider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * @param value  the initial value of the slider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * @throws IllegalArgumentException if orientation is not one of {@code VERTICAL}, {@code HORIZONTAL}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * @see BoundedRangeModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * @see #setOrientation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * @see #setMinimum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * @see #setMaximum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * @see #setValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    public JSlider(int orientation, int min, int max, int value)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        checkOrientation(orientation);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        this.orientation = orientation;
1309
35d4c1f87df5 6278700: JSlider created with BoundedRangeModel fires twice when changed
rupashka
parents: 1301
diff changeset
   274
        setModel(new DefaultBoundedRangeModel(value, 0, min, max));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        updateUI();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * Creates a horizontal slider using the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * BoundedRangeModel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    public JSlider(BoundedRangeModel brm)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        this.orientation = JSlider.HORIZONTAL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        setModel(brm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        updateUI();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * Gets the UI object which implements the L&F for this component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * @return the SliderUI object that implements the Slider L&F
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    public SliderUI getUI() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        return(SliderUI)ui;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * Sets the UI object which implements the L&F for this component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * @param ui the SliderUI L&F object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * @see UIDefaults#getUI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     *        bound: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     *       hidden: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     *    attribute: visualUpdate true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     *  description: The UI object that implements the slider's LookAndFeel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    public void setUI(SliderUI ui) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        super.setUI(ui);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * Resets the UI property to a value from the current look and feel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * @see JComponent#updateUI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    public void updateUI() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        setUI((SliderUI)UIManager.getUI(this));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        // The labels preferred size may be derived from the font
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        // of the slider, so we must update the UI of the slider first, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        // that of labels.  This way when setSize is called the right
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        // font is used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        updateLabelUIs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * Returns the name of the L&F class that renders this component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * @return "SliderUI"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * @see JComponent#getUIClassID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * @see UIDefaults#getUI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    public String getUIClassID() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        return uiClassID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * We pass Change events along to the listeners with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * the slider (instead of the model itself) as the event source.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    private class ModelListener implements ChangeListener, Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        public void stateChanged(ChangeEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            fireStateChanged();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * Subclasses that want to handle {@code ChangeEvent}s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * from the model differently
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * can override this to return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * an instance of a custom <code>ChangeListener</code> implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * The default {@code ChangeListener} simply calls the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * {@code fireStateChanged} method to forward {@code ChangeEvent}s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * to the {@code ChangeListener}s that have been added directly to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * slider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * @see #changeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * @see #fireStateChanged
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * @see javax.swing.event.ChangeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * @see javax.swing.BoundedRangeModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    protected ChangeListener createChangeListener() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        return new ModelListener();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * Adds a ChangeListener to the slider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * @param l the ChangeListener to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     * @see #fireStateChanged
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     * @see #removeChangeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    public void addChangeListener(ChangeListener l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        listenerList.add(ChangeListener.class, l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     * Removes a ChangeListener from the slider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * @param l the ChangeListener to remove
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * @see #fireStateChanged
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * @see #addChangeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
    public void removeChangeListener(ChangeListener l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        listenerList.remove(ChangeListener.class, l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * Returns an array of all the <code>ChangeListener</code>s added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * to this JSlider with addChangeListener().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * @return all of the <code>ChangeListener</code>s added or an empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     *         array if no listeners have been added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
    public ChangeListener[] getChangeListeners() {
676
8cf833d60e87 6709530: There are unnecessary code in slider classes, such as in JSlider and SliderUIs
rupashka
parents: 438
diff changeset
   408
        return listenerList.getListeners(ChangeListener.class);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     * Send a {@code ChangeEvent}, whose source is this {@code JSlider}, to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     * all {@code ChangeListener}s that have registered interest in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     * {@code ChangeEvent}s.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     * This method is called each time a {@code ChangeEvent} is received from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     * the model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     * The event instance is created if necessary, and stored in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * {@code changeEvent}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * @see #addChangeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     * @see EventListenerList
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
    protected void fireStateChanged() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        Object[] listeners = listenerList.getListenerList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        for (int i = listeners.length - 2; i >= 0; i -= 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
            if (listeners[i]==ChangeListener.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                if (changeEvent == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                    changeEvent = new ChangeEvent(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                ((ChangeListener)listeners[i+1]).stateChanged(changeEvent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     * Returns the {@code BoundedRangeModel} that handles the slider's three
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     * fundamental properties: minimum, maximum, value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     * @return the data model for this component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     * @see #setModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     * @see    BoundedRangeModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
    public BoundedRangeModel getModel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        return sliderModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     * Sets the {@code BoundedRangeModel} that handles the slider's three
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     * fundamental properties: minimum, maximum, value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     *<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     * Attempts to pass a {@code null} model to this method result in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     * undefined behavior, and, most likely, exceptions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     * @param  newModel the new, {@code non-null} <code>BoundedRangeModel</code> to use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     * @see #getModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     * @see    BoundedRangeModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     *       bound: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     * description: The sliders BoundedRangeModel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
    public void setModel(BoundedRangeModel newModel)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        BoundedRangeModel oldModel = getModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        if (oldModel != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
            oldModel.removeChangeListener(changeListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        sliderModel = newModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        if (newModel != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
            newModel.addChangeListener(changeListener);
1309
35d4c1f87df5 6278700: JSlider created with BoundedRangeModel fires twice when changed
rupashka
parents: 1301
diff changeset
   478
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
1309
35d4c1f87df5 6278700: JSlider created with BoundedRangeModel fires twice when changed
rupashka
parents: 1301
diff changeset
   480
        if (accessibleContext != null) {
35d4c1f87df5 6278700: JSlider created with BoundedRangeModel fires twice when changed
rupashka
parents: 1301
diff changeset
   481
            accessibleContext.firePropertyChange(
35d4c1f87df5 6278700: JSlider created with BoundedRangeModel fires twice when changed
rupashka
parents: 1301
diff changeset
   482
                                                AccessibleContext.ACCESSIBLE_VALUE_PROPERTY,
35d4c1f87df5 6278700: JSlider created with BoundedRangeModel fires twice when changed
rupashka
parents: 1301
diff changeset
   483
                                                (oldModel == null
35d4c1f87df5 6278700: JSlider created with BoundedRangeModel fires twice when changed
rupashka
parents: 1301
diff changeset
   484
                                                 ? null : Integer.valueOf(oldModel.getValue())),
35d4c1f87df5 6278700: JSlider created with BoundedRangeModel fires twice when changed
rupashka
parents: 1301
diff changeset
   485
                                                (newModel == null
35d4c1f87df5 6278700: JSlider created with BoundedRangeModel fires twice when changed
rupashka
parents: 1301
diff changeset
   486
                                                 ? null : Integer.valueOf(newModel.getValue())));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        firePropertyChange("model", oldModel, sliderModel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     * Returns the slider's current value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     * from the {@code BoundedRangeModel}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     * @return  the current value of the slider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     * @see     #setValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     * @see     BoundedRangeModel#getValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
    public int getValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        return getModel().getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     * Sets the slider's current value to {@code n}.  This method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     * forwards the new value to the model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     * The data model (an instance of {@code BoundedRangeModel})
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     * handles any mathematical
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     * issues arising from assigning faulty values.  See the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     * {@code BoundedRangeModel} documentation for details.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     * If the new value is different from the previous value,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     * all change listeners are notified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     * @param   n       the new value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     * @see     #getValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     * @see     #addChangeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     * @see     BoundedRangeModel#setValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
     *   preferred: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     * description: The sliders current value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
    public void setValue(int n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        BoundedRangeModel m = getModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        int oldValue = m.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        if (oldValue == n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
        m.setValue(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
        if (accessibleContext != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
            accessibleContext.firePropertyChange(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
                                                AccessibleContext.ACCESSIBLE_VALUE_PROPERTY,
438
2ae294e4518c 6613529: Avoid duplicate object creation within JDK packages
dav
parents: 2
diff changeset
   536
                                                Integer.valueOf(oldValue),
2ae294e4518c 6613529: Avoid duplicate object creation within JDK packages
dav
parents: 2
diff changeset
   537
                                                Integer.valueOf(m.getValue()));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     * Returns the minimum value supported by the slider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     * from the <code>BoundedRangeModel</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
     * @return the value of the model's minimum property
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
     * @see #setMinimum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     * @see     BoundedRangeModel#getMinimum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
    public int getMinimum() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        return getModel().getMinimum();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     * Sets the slider's minimum value to {@code minimum}.  This method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     * forwards the new minimum value to the model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     * The data model (an instance of {@code BoundedRangeModel})
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
     * handles any mathematical
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
     * issues arising from assigning faulty values.  See the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
     * {@code BoundedRangeModel} documentation for details.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
     * If the new minimum value is different from the previous minimum value,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     * all change listeners are notified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
     * @param minimum  the new minimum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
     * @see #getMinimum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     * @see    #addChangeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
     * @see BoundedRangeModel#setMinimum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     *       bound: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
     *   preferred: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
     * description: The sliders minimum value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
    public void setMinimum(int minimum) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
        int oldMin = getModel().getMinimum();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
        getModel().setMinimum(minimum);
438
2ae294e4518c 6613529: Avoid duplicate object creation within JDK packages
dav
parents: 2
diff changeset
   579
        firePropertyChange( "minimum", Integer.valueOf( oldMin ), Integer.valueOf( minimum ) );
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     * Returns the maximum value supported by the slider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     * from the <code>BoundedRangeModel</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     * @return the value of the model's maximum property
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
     * @see #setMaximum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
     * @see BoundedRangeModel#getMaximum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
    public int getMaximum() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
        return getModel().getMaximum();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
     * Sets the slider's maximum value to {@code maximum}.  This method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
     * forwards the new maximum value to the model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
     * The data model (an instance of {@code BoundedRangeModel})
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     * handles any mathematical
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     * issues arising from assigning faulty values.  See the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
     * {@code BoundedRangeModel} documentation for details.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
     * If the new maximum value is different from the previous maximum value,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
     * all change listeners are notified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
     * @param maximum  the new maximum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
     * @see #getMaximum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
     * @see #addChangeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
     * @see BoundedRangeModel#setMaximum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
     *       bound: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
     *   preferred: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
     * description: The sliders maximum value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
    public void setMaximum(int maximum) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
        int oldMax = getModel().getMaximum();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
        getModel().setMaximum(maximum);
438
2ae294e4518c 6613529: Avoid duplicate object creation within JDK packages
dav
parents: 2
diff changeset
   620
        firePropertyChange( "maximum", Integer.valueOf( oldMax ), Integer.valueOf( maximum ) );
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
     * Returns the {@code valueIsAdjusting} property from the model.  For
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
     * details on how this is used, see the {@code setValueIsAdjusting}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
     * documentation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
     * @return the value of the model's {@code valueIsAdjusting} property
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
     * @see #setValueIsAdjusting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
    public boolean getValueIsAdjusting() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
        return getModel().getValueIsAdjusting();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
     * Sets the model's {@code valueIsAdjusting} property.  Slider look and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
     * feel implementations should set this property to {@code true} when
679
bb198a1a00fa 6688110: JSlider has incorrect javadoc for the setValueIsAdjusting method
rupashka
parents: 677
diff changeset
   640
     * a knob drag begins, and to {@code false} when the drag ends.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
     * @param b the new value for the {@code valueIsAdjusting} property
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
     * @see   #getValueIsAdjusting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
     * @see   BoundedRangeModel#setValueIsAdjusting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
     *      expert: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
     * description: True if the slider knob is being dragged.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
    public void setValueIsAdjusting(boolean b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
        BoundedRangeModel m = getModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
        boolean oldValue = m.getValueIsAdjusting();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
        m.setValueIsAdjusting(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
        if ((oldValue != b) && (accessibleContext != null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
            accessibleContext.firePropertyChange(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
                                                AccessibleContext.ACCESSIBLE_STATE_PROPERTY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
                                                ((oldValue) ? AccessibleState.BUSY : null),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
                                                ((b) ? AccessibleState.BUSY : null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
     * Returns the "extent" from the <code>BoundedRangeModel</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
     * This respresents the range of values "covered" by the knob.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
     * @return an int representing the extent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
     * @see #setExtent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
     * @see BoundedRangeModel#getExtent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
    public int getExtent() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
        return getModel().getExtent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
     * Sets the size of the range "covered" by the knob.  Most look
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
     * and feel implementations will change the value by this amount
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
     * if the user clicks on either side of the knob.  This method just
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
     * forwards the new extent value to the model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
     * The data model (an instance of {@code BoundedRangeModel})
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
     * handles any mathematical
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
     * issues arising from assigning faulty values.  See the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
     * {@code BoundedRangeModel} documentation for details.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
     * If the new extent value is different from the previous extent value,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
     * all change listeners are notified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
     * @param extent the new extent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
     * @see   #getExtent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
     * @see   BoundedRangeModel#setExtent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
     *      expert: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
     * description: Size of the range covered by the knob.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
    public void setExtent(int extent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
        getModel().setExtent(extent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
     * Return this slider's vertical or horizontal orientation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
     * @return {@code SwingConstants.VERTICAL} or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
     *  {@code SwingConstants.HORIZONTAL}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
     * @see #setOrientation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
    public int getOrientation() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
        return orientation;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
     * Set the slider's orientation to either {@code SwingConstants.VERTICAL} or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
     * {@code SwingConstants.HORIZONTAL}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
     * @param orientation {@code HORIZONTAL} or {@code VERTICAL}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
     * @throws IllegalArgumentException if orientation is not one of {@code VERTICAL}, {@code HORIZONTAL}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
     * @see #getOrientation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
     *    preferred: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
     *        bound: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
     *    attribute: visualUpdate true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
     *  description: Set the scrollbars orientation to either VERTICAL or HORIZONTAL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
     *         enum: VERTICAL JSlider.VERTICAL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
     *               HORIZONTAL JSlider.HORIZONTAL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
    public void setOrientation(int orientation)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
        checkOrientation(orientation);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
        int oldValue = this.orientation;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
        this.orientation = orientation;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
        firePropertyChange("orientation", oldValue, orientation);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
        if ((oldValue != orientation) && (accessibleContext != null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
            accessibleContext.firePropertyChange(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
                                                AccessibleContext.ACCESSIBLE_STATE_PROPERTY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
                                                ((oldValue == VERTICAL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
                                                 ? AccessibleState.VERTICAL : AccessibleState.HORIZONTAL),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
                                                ((orientation == VERTICAL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
                                                 ? AccessibleState.VERTICAL : AccessibleState.HORIZONTAL));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
        if (orientation != oldValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
            revalidate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
    public void setFont(Font font) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
        super.setFont(font);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
        updateLabelSizes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
677
20718977427b 4987336: JSlider doesn't show label's animated icon
rupashka
parents: 676
diff changeset
   760
    /**
20718977427b 4987336: JSlider doesn't show label's animated icon
rupashka
parents: 676
diff changeset
   761
     * {@inheritDoc}
20718977427b 4987336: JSlider doesn't show label's animated icon
rupashka
parents: 676
diff changeset
   762
     * @since 1.7
20718977427b 4987336: JSlider doesn't show label's animated icon
rupashka
parents: 676
diff changeset
   763
     */
20718977427b 4987336: JSlider doesn't show label's animated icon
rupashka
parents: 676
diff changeset
   764
    public boolean imageUpdate(Image img, int infoflags, int x, int y, int w, int h) {
20718977427b 4987336: JSlider doesn't show label's animated icon
rupashka
parents: 676
diff changeset
   765
        if (!isShowing()) {
20718977427b 4987336: JSlider doesn't show label's animated icon
rupashka
parents: 676
diff changeset
   766
            return false;
20718977427b 4987336: JSlider doesn't show label's animated icon
rupashka
parents: 676
diff changeset
   767
        }
20718977427b 4987336: JSlider doesn't show label's animated icon
rupashka
parents: 676
diff changeset
   768
20718977427b 4987336: JSlider doesn't show label's animated icon
rupashka
parents: 676
diff changeset
   769
        // Check that there is a label with such image
20718977427b 4987336: JSlider doesn't show label's animated icon
rupashka
parents: 676
diff changeset
   770
        Enumeration elements = labelTable.elements();
20718977427b 4987336: JSlider doesn't show label's animated icon
rupashka
parents: 676
diff changeset
   771
20718977427b 4987336: JSlider doesn't show label's animated icon
rupashka
parents: 676
diff changeset
   772
        while (elements.hasMoreElements()) {
20718977427b 4987336: JSlider doesn't show label's animated icon
rupashka
parents: 676
diff changeset
   773
            Component component = (Component) elements.nextElement();
20718977427b 4987336: JSlider doesn't show label's animated icon
rupashka
parents: 676
diff changeset
   774
20718977427b 4987336: JSlider doesn't show label's animated icon
rupashka
parents: 676
diff changeset
   775
            if (component instanceof JLabel) {
20718977427b 4987336: JSlider doesn't show label's animated icon
rupashka
parents: 676
diff changeset
   776
                JLabel label = (JLabel) component;
20718977427b 4987336: JSlider doesn't show label's animated icon
rupashka
parents: 676
diff changeset
   777
20718977427b 4987336: JSlider doesn't show label's animated icon
rupashka
parents: 676
diff changeset
   778
                if (SwingUtilities.doesIconReferenceImage(label.getIcon(), img) ||
20718977427b 4987336: JSlider doesn't show label's animated icon
rupashka
parents: 676
diff changeset
   779
                        SwingUtilities.doesIconReferenceImage(label.getDisabledIcon(), img)) {
20718977427b 4987336: JSlider doesn't show label's animated icon
rupashka
parents: 676
diff changeset
   780
                    return super.imageUpdate(img, infoflags, x, y, w, h);
20718977427b 4987336: JSlider doesn't show label's animated icon
rupashka
parents: 676
diff changeset
   781
                }
20718977427b 4987336: JSlider doesn't show label's animated icon
rupashka
parents: 676
diff changeset
   782
            }
20718977427b 4987336: JSlider doesn't show label's animated icon
rupashka
parents: 676
diff changeset
   783
        }
20718977427b 4987336: JSlider doesn't show label's animated icon
rupashka
parents: 676
diff changeset
   784
20718977427b 4987336: JSlider doesn't show label's animated icon
rupashka
parents: 676
diff changeset
   785
        return false;
20718977427b 4987336: JSlider doesn't show label's animated icon
rupashka
parents: 676
diff changeset
   786
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
     * Returns the dictionary of what labels to draw at which values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
     * @return the <code>Dictionary</code> containing labels and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
     *    where to draw them
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
    public Dictionary getLabelTable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
        if ( labelTable == null && getMajorTickSpacing() > 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
            setLabelTable( createStandardLabels( getMajorTickSpacing() ) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
        return labelTable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
     * Used to specify what label will be drawn at any given value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
     * The key-value pairs are of this format:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
     * <code>{ Integer value, java.swing.JComponent label }</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
     * An easy way to generate a standard table of value labels is by using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
     * {@code createStandardLabels} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
     * Once the labels have been set, this method calls {@link #updateLabelUIs}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
     * Note that the labels are only painted if the {@code paintLabels}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
     * property is {@code true}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
     * @param labels new {@code Dictionary} of labels, or {@code null} to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
     *        remove all labels
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
     * @see #createStandardLabels(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
     * @see #getLabelTable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
     * @see #setPaintLabels
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
     *       hidden: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
     *        bound: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
     *    attribute: visualUpdate true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
     *  description: Specifies what labels will be drawn for any given value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
    public void setLabelTable( Dictionary labels ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
        Dictionary oldTable = labelTable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
        labelTable = labels;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
        updateLabelUIs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
        firePropertyChange("labelTable", oldTable, labelTable );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
        if (labels != oldTable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
            revalidate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
            repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
     * Updates the UIs for the labels in the label table by calling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
     * {@code updateUI} on each label.  The UIs are updated from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
     * the current look and feel.  The labels are also set to their
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
     * preferred size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
     * @see #setLabelTable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
     * @see JComponent#updateUI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
    protected void updateLabelUIs() {
676
8cf833d60e87 6709530: There are unnecessary code in slider classes, such as in JSlider and SliderUIs
rupashka
parents: 438
diff changeset
   849
        Dictionary labelTable = getLabelTable();
8cf833d60e87 6709530: There are unnecessary code in slider classes, such as in JSlider and SliderUIs
rupashka
parents: 438
diff changeset
   850
8cf833d60e87 6709530: There are unnecessary code in slider classes, such as in JSlider and SliderUIs
rupashka
parents: 438
diff changeset
   851
        if (labelTable == null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
        }
676
8cf833d60e87 6709530: There are unnecessary code in slider classes, such as in JSlider and SliderUIs
rupashka
parents: 438
diff changeset
   854
        Enumeration labels = labelTable.keys();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
        while ( labels.hasMoreElements() ) {
676
8cf833d60e87 6709530: There are unnecessary code in slider classes, such as in JSlider and SliderUIs
rupashka
parents: 438
diff changeset
   856
            JComponent component = (JComponent) labelTable.get(labels.nextElement());
8cf833d60e87 6709530: There are unnecessary code in slider classes, such as in JSlider and SliderUIs
rupashka
parents: 438
diff changeset
   857
            component.updateUI();
8cf833d60e87 6709530: There are unnecessary code in slider classes, such as in JSlider and SliderUIs
rupashka
parents: 438
diff changeset
   858
            component.setSize(component.getPreferredSize());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
    private void updateLabelSizes() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
        Dictionary labelTable = getLabelTable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
        if (labelTable != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
            Enumeration labels = labelTable.elements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
            while (labels.hasMoreElements()) {
676
8cf833d60e87 6709530: There are unnecessary code in slider classes, such as in JSlider and SliderUIs
rupashka
parents: 438
diff changeset
   867
                JComponent component = (JComponent) labels.nextElement();
8cf833d60e87 6709530: There are unnecessary code in slider classes, such as in JSlider and SliderUIs
rupashka
parents: 438
diff changeset
   868
                component.setSize(component.getPreferredSize());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
     * Creates a {@code Hashtable} of numerical text labels, starting at the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
     * slider minimum, and using the increment specified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
     * For example, if you call <code>createStandardLabels( 10 )</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
     * and the slider minimum is zero,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
     * then labels will be created for the values 0, 10, 20, 30, and so on.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
     * For the labels to be drawn on the slider, the returned {@code Hashtable}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
     * must be passed into {@code setLabelTable}, and {@code setPaintLabels}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
     * must be set to {@code true}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
     * For further details on the makeup of the returned {@code Hashtable}, see
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
     * the {@code setLabelTable} documentation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
     * @param  increment  distance between labels in the generated hashtable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
     * @return a new {@code Hashtable} of labels
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
     * @see #setLabelTable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
     * @see #setPaintLabels
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
     * @throws IllegalArgumentException if {@code increment} is less than or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
     *          equal to zero
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
    public Hashtable createStandardLabels( int increment ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
        return createStandardLabels( increment, getMinimum() );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
     * Creates a {@code Hashtable} of numerical text labels, starting at the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
     * starting point specified, and using the increment specified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
     * For example, if you call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
     * <code>createStandardLabels( 10, 2 )</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
     * then labels will be created for the values 2, 12, 22, 32, and so on.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
     * For the labels to be drawn on the slider, the returned {@code Hashtable}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
     * must be passed into {@code setLabelTable}, and {@code setPaintLabels}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
     * must be set to {@code true}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
     * For further details on the makeup of the returned {@code Hashtable}, see
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
     * the {@code setLabelTable} documentation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
     * @param  increment  distance between labels in the generated hashtable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
     * @param  start      value at which the labels will begin
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
     * @return a new {@code Hashtable} of labels
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
     * @see #setLabelTable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
     * @see #setPaintLabels
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
     * @exception IllegalArgumentException if {@code start} is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
     *          out of range, or if {@code increment} is less than or equal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
     *          to zero
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
    public Hashtable createStandardLabels( int increment, int start ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
        if ( start > getMaximum() || start < getMinimum() ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
            throw new IllegalArgumentException( "Slider label start point out of range." );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
        if ( increment <= 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
            throw new IllegalArgumentException( "Label incremement must be > 0" );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 715
diff changeset
   932
        class SmartHashtable extends Hashtable<Object, Object> implements PropertyChangeListener {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
            int increment = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
            int start = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
            boolean startAtMin = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
            class LabelUIResource extends JLabel implements UIResource {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
                public LabelUIResource( String text, int alignment ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
                    super( text, alignment );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
                    setName("Slider.label");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
                public Font getFont() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
                    Font font = super.getFont();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
                    if (font != null && !(font instanceof UIResource)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
                        return font;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
                    return JSlider.this.getFont();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
                public Color getForeground() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
                    Color fg = super.getForeground();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
                    if (fg != null && !(fg instanceof UIResource)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
                        return fg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
                    if (!(JSlider.this.getForeground() instanceof UIResource)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
                        return JSlider.this.getForeground();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
                    return fg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
            public SmartHashtable( int increment, int start ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
                super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
                this.increment = increment;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
                this.start = start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
                startAtMin = start == getMinimum();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
                createLabels();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
            public void propertyChange( PropertyChangeEvent e ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
                if ( e.getPropertyName().equals( "minimum" ) && startAtMin ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
                    start = getMinimum();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
                if ( e.getPropertyName().equals( "minimum" ) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
                     e.getPropertyName().equals( "maximum" ) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 715
diff changeset
   979
                    Enumeration keys = getLabelTable().keys();
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 715
diff changeset
   980
                    Hashtable<Object, Object> hashtable = new Hashtable<Object, Object>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
                    // Save the labels that were added by the developer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
                    while ( keys.hasMoreElements() ) {
676
8cf833d60e87 6709530: There are unnecessary code in slider classes, such as in JSlider and SliderUIs
rupashka
parents: 438
diff changeset
   984
                        Object key = keys.nextElement();
8cf833d60e87 6709530: There are unnecessary code in slider classes, such as in JSlider and SliderUIs
rupashka
parents: 438
diff changeset
   985
                        Object value = labelTable.get(key);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
                        if ( !(value instanceof LabelUIResource) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
                            hashtable.put( key, value );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
                    clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
                    createLabels();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
                    // Add the saved labels
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
                    keys = hashtable.keys();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
                    while ( keys.hasMoreElements() ) {
676
8cf833d60e87 6709530: There are unnecessary code in slider classes, such as in JSlider and SliderUIs
rupashka
parents: 438
diff changeset
   997
                        Object key = keys.nextElement();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
                        put( key, hashtable.get( key ) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
                    ((JSlider)e.getSource()).setLabelTable( this );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
            void createLabels() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
                for ( int labelIndex = start; labelIndex <= getMaximum(); labelIndex += increment ) {
438
2ae294e4518c 6613529: Avoid duplicate object creation within JDK packages
dav
parents: 2
diff changeset
  1007
                    put( Integer.valueOf( labelIndex ), new LabelUIResource( ""+labelIndex, JLabel.CENTER ) );
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
        SmartHashtable table = new SmartHashtable( increment, start );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
676
8cf833d60e87 6709530: There are unnecessary code in slider classes, such as in JSlider and SliderUIs
rupashka
parents: 438
diff changeset
  1014
        Dictionary labelTable = getLabelTable();
8cf833d60e87 6709530: There are unnecessary code in slider classes, such as in JSlider and SliderUIs
rupashka
parents: 438
diff changeset
  1015
8cf833d60e87 6709530: There are unnecessary code in slider classes, such as in JSlider and SliderUIs
rupashka
parents: 438
diff changeset
  1016
        if (labelTable != null && (labelTable instanceof PropertyChangeListener)) {
8cf833d60e87 6709530: There are unnecessary code in slider classes, such as in JSlider and SliderUIs
rupashka
parents: 438
diff changeset
  1017
            removePropertyChangeListener((PropertyChangeListener) labelTable);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
        addPropertyChangeListener( table );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
        return table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
     * Returns true if the value-range shown for the slider is reversed,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
     * @return true if the slider values are reversed from their normal order
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
     * @see #setInverted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
    public boolean getInverted() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
        return isInverted;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
     * Specify true to reverse the value-range shown for the slider and false to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
     * put the value range in the normal order.  The order depends on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
     * slider's <code>ComponentOrientation</code> property.  Normal (non-inverted)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
     * horizontal sliders with a <code>ComponentOrientation</code> value of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
     * <code>LEFT_TO_RIGHT</code> have their maximum on the right.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
     * Normal horizontal sliders with a <code>ComponentOrientation</code> value of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
     * <code>RIGHT_TO_LEFT</code> have their maximum on the left.  Normal vertical
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
     * sliders have their maximum on the top.  These labels are reversed when the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
     * slider is inverted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
     * By default, the value of this property is {@code false}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
     * @param b  true to reverse the slider values from their normal order
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
     *        bound: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
     *    attribute: visualUpdate true
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
     *  description: If true reverses the slider values from their normal order
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
    public void setInverted( boolean b ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
        boolean oldValue = isInverted;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
        isInverted = b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
        firePropertyChange("inverted", oldValue, isInverted);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
        if (b != oldValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
            repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
     * This method returns the major tick spacing.  The number that is returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
     * represents the distance, measured in values, between each major tick mark.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
     * If you have a slider with a range from 0 to 50 and the major tick spacing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
     * is set to 10, you will get major ticks next to the following values:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
     * 0, 10, 20, 30, 40, 50.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
     * @return the number of values between major ticks
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
     * @see #setMajorTickSpacing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
    public int getMajorTickSpacing() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
        return majorTickSpacing;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
     * This method sets the major tick spacing.  The number that is passed in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
     * represents the distance, measured in values, between each major tick mark.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
     * If you have a slider with a range from 0 to 50 and the major tick spacing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
     * is set to 10, you will get major ticks next to the following values:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
     * 0, 10, 20, 30, 40, 50.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
     * In order for major ticks to be painted, {@code setPaintTicks} must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
     * set to {@code true}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
     * This method will also set up a label table for you.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
     * If there is not already a label table, and the major tick spacing is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
     * {@code > 0}, and {@code getPaintLabels} returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
     * {@code true}, a standard label table will be generated (by calling
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
     * {@code createStandardLabels}) with labels at the major tick marks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
     * For the example above, you would get text labels: "0",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
     * "10", "20", "30", "40", "50".
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
     * The label table is then set on the slider by calling
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
     * {@code setLabelTable}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
     * @param  n  new value for the {@code majorTickSpacing} property
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
     * @see #getMajorTickSpacing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
     * @see #setPaintTicks
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
     * @see #setLabelTable
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
     * @see #createStandardLabels(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
     *        bound: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
     *    attribute: visualUpdate true
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
     *  description: Sets the number of values between major tick marks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
    public void setMajorTickSpacing(int n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
        int oldValue = majorTickSpacing;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
        majorTickSpacing = n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
        if ( labelTable == null && getMajorTickSpacing() > 0 && getPaintLabels() ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
            setLabelTable( createStandardLabels( getMajorTickSpacing() ) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
        firePropertyChange("majorTickSpacing", oldValue, majorTickSpacing);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
        if (majorTickSpacing != oldValue && getPaintTicks()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
            repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
     * This method returns the minor tick spacing.  The number that is returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
     * represents the distance, measured in values, between each minor tick mark.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
     * If you have a slider with a range from 0 to 50 and the minor tick spacing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
     * is set to 10, you will get minor ticks next to the following values:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
     * 0, 10, 20, 30, 40, 50.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
     * @return the number of values between minor ticks
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
     * @see #getMinorTickSpacing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
    public int getMinorTickSpacing() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
        return minorTickSpacing;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
     * This method sets the minor tick spacing.  The number that is passed in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
     * represents the distance, measured in values, between each minor tick mark.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
     * If you have a slider with a range from 0 to 50 and the minor tick spacing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
     * is set to 10, you will get minor ticks next to the following values:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
     * 0, 10, 20, 30, 40, 50.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
     * In order for minor ticks to be painted, {@code setPaintTicks} must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
     * set to {@code true}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
     * @param  n  new value for the {@code minorTickSpacing} property
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
     * @see #getMinorTickSpacing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
     * @see #setPaintTicks
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
     *        bound: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
     *    attribute: visualUpdate true
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
     *  description: Sets the number of values between minor tick marks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
    public void setMinorTickSpacing(int n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
        int oldValue = minorTickSpacing;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
        minorTickSpacing = n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
        firePropertyChange("minorTickSpacing", oldValue, minorTickSpacing);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
        if (minorTickSpacing != oldValue && getPaintTicks()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
            repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
     * Returns true if the knob (and the data value it represents)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
     * resolve to the closest tick mark next to where the user
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
     * positioned the knob.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
     * @return true if the value snaps to the nearest tick mark, else false
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
     * @see #setSnapToTicks
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
    public boolean getSnapToTicks() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
        return snapToTicks;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
     * Returns true if the knob (and the data value it represents)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
     * resolve to the closest slider value next to where the user
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
     * positioned the knob.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
     * @return true if the value snaps to the nearest slider value, else false
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
     * @see #setSnapToValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
    boolean getSnapToValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
        return snapToValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
     * Specifying true makes the knob (and the data value it represents)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
     * resolve to the closest tick mark next to where the user
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
     * positioned the knob.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
     * By default, this property is {@code false}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
     * @param b  true to snap the knob to the nearest tick mark
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
     * @see #getSnapToTicks
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
     *       bound: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
     * description: If true snap the knob to the nearest tick mark.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
    public void setSnapToTicks(boolean b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
        boolean oldValue = snapToTicks;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
        snapToTicks = b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
        firePropertyChange("snapToTicks", oldValue, snapToTicks);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
     * Specifying true makes the knob (and the data value it represents)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
     * resolve to the closest slider value next to where the user
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
     * positioned the knob. If the {@code snapToTicks} property has also been
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
     * set to {@code true}, the snap-to-ticks behavior will prevail.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
     * By default, the snapToValue property is {@code true}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
     * @param b  true to snap the knob to the nearest slider value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
     * @see #getSnapToValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
     * @see #setSnapToTicks
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
     *       bound: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
     * description: If true snap the knob to the nearest slider value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
    void setSnapToValue(boolean b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
        boolean oldValue = snapToValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
        snapToValue = b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
        firePropertyChange("snapToValue", oldValue, snapToValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
     * Tells if tick marks are to be painted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
     * @return true if tick marks are painted, else false
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
     * @see #setPaintTicks
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
    public boolean getPaintTicks() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
        return paintTicks;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
     * Determines whether tick marks are painted on the slider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
     * By default, this property is {@code false}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
     * @param  b  whether or not tick marks should be painted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
     * @see #getPaintTicks
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
     *        bound: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
     *    attribute: visualUpdate true
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
     *  description: If true tick marks are painted on the slider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
    public void setPaintTicks(boolean b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
        boolean oldValue = paintTicks;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
        paintTicks = b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
        firePropertyChange("paintTicks", oldValue, paintTicks);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
        if (paintTicks != oldValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
            revalidate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
            repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
     * Tells if the track (area the slider slides in) is to be painted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
     * @return true if track is painted, else false
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
     * @see #setPaintTrack
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
    public boolean getPaintTrack() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
        return paintTrack;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
     * Determines whether the track is painted on the slider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
     * By default, this property is {@code true}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
     * @param  b  whether or not to paint the slider track
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
     * @see #getPaintTrack
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
     *        bound: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
     *    attribute: visualUpdate true
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
     *  description: If true, the track is painted on the slider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
    public void setPaintTrack(boolean b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
        boolean oldValue = paintTrack;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
        paintTrack = b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
        firePropertyChange("paintTrack", oldValue, paintTrack);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
        if (paintTrack != oldValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
            repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
     * Tells if labels are to be painted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
     * @return true if labels are painted, else false
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
     * @see #setPaintLabels
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
    public boolean getPaintLabels() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
        return paintLabels;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
     * Determines whether labels are painted on the slider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
     * This method will also set up a label table for you.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
     * If there is not already a label table, and the major tick spacing is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
     * {@code > 0},
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
     * a standard label table will be generated (by calling
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
     * {@code createStandardLabels}) with labels at the major tick marks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
     * The label table is then set on the slider by calling
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
     * {@code setLabelTable}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
     * By default, this property is {@code false}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
     * @param  b  whether or not to paint labels
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
     * @see #getPaintLabels
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
     * @see #getLabelTable
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
     * @see #createStandardLabels(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
     *        bound: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
     *    attribute: visualUpdate true
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
     *  description: If true labels are painted on the slider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
    public void setPaintLabels(boolean b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
        boolean oldValue = paintLabels;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
        paintLabels = b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
        if ( labelTable == null && getMajorTickSpacing() > 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
            setLabelTable( createStandardLabels( getMajorTickSpacing() ) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
        firePropertyChange("paintLabels", oldValue, paintLabels);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
        if (paintLabels != oldValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
            revalidate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
            repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
     * See readObject() and writeObject() in JComponent for more
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
     * information about serialization in Swing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
    private void writeObject(ObjectOutputStream s) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
        s.defaultWriteObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
        if (getUIClassID().equals(uiClassID)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
            byte count = JComponent.getWriteObjCounter(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
            JComponent.setWriteObjCounter(this, --count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
            if (count == 0 && ui != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
                ui.installUI(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
     * Returns a string representation of this JSlider. This method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
     * is intended to be used only for debugging purposes, and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
     * content and format of the returned string may vary between
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
     * implementations. The returned string may be empty but may not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
     * be <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
     * @return  a string representation of this JSlider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
    protected String paramString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
        String paintTicksString = (paintTicks ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
                                   "true" : "false");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
        String paintTrackString = (paintTrack ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
                                   "true" : "false");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
        String paintLabelsString = (paintLabels ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
                                    "true" : "false");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
        String isInvertedString = (isInverted ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
                                   "true" : "false");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
        String snapToTicksString = (snapToTicks ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
                                    "true" : "false");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
        String snapToValueString = (snapToValue ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
                                    "true" : "false");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
        String orientationString = (orientation == HORIZONTAL ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
                                    "HORIZONTAL" : "VERTICAL");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
        return super.paramString() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
        ",isInverted=" + isInvertedString +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
        ",majorTickSpacing=" + majorTickSpacing +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
        ",minorTickSpacing=" + minorTickSpacing +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
        ",orientation=" + orientationString +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
        ",paintLabels=" + paintLabelsString +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
        ",paintTicks=" + paintTicksString +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
        ",paintTrack=" + paintTrackString +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
        ",snapToTicks=" + snapToTicksString +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
        ",snapToValue=" + snapToValueString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
/////////////////
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
// Accessibility support
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
////////////////
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
     * Gets the AccessibleContext associated with this JSlider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
     * For sliders, the AccessibleContext takes the form of an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
     * AccessibleJSlider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
     * A new AccessibleJSlider instance is created if necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
     * @return an AccessibleJSlider that serves as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
     *         AccessibleContext of this JSlider
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
    public AccessibleContext getAccessibleContext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
        if (accessibleContext == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
            accessibleContext = new AccessibleJSlider();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
        return accessibleContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
     * This class implements accessibility support for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
     * <code>JSlider</code> class.  It provides an implementation of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
     * Java Accessibility API appropriate to slider user-interface elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
     * <strong>Warning:</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
     * Serialized objects of this class will not be compatible with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
     * future Swing releases. The current serialization support is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
     * appropriate for short term storage or RMI between applications running
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
     * the same version of Swing.  As of 1.4, support for long term storage
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
     * of all JavaBeans<sup><font size="-2">TM</font></sup>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
     * has been added to the <code>java.beans</code> package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
     * Please see {@link java.beans.XMLEncoder}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
    protected class AccessibleJSlider extends AccessibleJComponent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
    implements AccessibleValue {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
         * Get the state set of this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
         * @return an instance of AccessibleState containing the current state
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
         * of the object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
         * @see AccessibleState
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
        public AccessibleStateSet getAccessibleStateSet() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
            AccessibleStateSet states = super.getAccessibleStateSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
            if (getValueIsAdjusting()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
                states.add(AccessibleState.BUSY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
            if (getOrientation() == VERTICAL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
                states.add(AccessibleState.VERTICAL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
                states.add(AccessibleState.HORIZONTAL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
            return states;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
         * Get the role of this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
         * @return an instance of AccessibleRole describing the role of the object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
        public AccessibleRole getAccessibleRole() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
            return AccessibleRole.SLIDER;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
         * Get the AccessibleValue associated with this object.  In the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
         * implementation of the Java Accessibility API for this class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
         * return this object, which is responsible for implementing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
         * AccessibleValue interface on behalf of itself.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
         * @return this object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
        public AccessibleValue getAccessibleValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
            return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
         * Get the accessible value of this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
         * @return The current value of this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
        public Number getCurrentAccessibleValue() {
438
2ae294e4518c 6613529: Avoid duplicate object creation within JDK packages
dav
parents: 2
diff changeset
  1483
            return Integer.valueOf(getValue());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
         * Set the value of this object as a Number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
         * @return True if the value was set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
        public boolean setCurrentAccessibleValue(Number n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
            // TIGER - 4422535
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
            if (n == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
            setValue(n.intValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1497
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
         * Get the minimum accessible value of this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
         * @return The minimum value of this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
        public Number getMinimumAccessibleValue() {
438
2ae294e4518c 6613529: Avoid duplicate object creation within JDK packages
dav
parents: 2
diff changeset
  1506
            return Integer.valueOf(getMinimum());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
         * Get the maximum accessible value of this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
         * @return The maximum value of this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
        public Number getMaximumAccessibleValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
            // TIGER - 4422362
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
            BoundedRangeModel model = JSlider.this.getModel();
438
2ae294e4518c 6613529: Avoid duplicate object creation within JDK packages
dav
parents: 2
diff changeset
  1517
            return Integer.valueOf(model.getMaximum() - model.getExtent());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
    } // AccessibleJSlider
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
}