Merge from mainline (gomp-merge-2005-02-26).
[official-gcc.git] / libjava / javax / swing / SpinnerNumberModel.java
blob370007cb895b7626b0d772bdd569788c24614222
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)
9 any later version.
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
19 02111-1307 USA.
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
24 combination.
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. */
38 package javax.swing;
40 import java.io.Serializable;
42 /**
43 * SpinnerNumberModel
45 * @author Ka-Hing Cheung
46 * @version 1.0
48 public class SpinnerNumberModel extends AbstractSpinnerModel
49 implements Serializable
51 /**
52 * For compatability with Sun's JDK
54 private static final long serialVersionUID = 7279176385485777821L;
56 /** DOCUMENT ME! */
57 private Number value;
59 /** DOCUMENT ME! */
60 private Comparable minimum;
62 /** DOCUMENT ME! */
63 private Comparable maximum;
65 /** DOCUMENT ME! */
66 private Number stepSize;
68 /**
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));
77 /**
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 &lt;= value &lt;= maximum does not
85 * hold
87 public SpinnerNumberModel(double value, double minimum, double maximum,
88 double stepSize)
90 this(new Double(value), new Double(minimum), new Double(maximum),
91 new Double(stepSize));
94 /**
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 &lt;= value &lt;= maximum does not
102 * hold
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 &lt;= value &lt;= maximum
120 * does not hold
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");
127 if (value == null)
128 throw new IllegalArgumentException("value may not be null");
129 if (minimum != null)
131 if (minimum.compareTo(value) > 0)
132 throw new IllegalArgumentException("minimum is not <= value");
134 else
135 minimum = new Comparable()
137 public int compareTo(Object obj)
139 return -1;
144 if (maximum != null)
146 if (maximum.compareTo(value) < 0)
147 throw new IllegalArgumentException("maximum is not >= value");
149 else
150 maximum = new Comparable()
152 public int compareTo(Object obj)
154 return 1;
159 this.value = value;
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 &lt;= value &lt;= maximum
171 * does not hold
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;
179 fireStateChanged();
183 * Gets the current value
185 * @return the current value
187 public Object getValue()
189 return value;
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()
200 Number num;
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()));
212 else
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()
226 Number num;
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()));
238 else
239 num = new Byte((byte) (value.byteValue() - stepSize.byteValue()));
241 return minimum.compareTo(num) <= 0 ? num : null;
245 * DOCUMENT ME!
247 * @return DOCUMENT ME!
249 public Number getNumber()
251 return value;
254 public Comparable getMinimum()
256 return minimum;
259 public void setMinimum(Comparable newMinimum)
261 if (minimum != newMinimum)
263 minimum = newMinimum;
264 fireStateChanged();
268 public Comparable getMaximum()
270 return maximum;
273 public void setMaximum(Comparable newMaximum)
275 if (maximum != newMaximum)
277 maximum = newMaximum;
278 fireStateChanged();
282 public Number getStepSize()
284 return stepSize;
287 public void setStepSize(Number newStepSize)
289 if (newStepSize == null)
290 throw new IllegalArgumentException();
292 if (stepSize != newStepSize)
294 stepSize = newStepSize;
295 fireStateChanged();