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