1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
package org.apache.tapestry.form.validator; |
16 |
|
|
17 |
|
import org.apache.tapestry.IMarkupWriter; |
18 |
|
import org.apache.tapestry.IRequestCycle; |
19 |
|
import org.apache.tapestry.form.FormComponentContributorContext; |
20 |
|
import org.apache.tapestry.form.IFormComponent; |
21 |
|
import org.apache.tapestry.form.ValidationMessages; |
22 |
|
import org.apache.tapestry.form.translator.NumberTranslator; |
23 |
|
import org.apache.tapestry.json.JSONLiteral; |
24 |
|
import org.apache.tapestry.json.JSONObject; |
25 |
|
import org.apache.tapestry.valid.ValidationConstants; |
26 |
|
import org.apache.tapestry.valid.ValidationConstraint; |
27 |
|
import org.apache.tapestry.valid.ValidationStrings; |
28 |
|
import org.apache.tapestry.valid.ValidatorException; |
29 |
|
|
30 |
|
import java.text.DecimalFormat; |
31 |
|
import java.text.DecimalFormatSymbols; |
32 |
|
import java.util.Locale; |
33 |
|
|
34 |
|
|
35 |
|
|
36 |
|
|
37 |
|
|
38 |
|
|
39 |
|
|
40 |
|
public class Max extends BaseValidator |
41 |
|
{ |
42 |
|
private double _max; |
43 |
|
|
44 |
|
public Max() |
45 |
1 |
{ |
46 |
1 |
} |
47 |
|
|
48 |
|
public Max(String initializer) |
49 |
|
{ |
50 |
5 |
super(initializer); |
51 |
5 |
} |
52 |
|
|
53 |
|
|
54 |
|
|
55 |
|
|
56 |
|
|
57 |
|
public void validate(IFormComponent field, ValidationMessages messages, Object object) |
58 |
|
throws ValidatorException |
59 |
|
{ |
60 |
3 |
Number value = (Number) object; |
61 |
|
|
62 |
3 |
if (value.doubleValue() > _max) |
63 |
1 |
throw new ValidatorException(buildMessage(messages, field), |
64 |
|
ValidationConstraint.TOO_LARGE); |
65 |
2 |
} |
66 |
|
|
67 |
|
private String getStringValue(Locale locale, IFormComponent field) |
68 |
|
{ |
69 |
5 |
String ret = null; |
70 |
6 |
NumberTranslator translator = (NumberTranslator)super.getFieldTranslator(field, NumberTranslator.class); |
71 |
|
|
72 |
5 |
if (translator != null) |
73 |
0 |
ret = translator.format(field, locale, new Double(_max)); |
74 |
|
else |
75 |
5 |
ret = String.valueOf(_max); |
76 |
|
|
77 |
5 |
return ret; |
78 |
|
} |
79 |
|
|
80 |
|
private String buildMessage(ValidationMessages messages, IFormComponent field) |
81 |
|
{ |
82 |
3 |
return messages.formatValidationMessage( |
83 |
|
getMessage(), |
84 |
|
ValidationStrings.VALUE_TOO_LARGE, |
85 |
|
new Object[] |
86 |
|
{ field.getDisplayName(), getStringValue(messages.getLocale(), field) }); |
87 |
|
} |
88 |
|
|
89 |
|
public void renderContribution(IMarkupWriter writer, IRequestCycle cycle, |
90 |
|
FormComponentContributorContext context, IFormComponent field) |
91 |
|
{ |
92 |
2 |
JSONObject profile = context.getProfile(); |
93 |
|
|
94 |
2 |
if (!profile.has(ValidationConstants.CONSTRAINTS)) { |
95 |
2 |
profile.put(ValidationConstants.CONSTRAINTS, new JSONObject()); |
96 |
|
} |
97 |
2 |
JSONObject cons = profile.getJSONObject(ValidationConstants.CONSTRAINTS); |
98 |
|
|
99 |
2 |
String maxString = getStringValue(context.getLocale(), field); |
100 |
2 |
String grouping = ""; |
101 |
|
|
102 |
2 |
DecimalFormatSymbols symbols = null; |
103 |
2 |
NumberTranslator translator = (NumberTranslator)super.getFieldTranslator(field, NumberTranslator.class); |
104 |
|
|
105 |
2 |
if (translator != null) { |
106 |
0 |
DecimalFormat format = translator.getDecimalFormat(context.getLocale()); |
107 |
|
|
108 |
0 |
if (format.isGroupingUsed()) { |
109 |
|
|
110 |
0 |
grouping += ",separator:" + JSONObject.quote(format.getDecimalFormatSymbols().getGroupingSeparator()); |
111 |
0 |
grouping += ",groupSize:" + format.getGroupingSize(); |
112 |
|
} else { |
113 |
|
|
114 |
0 |
grouping += ",separator:\"\""; |
115 |
|
} |
116 |
|
|
117 |
0 |
symbols = format.getDecimalFormatSymbols(); |
118 |
0 |
} else { |
119 |
|
|
120 |
2 |
symbols = new DecimalFormatSymbols(context.getLocale()); |
121 |
|
} |
122 |
|
|
123 |
2 |
accumulateProperty(cons, field.getClientId(), |
124 |
|
new JSONLiteral("[tapestry.form.validation.lessThanOrEqual," |
125 |
|
+ JSONObject.quote(maxString) |
126 |
|
+ ",{" |
127 |
|
+ "decimal:" + JSONObject.quote(symbols.getDecimalSeparator()) |
128 |
|
+ grouping |
129 |
|
+ "}]")); |
130 |
|
|
131 |
2 |
accumulateProfileProperty(field, profile, ValidationConstants.CONSTRAINTS, buildMessage(context, field)); |
132 |
2 |
} |
133 |
|
|
134 |
|
public void setMax(double max) |
135 |
|
{ |
136 |
6 |
_max = max; |
137 |
6 |
} |
138 |
|
|
139 |
|
public double getMax() |
140 |
|
{ |
141 |
1 |
return _max; |
142 |
|
} |
143 |
|
} |