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.HiveMind; |
18 |
|
import org.apache.tapestry.IMarkupWriter; |
19 |
|
import org.apache.tapestry.IRequestCycle; |
20 |
|
import org.apache.tapestry.form.AbstractFormComponentContributor; |
21 |
|
import org.apache.tapestry.form.FormComponentContributorContext; |
22 |
|
import org.apache.tapestry.form.IFormComponent; |
23 |
|
import org.apache.tapestry.form.ValidationMessages; |
24 |
|
import org.apache.tapestry.json.JSONObject; |
25 |
|
import org.apache.tapestry.valid.ValidationConstants; |
26 |
|
import org.apache.tapestry.valid.ValidatorException; |
27 |
|
|
28 |
|
import java.util.Locale; |
29 |
|
|
30 |
|
|
31 |
|
|
32 |
|
|
33 |
|
|
34 |
|
|
35 |
|
|
36 |
|
|
37 |
|
public abstract class AbstractTranslator extends AbstractFormComponentContributor implements |
38 |
|
Translator |
39 |
|
{ |
40 |
|
private boolean _trim; |
41 |
|
|
42 |
|
private String _message; |
43 |
|
|
44 |
|
public AbstractTranslator() |
45 |
43 |
{ |
46 |
43 |
} |
47 |
|
|
48 |
|
|
49 |
|
public AbstractTranslator(String initializer) |
50 |
|
{ |
51 |
0 |
super(initializer); |
52 |
0 |
} |
53 |
|
|
54 |
|
|
55 |
|
|
56 |
|
|
57 |
|
|
58 |
|
public String format(IFormComponent field, Locale locale, Object object) |
59 |
|
{ |
60 |
75 |
if (object == null) |
61 |
4 |
return ""; |
62 |
|
|
63 |
71 |
return formatObject(field, locale, object); |
64 |
|
} |
65 |
|
|
66 |
|
|
67 |
|
|
68 |
|
|
69 |
|
|
70 |
|
public Object parse(IFormComponent field, ValidationMessages messages, String text) |
71 |
|
throws ValidatorException |
72 |
|
{ |
73 |
20 |
String value = text == null ? null : (_trim ? text.trim() : text); |
74 |
|
|
75 |
20 |
return HiveMind.isBlank(value) ? getValueForEmptyInput() : parseText(field, messages, value); |
76 |
|
} |
77 |
|
|
78 |
|
protected abstract String formatObject(IFormComponent field, Locale locale, Object object); |
79 |
|
|
80 |
|
protected abstract Object parseText(IFormComponent field, ValidationMessages messages, String text) |
81 |
|
throws ValidatorException; |
82 |
|
|
83 |
|
|
84 |
|
|
85 |
|
|
86 |
|
|
87 |
|
|
88 |
|
|
89 |
|
|
90 |
|
protected Object getValueForEmptyInput() |
91 |
|
{ |
92 |
2 |
return null; |
93 |
|
} |
94 |
|
|
95 |
|
protected String buildMessage(ValidationMessages messages, IFormComponent field, String key) |
96 |
|
{ |
97 |
11 |
String label = field.getDisplayName(); |
98 |
|
|
99 |
11 |
Object[] parameters = getMessageParameters(messages.getLocale(), label); |
100 |
|
|
101 |
11 |
return messages.formatValidationMessage(_message, key, parameters); |
102 |
|
} |
103 |
|
|
104 |
|
protected Object[] getMessageParameters(Locale locale, String label) |
105 |
|
{ |
106 |
1 |
return new Object[] { label }; |
107 |
|
} |
108 |
|
|
109 |
|
|
110 |
|
|
111 |
|
|
112 |
|
public void renderContribution(IMarkupWriter writer, IRequestCycle cycle, |
113 |
|
FormComponentContributorContext context, IFormComponent field) |
114 |
|
{ |
115 |
8 |
super.renderContribution(writer, cycle, context, field); |
116 |
|
|
117 |
8 |
if (_trim) { |
118 |
3 |
JSONObject profile = context.getProfile(); |
119 |
|
|
120 |
3 |
accumulateProperty(profile, ValidationConstants.TRIM, field.getClientId()); |
121 |
|
} |
122 |
8 |
} |
123 |
|
|
124 |
|
public boolean isTrim() |
125 |
|
|
126 |
|
{ |
127 |
1 |
return _trim; |
128 |
|
} |
129 |
|
|
130 |
|
public void setTrim(boolean trim) |
131 |
|
{ |
132 |
14 |
_trim = trim; |
133 |
14 |
} |
134 |
|
|
135 |
|
public String getMessage() |
136 |
|
{ |
137 |
0 |
return _message; |
138 |
|
} |
139 |
|
|
140 |
|
public void setMessage(String message) |
141 |
|
{ |
142 |
11 |
_message = message; |
143 |
11 |
} |
144 |
|
} |