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