API Overview API Index Package Overview Direct link to this page
JDK 1.6
  javax.swing. SpinnerNumberModel View Javadoc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433

/*
 * @(#)SpinnerNumberModel.java	1.13 05/11/17
 *
 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
 * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

package javax.swing;

import java.util.*;
import java.io.Serializable;


/** 
 * A <code>SpinnerModel</code> for sequences of numbers. 
 * The upper and lower bounds of the sequence are defined
 * by properties called <code>minimum</code> and
 * <code>maximum</code>. The size of the increase or decrease
 * computed by the <code>nextValue</code> and
 * <code>previousValue</code> methods is defined by a property called
 * <code>stepSize</code>.  The <code>minimum</code> and 
 * <code>maximum</code> properties can be <code>null</code>
 * to indicate that the sequence has no lower or upper limit.
 * All of the properties in this class are defined in terms of two
 * generic types: <code>Number</code> and 
 * <code>Comparable</code>, so that all Java numeric types
 * may be accommodated.  Internally, there's only support for
 * values whose type is one of the primitive <code>Number</code> types:
 * <code>Double</code>, <code>Float</code>, <code>Long</code>,
 * <code>Integer</code>, <code>Short</code>, or <code>Byte</code>.  
 * <p> 
 * To create a <code>SpinnerNumberModel</code> for the integer 
 * range zero to one hundred, with
 * fifty as the initial value, one could write: 
 * <pre> 
 * Integer value = new Integer(50); 
 * Integer min = new Integer(0);
 * Integer max = new Integer(100); 
 * Integer step = new Integer(1); 
 * SpinnerNumberModel model = new SpinnerNumberModel(value, min, max, step); 
 * int fifty = model.getNumber().intValue(); 
 * </pre> 
 * <p> 
 * Spinners for integers and doubles are common, so special constructors 
 * for these cases are provided.  For example to create the model in 
 * the previous example, one could also write: 
 * <pre> 
 * SpinnerNumberModel model = new SpinnerNumberModel(50, 0, 100, 1); 
 * </pre>
 * <p>
 * This model inherits a <code>ChangeListener</code>. 
 * The <code>ChangeListeners</code> are notified
 * whenever the model's <code>value</code>, <code>stepSize</code>, 
 * <code>minimum</code>, or <code>maximum</code> properties changes.
 * 
 * @see JSpinner
 * @see SpinnerModel
 * @see AbstractSpinnerModel
 * @see SpinnerListModel
 * @see SpinnerDateModel
 * 
 * @version 1.13 11/17/05
 * @author Hans Muller
 * @since 1.4 
*/
public class SpinnerNumberModel extends AbstractSpinnerModel implements Serializable
{
    private Number stepSize, value;
    private Comparable minimum, maximum;


    /**
     * Constructs a <code>SpinnerModel</code> that represents
     * a closed sequence of 
     * numbers from <code>minimum</code> to <code>maximum</code>.  The
     * <code>nextValue</code> and <code>previousValue</code> methods 
     * compute elements of the sequence by adding or subtracting 
     * <code>stepSize</code> respectively.  All of the parameters
     * must be mutually <code>Comparable</code>, <code>value</code>
     * and <code>stepSize</code> must be instances of <code>Integer</code>
     * <code>Long</code>, <code>Float</code>, or <code>Double</code>.
     * <p>
     * The <code>minimum</code> and <code>maximum</code> parameters
     * can be <code>null</code> to indicate that the range doesn't
     * have an upper or lower bound.  
     * If <code>value</code> or <code>stepSize</code> is <code>null</code>,
     * or if both <code>minimum</code> and <code>maximum</code>
     * are specified and <code>mininum &gt; maximum</code> then an
     * <code>IllegalArgumentException</code> is thrown.
     * Similarly if <code>(minimum &lt;= value &lt;= maximum</code>) is false,
     * an <code>IllegalArgumentException</code> is thrown.
     * 
     * @param value the current (non <code>null</code>) value of the model
     * @param minimum the first number in the sequence or <code>null</code>
     * @param maximum the last number in the sequence or <code>null</code>
     * @param stepSize the difference between elements of the sequence
     * 
     * @throws IllegalArgumentException if stepSize or value is
     *     <code>null</code> or if the following expression is false:
     *     <code>minimum &lt;= value &lt;= maximum</code>
     */
    public SpinnerNumberModel(Number value, Comparable minimum, Comparable maximum, Number stepSize) {
	if ((value == null) || (stepSize == null)) {
	    throw new IllegalArgumentException("value and stepSize must be non-null");
	}
	if (!(((minimum == null) || (minimum.compareTo(value) <= 0)) && 
	      ((maximum == null) || (maximum.compareTo(value) >= 0)))) {
	    throw new IllegalArgumentException("(minimum <= value <= maximum) is false");
	}
	this.value = value;
	this.minimum = minimum;
	this.maximum = maximum;
	this.stepSize = stepSize;
    }


    /**
     * Constructs a <code>SpinnerNumberModel</code> with the specified
     * <code>value</code>, <code>minimum</code>/<code>maximum</code> bounds,
     * and <code>stepSize</code>.
     * 
     * @param value the current value of the model
     * @param minimum the first number in the sequence
     * @param maximum the last number in the sequence
     * @param stepSize the difference between elements of the sequence
     * @throws IllegalArgumentException if the following expression is false:
     *     <code>minimum &lt;= value &lt;= maximum</code>
     */
    public SpinnerNumberModel(int value, int minimum, int maximum, int stepSize) {
	this(new Integer(value), new Integer(minimum), new Integer(maximum), new Integer(stepSize));
    }


    /**
     * Constructs a <code>SpinnerNumberModel</code> with the specified
     * <code>value</code>, <code>minimum</code>/<code>maximum</code> bounds, 
     * and <code>stepSize</code>.
     * 
     * @param value the current value of the model
     * @param minimum the first number in the sequence
     * @param maximum the last number in the sequence
     * @param stepSize the difference between elements of the sequence
     * @throws IllegalArgumentException   if the following expression is false:
     *     <code>minimum &lt;= value &lt;= maximum</code>
     */
    public SpinnerNumberModel(double value, double minimum, double maximum, double stepSize) {
	this(new Double(value), new Double(minimum), new Double(maximum), new Double(stepSize));
    }


    /**
     * Constructs a <code>SpinnerNumberModel</code> with no
     * <code>minimum</code> or <code>maximum</code> value, 
     * <code>stepSize</code> equal to one, and an initial value of zero.
     */
    public SpinnerNumberModel() {
	this(new Integer(0), null, null, new Integer(1));
    }


    /**
     * Changes the lower bound for numbers in this sequence. 
     * If <code>minimum</code> is <code>null</code>,
     * then there is no lower bound.  No bounds checking is done here;
     * the new <code>minimum</code> value may invalidate the
     * <code>(minimum &lt;= value &lt= maximum)</code>
     * invariant enforced by the constructors.  This is to simplify updating
     * the model, naturally one should ensure that the invariant is true
     * before calling the <code>getNextValue</code>,
     * <code>getPreviousValue</code>, or <code>setValue</code> methods.
     * <p>
     * Typically this property is a <code>Number</code> of the same type
     * as the <code>value</code> however it's possible to use any
     * <code>Comparable</code> with a <code>compareTo</code>
     * method for a <code>Number</code> with the same type as the value. 
     * For example if value was a <code>Long</code>,
     * <code>minimum</code> might be a Date subclass defined like this:
     * <pre>
     * MyDate extends Date {  // Date already implements Comparable
     *     public int compareTo(Long o) {
     *         long t = getTime();
     *         return (t < o.longValue() ? -1 : (t == o.longValue() ? 0 : 1));
     *     }
     * }
     * </pre>
     * <p>
     * This method fires a <code>ChangeEvent</code>
     * if the <code>minimum</code> has changed.
     * 
     * @param minimum a <code>Comparable</code> that has a
     *     <code>compareTo</code> method for <code>Number</code>s with 
     *     the same type as <code>value</code>
     * @see #getMinimum
     * @see #setMaximum
     * @see SpinnerModel#addChangeListener
     */
    public void setMinimum(Comparable minimum) {
	if ((minimum == null) ? (this.minimum != null) : !minimum.equals(this.minimum)) {
	    this.minimum = minimum;
	    fireStateChanged();
	}
    }


    /**
     * Returns the first number in this sequence.
     * 
     * @return the value of the <code>minimum</code> property
     * @see #setMinimum
     */
    public Comparable getMinimum() {
	return minimum;
    }


    /**
     * Changes the upper bound for numbers in this sequence. 
     * If <code>maximum</code> is <code>null</code>, then there
     * is no upper bound.  No bounds checking is done here; the new 
     * <code>maximum</code> value may invalidate the
     * <code>(minimum <= value < maximum)</code>
     * invariant enforced by the constructors.  This is to simplify updating
     * the model, naturally one should ensure that the invariant is true
     * before calling the <code>next</code>, <code>previous</code>,
     * or <code>setValue</code> methods.  
     * <p>
     * Typically this property is a <code>Number</code> of the same type
     * as the <code>value</code> however it's possible to use any
     * <code>Comparable</code> with a <code>compareTo</code>
     * method for a <code>Number</code> with the same type as the value. 
     * See <a href="#setMinimum(java.lang.Comparable)">
     * <code>setMinimum</code></a> for an example.
     * <p>
     * This method fires a <code>ChangeEvent</code> if the
     * <code>maximum</code> has changed.
     * 
     * @param maximum a <code>Comparable</code> that has a
     *     <code>compareTo</code> method for <code>Number</code>s with 
     *     the same type as <code>value</code>
     * @see #getMaximum
     * @see #setMinimum
     * @see SpinnerModel#addChangeListener
     */
    public void setMaximum(Comparable maximum) {
	if ((maximum == null) ? (this.maximum != null) : !maximum.equals(this.maximum)) {
	    this.maximum = maximum;
	    fireStateChanged();
	}
    }


    /**
     * Returns the last number in the sequence.
     * 
     * @return the value of the <code>maximum</code> property
     * @see #setMaximum
     */
    public Comparable getMaximum() {
	return maximum;
    }


    /**
     * Changes the size of the value change computed by the
     * <code>getNextValue</code> and <code>getPreviousValue</code>
     * methods.  An <code>IllegalArgumentException</code>
     * is thrown if <code>stepSize</code> is <code>null</code>.
     * <p>
     * This method fires a <code>ChangeEvent</code> if the
     * <code>stepSize</code> has changed.
     * 
     * @param stepSize the size of the value change computed by the 
     *     <code>getNextValue</code> and <code>getPreviousValue</code> methods
     * @see #getNextValue
     * @see #getPreviousValue
     * @see #getStepSize
     * @see SpinnerModel#addChangeListener
     */
    public void setStepSize(Number stepSize) {
	if (stepSize == null) {
	    throw new IllegalArgumentException("null stepSize");
	}
	if (!stepSize.equals(this.stepSize)) {
	    this.stepSize = stepSize;
	    fireStateChanged();
	}
    }


    /**
     * Returns the size of the value change computed by the
     * <code>getNextValue</code> 
     * and <code>getPreviousValue</code> methods.  
     * 
     * @return the value of the <code>stepSize</code> property
     * @see #setStepSize
     */
    public Number getStepSize() {
	return stepSize;
    }


    private Number incrValue(int dir) 
    {
	Number newValue;
	if ((value instanceof Float) || (value instanceof Double)) {
	    double v = value.doubleValue() + (stepSize.doubleValue() * (double)dir);
	    if (value instanceof Double) {
		newValue = new Double(v);
	    }
	    else {
		newValue = new Float(v);
	    }
	}
	else {
	    long v = value.longValue() + (stepSize.longValue() * (long)dir);

	    if (value instanceof Long) {
		newValue = new Long(v);
	    }
	    else if (value instanceof Integer) {
		newValue = new Integer((int)v);
	    }
	    else if (value instanceof Short) {
		newValue = new Short((short)v);
	    }
	    else {
		newValue = new Byte((byte)v);
	    }
	}

	if ((maximum != null) && (maximum.compareTo(newValue) < 0)) {
	    return null;
	}
	if ((minimum != null) && (minimum.compareTo(newValue) > 0)) {
	    return null;
	}
	else {
	    return newValue;
	}
    }


    /**
     * Returns the next number in the sequence.
     * 
     * @return <code>value + stepSize</code> or <code>null</code> if the sum 
     *     exceeds <code>maximum</code>.
     * 
     * @see SpinnerModel#getNextValue
     * @see #getPreviousValue
     * @see #setStepSize
     */
    public Object getNextValue() {
	return incrValue(+1);
    }


    /**
     * Returns the previous number in the sequence.
     * 
     * @return <code>value - stepSize</code>, or
     *     <code>null</code> if the sum is less 
     *     than <code>minimum</code>.
     * 
     * @see SpinnerModel#getPreviousValue
     * @see #getNextValue
     * @see #setStepSize
     */
    public Object getPreviousValue() {
	return incrValue(-1);
    }


    /**
     * Returns the value of the current element of the sequence.
     * 
     * @return the value property
     * @see #setValue
     */
    public Number getNumber() {
	return value;
    }


    /**
     * Returns the value of the current element of the sequence.
     * 
     * @return the value property
     * @see #setValue
     * @see #getNumber
     */
    public Object getValue() {
	return value;
    }


    /**
     * Sets the current value for this sequence.  If <code>value</code> is
     * <code>null</code>, or not a <code>Number</code>, an
     * <code>IllegalArgumentException</code> is thrown.  No 
     * bounds checking is done here; the new value may invalidate the 
     * <code>(minimum &lt;= value &lt;= maximum)</code>
     * invariant enforced by the constructors.   It's also possible to set 
     * the value to be something that wouldn't naturally occur in the sequence,
     * i.e. a value that's not modulo the <code>stepSize</code>. 
     * This is to simplify updating the model, and to accommodate
     * spinners that don't want to restrict values that have been
     * directly entered by the user. Naturally, one should ensure that the 
     * <code>(minimum &lt;= value &lt;= maximum)</code> invariant is true
     * before calling the <code>next</code>, <code>previous</code>, or
     * <code>setValue</code> methods.  
     * <p>
     * This method fires a <code>ChangeEvent</code> if the value has changed.
     * 
     * @param value the current (non <code>null</code>) <code>Number</code>
     *         for this sequence
     * @throws IllegalArgumentException if <code>value</code> is
     *         <code>null</code> or not a <code>Number</code>
     * @see #getNumber
     * @see #getValue
     * @see SpinnerModel#addChangeListener
     */
    public void setValue(Object value) {
	if ((value == null) || !(value instanceof Number)) {
	    throw new IllegalArgumentException("illegal value");
	}
	if (!value.equals(this.value)) {
	    this.value = (Number)value;
	    fireStateChanged();
	}
    }
}

Generated By: JavaOnTracks Doclet 0.1.4     ©Thibaut Colar