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 org.apache.log4j.Logger;
19 import org.kuali.rice.core.api.util.RiceKeyConstants;
20 import org.kuali.rice.core.api.util.type.KualiDecimal;
21 import org.kuali.rice.core.api.util.type.KualiInteger;
22 import org.kuali.rice.core.web.format.FormatException;
23
24 import java.beans.PropertyEditorSupport;
25 import java.io.Serializable;
26 import java.text.DecimalFormat;
27 import java.text.NumberFormat;
28 import java.text.ParseException;
29
30
31
32
33
34
35
36
37 public class UifCurrencyEditor extends PropertyEditorSupport implements Serializable {
38 private static final long serialVersionUID = 6692868638156609014L;
39 private static Logger LOG = Logger.getLogger(UifCurrencyEditor.class);
40
41
42
43
44
45
46 @Override
47 public String getAsText() {
48 Object obj = this.getValue();
49
50 LOG.debug("format '" + obj + "'");
51 if (obj == null)
52 return null;
53
54 NumberFormat formatter = getCurrencyInstanceUsingParseBigDecimal();
55 String string = null;
56
57 try {
58 Number number = (Number) obj;
59 if (obj instanceof KualiInteger) {
60 formatter.setMaximumFractionDigits(0);
61 }
62 string = formatter.format(number.doubleValue());
63 } catch (IllegalArgumentException e) {
64 throw new FormatException("formatting", RiceKeyConstants.ERROR_CURRENCY, obj.toString(), e);
65 } catch (ClassCastException e) {
66 throw new FormatException("formatting", RiceKeyConstants.ERROR_CURRENCY, obj.toString(), e);
67 }
68
69 return string;
70 }
71
72
73
74
75
76
77
78 private NumberFormat getCurrencyInstanceUsingParseBigDecimal() {
79 NumberFormat formatter = NumberFormat.getCurrencyInstance();
80 if (formatter instanceof DecimalFormat) {
81 ((DecimalFormat) formatter).setParseBigDecimal(true);
82 }
83 return formatter;
84 }
85
86
87
88
89
90
91
92
93 @Override
94 public void setAsText(String text) {
95 this.setValue(convertToObject(text));
96 }
97
98
99
100
101
102
103
104
105
106
107
108 protected Object convertToObject(String text) {
109 KualiDecimal value = null;
110
111 LOG.debug("convertToObject '" + text + "'");
112
113 if (text != null) {
114 text = text.trim();
115 NumberFormat formatter = getCurrencyInstanceUsingParseBigDecimal();
116
117
118 if (formatter instanceof DecimalFormat) {
119 String prefix = ((DecimalFormat) formatter).getPositivePrefix();
120 String suffix = ((DecimalFormat) formatter).getPositiveSuffix();
121 if (!prefix.equals("") && !text.startsWith(prefix)) {
122 text = prefix.concat(text);
123 }
124 if (!suffix.equals("") && !text.endsWith(suffix)) {
125 text = text.concat(suffix);
126 }
127 }
128 try {
129 Number parsedNumber = formatter.parse(text);
130 value = new KualiDecimal(parsedNumber.toString());
131 } catch (NumberFormatException e) {
132 throw new FormatException("parsing", RiceKeyConstants.ERROR_CURRENCY, text, e);
133 } catch (ParseException e) {
134 throw new FormatException("parsing", RiceKeyConstants.ERROR_CURRENCY, text, e);
135 }
136 }
137 return value;
138 }
139
140 }