001 /** 002 * Copyright 2005-2012 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 @Override 036 public String getAsText() { 037 if(this.getValue() == null) { 038 return ""; 039 } 040 else if(((Boolean)this.getValue()).booleanValue()) { 041 return TRUE_VALUE; 042 } 043 else { 044 return FALSE_VALUE; 045 } 046 } 047 048 @Override 049 public void setAsText(String text) throws IllegalArgumentException { 050 String input = null; 051 052 if(text != null) { 053 StringBuilder builder = new StringBuilder(); 054 builder.append("/").append(text.toLowerCase()).append("/"); 055 input = builder.toString(); 056 057 if(TRUE_VALUES.contains(input)) { 058 this.setValue(Boolean.TRUE); 059 } 060 else if(FALSE_VALUES.contains(input)) { 061 this.setValue(Boolean.FALSE); 062 } 063 else { 064 input = null; 065 } 066 } 067 068 if(input == null) { 069 throw new IllegalArgumentException("Invalid boolean input: " + text); 070 } 071 } 072 073 }