OSDN Git Service

Imported Classpath 0.18.
[pf3gnuchains/gcc-fork.git] / libjava / classpath / javax / swing / SpinnerNumberModel.java
1 /* SpinnerNumberModel.java --
2    Copyright (C) 2002, 2004 Free Software Foundation, Inc.
3
4 This file is part of GNU Classpath.
5
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.
10
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.
15
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
19 02110-1301 USA.
20
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.
25
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. */
37
38 package javax.swing;
39
40 import java.io.Serializable;
41
42 /**
43  * SpinnerNumberModel
44  *
45  * @author Ka-Hing Cheung
46  * @version 1.0
47  */
48 public class SpinnerNumberModel extends AbstractSpinnerModel
49   implements Serializable
50 {
51   /**
52    * For compatability with Sun's JDK
53    */
54   private static final long serialVersionUID = 7279176385485777821L;
55
56   /** DOCUMENT ME! */
57   private Number value;
58
59   /** DOCUMENT ME! */
60   private Comparable minimum;
61
62   /** DOCUMENT ME! */
63   private Comparable maximum;
64
65   /** DOCUMENT ME! */
66   private Number stepSize;
67
68   /**
69    * Creates a <code>SpinnerNumberModel</code> with initial value 0, step 1,
70    * and no maximum nor minimum.
71    */
72   public SpinnerNumberModel()
73   {
74     this(new Integer(0), null, null, new Integer(1));
75   }
76
77   /**
78    * Creates a <code>SpinnerNumberModel</code> with double precision
79    *
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
86    */
87   public SpinnerNumberModel(double value, double minimum, double maximum,
88                             double stepSize)
89   {
90     this(new Double(value), new Double(minimum), new Double(maximum),
91          new Double(stepSize));
92   }
93
94   /**
95    * Creates a <code>SpinnerNumberModel</code> with integer precision
96    *
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
103    */
104   public SpinnerNumberModel(int value, int minimum, int maximum, int stepSize)
105   {
106     this(new Integer(value), new Integer(minimum), new Integer(maximum),
107          new Integer(stepSize));
108   }
109
110   /**
111    * Creates a <code>SpinnerNumberModel</code> with <code>Number</code>s and
112    * <code>Comparable</code>s.
113    *
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
118    *
119    * @throws IllegalArgumentException if minimum &lt;= value &lt;= maximum
120    *         does not hold
121    */
122   public SpinnerNumberModel(Number value, Comparable minimum,
123                             Comparable maximum, Number stepSize)
124   {
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)
130       {
131         if (minimum.compareTo(value) > 0)
132           throw new IllegalArgumentException("minimum is not <= value");
133       }
134     else
135       minimum = new Comparable()
136           {
137             public int compareTo(Object obj)
138             {
139               return -1;
140             }
141           };
142
143
144     if (maximum != null)
145       {
146         if (maximum.compareTo(value) < 0)
147           throw new IllegalArgumentException("maximum is not >= value");
148       }
149     else
150       maximum = new Comparable()
151           {
152             public int compareTo(Object obj)
153             {
154               return 1;
155             }
156           };
157
158
159     this.value = value;
160     this.stepSize = stepSize;
161     this.minimum = minimum;
162     this.maximum = maximum;
163   }
164
165   /**
166    * Sets the new value and fire a change event
167    *
168    * @param value the new value
169    *
170    * @throws IllegalArgumentException if minimum &lt;= value &lt;= maximum
171    *         does not hold
172    */
173   public void setValue(Object value)
174   {
175     if (! (value instanceof Number))
176       throw new IllegalArgumentException("value must be a Number");
177
178     this.value = (Number) value;
179     fireStateChanged();
180   }
181
182   /**
183    * Gets the current value
184    *
185    * @return the current value
186    */
187   public Object getValue()
188   {
189     return value;
190   }
191
192   /**
193    * Gets the next value without changing the current value, or null if the
194    * current value is maximum.
195    *
196    * @return the next value
197    */
198   public Object getNextValue()
199   {
200     Number num;
201
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()));
214
215     return maximum.compareTo(num) >= 0 ? num : null;
216   }
217
218   /**
219    * Gets the previous value without changing the current value, or null if
220    * the current value is minimum.
221    *
222    * @return the previous value
223    */
224   public Object getPreviousValue()
225   {
226     Number num;
227
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()));
240
241     return minimum.compareTo(num) <= 0 ? num : null;
242   }
243
244   /**
245    * DOCUMENT ME!
246    *
247    * @return DOCUMENT ME!
248    */
249   public Number getNumber()
250   {
251     return value;
252   }
253
254   public Comparable getMinimum()
255   {
256     return minimum;
257   }
258
259   public void setMinimum(Comparable newMinimum)
260   {
261     if (minimum != newMinimum)
262       {
263         minimum = newMinimum;
264         fireStateChanged();
265       }
266   }
267
268   public Comparable getMaximum()
269   {
270     return maximum;
271   }
272
273   public void setMaximum(Comparable newMaximum)
274   {
275     if (maximum != newMaximum)
276       {
277         maximum = newMaximum;
278         fireStateChanged();
279       }
280   }
281
282   public Number getStepSize()
283   {
284     return stepSize;
285   }
286
287   public void setStepSize(Number newStepSize)
288   {
289     if (newStepSize == null)
290       throw new IllegalArgumentException();
291
292     if (stepSize != newStepSize)
293       {
294         stepSize = newStepSize;
295         fireStateChanged();
296       }
297   }
298 }