View Javadoc

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