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.Format; |
18 |
| import java.text.ParseException; |
19 |
| import java.util.Locale; |
20 |
| |
21 |
| import org.apache.hivemind.HiveMind; |
22 |
| import org.apache.hivemind.util.PropertyUtils; |
23 |
| import org.apache.tapestry.form.IFormComponent; |
24 |
| import org.apache.tapestry.form.ValidationMessages; |
25 |
| import org.apache.tapestry.valid.ValidationConstraint; |
26 |
| import org.apache.tapestry.valid.ValidatorException; |
27 |
| |
28 |
| |
29 |
| |
30 |
| |
31 |
| |
32 |
| |
33 |
| |
34 |
| public abstract class FormatTranslator extends AbstractTranslator |
35 |
| { |
36 |
| private String _pattern; |
37 |
| |
38 |
| protected abstract String defaultPattern(); |
39 |
| |
40 |
| |
41 |
| |
42 |
| |
43 |
| |
44 |
14
| protected String formatObject(IFormComponent field, Locale locale, Object object)
|
45 |
| { |
46 |
| |
47 |
| |
48 |
| |
49 |
14
| Format format = getFormat(locale);
|
50 |
| |
51 |
14
| return format.format(object);
|
52 |
| } |
53 |
| |
54 |
| |
55 |
| |
56 |
| |
57 |
| |
58 |
20
| protected Object parseText(IFormComponent field, ValidationMessages messages, String text)
|
59 |
| throws ValidatorException |
60 |
| { |
61 |
20
| Format format = getFormat(messages.getLocale());
|
62 |
| |
63 |
20
| try
|
64 |
| { |
65 |
20
| return format.parseObject(text);
|
66 |
| } |
67 |
| catch (ParseException ex) |
68 |
| { |
69 |
8
| throw new ValidatorException(buildMessage(messages, field, getMessageKey()),
|
70 |
| getConstraint()); |
71 |
| } |
72 |
| } |
73 |
| |
74 |
| protected abstract ValidationConstraint getConstraint(); |
75 |
| |
76 |
| protected abstract Format getFormat(Locale locale); |
77 |
| |
78 |
| protected abstract String getMessageKey(); |
79 |
| |
80 |
48
| public String getPattern()
|
81 |
| { |
82 |
48
| return _pattern;
|
83 |
| } |
84 |
| |
85 |
16
| public void setPattern(String pattern)
|
86 |
| { |
87 |
16
| _pattern = pattern;
|
88 |
| } |
89 |
| |
90 |
50
| public FormatTranslator()
|
91 |
| { |
92 |
50
| _pattern = defaultPattern();
|
93 |
| } |
94 |
| |
95 |
| |
96 |
2
| public FormatTranslator(String initializer)
|
97 |
| { |
98 |
2
| PropertyUtils.configureProperties(this, initializer);
|
99 |
| |
100 |
2
| if (HiveMind.isBlank(_pattern))
|
101 |
| { |
102 |
0
| _pattern = defaultPattern();
|
103 |
| } |
104 |
| } |
105 |
| } |