001    /**
002     * Copyright 2005-2014 The Kuali Foundation
003     *
004     * Licensed under the Educational Community 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.opensource.org/licenses/ecl2.php
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    package org.kuali.rice.krad.web.bind;
017    
018    import java.beans.PropertyEditorSupport;
019    import java.io.Serializable;
020    
021    /**
022     * PropertyEditor for booleans supports y/n which the spring version does not
023     *
024     * @author Kuali Rice Team (rice.collab@kuali.org)
025     */
026    public class UifBooleanEditor extends PropertyEditorSupport implements Serializable {
027            private static final long serialVersionUID = -6333792216543862346L;
028    
029            private static final String TRUE_VALUES = "/true/yes/y/on/1/";
030            private static final String FALSE_VALUES = "/false/no/n/off/0/";
031    
032            private static final String TRUE_VALUE = "true";
033            private static final String FALSE_VALUE = "false";
034    
035        /**
036         * Gets the property value as a string suitable for presentation
037         * to a human to edit
038         *
039         * @return The property value as a string suitable for presentation
040         *       to a human to edit.
041         * <p>   Returns String "true" or "false".
042         * <p>   Returns "null" is the value can't be expressed as a string.
043         * <p>   If a non-null value is returned, then the PropertyEditor should
044         *       be prepared to parse that string back in setAsText().
045         */
046        @Override
047            public String getAsText() {
048                    if(this.getValue() == null) {
049                            return "";
050                    }
051                    else if(((Boolean)this.getValue()).booleanValue()) {
052                            return TRUE_VALUE;
053                    }
054                    else {
055                            return FALSE_VALUE;
056                    }
057            }
058    
059        /**
060         * Sets the property value by parsing a given String
061         *
062         * <p>
063         *     The text is compared against the configured acceptable string values for
064         *     boolean true and false
065         * </p>
066         *
067         * @param text  The string to be parsed.
068         * @throws IllegalArgumentException if text does not contain either true or false
069         */
070            @Override
071            public void setAsText(String text) throws IllegalArgumentException {
072                    String input = null;
073    
074                    if(text != null) {
075                            StringBuilder builder = new StringBuilder();
076                            builder.append("/").append(text.toLowerCase()).append("/");
077                            input = builder.toString();
078    
079                            if(TRUE_VALUES.contains(input)) {
080                                    this.setValue(Boolean.TRUE);
081                            }
082                            else if(FALSE_VALUES.contains(input)) {
083                                    this.setValue(Boolean.FALSE);
084                            }
085                            else {
086                                    input = null;
087                            }
088                    }
089    
090                    if(input == null) {
091                            throw new IllegalArgumentException("Invalid boolean input: " + text);
092                    }
093            }
094    
095    }