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 Min extends BaseValidator |
41 |
|
{ |
42 |
|
private double _min; |
43 |
|
|
44 |
|
public Min() |
45 |
1 |
{ |
46 |
1 |
} |
47 |
|
|
48 |
|
public Min(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 (_min > value.doubleValue()) |
63 |
2 |
throw new ValidatorException(buildMessage(messages, field), ValidationConstraint.TOO_SMALL); |
64 |
1 |
} |
65 |
|
|
66 |
|
private String getStringValue(Locale locale, IFormComponent field) |
67 |
|
{ |
68 |
6 |
String ret = null; |
69 |
7 |
NumberTranslator translator = (NumberTranslator)super.getFieldTranslator(field, NumberTranslator.class); |
70 |
|
|
71 |
6 |
if (translator != null) |
72 |
0 |
ret = translator.format(field, locale, new Double(_min)); |
73 |
|
else |
74 |
6 |
ret = String.valueOf(_min); |
75 |
|
|
76 |
6 |
return ret; |
77 |
|
} |
78 |
|
|
79 |
|
private String buildMessage(ValidationMessages messages, IFormComponent field) |
80 |
|
{ |
81 |
4 |
return messages.formatValidationMessage( |
82 |
|
getMessage(), |
83 |
|
ValidationStrings.VALUE_TOO_SMALL, |
84 |
|
new Object[] { |
85 |
|
field.getDisplayName(), getStringValue(messages.getLocale(), field) |
86 |
|
}); |
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 |
context.addInitializationScript(field, "dojo.require(\"dojo.i18n.number\");"); |
100 |
|
|
101 |
2 |
String minString = getStringValue(context.getLocale(), field); |
102 |
2 |
String grouping = ""; |
103 |
|
|
104 |
2 |
DecimalFormatSymbols symbols = null; |
105 |
2 |
NumberTranslator translator = (NumberTranslator)super.getFieldTranslator(field, NumberTranslator.class); |
106 |
|
|
107 |
2 |
if (translator != null) { |
108 |
0 |
DecimalFormat format = translator.getDecimalFormat(context.getLocale()); |
109 |
|
|
110 |
0 |
if (format.isGroupingUsed()) { |
111 |
|
|
112 |
0 |
grouping += ",separator:" + JSONObject.quote(format.getDecimalFormatSymbols().getGroupingSeparator()); |
113 |
0 |
grouping += ",groupSize:" + format.getGroupingSize(); |
114 |
|
} else { |
115 |
|
|
116 |
0 |
grouping += ",separator:\"\""; |
117 |
|
} |
118 |
|
|
119 |
0 |
symbols = format.getDecimalFormatSymbols(); |
120 |
0 |
} else { |
121 |
|
|
122 |
2 |
symbols = new DecimalFormatSymbols(context.getLocale()); |
123 |
|
} |
124 |
|
|
125 |
2 |
accumulateProperty(cons, field.getClientId(), |
126 |
|
new JSONLiteral("[tapestry.form.validation.greaterThanOrEqual," |
127 |
|
+ JSONObject.quote(minString) |
128 |
|
+ ",{" |
129 |
|
+ "decimal:" + JSONObject.quote(symbols.getDecimalSeparator()) |
130 |
|
+ grouping |
131 |
|
+ "}]")); |
132 |
|
|
133 |
2 |
accumulateProfileProperty(field, profile, ValidationConstants.CONSTRAINTS, buildMessage(context, field)); |
134 |
2 |
} |
135 |
|
|
136 |
|
public void setMin(double min) |
137 |
|
{ |
138 |
6 |
_min = min; |
139 |
6 |
} |
140 |
|
|
141 |
|
public double getMin() |
142 |
|
{ |
143 |
1 |
return _min; |
144 |
|
} |
145 |
|
} |