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