| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| UifBooleanEditor |
|
| 5.5;5.5 |
| 1 | /* | |
| 2 | * Copyright 2007 The Kuali Foundation | |
| 3 | * | |
| 4 | * Licensed under the Educational Community License, Version 1.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/ecl1.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.kns.web.spring; | |
| 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 | 0 | 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 | 0 | if(this.getValue() == null) { |
| 37 | 0 | return ""; |
| 38 | } | |
| 39 | 0 | else if(((Boolean)this.getValue()).booleanValue()) { |
| 40 | 0 | return TRUE_VALUE; |
| 41 | } | |
| 42 | else { | |
| 43 | 0 | return FALSE_VALUE; |
| 44 | } | |
| 45 | } | |
| 46 | ||
| 47 | @Override | |
| 48 | public void setAsText(String text) throws IllegalArgumentException { | |
| 49 | 0 | String input = null; |
| 50 | ||
| 51 | 0 | if(text != null) { |
| 52 | 0 | StringBuilder builder = new StringBuilder(); |
| 53 | 0 | builder.append("/").append(text.toLowerCase()).append("/"); |
| 54 | 0 | input = builder.toString(); |
| 55 | ||
| 56 | 0 | if(TRUE_VALUES.contains(input)) { |
| 57 | 0 | this.setValue(Boolean.TRUE); |
| 58 | } | |
| 59 | 0 | else if(FALSE_VALUES.contains(input)) { |
| 60 | 0 | this.setValue(Boolean.FALSE); |
| 61 | } | |
| 62 | else { | |
| 63 | 0 | input = null; |
| 64 | } | |
| 65 | } | |
| 66 | ||
| 67 | 0 | if(input == null) { |
| 68 | 0 | throw new IllegalArgumentException("Invalid boolean input: " + text); |
| 69 | } | |
| 70 | 0 | } |
| 71 | ||
| 72 | } |