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 | 0 | { |
46 | 0 | } |
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 | 0 | if (object == null) |
61 | 0 | return ""; |
62 | |
|
63 | 0 | 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 | 0 | String value = text == null ? null : (_trim ? text.trim() : text); |
74 | |
|
75 | 0 | 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 | 0 | return null; |
93 | |
} |
94 | |
|
95 | |
protected String buildMessage(ValidationMessages messages, IFormComponent field, String key) |
96 | |
{ |
97 | 0 | String label = field.getDisplayName(); |
98 | |
|
99 | 0 | Object[] parameters = getMessageParameters(messages.getLocale(), label); |
100 | |
|
101 | 0 | return messages.formatValidationMessage(_message, key, parameters); |
102 | |
} |
103 | |
|
104 | |
protected Object[] getMessageParameters(Locale locale, String label) |
105 | |
{ |
106 | 0 | return new Object[] { label }; |
107 | |
} |
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
public void renderContribution(IMarkupWriter writer, IRequestCycle cycle, |
113 | |
FormComponentContributorContext context, IFormComponent field) |
114 | |
{ |
115 | 0 | super.renderContribution(writer, cycle, context, field); |
116 | |
|
117 | 0 | if (_trim) { |
118 | 0 | JSONObject profile = context.getProfile(); |
119 | |
|
120 | 0 | accumulateProperty(profile, ValidationConstants.TRIM, field.getClientId()); |
121 | |
} |
122 | 0 | } |
123 | |
|
124 | |
public boolean isTrim() |
125 | |
|
126 | |
{ |
127 | 0 | return _trim; |
128 | |
} |
129 | |
|
130 | |
public void setTrim(boolean trim) |
131 | |
{ |
132 | 0 | _trim = trim; |
133 | 0 | } |
134 | |
|
135 | |
public String getMessage() |
136 | |
{ |
137 | 0 | return _message; |
138 | |
} |
139 | |
|
140 | |
public void setMessage(String message) |
141 | |
{ |
142 | 0 | _message = message; |
143 | 0 | } |
144 | |
} |