1 /* SpinnerNumberModel.java --
2 Copyright (C) 2002, 2004, 2006 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
40 import java
.io
.Serializable
;
42 import javax
.swing
.event
.ChangeEvent
;
45 * A model used by the {@link JSpinner} component.
47 * @author Ka-Hing Cheung
50 public class SpinnerNumberModel
extends AbstractSpinnerModel
51 implements Serializable
54 * For compatability with Sun's JDK
56 private static final long serialVersionUID
= 7279176385485777821L;
58 /** The current value. */
61 /** The minimum value (or <code>null</code>). */
62 private Comparable minimum
;
64 /** The maximum value (or <code>null</code>). */
65 private Comparable maximum
;
68 private Number stepSize
;
71 * Creates a <code>SpinnerNumberModel</code> with initial value 0, step 1,
72 * and no maximum nor minimum.
74 public SpinnerNumberModel()
76 this(new Integer(0), null, null, new Integer(1));
80 * Creates a <code>SpinnerNumberModel</code> with double precision.
82 * @param value the initial value
83 * @param minimum the minimum value
84 * @param maximum the maximum value
85 * @param stepSize the step size
86 * @throws IllegalArgumentException if minimum <= value <= maximum does
89 public SpinnerNumberModel(double value
, double minimum
, double maximum
,
92 this(new Double(value
), new Double(minimum
), new Double(maximum
),
93 new Double(stepSize
));
97 * Creates a <code>SpinnerNumberModel</code> with integer precision.
99 * @param value the initial value
100 * @param minimum the minimum value
101 * @param maximum the maximum value
102 * @param stepSize the step size
103 * @throws IllegalArgumentException if minimum <= value <= maximum does
106 public SpinnerNumberModel(int value
, int minimum
, int maximum
, int stepSize
)
108 this(new Integer(value
), new Integer(minimum
), new Integer(maximum
),
109 new Integer(stepSize
));
113 * Creates a <code>SpinnerNumberModel</code> with the given attributes.
115 * @param value the initial value.
116 * @param minimum the minimum value (<code>null</code> permitted).
117 * @param maximum the maximum value (<code>null</code> permitted).
118 * @param stepSize the step size.
120 * @throws IllegalArgumentException if minimum <= value <= maximum
122 * @throws IllegalArgumentException if <code>value</code> is
124 * @throws IllegalArgumentException if <code>stepSize</code> is
127 public SpinnerNumberModel(Number value
, Comparable minimum
,
128 Comparable maximum
, Number stepSize
)
130 if (stepSize
== null)
131 throw new IllegalArgumentException("stepSize may not be null");
133 throw new IllegalArgumentException("value may not be null");
136 if (minimum
.compareTo(value
) > 0)
137 throw new IllegalArgumentException("minimum is not <= value");
141 if (maximum
.compareTo(value
) < 0)
142 throw new IllegalArgumentException("maximum is not >= value");
146 this.stepSize
= stepSize
;
147 this.minimum
= minimum
;
148 this.maximum
= maximum
;
152 * Sets the current value and, if the new value is different to the old
153 * value, sends a {@link ChangeEvent} to all registered listeners.
155 * @param value the new value (<code>null</code> not permitted, must be an
156 * instance of <code>Number</code>).
158 * @throws IllegalArgumentException if <code>value</code> is not an instance
159 * of <code>Number</code>.
161 public void setValue(Object value
)
163 if (! (value
instanceof Number
))
164 throw new IllegalArgumentException("value must be a Number");
166 if (!this.value
.equals(value
))
168 this.value
= (Number
) value
;
174 * Returns the current value.
176 * @return The current value.
178 public Object
getValue()
184 * Returns the next value, or <code>null</code> if adding the step size to
185 * the current value results in a value greater than the maximum value.
186 * The current value is not changed.
188 * @return The next value, or <code>null</code> if the current value is the
189 * maximum value represented by this model.
191 public Object
getNextValue()
195 if (value
instanceof Double
)
196 num
= new Double(value
.doubleValue() + stepSize
.doubleValue());
197 else if (value
instanceof Float
)
198 num
= new Double(value
.floatValue() + stepSize
.floatValue());
199 else if (value
instanceof Long
)
200 num
= new Long(value
.longValue() + stepSize
.longValue());
201 else if (value
instanceof Integer
)
202 num
= new Integer(value
.intValue() + stepSize
.intValue());
203 else if (value
instanceof Short
)
204 num
= new Short((short) (value
.shortValue() + stepSize
.shortValue()));
206 num
= new Byte((byte) (value
.byteValue() + stepSize
.byteValue()));
208 // check upper bound if set
209 if ((maximum
!= null) && maximum
.compareTo(num
) < 0)
216 * Returns the previous value, or <code>null</code> if subtracting the
217 * step size from the current value results in a value less than the minimum
218 * value. The current value is not changed.
220 * @return The previous value, or <code>null</code> if the current value
221 * is the minimum value represented by this model.
223 public Object
getPreviousValue()
227 if (value
instanceof Double
)
228 num
= new Double(value
.doubleValue() - stepSize
.doubleValue());
229 else if (value
instanceof Float
)
230 num
= new Double(value
.floatValue() - stepSize
.floatValue());
231 else if (value
instanceof Long
)
232 num
= new Long(value
.longValue() - stepSize
.longValue());
233 else if (value
instanceof Integer
)
234 num
= new Integer(value
.intValue() - stepSize
.intValue());
235 else if (value
instanceof Short
)
236 num
= new Short((short) (value
.shortValue() - stepSize
.shortValue()));
238 num
= new Byte((byte) (value
.byteValue() - stepSize
.byteValue()));
240 // check lower bound if set
241 if ((minimum
!= null) && minimum
.compareTo(num
) > 0)
248 * Returns the current value.
250 * @return The current value.
252 public Number
getNumber()
258 * Returns the minimum value, or <code>null</code> if there is no minimum.
260 * @return The minimum value.
262 public Comparable
getMinimum()
268 * Sets the minimum value and, if the new value is different to the old
269 * value, sends a {@link ChangeEvent} to all registered listeners. A
270 * <code>null</code> value is interpreted as "no minimum value". No check
271 * is made to ensure that the new minimum is less than or equal to the
272 * current value, the caller is responsible for ensuring that this
273 * relationship holds.
275 * @param newMinimum the new minimum value (<code>null</code> permitted).
277 public void setMinimum(Comparable newMinimum
)
279 if (minimum
!= null ?
!minimum
.equals(newMinimum
) : newMinimum
!= null)
281 minimum
= newMinimum
;
287 * Returns the maximum value, or <code>null</code> if there is no maximum.
289 * @return The maximum value.
291 public Comparable
getMaximum()
297 * Sets the maximum value and, if the new value is different to the old
298 * value, sends a {@link ChangeEvent} to all registered listeners. A
299 * <code>null</code> value is interpreted as "no maximum value". No check
300 * is made to ensure that the new maximum is greater than or equal to the
301 * current value, the caller is responsible for ensuring that this
302 * relationship holds.
304 * @param newMaximum the new maximum (<code>null</code> permitted).
306 public void setMaximum(Comparable newMaximum
)
308 if (maximum
!= null ?
!maximum
.equals(newMaximum
) : newMaximum
!= null)
310 maximum
= newMaximum
;
316 * Returns the step size.
318 * @return The step size.
320 public Number
getStepSize()
326 * Sets the step size and, if the new step size is different to the old
327 * step size, sends a {@link ChangeEvent} to all registered listeners.
329 * @param newStepSize the new step size (<code>null</code> not permitted).
331 * @throws IllegalArgumentException if <code>newStepSize</code> is
334 public void setStepSize(Number newStepSize
)
336 if (newStepSize
== null)
337 throw new IllegalArgumentException();
339 if (!stepSize
.equals(newStepSize
))
341 stepSize
= newStepSize
;