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.text.MessageFormat; |
18 |
| import java.util.Locale; |
19 |
| |
20 |
| import org.apache.hivemind.HiveMind; |
21 |
| import org.apache.tapestry.IForm; |
22 |
| import org.apache.tapestry.IMarkupWriter; |
23 |
| import org.apache.tapestry.IRequestCycle; |
24 |
| import org.apache.tapestry.form.AbstractFormComponentContributor; |
25 |
| import org.apache.tapestry.form.FormComponentContributorContext; |
26 |
| import org.apache.tapestry.form.IFormComponent; |
27 |
| import org.apache.tapestry.valid.ValidationStrings; |
28 |
| import org.apache.tapestry.valid.ValidatorException; |
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 |
30
| public AbstractTranslator()
|
45 |
| { |
46 |
| } |
47 |
| |
48 |
| |
49 |
0
| public AbstractTranslator(String initializer)
|
50 |
| { |
51 |
0
| super(initializer);
|
52 |
| } |
53 |
| |
54 |
| |
55 |
| |
56 |
| |
57 |
| |
58 |
8
| public String format(IFormComponent field, Object object)
|
59 |
| { |
60 |
8
| return (object != null) ? formatObject(field, object) : "";
|
61 |
| } |
62 |
| |
63 |
| |
64 |
| |
65 |
| |
66 |
| |
67 |
15
| public Object parse(IFormComponent field, String text) throws ValidatorException
|
68 |
| { |
69 |
15
| String value = _trim ? text.trim() : text;
|
70 |
| |
71 |
15
| return HiveMind.isBlank(value) ? getEmpty() : parseText(field, value);
|
72 |
| } |
73 |
| |
74 |
| protected abstract String formatObject(IFormComponent field, Object object); |
75 |
| |
76 |
| protected abstract Object parseText(IFormComponent field, String text) |
77 |
| throws ValidatorException; |
78 |
| |
79 |
1
| protected Object getEmpty()
|
80 |
| { |
81 |
1
| return null;
|
82 |
| } |
83 |
| |
84 |
7
| protected String buildMessage(IFormComponent field, String key)
|
85 |
| { |
86 |
7
| Locale locale = field.getPage().getLocale();
|
87 |
| |
88 |
7
| String pattern = (_message == null) ? ValidationStrings.getMessagePattern(key, locale)
|
89 |
| : _message; |
90 |
| |
91 |
7
| String name = field.getDisplayName();
|
92 |
| |
93 |
7
| return MessageFormat.format(pattern, getMessageParameters(locale, name));
|
94 |
| } |
95 |
| |
96 |
0
| protected Object[] getMessageParameters(Locale locale, String label)
|
97 |
| { |
98 |
0
| return new Object[]
|
99 |
| { label }; |
100 |
| } |
101 |
| |
102 |
| |
103 |
| |
104 |
| |
105 |
| |
106 |
7
| public void renderContribution(IMarkupWriter writer, IRequestCycle cycle, FormComponentContributorContext context, IFormComponent field)
|
107 |
| { |
108 |
7
| super.renderContribution(writer, cycle, context, field);
|
109 |
| |
110 |
7
| if (_trim)
|
111 |
| { |
112 |
3
| IForm form = field.getForm();
|
113 |
| |
114 |
3
| addSubmitHandler(form, "trim(document." + form.getName() + "." + field.getName() + ")");
|
115 |
| } |
116 |
| } |
117 |
| |
118 |
0
| public boolean isTrim()
|
119 |
| { |
120 |
0
| return _trim;
|
121 |
| } |
122 |
| |
123 |
6
| public void setTrim(boolean trim)
|
124 |
| { |
125 |
6
| _trim = trim;
|
126 |
| } |
127 |
| |
128 |
0
| public String getMessage()
|
129 |
| { |
130 |
0
| return _message;
|
131 |
| } |
132 |
| |
133 |
3
| public void setMessage(String message)
|
134 |
| { |
135 |
3
| _message = message;
|
136 |
| } |
137 |
| } |