KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jdesktop > swing > data > NumberMetaData


1 /*
2  * $Id: NumberMetaData.java,v 1.2 2004/09/08 23:36:49 aim Exp $
3  *
4  * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
5  * Santa Clara, California 95054, U.S.A. All rights reserved.
6  */

7
8 package org.jdesktop.swing.data;
9
10 import java.util.Locale JavaDoc;
11
12 import javax.swing.UIDefaults JavaDoc;
13
14 /**
15  * <p>
16  * Class for representing meta-data for a numerical data field which is
17  * one of the following types:
18  * <ul>
19  * <li>java.lang.Integer</li>
20  * <li>java.lang.Long</li>
21  * <li>java.lang.Short</li>
22  * <li>java.lang.Float</li>
23  * <li>java.lang.Double</li>
24  * </ul>
25  * This meta-data class defines additional properties and edit constraints
26  * which are applicable to numerical values, such as minimum, maximum,
27  * whether or not the value is a currency, etc. Example usage:
28  * <pre><code>
29  * NumberMetaData metaData = new NumberMetaData(&quot;interestrate&quot;,
30  * Float.class, &quot;Interest Rate&quot;);
31  * metaData.setMinimum(new Float(4.5));
32  * metaData.setMaximum(new Float(6.8));
33  * </code></pre>
34  * Setting a minimum and/or maximum constraint will implicitly cause a range
35  * validator to be added to the meta-data object.
36  * </p>
37  *
38  * @author Amy Fowler
39  * @version 1.0
40  */

41
42 public class NumberMetaData extends MetaData {
43
44     protected Number JavaDoc minimum = null;
45     protected Number JavaDoc maximum = null;
46     protected boolean currency = false;
47
48     private Validator rangeValidator = null;
49
50     /**
51       * Instantiates a meta-data object with a default name &quot;numbervalue&quot; and
52       * a default field class equal to <code>java.lang.Integer</code>.
53       * This provides the no-argument constructor required for JavaBeans.
54       * It is recommended that the program explicitly set a meaningful
55       * &quot;name&quot; property.
56       */

57      public NumberMetaData() {
58          this("numbervalue");
59      }
60
61     /**
62      * Instantiates a meta-data object with the specified name and
63      * a default field class equal to <code>java.lang.Integer</code>.
64      * @param name String containing the name of the data field
65      */

66     public NumberMetaData(String JavaDoc name) {
67         super(name);
68         this.klass = Integer JavaDoc.class;
69     }
70
71     /**
72      * Instantiates a meta-data object with the specified name and
73      * field class.
74      * @param name String containing the name of the data field
75      * @param klass Class indicating type of data field
76      */

77     public NumberMetaData(String JavaDoc name, Class JavaDoc klass) {
78         this(name);
79         this.klass = klass;
80     }
81
82     /**
83      * Instantiates a meta-data object with the specified name,
84      * field class, and label.
85      * @param name String containing the name of the data field
86      * @param klass Class indicating type of data field
87      * @param label String containing the user-displayable label for the
88      * data field
89      */

90     public NumberMetaData(String JavaDoc name, Class JavaDoc klass, String JavaDoc label) {
91         this(name, klass);
92         this.label = label;
93     }
94
95     /**
96      * Gets the meta-data &quot;minimum&quot; property which indicates
97      * the minimum value of the associated data field.
98      * The default is <code>null</code>, which indicates no minimum.
99      * @see #setMinimum
100      * @return Number containing the minimum value of the data field.
101      */

102     public Number JavaDoc getMinimum() {
103         return minimum;
104     }
105
106     /**
107      * Sets the meta-data &quot;minimum&quot; property.
108      * Setting a minimum and/or maximum will cause an appropriate
109      * range validator object to be added to this meta-data object.
110      * @see #getMinimum
111      * @param minimum Number containing the minimum value of the data field.
112      * @throws IllegalArgumentException if the minimum object's class does
113      * not equal the meta-data's field class
114      */

115     public void setMinimum(Number JavaDoc minimum) {
116         if (klass != minimum.getClass()) {
117             throw new IllegalArgumentException JavaDoc(getName() + ": minimum value is class "+
118                                                minimum.getClass().getName() +
119                                                " but should be "+ getClass().getName());
120         }
121         Number JavaDoc oldMinimum = this.minimum;
122         this.minimum = minimum;
123         setupRangeValidator();
124         firePropertyChange("minimum", oldMinimum, minimum);
125     }
126
127     /**
128      * Gets the meta-data &quot;maximum&quot; property which indicates
129      * the maximum value of the associated data field.
130      * The default is <code>null</code>, which indicates no maximum.
131      * @see #setMaximum
132      * @return Number containing the maximum value of the data field.
133      */

134     public Number JavaDoc getMaximum() {
135         return maximum;
136     }
137
138     /**
139      * Sets the meta-data &quot;maximum&quot; property.
140      * Setting a minimum and/or maximum will cause an appropriate
141      * range validator object to be added to this meta-data object.
142      * @see #getMaximum
143      * @param maximum Number containing the maximum value of the data field.
144      * @throws IllegalArgumentException if the maximum object's class does
145      * not equal the meta-data's field class
146      */

147     public void setMaximum(Number JavaDoc maximum) {
148         if (getElementClass() != maximum.getClass()) {
149             throw new IllegalArgumentException JavaDoc(getName() +
150                                                ": maximum value is class " +
151                                                maximum.getClass().getName() +
152                                                " but should be " +
153                                                getClass().getName());
154         }
155         Number JavaDoc oldMaximum = this.maximum;
156         this.maximum = maximum;
157         setupRangeValidator();
158         firePropertyChange("maximum", oldMaximum, maximum);
159     }
160
161     /**
162      * Gets the meta-data &quot;currency&quot; property which indicates
163      * whether this data field represents a currency value.
164      * The default is <code>false</code>.
165      * @see #setCurrency
166      * @return boolean indicating whether the data field represents a currency
167      */

168     public boolean isCurrency() {
169         return currency;
170     }
171
172     /**
173      * Sets the meta-data &quot;currency&quot; property.
174      * @see #isCurrency
175      * @param currency boolean indicating whether the data field represents a currency
176      */

177     public void setCurrency(boolean currency) {
178         boolean oldCurrency = this.currency;
179         this.currency = currency;
180         firePropertyChange("currency", oldCurrency, currency);
181     }
182
183     private void setupRangeValidator() {
184         if (maximum != null || minimum != null) {
185             if (rangeValidator == null) {
186                 rangeValidator = getRangeValidator();
187                 addValidator(rangeValidator);
188             }
189         } else if (rangeValidator != null) {
190             removeValidator(rangeValidator);
191             rangeValidator = null;
192         }
193     }
194
195     private Validator getRangeValidator() {
196         if (klass.equals(Integer JavaDoc.class)) {
197             return new IntegerRangeValidator();
198         } else if (klass.equals(Long JavaDoc.class)) {
199             return new LongRangeValidator();
200         } else if (klass.equals(Float JavaDoc.class)) {
201             return new FloatRangeValidator();
202         } else if (klass.equals(Short JavaDoc.class)) {
203             return new ShortRangeValidator();
204         } else if (klass.equals(Double JavaDoc.class)) {
205             return new DoubleRangeValidator();
206         }
207         return null;
208     }
209
210     /**@todo aim: these could be exposed as public static Validator classes,
211      * but they would then have to become stateful.
212      */

213     private class IntegerRangeValidator implements Validator {
214
215         public boolean validate(Object JavaDoc value, Locale JavaDoc locale, String JavaDoc[] error) {
216
217             int intValue = ((Integer JavaDoc)value).intValue();
218             if (maximum != null &&
219                 intValue > ((Integer JavaDoc)maximum).intValue()) {
220                 error[0] = getName()+ ": value " + intValue + " exceeds maximum "+
221                         ((Integer JavaDoc)maximum).intValue();
222                 return false;
223             }
224             if (minimum != null &&
225                 intValue < ((Integer JavaDoc)minimum).intValue()) {
226                 error[0] = getName() + ": value " + intValue + " is less than the minimum" +
227                         ((Integer JavaDoc)minimum).intValue();
228                 return false;
229             }
230             return true;
231         }
232     }
233
234     private class LongRangeValidator implements Validator {
235
236         public boolean validate(Object JavaDoc value, Locale JavaDoc locale, String JavaDoc[] error) {
237
238             long longValue = ((Long JavaDoc)value).longValue();
239             if (maximum != null &&
240                 longValue > ((Long JavaDoc)maximum).longValue()) {
241                 error[0] = getName()+ ": value " + longValue + " exceeds maximum "+
242                         ((Long JavaDoc)maximum).longValue();
243                 return false;
244             }
245             if (minimum != null &&
246                 longValue < ((Long JavaDoc)minimum).longValue()) {
247                 error[0] = getName() + ": value " + longValue + " is less than the minimum" +
248                         ((Long JavaDoc)minimum).longValue();
249                 return false;
250             }
251             return true;
252         }
253     }
254
255     private class ShortRangeValidator implements Validator {
256
257         public boolean validate(Object JavaDoc value, Locale JavaDoc locale, String JavaDoc[] error) {
258
259             short shortValue = ((Short JavaDoc)value).shortValue();
260             if (maximum != null &&
261                 shortValue > ((Short JavaDoc)maximum).shortValue()) {
262                 error[0] = getName()+ ": value " + shortValue + " exceeds maximum "+
263                         ((Short JavaDoc)maximum).shortValue();
264                 return false;
265             }
266             if (minimum != null &&
267                 shortValue < ((Short JavaDoc)minimum).shortValue()) {
268                 error[0] = getName() + ": value " + shortValue + " is less than the minimum" +
269                         ((Short JavaDoc)minimum).shortValue();
270                 return false;
271             }
272             return true;
273         }
274     }
275
276     private class FloatRangeValidator implements Validator {
277
278         public boolean validate(Object JavaDoc value, Locale JavaDoc locale, String JavaDoc[] error) {
279
280             float floatValue = ((Float JavaDoc)value).floatValue();
281             if (maximum != null &&
282                 floatValue > ((Float JavaDoc)maximum).floatValue()) {
283                 error[0] = getName()+ ": value " + floatValue + " exceeds maximum "+
284                         ((Float JavaDoc)maximum).floatValue();
285                 return false;
286             }
287             if (minimum != null &&
288                 floatValue < ((Float JavaDoc)minimum).floatValue()) {
289                 error[0] = getName() + ": value " + floatValue + " is less than the minimum" +
290                         ((Float JavaDoc)minimum).floatValue();
291                 return false;
292             }
293             return true;
294         }
295     }
296
297     private class DoubleRangeValidator implements Validator {
298
299         public boolean validate(Object JavaDoc value, Locale JavaDoc locale, String JavaDoc[] error) {
300
301             double doubleValue = ((Double JavaDoc)value).doubleValue();
302             if (maximum != null &&
303                 doubleValue > ((Double JavaDoc)maximum).doubleValue()) {
304                 error[0] = getName()+ ": value " + doubleValue + " exceeds maximum "+
305                         ((Double JavaDoc)maximum).doubleValue();
306                 return false;
307             }
308             if (minimum != null &&
309                 doubleValue < ((Double JavaDoc)minimum).doubleValue()) {
310                 error[0] = getName() + ": value " + doubleValue + " is less than the minimum" +
311                         ((Double JavaDoc)minimum).doubleValue();
312                 return false;
313             }
314             return true;
315         }
316     }
317 }
318
Popular Tags