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