KickJava   Java API By Example, From Geeks To Geeks.

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


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
21 import org.springframework.util.StringUtils;
22
23 /**
24  * Property editor for Boolean/boolean properties.
25  *
26  * <p>This is not meant to be used as system PropertyEditor but rather as
27  * locale-specific Boolean editor within custom controller code, to parse
28  * UI-caused boolean strings into boolean properties of beans and check
29  * them in the UI form.
30  *
31  * <p>In web MVC code, this editor will typically be registered with
32  * <code>binder.registerCustomEditor</code> calls in an implementation
33  * of BaseCommandController's <code>initBinder</code> method.
34  *
35  * @author Juergen Hoeller
36  * @since 10.06.2003
37  * @see org.springframework.validation.DataBinder#registerCustomEditor
38  * @see org.springframework.web.servlet.mvc.BaseCommandController#initBinder
39  */

40 public class CustomBooleanEditor extends PropertyEditorSupport JavaDoc {
41
42     public static final String JavaDoc VALUE_TRUE = "true";
43     public static final String JavaDoc VALUE_FALSE = "false";
44
45     public static final String JavaDoc VALUE_ON = "on";
46     public static final String JavaDoc VALUE_OFF = "off";
47
48     public static final String JavaDoc VALUE_YES = "yes";
49     public static final String JavaDoc VALUE_NO = "no";
50
51     public static final String JavaDoc VALUE_1 = "1";
52     public static final String JavaDoc VALUE_0 = "0";
53
54
55     private final String JavaDoc trueString;
56
57     private final String JavaDoc falseString;
58
59     private final boolean allowEmpty;
60
61
62     /**
63      * Create a new CustomBooleanEditor instance, with "true"/"on"/"yes"
64      * and "false"/"off"/"no" as recognized String values.
65      * <p>The "allowEmpty" parameter states if an empty String should
66      * be allowed for parsing, i.e. get interpreted as null value.
67      * Else, an IllegalArgumentException gets thrown in that case.
68      * @param allowEmpty if empty strings should be allowed
69      */

70     public CustomBooleanEditor(boolean allowEmpty) {
71         this(null, null, allowEmpty);
72     }
73
74     /**
75      * Create a new CustomBooleanEditor instance,
76      * with configurable String values for true and false.
77      * <p>The "allowEmpty" parameter states if an empty String should
78      * be allowed for parsing, i.e. get interpreted as null value.
79      * Else, an IllegalArgumentException gets thrown in that case.
80      * @param trueString the String value that represents true:
81      * for example, "true" (VALUE_TRUE), "on" (VALUE_ON),
82      * "yes" (VALUE_YES) or some custom value
83      * @param falseString the String value that represents false:
84      * for example, "false" (VALUE_FALSE), "off" (VALUE_OFF),
85      * "no" (VALUE_NO) or some custom value
86      * @param allowEmpty if empty strings should be allowed
87      * @see #VALUE_TRUE
88      * @see #VALUE_FALSE
89      * @see #VALUE_ON
90      * @see #VALUE_OFF
91      * @see #VALUE_YES
92      * @see #VALUE_NO
93      */

94     public CustomBooleanEditor(String JavaDoc trueString, String JavaDoc falseString, boolean allowEmpty) {
95         this.trueString = trueString;
96         this.falseString = falseString;
97         this.allowEmpty = allowEmpty;
98     }
99
100     public void setAsText(String JavaDoc text) throws IllegalArgumentException JavaDoc {
101         if (this.allowEmpty && !StringUtils.hasText(text)) {
102             // Treat empty String as null value.
103
setValue(null);
104         }
105         else if (this.trueString != null && text.equalsIgnoreCase(this.trueString)) {
106             setValue(Boolean.TRUE);
107         }
108         else if (this.falseString != null && text.equalsIgnoreCase(this.falseString)) {
109             setValue(Boolean.FALSE);
110         }
111         else if (this.trueString == null &&
112                 (text.equalsIgnoreCase(VALUE_TRUE) || text.equalsIgnoreCase(VALUE_ON) ||
113                 text.equalsIgnoreCase(VALUE_YES) || text.equals(VALUE_1))) {
114             setValue(Boolean.TRUE);
115         }
116         else if (this.falseString == null &&
117                 (text.equalsIgnoreCase(VALUE_FALSE) || text.equalsIgnoreCase(VALUE_OFF) ||
118                 text.equalsIgnoreCase(VALUE_NO) || text.equals(VALUE_0))) {
119             setValue(Boolean.FALSE);
120         }
121         else {
122             throw new IllegalArgumentException JavaDoc("Invalid boolean value [" + text + "]");
123         }
124     }
125
126     public String JavaDoc getAsText() {
127         if (Boolean.TRUE.equals(getValue())) {
128             return (this.trueString != null ? this.trueString : VALUE_TRUE);
129         }
130         else if (Boolean.FALSE.equals(getValue())) {
131             return (this.falseString != null ? this.falseString : VALUE_FALSE);
132         }
133         else {
134             return "";
135         }
136     }
137
138 }
139
Popular Tags