1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
package org.apache.tapestry.form.translator; |
16 |
|
|
17 |
|
import org.apache.hivemind.util.PropertyUtils; |
18 |
|
import org.apache.tapestry.IMarkupWriter; |
19 |
|
import org.apache.tapestry.IRequestCycle; |
20 |
|
import org.apache.tapestry.form.FormComponentContributorContext; |
21 |
|
import org.apache.tapestry.form.IFormComponent; |
22 |
|
import org.apache.tapestry.json.JSONLiteral; |
23 |
|
import org.apache.tapestry.json.JSONObject; |
24 |
|
import org.apache.tapestry.valid.ValidationConstants; |
25 |
|
import org.apache.tapestry.valid.ValidationConstraint; |
26 |
|
import org.apache.tapestry.valid.ValidationStrings; |
27 |
|
|
28 |
|
import java.text.DecimalFormat; |
29 |
|
import java.text.DecimalFormatSymbols; |
30 |
|
import java.text.Format; |
31 |
|
import java.util.Locale; |
32 |
|
|
33 |
|
|
34 |
|
|
35 |
|
|
36 |
|
|
37 |
|
|
38 |
|
|
39 |
|
public class NumberTranslator extends FormatTranslator |
40 |
|
{ |
41 |
17 |
private boolean _omitZero = false; |
42 |
|
|
43 |
|
public NumberTranslator() |
44 |
13 |
{ |
45 |
13 |
} |
46 |
|
|
47 |
|
|
48 |
|
public NumberTranslator(String initializer) |
49 |
4 |
{ |
50 |
4 |
PropertyUtils.configureProperties(this, initializer); |
51 |
4 |
} |
52 |
|
|
53 |
|
protected String formatObject(IFormComponent field, Locale locale, Object object) |
54 |
|
{ |
55 |
6 |
Number number = (Number) object; |
56 |
|
|
57 |
6 |
if (_omitZero) |
58 |
|
{ |
59 |
1 |
if (number.doubleValue() == 0) |
60 |
1 |
return ""; |
61 |
|
} |
62 |
|
|
63 |
5 |
return super.formatObject(field, locale, object); |
64 |
|
} |
65 |
|
|
66 |
|
protected Object getValueForEmptyInput() |
67 |
|
{ |
68 |
2 |
return _omitZero ? null : new Double(0); |
69 |
|
} |
70 |
|
|
71 |
|
|
72 |
|
|
73 |
|
|
74 |
|
protected String defaultPattern() |
75 |
|
{ |
76 |
17 |
return "#"; |
77 |
|
} |
78 |
|
|
79 |
|
|
80 |
|
|
81 |
|
|
82 |
|
protected Format getFormat(Locale locale) |
83 |
|
{ |
84 |
10 |
return getDecimalFormat(locale); |
85 |
|
} |
86 |
|
|
87 |
|
public DecimalFormat getDecimalFormat(Locale locale) |
88 |
|
{ |
89 |
20 |
return new DecimalFormat(getPattern(), new DecimalFormatSymbols(locale)); |
90 |
|
} |
91 |
|
|
92 |
|
|
93 |
|
|
94 |
|
|
95 |
|
protected String getMessageKey() |
96 |
|
{ |
97 |
6 |
return ValidationStrings.INVALID_NUMBER; |
98 |
|
} |
99 |
|
|
100 |
|
|
101 |
|
|
102 |
|
|
103 |
|
|
104 |
|
protected Object[] getMessageParameters(Locale locale, String label) |
105 |
|
{ |
106 |
6 |
String pattern = getDecimalFormat(locale).toLocalizedPattern(); |
107 |
|
|
108 |
6 |
return new Object[] { label, pattern }; |
109 |
|
} |
110 |
|
|
111 |
|
|
112 |
|
|
113 |
|
|
114 |
|
|
115 |
|
|
116 |
|
public void renderContribution(IMarkupWriter writer, IRequestCycle cycle, |
117 |
|
FormComponentContributorContext context, IFormComponent field) |
118 |
|
{ |
119 |
4 |
super.renderContribution(writer, cycle, context, field); |
120 |
|
|
121 |
4 |
String message = buildMessage(context, field, getMessageKey()); |
122 |
|
|
123 |
4 |
JSONObject profile = context.getProfile(); |
124 |
4 |
if (!profile.has(ValidationConstants.CONSTRAINTS)) { |
125 |
4 |
profile.put(ValidationConstants.CONSTRAINTS, new JSONObject()); |
126 |
|
} |
127 |
4 |
JSONObject cons = profile.getJSONObject(ValidationConstants.CONSTRAINTS); |
128 |
|
|
129 |
4 |
context.addInitializationScript(field, "dojo.require(\"dojo.i18n.number\");"); |
130 |
|
|
131 |
4 |
DecimalFormat format = getDecimalFormat(context.getLocale()); |
132 |
|
|
133 |
4 |
String grouping = ""; |
134 |
4 |
if (format.isGroupingUsed()) { |
135 |
|
|
136 |
1 |
grouping += ",separator:" + JSONObject.quote(format.getDecimalFormatSymbols().getGroupingSeparator()); |
137 |
1 |
grouping += ",groupSize:" + format.getGroupingSize(); |
138 |
|
} else { |
139 |
|
|
140 |
3 |
grouping += ",separator:\"\""; |
141 |
|
} |
142 |
|
|
143 |
4 |
cons.accumulate(field.getClientId(), |
144 |
|
new JSONLiteral("[dojo.i18n.number.isReal,null,{" |
145 |
|
+ "places:" + format.getMaximumFractionDigits() + "," |
146 |
|
+ "decimal:" |
147 |
|
+ JSONObject.quote(format.getDecimalFormatSymbols().getDecimalSeparator()) |
148 |
|
+ grouping |
149 |
|
+ "}]")); |
150 |
|
|
151 |
4 |
accumulateProfileProperty(field, profile, ValidationConstants.CONSTRAINTS, message); |
152 |
4 |
} |
153 |
|
|
154 |
|
|
155 |
|
|
156 |
|
|
157 |
|
protected ValidationConstraint getConstraint() |
158 |
|
{ |
159 |
2 |
return ValidationConstraint.NUMBER_FORMAT; |
160 |
|
} |
161 |
|
|
162 |
|
|
163 |
|
|
164 |
|
|
165 |
|
|
166 |
|
|
167 |
|
|
168 |
|
|
169 |
|
|
170 |
|
|
171 |
|
public void setOmitZero(boolean omitZero) |
172 |
|
{ |
173 |
3 |
_omitZero = omitZero; |
174 |
3 |
} |
175 |
|
|
176 |
|
public boolean isOmitZero() |
177 |
|
{ |
178 |
0 |
return _omitZero; |
179 |
|
} |
180 |
|
} |