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: }
|