KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > beans > propertyeditors > CustomNumberEditor


1 /*
2  * Copyright 2002-2006 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.beans.propertyeditors;
18
19 import java.beans.PropertyEditorSupport JavaDoc;
20 import java.text.NumberFormat JavaDoc;
21
22 import org.springframework.util.NumberUtils;
23 import org.springframework.util.StringUtils;
24
25 /**
26  * Property editor for any Number subclass like Integer, Long, Float, Double.
27  * Can use a given NumberFormat for (locale-specific) parsing and rendering,
28  * or alternatively the default <code>valueOf</code> respectively
29  * <code>toString</code> methods.
30  *
31  * <p>This is not meant to be used as system PropertyEditor but rather as
32  * locale-specific number editor within custom controller code, to parse
33  * user-entered number strings into Number properties of beans, and render
34  * them in the UI form.
35  *
36  * <p>In web MVC code, this editor will typically be registered with
37  * <code>binder.registerCustomEditor</code> calls in an implementation
38  * of BaseCommandController's <code>initBinder</code> method.
39  *
40  * @author Juergen Hoeller
41  * @since 06.06.2003
42  * @see java.lang.Number
43  * @see java.text.NumberFormat
44  * @see org.springframework.validation.DataBinder#registerCustomEditor
45  * @see org.springframework.web.servlet.mvc.BaseCommandController#initBinder
46  */

47 public class CustomNumberEditor extends PropertyEditorSupport JavaDoc {
48
49     private final Class JavaDoc numberClass;
50
51     private final NumberFormat JavaDoc numberFormat;
52
53     private final boolean allowEmpty;
54
55
56     /**
57      * Create a new CustomNumberEditor instance, using the default
58      * <code>valueOf</code> methods for parsing and <code>toString</code>
59      * methods for rendering.
60      * <p>The "allowEmpty" parameter states if an empty String should
61      * be allowed for parsing, i.e. get interpreted as <code>null</code> value.
62      * Else, an IllegalArgumentException gets thrown in that case.
63      * @param numberClass Number subclass to generate
64      * @param allowEmpty if empty strings should be allowed
65      * @throws IllegalArgumentException if an invalid numberClass has been specified
66      * @see org.springframework.util.NumberUtils#parseNumber(String, Class)
67      * @see Integer#valueOf
68      * @see Integer#toString
69      */

70     public CustomNumberEditor(Class JavaDoc numberClass, boolean allowEmpty) throws IllegalArgumentException JavaDoc {
71         this(numberClass, null, allowEmpty);
72     }
73
74     /**
75      * Create a new CustomNumberEditor instance, using the given NumberFormat
76      * for parsing and rendering.
77      * <p>The allowEmpty parameter states if an empty String should
78      * be allowed for parsing, i.e. get interpreted as <code>null</code> value.
79      * Else, an IllegalArgumentException gets thrown in that case.
80      * @param numberClass Number subclass to generate
81      * @param numberFormat NumberFormat to use for parsing and rendering
82      * @param allowEmpty if empty strings should be allowed
83      * @throws IllegalArgumentException if an invalid numberClass has been specified
84      * @see org.springframework.util.NumberUtils#parseNumber(String, Class, java.text.NumberFormat)
85      * @see java.text.NumberFormat#parse
86      * @see java.text.NumberFormat#format
87      */

88     public CustomNumberEditor(Class JavaDoc numberClass, NumberFormat JavaDoc numberFormat, boolean allowEmpty)
89         throws IllegalArgumentException JavaDoc {
90
91         if (numberClass == null || !Number JavaDoc.class.isAssignableFrom(numberClass)) {
92             throw new IllegalArgumentException JavaDoc("Property class must be a subclass of Number");
93         }
94         this.numberClass = numberClass;
95         this.numberFormat = numberFormat;
96         this.allowEmpty = allowEmpty;
97     }
98
99
100     /**
101      * Parse the Number from the given text, using the specified NumberFormat.
102      */

103     public void setAsText(String JavaDoc text) throws IllegalArgumentException JavaDoc {
104         if (this.allowEmpty && !StringUtils.hasText(text)) {
105             // Treat empty String as null value.
106
setValue(null);
107         }
108         else if (this.numberFormat != null) {
109             // Use given NumberFormat for parsing text.
110
setValue(NumberUtils.parseNumber(text, this.numberClass, this.numberFormat));
111         }
112         else {
113             // Use default valueOf methods for parsing text.
114
setValue(NumberUtils.parseNumber(text, this.numberClass));
115         }
116     }
117
118     /**
119      * Coerce a Number value into the required target class, if necessary.
120      */

121     public void setValue(Object JavaDoc value) {
122         if (value instanceof Number JavaDoc) {
123             super.setValue(NumberUtils.convertNumberToTargetClass((Number JavaDoc) value, this.numberClass));
124         }
125         else {
126             super.setValue(value);
127         }
128     }
129
130     /**
131      * Format the Number as String, using the specified NumberFormat.
132      */

133     public String JavaDoc getAsText() {
134         Object JavaDoc value = getValue();
135         if (value == null) {
136             return "";
137         }
138         if (this.numberFormat != null) {
139             // Use NumberFormat for rendering value.
140
return this.numberFormat.format(value);
141         }
142         else {
143             // Use toString method for rendering value.
144
return value.toString();
145         }
146     }
147
148 }
149
Popular Tags