Source Code Cross Referenced for CustomNumberEditor.java in  » J2EE » spring-framework-2.5 » org » springframework » beans » propertyeditors » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » J2EE » spring framework 2.5 » org.springframework.beans.propertyeditors 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright 2002-2006 the original author or authors.
003:         *
004:         * Licensed under the Apache License, Version 2.0 (the "License");
005:         * you may not use this file except in compliance with the License.
006:         * You may obtain a copy of the License at
007:         *
008:         *      http://www.apache.org/licenses/LICENSE-2.0
009:         *
010:         * Unless required by applicable law or agreed to in writing, software
011:         * distributed under the License is distributed on an "AS IS" BASIS,
012:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013:         * See the License for the specific language governing permissions and
014:         * limitations under the License.
015:         */
016:
017:        package org.springframework.beans.propertyeditors;
018:
019:        import java.beans.PropertyEditorSupport;
020:        import java.text.NumberFormat;
021:
022:        import org.springframework.util.NumberUtils;
023:        import org.springframework.util.StringUtils;
024:
025:        /**
026:         * Property editor for any Number subclass like Integer, Long, Float, Double.
027:         * Can use a given NumberFormat for (locale-specific) parsing and rendering,
028:         * or alternatively the default <code>valueOf</code> respectively
029:         * <code>toString</code> methods.
030:         *
031:         * <p>This is not meant to be used as system PropertyEditor but rather as
032:         * locale-specific number editor within custom controller code, to parse
033:         * user-entered number strings into Number properties of beans, and render
034:         * them in the UI form.
035:         *
036:         * <p>In web MVC code, this editor will typically be registered with
037:         * <code>binder.registerCustomEditor</code> calls in an implementation
038:         * of BaseCommandController's <code>initBinder</code> method.
039:         *
040:         * @author Juergen Hoeller
041:         * @since 06.06.2003
042:         * @see java.lang.Number
043:         * @see java.text.NumberFormat
044:         * @see org.springframework.validation.DataBinder#registerCustomEditor
045:         * @see org.springframework.web.servlet.mvc.BaseCommandController#initBinder
046:         */
047:        public class CustomNumberEditor extends PropertyEditorSupport {
048:
049:            private final Class numberClass;
050:
051:            private final NumberFormat numberFormat;
052:
053:            private final boolean allowEmpty;
054:
055:            /**
056:             * Create a new CustomNumberEditor instance, using the default
057:             * <code>valueOf</code> methods for parsing and <code>toString</code>
058:             * methods for rendering.
059:             * <p>The "allowEmpty" parameter states if an empty String should
060:             * be allowed for parsing, i.e. get interpreted as <code>null</code>  value.
061:             * Else, an IllegalArgumentException gets thrown in that case.
062:             * @param numberClass Number subclass to generate
063:             * @param allowEmpty if empty strings should be allowed
064:             * @throws IllegalArgumentException if an invalid numberClass has been specified
065:             * @see org.springframework.util.NumberUtils#parseNumber(String, Class)
066:             * @see Integer#valueOf
067:             * @see Integer#toString
068:             */
069:            public CustomNumberEditor(Class numberClass, boolean allowEmpty)
070:                    throws IllegalArgumentException {
071:                this (numberClass, null, allowEmpty);
072:            }
073:
074:            /**
075:             * Create a new CustomNumberEditor instance, using the given NumberFormat
076:             * for parsing and rendering.
077:             * <p>The allowEmpty parameter states if an empty String should
078:             * be allowed for parsing, i.e. get interpreted as <code>null</code>  value.
079:             * Else, an IllegalArgumentException gets thrown in that case.
080:             * @param numberClass Number subclass to generate
081:             * @param numberFormat NumberFormat to use for parsing and rendering
082:             * @param allowEmpty if empty strings should be allowed
083:             * @throws IllegalArgumentException if an invalid numberClass has been specified
084:             * @see org.springframework.util.NumberUtils#parseNumber(String, Class, java.text.NumberFormat)
085:             * @see java.text.NumberFormat#parse
086:             * @see java.text.NumberFormat#format
087:             */
088:            public CustomNumberEditor(Class numberClass,
089:                    NumberFormat numberFormat, boolean allowEmpty)
090:                    throws IllegalArgumentException {
091:
092:                if (numberClass == null
093:                        || !Number.class.isAssignableFrom(numberClass)) {
094:                    throw new IllegalArgumentException(
095:                            "Property class must be a subclass of Number");
096:                }
097:                this .numberClass = numberClass;
098:                this .numberFormat = numberFormat;
099:                this .allowEmpty = allowEmpty;
100:            }
101:
102:            /**
103:             * Parse the Number from the given text, using the specified NumberFormat.
104:             */
105:            public void setAsText(String text) throws IllegalArgumentException {
106:                if (this .allowEmpty && !StringUtils.hasText(text)) {
107:                    // Treat empty String as null value.
108:                    setValue(null);
109:                } else if (this .numberFormat != null) {
110:                    // Use given NumberFormat for parsing text.
111:                    setValue(NumberUtils.parseNumber(text, this .numberClass,
112:                            this .numberFormat));
113:                } else {
114:                    // Use default valueOf methods for parsing text.
115:                    setValue(NumberUtils.parseNumber(text, this .numberClass));
116:                }
117:            }
118:
119:            /**
120:             * Coerce a Number value into the required target class, if necessary.
121:             */
122:            public void setValue(Object value) {
123:                if (value instanceof  Number) {
124:                    super .setValue(NumberUtils.convertNumberToTargetClass(
125:                            (Number) value, this .numberClass));
126:                } else {
127:                    super .setValue(value);
128:                }
129:            }
130:
131:            /**
132:             * Format the Number as String, using the specified NumberFormat.
133:             */
134:            public String getAsText() {
135:                Object value = getValue();
136:                if (value == null) {
137:                    return "";
138:                }
139:                if (this .numberFormat != null) {
140:                    // Use NumberFormat for rendering value.
141:                    return this .numberFormat.format(value);
142:                } else {
143:                    // Use toString method for rendering value.
144:                    return value.toString();
145:                }
146:            }
147:
148:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.