Source Code Cross Referenced for CustomBooleanEditor.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-2007 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:
021:        import org.springframework.util.StringUtils;
022:
023:        /**
024:         * Property editor for Boolean/boolean properties.
025:         *
026:         * <p>This is not meant to be used as system PropertyEditor but rather as
027:         * locale-specific Boolean editor within custom controller code, to parse
028:         * UI-caused boolean strings into boolean properties of beans and check
029:         * them in the UI form.
030:         *
031:         * <p>In web MVC code, this editor will typically be registered with
032:         * <code>binder.registerCustomEditor</code> calls in an implementation
033:         * of BaseCommandController's <code>initBinder</code> method.
034:         *
035:         * @author Juergen Hoeller
036:         * @since 10.06.2003
037:         * @see org.springframework.validation.DataBinder#registerCustomEditor
038:         * @see org.springframework.web.servlet.mvc.BaseCommandController#initBinder
039:         */
040:        public class CustomBooleanEditor extends PropertyEditorSupport {
041:
042:            public static final String VALUE_TRUE = "true";
043:            public static final String VALUE_FALSE = "false";
044:
045:            public static final String VALUE_ON = "on";
046:            public static final String VALUE_OFF = "off";
047:
048:            public static final String VALUE_YES = "yes";
049:            public static final String VALUE_NO = "no";
050:
051:            public static final String VALUE_1 = "1";
052:            public static final String VALUE_0 = "0";
053:
054:            private final String trueString;
055:
056:            private final String falseString;
057:
058:            private final boolean allowEmpty;
059:
060:            /**
061:             * Create a new CustomBooleanEditor instance, with "true"/"on"/"yes"
062:             * and "false"/"off"/"no" as recognized String values.
063:             * <p>The "allowEmpty" parameter states if an empty String should
064:             * be allowed for parsing, i.e. get interpreted as null value.
065:             * Else, an IllegalArgumentException gets thrown in that case.
066:             * @param allowEmpty if empty strings should be allowed
067:             */
068:            public CustomBooleanEditor(boolean allowEmpty) {
069:                this (null, null, allowEmpty);
070:            }
071:
072:            /**
073:             * Create a new CustomBooleanEditor instance,
074:             * with configurable String values for true and false.
075:             * <p>The "allowEmpty" parameter states if an empty String should
076:             * be allowed for parsing, i.e. get interpreted as null value.
077:             * Else, an IllegalArgumentException gets thrown in that case.
078:             * @param trueString the String value that represents true:
079:             * for example, "true" (VALUE_TRUE), "on" (VALUE_ON),
080:             * "yes" (VALUE_YES) or some custom value
081:             * @param falseString the String value that represents false:
082:             * for example, "false" (VALUE_FALSE), "off" (VALUE_OFF),
083:             * "no" (VALUE_NO) or some custom value
084:             * @param allowEmpty if empty strings should be allowed
085:             * @see #VALUE_TRUE
086:             * @see #VALUE_FALSE
087:             * @see #VALUE_ON
088:             * @see #VALUE_OFF
089:             * @see #VALUE_YES
090:             * @see #VALUE_NO
091:             */
092:            public CustomBooleanEditor(String trueString, String falseString,
093:                    boolean allowEmpty) {
094:                this .trueString = trueString;
095:                this .falseString = falseString;
096:                this .allowEmpty = allowEmpty;
097:            }
098:
099:            public void setAsText(String text) throws IllegalArgumentException {
100:                String input = (text != null ? text.trim() : null);
101:                if (this .allowEmpty && !StringUtils.hasLength(input)) {
102:                    // Treat empty String as null value.
103:                    setValue(null);
104:                } else if (this .trueString != null
105:                        && input.equalsIgnoreCase(this .trueString)) {
106:                    setValue(Boolean.TRUE);
107:                } else if (this .falseString != null
108:                        && input.equalsIgnoreCase(this .falseString)) {
109:                    setValue(Boolean.FALSE);
110:                } else if (this .trueString == null
111:                        && (input.equalsIgnoreCase(VALUE_TRUE)
112:                                || input.equalsIgnoreCase(VALUE_ON)
113:                                || input.equalsIgnoreCase(VALUE_YES) || input
114:                                .equals(VALUE_1))) {
115:                    setValue(Boolean.TRUE);
116:                } else if (this .falseString == null
117:                        && (input.equalsIgnoreCase(VALUE_FALSE)
118:                                || input.equalsIgnoreCase(VALUE_OFF)
119:                                || input.equalsIgnoreCase(VALUE_NO) || input
120:                                .equals(VALUE_0))) {
121:                    setValue(Boolean.FALSE);
122:                } else {
123:                    throw new IllegalArgumentException(
124:                            "Invalid boolean value [" + text + "]");
125:                }
126:            }
127:
128:            public String getAsText() {
129:                if (Boolean.TRUE.equals(getValue())) {
130:                    return (this .trueString != null ? this .trueString
131:                            : VALUE_TRUE);
132:                } else if (Boolean.FALSE.equals(getValue())) {
133:                    return (this .falseString != null ? this .falseString
134:                            : VALUE_FALSE);
135:                } else {
136:                    return "";
137:                }
138:            }
139:
140:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.