1 /* SpinnerNumberModel.java --
2 Copyright (C) 2002, 2004 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., 59 Temple Place, Suite 330, 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
;
45 * @author Ka-Hing Cheung
48 public class SpinnerNumberModel
extends AbstractSpinnerModel
49 implements Serializable
52 * For compatability with Sun's JDK
54 private static final long serialVersionUID
= 7279176385485777821L;
60 private Comparable minimum
;
63 private Comparable maximum
;
66 private Number stepSize
;
69 * Creates a <code>SpinnerNumberModel</code> with initial value 0, step 1,
70 * and no maximum nor minimum.
72 public SpinnerNumberModel()
74 this(new Integer(0), null, null, new Integer(1));
78 * Creates a <code>SpinnerNumberModel</code> with double precision
80 * @param value the initial value
81 * @param minimum the minimum value
82 * @param maximum the maximum value
83 * @param stepSize the step size
84 * @throws IllegalArgumentException if minimum <= value <= maximum does not
87 public SpinnerNumberModel(double value
, double minimum
, double maximum
,
90 this(new Double(value
), new Double(minimum
), new Double(maximum
),
91 new Double(stepSize
));
95 * Creates a <code>SpinnerNumberModel</code> with integer precision
97 * @param value the initial value
98 * @param minimum the minimum value
99 * @param maximum the maximum value
100 * @param stepSize the step size
101 * @throws IllegalArgumentException if minimum <= value <= maximum does not
104 public SpinnerNumberModel(int value
, int minimum
, int maximum
, int stepSize
)
106 this(new Integer(value
), new Integer(minimum
), new Integer(maximum
),
107 new Integer(stepSize
));
111 * Creates a <code>SpinnerNumberModel</code> with <code>Number</code>s and
112 * <code>Comparable</code>s.
114 * @param value the initial value
115 * @param minimum the minimum value, if null there's no minimum
116 * @param maximum the maximum value, if null there's no maximum
117 * @param stepSize the step size
119 * @throws IllegalArgumentException if minimum <= value <= maximum
122 public SpinnerNumberModel(Number value
, Comparable minimum
,
123 Comparable maximum
, Number stepSize
)
125 if (stepSize
== null)
126 throw new IllegalArgumentException("stepSize may not be null");
128 throw new IllegalArgumentException("value may not be null");
131 if (minimum
.compareTo(value
) > 0)
132 throw new IllegalArgumentException("minimum is not <= value");
135 minimum
= new Comparable()
137 public int compareTo(Object obj
)
146 if (maximum
.compareTo(value
) < 0)
147 throw new IllegalArgumentException("maximum is not >= value");
150 maximum
= new Comparable()
152 public int compareTo(Object obj
)
160 this.stepSize
= stepSize
;
161 this.minimum
= minimum
;
162 this.maximum
= maximum
;
166 * Sets the new value and fire a change event
168 * @param value the new value
170 * @throws IllegalArgumentException if minimum <= value <= maximum
173 public void setValue(Object value
)
175 if (! (value
instanceof Number
))
176 throw new IllegalArgumentException("value must be a Number");
178 this.value
= (Number
) value
;
183 * Gets the current value
185 * @return the current value
187 public Object
getValue()
193 * Gets the next value without changing the current value, or null if the
194 * current value is maximum.
196 * @return the next value
198 public Object
getNextValue()
202 if (value
instanceof Double
)
203 num
= new Double(value
.doubleValue() + stepSize
.doubleValue());
204 else if (value
instanceof Float
)
205 num
= new Double(value
.floatValue() + stepSize
.floatValue());
206 else if (value
instanceof Long
)
207 num
= new Long(value
.longValue() + stepSize
.longValue());
208 else if (value
instanceof Integer
)
209 num
= new Integer(value
.intValue() + stepSize
.intValue());
210 else if (value
instanceof Short
)
211 num
= new Short((short) (value
.shortValue() + stepSize
.shortValue()));
213 num
= new Byte((byte) (value
.byteValue() + stepSize
.byteValue()));
215 return maximum
.compareTo(num
) >= 0 ? num
: null;
219 * Gets the previous value without changing the current value, or null if
220 * the current value is minimum.
222 * @return the previous value
224 public Object
getPreviousValue()
228 if (value
instanceof Double
)
229 num
= new Double(value
.doubleValue() - stepSize
.doubleValue());
230 else if (value
instanceof Float
)
231 num
= new Double(value
.floatValue() - stepSize
.floatValue());
232 else if (value
instanceof Long
)
233 num
= new Long(value
.longValue() - stepSize
.longValue());
234 else if (value
instanceof Integer
)
235 num
= new Integer(value
.intValue() - stepSize
.intValue());
236 else if (value
instanceof Short
)
237 num
= new Short((short) (value
.shortValue() - stepSize
.shortValue()));
239 num
= new Byte((byte) (value
.byteValue() - stepSize
.byteValue()));
241 return minimum
.compareTo(num
) <= 0 ? num
: null;
247 * @return DOCUMENT ME!
249 public Number
getNumber()
254 public Comparable
getMinimum()
259 public void setMinimum(Comparable newMinimum
)
261 if (minimum
!= newMinimum
)
263 minimum
= newMinimum
;
268 public Comparable
getMaximum()
273 public void setMaximum(Comparable newMaximum
)
275 if (maximum
!= newMaximum
)
277 maximum
= newMaximum
;
282 public Number
getStepSize()
287 public void setStepSize(Number newStepSize
)
289 if (newStepSize
== null)
290 throw new IllegalArgumentException();
292 if (stepSize
!= newStepSize
)
294 stepSize
= newStepSize
;