View Javadoc

1   /**
2    * Copyright 2005-2011 The Kuali Foundation
3    *
4    * Licensed under the Educational Community 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.opensource.org/licenses/ecl2.php
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  package org.kuali.rice.krad.web.bind;
17  
18  import java.beans.PropertyEditorSupport;
19  
20  /**
21   * This PropertyEditor for booleans supports y/n which the spring version does not. 
22   * 
23   * @author Kuali Rice Team (rice.collab@kuali.org)
24   *
25   */
26  public class UifBooleanEditor extends PropertyEditorSupport {
27  	
28  	private static final String TRUE_VALUES = "/true/yes/y/on/1/";
29  	private static final String FALSE_VALUES = "/false/no/n/off/0/";
30  	
31  	private static final String TRUE_VALUE = "true";
32  	private static final String FALSE_VALUE = "false";
33  
34  	@Override
35  	public String getAsText() {
36  		if(this.getValue() == null) {
37  			return "";
38  		}
39  		else if(((Boolean)this.getValue()).booleanValue()) {
40  			return TRUE_VALUE;
41  		}
42  		else {
43  			return FALSE_VALUE;
44  		}
45  	}
46  
47  	@Override
48  	public void setAsText(String text) throws IllegalArgumentException {
49  		String input = null;
50  		
51  		if(text != null) {
52  			StringBuilder builder = new StringBuilder();
53  			builder.append("/").append(text.toLowerCase()).append("/");
54  			input = builder.toString();
55  			
56  			if(TRUE_VALUES.contains(input)) {
57  				this.setValue(Boolean.TRUE);
58  			}
59  			else if(FALSE_VALUES.contains(input)) {
60  				this.setValue(Boolean.FALSE);
61  			}
62  			else {
63  				input = null;
64  			}
65  		}
66  
67  		if(input == null) {
68  			throw new IllegalArgumentException("Invalid boolean input: " + text);
69  		}
70  	}
71  
72  }