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 |
35
| public AbstractTranslator()
|
43 |
| { |
44 |
| } |
45 |
| |
46 |
| |
47 |
0
| public AbstractTranslator(String initializer)
|
48 |
| { |
49 |
0
| super(initializer);
|
50 |
| } |
51 |
| |
52 |
| |
53 |
| |
54 |
| |
55 |
| |
56 |
12
| public String format(IFormComponent field, Locale locale, Object object)
|
57 |
| { |
58 |
12
| if (object == null)
|
59 |
3
| return "";
|
60 |
| |
61 |
9
| return formatObject(field, locale, object);
|
62 |
| } |
63 |
| |
64 |
| |
65 |
| |
66 |
| |
67 |
| |
68 |
15
| public Object parse(IFormComponent field, ValidationMessages messages, String text)
|
69 |
| throws ValidatorException |
70 |
| { |
71 |
15
| String value = _trim ? text.trim() : text;
|
72 |
| |
73 |
15
| return HiveMind.isBlank(value) ? getEmpty() : parseText(field, messages, value);
|
74 |
| } |
75 |
| |
76 |
| protected abstract String formatObject(IFormComponent field, Locale locale, Object object); |
77 |
| |
78 |
| protected abstract Object parseText(IFormComponent field, ValidationMessages messages, |
79 |
| String text) throws ValidatorException; |
80 |
| |
81 |
1
| protected Object getEmpty()
|
82 |
| { |
83 |
1
| return null;
|
84 |
| } |
85 |
| |
86 |
7
| protected String buildMessage(ValidationMessages messages, IFormComponent field, String key)
|
87 |
| { |
88 |
7
| String label = field.getDisplayName();
|
89 |
| |
90 |
7
| Object[] parameters = getMessageParameters(messages.getLocale(), label);
|
91 |
| |
92 |
7
| return messages.formatValidationMessage(_message, key, parameters);
|
93 |
| } |
94 |
| |
95 |
0
| protected Object[] getMessageParameters(Locale locale, String label)
|
96 |
| { |
97 |
0
| return new Object[]
|
98 |
| { label }; |
99 |
| } |
100 |
| |
101 |
| |
102 |
| |
103 |
| |
104 |
| |
105 |
7
| public void renderContribution(IMarkupWriter writer, IRequestCycle cycle,
|
106 |
| FormComponentContributorContext context, IFormComponent field) |
107 |
| { |
108 |
7
| super.renderContribution(writer, cycle, context, field);
|
109 |
| |
110 |
7
| if (_trim)
|
111 |
3
| context.addSubmitHandler("function (event) { Tapestry.trim_field_value('" + field.getClientId() + "'); }");
|
112 |
| } |
113 |
| |
114 |
0
| public boolean isTrim()
|
115 |
| |
116 |
| { |
117 |
0
| return _trim;
|
118 |
| } |
119 |
| |
120 |
6
| public void setTrim(boolean trim)
|
121 |
| { |
122 |
6
| _trim = trim;
|
123 |
| } |
124 |
| |
125 |
0
| public String getMessage()
|
126 |
| { |
127 |
0
| return _message;
|
128 |
| } |
129 |
| |
130 |
3
| public void setMessage(String message)
|
131 |
| { |
132 |
3
| _message = message;
|
133 |
| } |
134 |
| } |