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 | 0 | private boolean _omitZero = false; |
42 | |
|
43 | |
public NumberTranslator() |
44 | 0 | { |
45 | 0 | } |
46 | |
|
47 | |
|
48 | |
public NumberTranslator(String initializer) |
49 | 0 | { |
50 | 0 | PropertyUtils.configureProperties(this, initializer); |
51 | 0 | } |
52 | |
|
53 | |
protected String formatObject(IFormComponent field, Locale locale, Object object) |
54 | |
{ |
55 | 0 | Number number = (Number) object; |
56 | |
|
57 | 0 | if (_omitZero) |
58 | |
{ |
59 | 0 | if (number.doubleValue() == 0) |
60 | 0 | return ""; |
61 | |
} |
62 | |
|
63 | 0 | return super.formatObject(field, locale, object); |
64 | |
} |
65 | |
|
66 | |
protected Object getValueForEmptyInput() |
67 | |
{ |
68 | 0 | return _omitZero ? null : new Double(0); |
69 | |
} |
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
protected String defaultPattern() |
75 | |
{ |
76 | 0 | return "#"; |
77 | |
} |
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
protected Format getFormat(Locale locale) |
83 | |
{ |
84 | 0 | return getDecimalFormat(locale); |
85 | |
} |
86 | |
|
87 | |
public DecimalFormat getDecimalFormat(Locale locale) |
88 | |
{ |
89 | 0 | return new DecimalFormat(getPattern(), new DecimalFormatSymbols(locale)); |
90 | |
} |
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
protected String getMessageKey() |
96 | |
{ |
97 | 0 | return ValidationStrings.INVALID_NUMBER; |
98 | |
} |
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
protected Object[] getMessageParameters(Locale locale, String label) |
105 | |
{ |
106 | 0 | String pattern = getDecimalFormat(locale).toLocalizedPattern(); |
107 | |
|
108 | 0 | 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 | 0 | super.renderContribution(writer, cycle, context, field); |
120 | |
|
121 | 0 | String message = buildMessage(context, field, getMessageKey()); |
122 | |
|
123 | 0 | JSONObject profile = context.getProfile(); |
124 | 0 | if (!profile.has(ValidationConstants.CONSTRAINTS)) { |
125 | 0 | profile.put(ValidationConstants.CONSTRAINTS, new JSONObject()); |
126 | |
} |
127 | 0 | JSONObject cons = profile.getJSONObject(ValidationConstants.CONSTRAINTS); |
128 | |
|
129 | 0 | DecimalFormat format = getDecimalFormat(context.getLocale()); |
130 | |
|
131 | 0 | String grouping = ""; |
132 | 0 | if (format.isGroupingUsed()) { |
133 | |
|
134 | 0 | grouping += ",separator:" + JSONObject.quote(format.getDecimalFormatSymbols().getGroupingSeparator()); |
135 | 0 | grouping += ",groupSize:" + format.getGroupingSize(); |
136 | |
} else { |
137 | |
|
138 | 0 | grouping += ",separator:\"\""; |
139 | |
} |
140 | |
|
141 | 0 | cons.accumulate(field.getClientId(), |
142 | |
new JSONLiteral("[tapestry.form.validation.isReal,null,{" |
143 | |
+ "places:" + format.getMaximumFractionDigits() + "," |
144 | |
+ "decimal:" |
145 | |
+ JSONObject.quote(format.getDecimalFormatSymbols().getDecimalSeparator()) |
146 | |
+ grouping |
147 | |
+ "}]")); |
148 | |
|
149 | 0 | accumulateProfileProperty(field, profile, ValidationConstants.CONSTRAINTS, message); |
150 | 0 | } |
151 | |
|
152 | |
|
153 | |
|
154 | |
|
155 | |
protected ValidationConstraint getConstraint() |
156 | |
{ |
157 | 0 | return ValidationConstraint.NUMBER_FORMAT; |
158 | |
} |
159 | |
|
160 | |
|
161 | |
|
162 | |
|
163 | |
|
164 | |
|
165 | |
|
166 | |
|
167 | |
|
168 | |
|
169 | |
public void setOmitZero(boolean omitZero) |
170 | |
{ |
171 | 0 | _omitZero = omitZero; |
172 | 0 | } |
173 | |
|
174 | |
public boolean isOmitZero() |
175 | |
{ |
176 | 0 | return _omitZero; |
177 | |
} |
178 | |
} |