Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
UifKnsFormatterPropertyEditor |
|
| 4.0;4 |
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 org.kuali.rice.core.web.format.FormatException; | |
19 | import org.kuali.rice.core.web.format.Formatter; | |
20 | ||
21 | import java.beans.PropertyEditorSupport; | |
22 | ||
23 | ||
24 | /** | |
25 | * Used to convert KNS style Formatter classes to Property Editors that can be used by Spring | |
26 | * | |
27 | * @author Kuali Rice Team (rice.collab@kuali.org) | |
28 | */ | |
29 | public class UifKnsFormatterPropertyEditor extends PropertyEditorSupport { | |
30 | 0 | private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(UifKnsFormatterPropertyEditor.class); |
31 | ||
32 | Formatter formatter; | |
33 | ||
34 | /** | |
35 | * This constructs a ... | |
36 | * | |
37 | * @param formatter | |
38 | */ | |
39 | public UifKnsFormatterPropertyEditor(Class<? extends Formatter> formatterClass) { | |
40 | 0 | super(); |
41 | try { | |
42 | 0 | formatter = formatterClass.newInstance(); |
43 | } | |
44 | 0 | catch (InstantiationException e) { |
45 | 0 | throw new FormatException("Couldn't create an instance of class " + formatterClass, e); |
46 | } | |
47 | 0 | catch (IllegalAccessException e) { |
48 | 0 | throw new FormatException("Couldn't create an instance of class " + formatterClass, e); |
49 | 0 | } |
50 | ||
51 | // if (settings != null) | |
52 | // formatter.setSettings(Collections.unmodifiableMap(settings)); | |
53 | 0 | formatter.setPropertyType(formatterClass); |
54 | 0 | } |
55 | ||
56 | @Override | |
57 | public String getAsText() { | |
58 | try { | |
59 | 0 | return (String) formatter.formatForPresentation(getValue()); |
60 | 0 | } catch (FormatException e) { |
61 | 0 | LOG.error("FormatException: " + e.getLocalizedMessage(), e); |
62 | 0 | throw e; |
63 | } | |
64 | } | |
65 | ||
66 | @Override | |
67 | public void setAsText(String text) throws IllegalArgumentException { | |
68 | try { | |
69 | 0 | this.setValue(formatter.convertFromPresentationFormat(text)); |
70 | 0 | } catch (FormatException e) { |
71 | 0 | LOG.error("FormatException: " + e.getLocalizedMessage(), e); |
72 | 0 | throw e; |
73 | 0 | } |
74 | // String input = null; | |
75 | // | |
76 | // if(text != null) { | |
77 | // StringBuilder builder = new StringBuilder(); | |
78 | // builder.append("/").append(text.toLowerCase()).append("/"); | |
79 | // input = builder.toString(); | |
80 | // | |
81 | // if(TRUE_VALUES.contains(input)) { | |
82 | // this.setValue(Boolean.TRUE); | |
83 | // } | |
84 | // else if(FALSE_VALUES.contains(input)) { | |
85 | // this.setValue(Boolean.FALSE); | |
86 | // } | |
87 | // else { | |
88 | // input = null; | |
89 | // } | |
90 | // } | |
91 | // | |
92 | // if(input == null) { | |
93 | // throw new IllegalArgumentException("Invalid boolean input: " + text); | |
94 | // } | |
95 | 0 | } |
96 | ||
97 | ||
98 | } |