KickJava   Java API By Example, From Geeks To Geeks.

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


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.DecimalFormat JavaDoc;
18 import java.text.DecimalFormatSymbols JavaDoc;
19 import java.text.Format JavaDoc;
20 import java.util.Locale JavaDoc;
21
22 import org.apache.tapestry.IForm;
23 import org.apache.tapestry.IMarkupWriter;
24 import org.apache.tapestry.IRequestCycle;
25 import org.apache.tapestry.form.FormComponentContributorContext;
26 import org.apache.tapestry.form.IFormComponent;
27 import org.apache.tapestry.valid.ValidationConstraint;
28 import org.apache.tapestry.valid.ValidationStrings;
29
30 /**
31  * A {@link java.text.DecimalFormat}-based {@link Translator} implementation.
32  *
33  * @author Paul Ferraro
34  * @since 4.0
35  */

36 public class NumberTranslator extends FormatTranslator
37 {
38
39     public NumberTranslator()
40     {
41     }
42
43     // Needed until HIVEMIND-134 fix is available
44
public NumberTranslator(String JavaDoc initializer)
45     {
46         super(initializer);
47     }
48
49     /**
50      * @see org.apache.tapestry.form.AbstractFormComponentContributor#defaultScript()
51      */

52     protected String JavaDoc defaultScript()
53     {
54         return "/org/apache/tapestry/form/translator/NumberTranslator.js";
55     }
56
57     /**
58      * @see org.apache.tapestry.form.translator.FormatTranslator#defaultPattern()
59      */

60     protected String JavaDoc defaultPattern()
61     {
62         return "#";
63     }
64
65     /**
66      * @see org.apache.tapestry.form.translator.FormatTranslator#getFormat(java.util.Locale)
67      */

68     protected Format JavaDoc getFormat(Locale JavaDoc locale)
69     {
70         return getDecimalFormat(locale);
71     }
72
73     public DecimalFormat JavaDoc getDecimalFormat(Locale JavaDoc locale)
74     {
75         return new DecimalFormat JavaDoc(getPattern(), new DecimalFormatSymbols JavaDoc(locale));
76     }
77
78     /**
79      * @see org.apache.tapestry.form.translator.FormatTranslator#getMessageKey()
80      */

81     protected String JavaDoc getMessageKey()
82     {
83         return ValidationStrings.INVALID_NUMBER;
84     }
85
86     /**
87      * @see org.apache.tapestry.form.translator.AbstractTranslator#getMessageParameters(java.util.Locale,
88      * java.lang.String)
89      */

90     protected Object JavaDoc[] getMessageParameters(Locale JavaDoc locale, String JavaDoc label)
91     {
92         String JavaDoc pattern = getDecimalFormat(locale).toLocalizedPattern();
93
94         return new Object JavaDoc[]
95         { label, pattern };
96     }
97
98     /**
99      * @see org.apache.tapestry.form.FormComponentContributor#renderContribution(org.apache.tapestry.IMarkupWriter,
100      * org.apache.tapestry.IRequestCycle, FormComponentContributorContext, org.apache.tapestry.form.IFormComponent)
101      */

102     public void renderContribution(IMarkupWriter writer, IRequestCycle cycle, FormComponentContributorContext context, IFormComponent field)
103     {
104         super.renderContribution(writer, cycle, context, field);
105
106         String JavaDoc message = buildMessage(field, getMessageKey());
107         IForm form = field.getForm();
108
109         addSubmitHandler(form, "validate_number(event, document." + form.getName() + "."
110                 + field.getName() + ",'" + message + "')");
111     }
112
113     /**
114      * @see org.apache.tapestry.form.translator.FormatTranslator#getConstraint()
115      */

116     protected ValidationConstraint getConstraint()
117     {
118         return ValidationConstraint.NUMBER_FORMAT;
119     }
120 }
121
Popular Tags