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