KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > form > translator > AbstractTranslator


1 // Copyright 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.tapestry.form.translator;
16
17 import java.text.MessageFormat JavaDoc;
18 import java.util.Locale JavaDoc;
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  * Abstract {@link Translator} implementation that provides default behavior for trimming, null
32  * object, and empty text handling.
33  *
34  * @author Paul Ferraro
35  * @since 4.0
36  */

37 public abstract class AbstractTranslator extends AbstractFormComponentContributor implements
38         Translator
39 {
40     private boolean _trim;
41
42     private String JavaDoc _message;
43
44     public AbstractTranslator()
45     {
46     }
47
48     // Needed until HIVEMIND-134 fix is available
49
public AbstractTranslator(String JavaDoc initializer)
50     {
51         super(initializer);
52     }
53
54     /**
55      * @see org.apache.tapestry.form.translator.Translator#format(org.apache.tapestry.form.IFormComponent,
56      * java.lang.Object)
57      */

58     public String JavaDoc format(IFormComponent field, Object JavaDoc object)
59     {
60         return (object != null) ? formatObject(field, object) : "";
61     }
62
63     /**
64      * @see org.apache.tapestry.form.translator.Translator#parse(org.apache.tapestry.form.IFormComponent,
65      * java.lang.String)
66      */

67     public Object JavaDoc parse(IFormComponent field, String JavaDoc text) throws ValidatorException
68     {
69         String JavaDoc value = _trim ? text.trim() : text;
70
71         return HiveMind.isBlank(value) ? getEmpty() : parseText(field, value);
72     }
73
74     protected abstract String JavaDoc formatObject(IFormComponent field, Object JavaDoc object);
75
76     protected abstract Object JavaDoc parseText(IFormComponent field, String JavaDoc text)
77             throws ValidatorException;
78
79     protected Object JavaDoc getEmpty()
80     {
81         return null;
82     }
83
84     protected String JavaDoc buildMessage(IFormComponent field, String JavaDoc key)
85     {
86         Locale JavaDoc locale = field.getPage().getLocale();
87
88         String JavaDoc pattern = (_message == null) ? ValidationStrings.getMessagePattern(key, locale)
89                 : _message;
90
91         String JavaDoc name = field.getDisplayName();
92
93         return MessageFormat.format(pattern, getMessageParameters(locale, name));
94     }
95
96     protected Object JavaDoc[] getMessageParameters(Locale JavaDoc locale, String JavaDoc label)
97     {
98         return new Object JavaDoc[]
99         { label };
100     }
101
102     /**
103      * @see org.apache.tapestry.form.FormComponentContributor#renderContribution(org.apache.tapestry.IRequestCycle,
104      * org.apache.tapestry.form.IFormComponent)
105      */

106     public void renderContribution(IMarkupWriter writer, IRequestCycle cycle, FormComponentContributorContext context, IFormComponent field)
107     {
108         super.renderContribution(writer, cycle, context, field);
109
110         if (_trim)
111         {
112             IForm form = field.getForm();
113
114             addSubmitHandler(form, "trim(document." + form.getName() + "." + field.getName() + ")");
115         }
116     }
117
118     public boolean isTrim()
119     {
120         return _trim;
121     }
122
123     public void setTrim(boolean trim)
124     {
125         _trim = trim;
126     }
127
128     public String JavaDoc getMessage()
129     {
130         return _message;
131     }
132
133     public void setMessage(String JavaDoc message)
134     {
135         _message = message;
136     }
137 }
138
Popular Tags