jdk/src/share/classes/javax/swing/SpinnerNumberModel.java
author xdono
Wed, 02 Jul 2008 12:55:45 -0700
changeset 715 f16baef3a20e
parent 438 2ae294e4518c
child 5506 202f599c92aa
permissions -rw-r--r--
6719955: Update copyright year Summary: Update copyright year for files that have been modified in 2008 Reviewed-by: ohair, tbell
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
715
f16baef3a20e 6719955: Update copyright year
xdono
parents: 438
diff changeset
     2
 * Copyright 2000-2008 Sun Microsystems, Inc.  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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
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>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * Integer value = new Integer(50);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * Integer min = new Integer(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * Integer max = new Integer(100);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * Integer step = new Integer(1);
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
*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
public class SpinnerNumberModel extends AbstractSpinnerModel implements Serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    private Number stepSize, value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    private Comparable minimum, maximum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * Constructs a <code>SpinnerModel</code> that represents
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * a closed sequence of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * numbers from <code>minimum</code> to <code>maximum</code>.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * <code>nextValue</code> and <code>previousValue</code> methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * compute elements of the sequence by adding or subtracting
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * <code>stepSize</code> respectively.  All of the parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * must be mutually <code>Comparable</code>, <code>value</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * and <code>stepSize</code> must be instances of <code>Integer</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * <code>Long</code>, <code>Float</code>, or <code>Double</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * The <code>minimum</code> and <code>maximum</code> parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * can be <code>null</code> to indicate that the range doesn't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * have an upper or lower bound.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * If <code>value</code> or <code>stepSize</code> is <code>null</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * or if both <code>minimum</code> and <code>maximum</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * are specified and <code>mininum &gt; maximum</code> then an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * <code>IllegalArgumentException</code> is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * Similarly if <code>(minimum &lt;= value &lt;= maximum</code>) is false,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * an <code>IllegalArgumentException</code> is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * @param value the current (non <code>null</code>) value of the model
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * @param minimum the first number in the sequence or <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * @param maximum the last number in the sequence or <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * @param stepSize the difference between elements of the sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * @throws IllegalArgumentException if stepSize or value is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     *     <code>null</code> or if the following expression is false:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     *     <code>minimum &lt;= value &lt;= maximum</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    public SpinnerNumberModel(Number value, Comparable minimum, Comparable maximum, Number stepSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        if ((value == null) || (stepSize == null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            throw new IllegalArgumentException("value and stepSize must be non-null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        if (!(((minimum == null) || (minimum.compareTo(value) <= 0)) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
              ((maximum == null) || (maximum.compareTo(value) >= 0)))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            throw new IllegalArgumentException("(minimum <= value <= maximum) is false");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        this.value = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        this.minimum = minimum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        this.maximum = maximum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        this.stepSize = stepSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * Constructs a <code>SpinnerNumberModel</code> with the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * <code>value</code>, <code>minimum</code>/<code>maximum</code> bounds,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * and <code>stepSize</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * @param value the current value of the model
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * @param minimum the first number in the sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * @param maximum the last number in the sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * @param stepSize the difference between elements of the sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * @throws IllegalArgumentException if the following expression is false:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     *     <code>minimum &lt;= value &lt;= maximum</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    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
   147
        this(Integer.valueOf(value), Integer.valueOf(minimum), Integer.valueOf(maximum), Integer.valueOf(stepSize));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * Constructs a <code>SpinnerNumberModel</code> with the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * <code>value</code>, <code>minimum</code>/<code>maximum</code> bounds,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * and <code>stepSize</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * @param value the current value of the model
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * @param minimum the first number in the sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * @param maximum the last number in the sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * @param stepSize the difference between elements of the sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * @throws IllegalArgumentException   if the following expression is false:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     *     <code>minimum &lt;= value &lt;= maximum</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    public SpinnerNumberModel(double value, double minimum, double maximum, double stepSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        this(new Double(value), new Double(minimum), new Double(maximum), new Double(stepSize));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * Constructs a <code>SpinnerNumberModel</code> with no
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * <code>minimum</code> or <code>maximum</code> value,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * <code>stepSize</code> equal to one, and an initial value of zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    public SpinnerNumberModel() {
438
2ae294e4518c 6613529: Avoid duplicate object creation within JDK packages
dav
parents: 2
diff changeset
   174
        this(Integer.valueOf(0), null, null, Integer.valueOf(1));
2
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * Changes the lower bound for numbers in this sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * If <code>minimum</code> is <code>null</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * then there is no lower bound.  No bounds checking is done here;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * the new <code>minimum</code> value may invalidate the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * <code>(minimum &lt;= value &lt= maximum)</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * invariant enforced by the constructors.  This is to simplify updating
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * the model, naturally one should ensure that the invariant is true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * before calling the <code>getNextValue</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * <code>getPreviousValue</code>, or <code>setValue</code> methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * Typically this property is a <code>Number</code> of the same type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * as the <code>value</code> however it's possible to use any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * <code>Comparable</code> with a <code>compareTo</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * method for a <code>Number</code> with the same type as the value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * For example if value was a <code>Long</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * <code>minimum</code> might be a Date subclass defined like this:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * MyDate extends Date {  // Date already implements Comparable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     *     public int compareTo(Long o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     *         long t = getTime();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     *         return (t < o.longValue() ? -1 : (t == o.longValue() ? 0 : 1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     *     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * This method fires a <code>ChangeEvent</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * if the <code>minimum</code> has changed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * @param minimum a <code>Comparable</code> that has a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     *     <code>compareTo</code> method for <code>Number</code>s with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     *     the same type as <code>value</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * @see #getMinimum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * @see #setMaximum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * @see SpinnerModel#addChangeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    public void setMinimum(Comparable minimum) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        if ((minimum == null) ? (this.minimum != null) : !minimum.equals(this.minimum)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            this.minimum = minimum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            fireStateChanged();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    }
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
     * Returns the first number in this sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * @return the value of the <code>minimum</code> property
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * @see #setMinimum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    public Comparable getMinimum() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        return minimum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * Changes the upper bound for numbers in this sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * If <code>maximum</code> is <code>null</code>, then there
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * is no upper bound.  No bounds checking is done here; the new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * <code>maximum</code> value may invalidate the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * <code>(minimum <= value < maximum)</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * invariant enforced by the constructors.  This is to simplify updating
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * the model, naturally one should ensure that the invariant is true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * before calling the <code>next</code>, <code>previous</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * or <code>setValue</code> methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * Typically this property is a <code>Number</code> of the same type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * as the <code>value</code> however it's possible to use any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * <code>Comparable</code> with a <code>compareTo</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * method for a <code>Number</code> with the same type as the value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * See <a href="#setMinimum(java.lang.Comparable)">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * <code>setMinimum</code></a> for an example.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * This method fires a <code>ChangeEvent</code> if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * <code>maximum</code> has changed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * @param maximum a <code>Comparable</code> that has a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     *     <code>compareTo</code> method for <code>Number</code>s with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     *     the same type as <code>value</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * @see #getMaximum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * @see #setMinimum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * @see SpinnerModel#addChangeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    public void setMaximum(Comparable maximum) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        if ((maximum == null) ? (this.maximum != null) : !maximum.equals(this.maximum)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            this.maximum = maximum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            fireStateChanged();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * Returns the last number in the sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * @return the value of the <code>maximum</code> property
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * @see #setMaximum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    public Comparable getMaximum() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        return maximum;
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * Changes the size of the value change computed by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * <code>getNextValue</code> and <code>getPreviousValue</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * methods.  An <code>IllegalArgumentException</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * is thrown if <code>stepSize</code> is <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * This method fires a <code>ChangeEvent</code> if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * <code>stepSize</code> has changed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * @param stepSize the size of the value change computed by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     *     <code>getNextValue</code> and <code>getPreviousValue</code> methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * @see #getNextValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * @see #getPreviousValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * @see #getStepSize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * @see SpinnerModel#addChangeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    public void setStepSize(Number stepSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        if (stepSize == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            throw new IllegalArgumentException("null stepSize");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        if (!stepSize.equals(this.stepSize)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            this.stepSize = stepSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            fireStateChanged();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * Returns the size of the value change computed by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * <code>getNextValue</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * and <code>getPreviousValue</code> methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * @return the value of the <code>stepSize</code> property
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * @see #setStepSize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    public Number getStepSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        return stepSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    private Number incrValue(int dir)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        Number newValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        if ((value instanceof Float) || (value instanceof Double)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            double v = value.doubleValue() + (stepSize.doubleValue() * (double)dir);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            if (value instanceof Double) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                newValue = new Double(v);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                newValue = new Float(v);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            long v = value.longValue() + (stepSize.longValue() * (long)dir);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            if (value instanceof Long) {
438
2ae294e4518c 6613529: Avoid duplicate object creation within JDK packages
dav
parents: 2
diff changeset
   336
                newValue = Long.valueOf(v);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            else if (value instanceof Integer) {
438
2ae294e4518c 6613529: Avoid duplicate object creation within JDK packages
dav
parents: 2
diff changeset
   339
                newValue = Integer.valueOf((int)v);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            else if (value instanceof Short) {
438
2ae294e4518c 6613529: Avoid duplicate object creation within JDK packages
dav
parents: 2
diff changeset
   342
                newValue = Short.valueOf((short)v);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            else {
438
2ae294e4518c 6613529: Avoid duplicate object creation within JDK packages
dav
parents: 2
diff changeset
   345
                newValue = Byte.valueOf((byte)v);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        if ((maximum != null) && (maximum.compareTo(newValue) < 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        if ((minimum != null) && (minimum.compareTo(newValue) > 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            return newValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * Returns the next number in the sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * @return <code>value + stepSize</code> or <code>null</code> if the sum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     *     exceeds <code>maximum</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * @see SpinnerModel#getNextValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * @see #getPreviousValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * @see #setStepSize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    public Object getNextValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        return incrValue(+1);
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * Returns the previous number in the sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     * @return <code>value - stepSize</code>, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     *     <code>null</code> if the sum is less
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     *     than <code>minimum</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * @see SpinnerModel#getPreviousValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * @see #getNextValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     * @see #setStepSize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    public Object getPreviousValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        return incrValue(-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * Returns the value of the current element of the sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * @return the value property
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * @see #setValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
    public Number getNumber() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * Returns the value of the current element of the sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     * @return the value property
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     * @see #setValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     * @see #getNumber
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
    public Object getValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     * Sets the current value for this sequence.  If <code>value</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     * <code>null</code>, or not a <code>Number</code>, an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     * <code>IllegalArgumentException</code> is thrown.  No
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     * bounds checking is done here; the new value may invalidate the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * <code>(minimum &lt;= value &lt;= maximum)</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     * invariant enforced by the constructors.   It's also possible to set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * the value to be something that wouldn't naturally occur in the sequence,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     * i.e. a value that's not modulo the <code>stepSize</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     * This is to simplify updating the model, and to accommodate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     * spinners that don't want to restrict values that have been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     * directly entered by the user. Naturally, one should ensure that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     * <code>(minimum &lt;= value &lt;= maximum)</code> invariant is true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     * before calling the <code>next</code>, <code>previous</code>, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     * <code>setValue</code> methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     * This method fires a <code>ChangeEvent</code> if the value has changed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     * @param value the current (non <code>null</code>) <code>Number</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     *         for this sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     * @throws IllegalArgumentException if <code>value</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     *         <code>null</code> or not a <code>Number</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     * @see #getNumber
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     * @see #getValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     * @see SpinnerModel#addChangeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
    public void setValue(Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        if ((value == null) || !(value instanceof Number)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
            throw new IllegalArgumentException("illegal value");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        if (!value.equals(this.value)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
            this.value = (Number)value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
            fireStateChanged();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
}