KickJava   Java API By Example, From Geeks To Geeks.

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


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.DateFormatSymbols JavaDoc;
18 import java.text.Format JavaDoc;
19 import java.text.SimpleDateFormat JavaDoc;
20 import java.util.Locale JavaDoc;
21
22 import org.apache.tapestry.valid.ValidationConstraint;
23 import org.apache.tapestry.valid.ValidationStrings;
24
25 /**
26  * A {@link java.text.SimpleDateFormat}-based {@link Translator} implementation.
27  *
28  * @author Paul Ferraro
29  * @since 4.0
30  */

31 public class DateTranslator extends FormatTranslator
32 {
33
34     public DateTranslator()
35     {
36     }
37
38     // Needed until HIVEMIND-134 fix is available
39
public DateTranslator(String JavaDoc initializer)
40     {
41         super(initializer);
42     }
43
44     /**
45      * @see org.apache.tapestry.form.translator.FormatTranslator#defaultPattern()
46      */

47     protected String JavaDoc defaultPattern()
48     {
49         return "MM/dd/yyyy";
50     }
51
52     /**
53      * @see org.apache.tapestry.form.translator.FormatTranslator#getFormat(java.util.Locale)
54      */

55     protected Format JavaDoc getFormat(Locale JavaDoc locale)
56     {
57         return getDateFormat(locale);
58     }
59
60     public SimpleDateFormat JavaDoc getDateFormat(Locale JavaDoc locale)
61     {
62         return new SimpleDateFormat JavaDoc(getPattern(), new DateFormatSymbols JavaDoc(locale));
63     }
64
65     /**
66      * @see org.apache.tapestry.form.translator.FormatTranslator#getMessageKey()
67      */

68     protected String JavaDoc getMessageKey()
69     {
70         return ValidationStrings.INVALID_DATE;
71     }
72
73     /**
74      * @see org.apache.tapestry.form.translator.AbstractTranslator#getMessageParameters(java.util.Locale,
75      * java.lang.String)
76      */

77     protected Object JavaDoc[] getMessageParameters(Locale JavaDoc locale, String JavaDoc label)
78     {
79         String JavaDoc pattern = getDateFormat(locale).toLocalizedPattern().toUpperCase(locale);
80
81         return new Object JavaDoc[]
82         { label, pattern };
83     }
84
85     /**
86      * @see org.apache.tapestry.form.translator.FormatTranslator#getConstraint()
87      */

88     protected ValidationConstraint getConstraint()
89     {
90         return ValidationConstraint.DATE_FORMAT;
91     }
92 }
93
Popular Tags