jdk/src/java.desktop/share/classes/javax/swing/SpinnerNumberModel.java
author aghaisas
Mon, 10 Jul 2017 14:55:29 +0530
changeset 47151 362dcbee0613
parent 37714 7a0b1c7e7054
permissions -rw-r--r--
6919529: NPE from MultiUIDefaults.getUIError Reviewed-by: aghaisas, psadhukhan, serb Contributed-by: shashidhara.veerabhadraiah@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23697
e556a715949f 8034169: Fix serial lint warnings in javax.swing
darcy
parents: 23010
diff changeset
     2
 * Copyright (c) 2000, 2014, 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: 715
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: 715
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: 715
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
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 java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.Serializable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * A <code>SpinnerModel</code> for sequences of numbers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * The upper and lower bounds of the sequence are defined
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * by properties called <code>minimum</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * <code>maximum</code>. The size of the increase or decrease
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * computed by the <code>nextValue</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * <code>previousValue</code> methods is defined by a property called
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * <code>stepSize</code>.  The <code>minimum</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * <code>maximum</code> properties can be <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * to indicate that the sequence has no lower or upper limit.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * All of the properties in this class are defined in terms of two
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * generic types: <code>Number</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * <code>Comparable</code>, so that all Java numeric types
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * may be accommodated.  Internally, there's only support for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * values whose type is one of the primitive <code>Number</code> types:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * <code>Double</code>, <code>Float</code>, <code>Long</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * <code>Integer</code>, <code>Short</code>, or <code>Byte</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * To create a <code>SpinnerNumberModel</code> for the integer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * range zero to one hundred, with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * fifty as the initial value, one could write:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * <pre>
37714
7a0b1c7e7054 8154213: clean up uses of boxed primitive constructors in the java.desktop module
prr
parents: 25859
diff changeset
    54
 * Integer value = Integer.valueOf(50);
7a0b1c7e7054 8154213: clean up uses of boxed primitive constructors in the java.desktop module
prr
parents: 25859
diff changeset
    55
 * Integer min = Integer.valueOf(0);
7a0b1c7e7054 8154213: clean up uses of boxed primitive constructors in the java.desktop module
prr
parents: 25859
diff changeset
    56
 * Integer max = Integer.valueOf(100);
7a0b1c7e7054 8154213: clean up uses of boxed primitive constructors in the java.desktop module
prr
parents: 25859
diff changeset
    57
 * Integer step = Integer.valueOf(1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * SpinnerNumberModel model = new SpinnerNumberModel(value, min, max, step);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * int fifty = model.getNumber().intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * Spinners for integers and doubles are common, so special constructors
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * for these cases are provided.  For example to create the model in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * the previous example, one could also write:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * SpinnerNumberModel model = new SpinnerNumberModel(50, 0, 100, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * This model inherits a <code>ChangeListener</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * The <code>ChangeListeners</code> are notified
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * whenever the model's <code>value</code>, <code>stepSize</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * <code>minimum</code>, or <code>maximum</code> properties changes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * @see JSpinner
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * @see SpinnerModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * @see AbstractSpinnerModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * @see SpinnerListModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * @see SpinnerDateModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * @author Hans Muller
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
*/
23697
e556a715949f 8034169: Fix serial lint warnings in javax.swing
darcy
parents: 23010
diff changeset
    83
@SuppressWarnings("serial") // Superclass is not serializable across versions
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
public class SpinnerNumberModel extends AbstractSpinnerModel implements Serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    private Number stepSize, value;
25568
b906a74c6882 8043550: Fix raw and unchecked lint warnings in javax.swing.*
darcy
parents: 23697
diff changeset
    87
    // Both minimum and maximum are logically Comparable<? extends
b906a74c6882 8043550: Fix raw and unchecked lint warnings in javax.swing.*
darcy
parents: 23697
diff changeset
    88
    // Number>, but that type is awkward to use since different
b906a74c6882 8043550: Fix raw and unchecked lint warnings in javax.swing.*
darcy
parents: 23697
diff changeset
    89
    // instances of Number are not naturally Comparable. For example,
b906a74c6882 8043550: Fix raw and unchecked lint warnings in javax.swing.*
darcy
parents: 23697
diff changeset
    90
    // a Double implements Comparable<Double> and an Integer
b906a74c6882 8043550: Fix raw and unchecked lint warnings in javax.swing.*
darcy
parents: 23697
diff changeset
    91
    // implements Comparable<Integer>. Neither Integer nor Double will
b906a74c6882 8043550: Fix raw and unchecked lint warnings in javax.swing.*
darcy
parents: 23697
diff changeset
    92
    // have a bridge method for Comparable<Number>. However, it safe
b906a74c6882 8043550: Fix raw and unchecked lint warnings in javax.swing.*
darcy
parents: 23697
diff changeset
    93
    // to cast Comparable<?> to Comparable<Object> since all
b906a74c6882 8043550: Fix raw and unchecked lint warnings in javax.swing.*
darcy
parents: 23697
diff changeset
    94
    // Comparables will have a compare(Object> method, possibly as a
b906a74c6882 8043550: Fix raw and unchecked lint warnings in javax.swing.*
darcy
parents: 23697
diff changeset
    95
    // bridge.
b906a74c6882 8043550: Fix raw and unchecked lint warnings in javax.swing.*
darcy
parents: 23697
diff changeset
    96
    private Comparable<?> minimum, maximum;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * Constructs a <code>SpinnerModel</code> that represents
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * a closed sequence of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * numbers from <code>minimum</code> to <code>maximum</code>.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * <code>nextValue</code> and <code>previousValue</code> methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * compute elements of the sequence by adding or subtracting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * <code>stepSize</code> respectively.  All of the parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * must be mutually <code>Comparable</code>, <code>value</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * and <code>stepSize</code> must be instances of <code>Integer</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * <code>Long</code>, <code>Float</code>, or <code>Double</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * The <code>minimum</code> and <code>maximum</code> parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * can be <code>null</code> to indicate that the range doesn't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * have an upper or lower bound.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * If <code>value</code> or <code>stepSize</code> is <code>null</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * or if both <code>minimum</code> and <code>maximum</code>
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20169
diff changeset
   115
     * are specified and <code>minimum &gt; maximum</code> then an
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * <code>IllegalArgumentException</code> is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * Similarly if <code>(minimum &lt;= value &lt;= maximum</code>) is false,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * an <code>IllegalArgumentException</code> is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * @param value the current (non <code>null</code>) value of the model
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * @param minimum the first number in the sequence or <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * @param maximum the last number in the sequence or <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * @param stepSize the difference between elements of the sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * @throws IllegalArgumentException if stepSize or value is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     *     <code>null</code> or if the following expression is false:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     *     <code>minimum &lt;= value &lt;= maximum</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     */
25568
b906a74c6882 8043550: Fix raw and unchecked lint warnings in javax.swing.*
darcy
parents: 23697
diff changeset
   129
    @SuppressWarnings("unchecked") // Casts to Comparable<Object>
b906a74c6882 8043550: Fix raw and unchecked lint warnings in javax.swing.*
darcy
parents: 23697
diff changeset
   130
    public SpinnerNumberModel(Number value,
b906a74c6882 8043550: Fix raw and unchecked lint warnings in javax.swing.*
darcy
parents: 23697
diff changeset
   131
                               Comparable<?> minimum,
b906a74c6882 8043550: Fix raw and unchecked lint warnings in javax.swing.*
darcy
parents: 23697
diff changeset
   132
                               Comparable<?> maximum,
b906a74c6882 8043550: Fix raw and unchecked lint warnings in javax.swing.*
darcy
parents: 23697
diff changeset
   133
                               Number stepSize) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        if ((value == null) || (stepSize == null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            throw new IllegalArgumentException("value and stepSize must be non-null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        }
25568
b906a74c6882 8043550: Fix raw and unchecked lint warnings in javax.swing.*
darcy
parents: 23697
diff changeset
   137
        if (!(((minimum == null) || (((Comparable<Object>)minimum).compareTo(value) <= 0)) &&
b906a74c6882 8043550: Fix raw and unchecked lint warnings in javax.swing.*
darcy
parents: 23697
diff changeset
   138
              ((maximum == null) || (((Comparable<Object>)maximum).compareTo(value) >= 0)))) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            throw new IllegalArgumentException("(minimum <= value <= maximum) is false");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        this.value = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        this.minimum = minimum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        this.maximum = maximum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        this.stepSize = stepSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * Constructs a <code>SpinnerNumberModel</code> with the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * <code>value</code>, <code>minimum</code>/<code>maximum</code> bounds,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * and <code>stepSize</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * @param value the current value of the model
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * @param minimum the first number in the sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * @param maximum the last number in the sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * @param stepSize the difference between elements of the sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * @throws IllegalArgumentException if the following expression is false:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     *     <code>minimum &lt;= value &lt;= maximum</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    public SpinnerNumberModel(int value, int minimum, int maximum, int stepSize) {
438
2ae294e4518c 6613529: Avoid duplicate object creation within JDK packages
dav
parents: 2
diff changeset
   161
        this(Integer.valueOf(value), Integer.valueOf(minimum), Integer.valueOf(maximum), Integer.valueOf(stepSize));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * Constructs a <code>SpinnerNumberModel</code> with the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * <code>value</code>, <code>minimum</code>/<code>maximum</code> bounds,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * and <code>stepSize</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * @param value the current value of the model
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * @param minimum the first number in the sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * @param maximum the last number in the sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * @param stepSize the difference between elements of the sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * @throws IllegalArgumentException   if the following expression is false:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     *     <code>minimum &lt;= value &lt;= maximum</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    public SpinnerNumberModel(double value, double minimum, double maximum, double stepSize) {
37714
7a0b1c7e7054 8154213: clean up uses of boxed primitive constructors in the java.desktop module
prr
parents: 25859
diff changeset
   178
        this(Double.valueOf(value), Double.valueOf(minimum),
7a0b1c7e7054 8154213: clean up uses of boxed primitive constructors in the java.desktop module
prr
parents: 25859
diff changeset
   179
             Double.valueOf(maximum), Double.valueOf(stepSize));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * Constructs a <code>SpinnerNumberModel</code> with no
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * <code>minimum</code> or <code>maximum</code> value,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * <code>stepSize</code> equal to one, and an initial value of zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    public SpinnerNumberModel() {
438
2ae294e4518c 6613529: Avoid duplicate object creation within JDK packages
dav
parents: 2
diff changeset
   189
        this(Integer.valueOf(0), null, null, Integer.valueOf(1));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * Changes the lower bound for numbers in this sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * If <code>minimum</code> is <code>null</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * then there is no lower bound.  No bounds checking is done here;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * the new <code>minimum</code> value may invalidate the
20169
d7fa6d7586c9 8025085: [javadoc] some errors in javax/swing
yan
parents: 5506
diff changeset
   198
     * <code>(minimum &lt;= value &lt;= maximum)</code>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * invariant enforced by the constructors.  This is to simplify updating
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * the model, naturally one should ensure that the invariant is true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * before calling the <code>getNextValue</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * <code>getPreviousValue</code>, or <code>setValue</code> methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * Typically this property is a <code>Number</code> of the same type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * as the <code>value</code> however it's possible to use any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * <code>Comparable</code> with a <code>compareTo</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * method for a <code>Number</code> with the same type as the value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * For example if value was a <code>Long</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * <code>minimum</code> might be a Date subclass defined like this:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * MyDate extends Date {  // Date already implements Comparable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     *     public int compareTo(Long o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     *         long t = getTime();
20169
d7fa6d7586c9 8025085: [javadoc] some errors in javax/swing
yan
parents: 5506
diff changeset
   214
     *         return (t &lt; o.longValue() ? -1 : (t == o.longValue() ? 0 : 1));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     *     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * This method fires a <code>ChangeEvent</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * if the <code>minimum</code> has changed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * @param minimum a <code>Comparable</code> that has a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     *     <code>compareTo</code> method for <code>Number</code>s with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     *     the same type as <code>value</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * @see #getMinimum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * @see #setMaximum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * @see SpinnerModel#addChangeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     */
25568
b906a74c6882 8043550: Fix raw and unchecked lint warnings in javax.swing.*
darcy
parents: 23697
diff changeset
   229
    public void setMinimum(Comparable<?> minimum) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        if ((minimum == null) ? (this.minimum != null) : !minimum.equals(this.minimum)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            this.minimum = minimum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            fireStateChanged();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * Returns the first number in this sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * @return the value of the <code>minimum</code> property
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * @see #setMinimum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     */
25568
b906a74c6882 8043550: Fix raw and unchecked lint warnings in javax.swing.*
darcy
parents: 23697
diff changeset
   243
    public Comparable<?> getMinimum() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        return minimum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * Changes the upper bound for numbers in this sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * If <code>maximum</code> is <code>null</code>, then there
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * is no upper bound.  No bounds checking is done here; the new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * <code>maximum</code> value may invalidate the
20169
d7fa6d7586c9 8025085: [javadoc] some errors in javax/swing
yan
parents: 5506
diff changeset
   253
     * <code>(minimum &lt;= value &lt; maximum)</code>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * invariant enforced by the constructors.  This is to simplify updating
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * the model, naturally one should ensure that the invariant is true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * before calling the <code>next</code>, <code>previous</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * or <code>setValue</code> methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * Typically this property is a <code>Number</code> of the same type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * as the <code>value</code> however it's possible to use any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * <code>Comparable</code> with a <code>compareTo</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * method for a <code>Number</code> with the same type as the value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * See <a href="#setMinimum(java.lang.Comparable)">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * <code>setMinimum</code></a> for an example.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * This method fires a <code>ChangeEvent</code> if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * <code>maximum</code> has changed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * @param maximum a <code>Comparable</code> that has a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     *     <code>compareTo</code> method for <code>Number</code>s with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     *     the same type as <code>value</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * @see #getMaximum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * @see #setMinimum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * @see SpinnerModel#addChangeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     */
25568
b906a74c6882 8043550: Fix raw and unchecked lint warnings in javax.swing.*
darcy
parents: 23697
diff changeset
   276
    public void setMaximum(Comparable<?> maximum) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        if ((maximum == null) ? (this.maximum != null) : !maximum.equals(this.maximum)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            this.maximum = maximum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            fireStateChanged();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * Returns the last number in the sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * @return the value of the <code>maximum</code> property
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * @see #setMaximum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     */
25568
b906a74c6882 8043550: Fix raw and unchecked lint warnings in javax.swing.*
darcy
parents: 23697
diff changeset
   290
    public Comparable<?> getMaximum() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        return maximum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * Changes the size of the value change computed by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * <code>getNextValue</code> and <code>getPreviousValue</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * methods.  An <code>IllegalArgumentException</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * is thrown if <code>stepSize</code> is <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * This method fires a <code>ChangeEvent</code> if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * <code>stepSize</code> has changed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * @param stepSize the size of the value change computed by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     *     <code>getNextValue</code> and <code>getPreviousValue</code> methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * @see #getNextValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * @see #getPreviousValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * @see #getStepSize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * @see SpinnerModel#addChangeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    public void setStepSize(Number stepSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        if (stepSize == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            throw new IllegalArgumentException("null stepSize");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        if (!stepSize.equals(this.stepSize)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            this.stepSize = stepSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            fireStateChanged();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * Returns the size of the value change computed by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * <code>getNextValue</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * and <code>getPreviousValue</code> methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * @return the value of the <code>stepSize</code> property
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * @see #setStepSize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    public Number getStepSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        return stepSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
25568
b906a74c6882 8043550: Fix raw and unchecked lint warnings in javax.swing.*
darcy
parents: 23697
diff changeset
   334
    @SuppressWarnings("unchecked") // Casts to Comparable<Object>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    private Number incrValue(int dir)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        Number newValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        if ((value instanceof Float) || (value instanceof Double)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            double v = value.doubleValue() + (stepSize.doubleValue() * (double)dir);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            if (value instanceof Double) {
37714
7a0b1c7e7054 8154213: clean up uses of boxed primitive constructors in the java.desktop module
prr
parents: 25859
diff changeset
   341
                newValue = Double.valueOf(v);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            else {
37714
7a0b1c7e7054 8154213: clean up uses of boxed primitive constructors in the java.desktop module
prr
parents: 25859
diff changeset
   344
                newValue = Float.valueOf((float)v);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            }
25568
b906a74c6882 8043550: Fix raw and unchecked lint warnings in javax.swing.*
darcy
parents: 23697
diff changeset
   346
        } else {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            long v = value.longValue() + (stepSize.longValue() * (long)dir);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            if (value instanceof Long) {
438
2ae294e4518c 6613529: Avoid duplicate object creation within JDK packages
dav
parents: 2
diff changeset
   350
                newValue = Long.valueOf(v);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            else if (value instanceof Integer) {
438
2ae294e4518c 6613529: Avoid duplicate object creation within JDK packages
dav
parents: 2
diff changeset
   353
                newValue = Integer.valueOf((int)v);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            else if (value instanceof Short) {
438
2ae294e4518c 6613529: Avoid duplicate object creation within JDK packages
dav
parents: 2
diff changeset
   356
                newValue = Short.valueOf((short)v);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            else {
438
2ae294e4518c 6613529: Avoid duplicate object creation within JDK packages
dav
parents: 2
diff changeset
   359
                newValue = Byte.valueOf((byte)v);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
25568
b906a74c6882 8043550: Fix raw and unchecked lint warnings in javax.swing.*
darcy
parents: 23697
diff changeset
   363
        if ((maximum != null) && (((Comparable<Object>)maximum).compareTo(newValue) < 0)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        }
25568
b906a74c6882 8043550: Fix raw and unchecked lint warnings in javax.swing.*
darcy
parents: 23697
diff changeset
   366
        if ((minimum != null) && (((Comparable<Object>)minimum).compareTo(newValue) > 0)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            return newValue;
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * Returns the next number in the sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     * @return <code>value + stepSize</code> or <code>null</code> if the sum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     *     exceeds <code>maximum</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * @see SpinnerModel#getNextValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     * @see #getPreviousValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * @see #setStepSize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    public Object getNextValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        return incrValue(+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * Returns the previous number in the sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * @return <code>value - stepSize</code>, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     *     <code>null</code> if the sum is less
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     *     than <code>minimum</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * @see SpinnerModel#getPreviousValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * @see #getNextValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * @see #setStepSize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
    public Object getPreviousValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        return incrValue(-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     * Returns the value of the current element of the sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * @return the value property
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     * @see #setValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    public Number getNumber() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     * Returns the value of the current element of the sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * @return the value property
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     * @see #setValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * @see #getNumber
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
    public Object getValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     * Sets the current value for this sequence.  If <code>value</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     * <code>null</code>, or not a <code>Number</code>, an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     * <code>IllegalArgumentException</code> is thrown.  No
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     * bounds checking is done here; the new value may invalidate the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     * <code>(minimum &lt;= value &lt;= maximum)</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     * invariant enforced by the constructors.   It's also possible to set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     * the value to be something that wouldn't naturally occur in the sequence,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     * i.e. a value that's not modulo the <code>stepSize</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     * This is to simplify updating the model, and to accommodate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     * spinners that don't want to restrict values that have been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     * directly entered by the user. Naturally, one should ensure that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     * <code>(minimum &lt;= value &lt;= maximum)</code> invariant is true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     * before calling the <code>next</code>, <code>previous</code>, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     * <code>setValue</code> methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     * This method fires a <code>ChangeEvent</code> if the value has changed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     * @param value the current (non <code>null</code>) <code>Number</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     *         for this sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     * @throws IllegalArgumentException if <code>value</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     *         <code>null</code> or not a <code>Number</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     * @see #getNumber
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     * @see #getValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     * @see SpinnerModel#addChangeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
    public void setValue(Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        if ((value == null) || !(value instanceof Number)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
            throw new IllegalArgumentException("illegal value");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        if (!value.equals(this.value)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
            this.value = (Number)value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
            fireStateChanged();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
}