1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krad.web.bind;
17
18 import java.beans.PropertyEditorSupport;
19 import java.io.Serializable;
20
21
22
23
24
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 }