001 // Copyright 2005 The Apache Software Foundation
002 //
003 // Licensed under the Apache License, Version 2.0 (the "License");
004 // you may not use this file except in compliance with the License.
005 // You may obtain a copy of the License at
006 //
007 // http://www.apache.org/licenses/LICENSE-2.0
008 //
009 // Unless required by applicable law or agreed to in writing, software
010 // distributed under the License is distributed on an "AS IS" BASIS,
011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012 // See the License for the specific language governing permissions and
013 // limitations under the License.
014
015 package org.apache.tapestry.form.translator;
016
017 import java.text.DateFormatSymbols;
018 import java.text.Format;
019 import java.text.SimpleDateFormat;
020 import java.util.Locale;
021
022 import org.apache.tapestry.valid.ValidationConstraint;
023 import org.apache.tapestry.valid.ValidationStrings;
024
025 /**
026 * A {@link java.text.SimpleDateFormat}-based {@link Translator} implementation.
027 *
028 * @author Paul Ferraro
029 * @since 4.0
030 */
031 public class DateTranslator extends FormatTranslator
032 {
033
034 public DateTranslator()
035 {
036 }
037
038 // Needed until HIVEMIND-134 fix is available
039 public DateTranslator(String initializer)
040 {
041 super(initializer);
042 }
043
044 /**
045 * @see org.apache.tapestry.form.translator.FormatTranslator#defaultPattern()
046 */
047 protected String defaultPattern()
048 {
049 return "MM/dd/yyyy";
050 }
051
052 /**
053 * @see org.apache.tapestry.form.translator.FormatTranslator#getFormat(java.util.Locale)
054 */
055 protected Format getFormat(Locale locale)
056 {
057 return getDateFormat(locale);
058 }
059
060 public SimpleDateFormat getDateFormat(Locale locale)
061 {
062 return new SimpleDateFormat(getPattern(), new DateFormatSymbols(locale));
063 }
064
065 /**
066 * @see org.apache.tapestry.form.translator.FormatTranslator#getMessageKey()
067 */
068 protected String getMessageKey()
069 {
070 return ValidationStrings.INVALID_DATE;
071 }
072
073 /**
074 * @see org.apache.tapestry.form.translator.AbstractTranslator#getMessageParameters(java.util.Locale,
075 * java.lang.String)
076 */
077 protected Object[] getMessageParameters(Locale locale, String label)
078 {
079 String pattern = getDateFormat(locale).toLocalizedPattern().toUpperCase(locale);
080
081 return new Object[]
082 { label, pattern };
083 }
084
085 /**
086 * @see org.apache.tapestry.form.translator.FormatTranslator#getConstraint()
087 */
088 protected ValidationConstraint getConstraint()
089 {
090 return ValidationConstraint.DATE_FORMAT;
091 }
092 }