001// Copyright 2004, 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
015package org.apache.tapestry.contrib.valid;
016
017import java.text.DateFormat;
018import java.util.Date;
019
020import org.apache.tapestry.valid.DateValidator;
021import org.apache.tapestry.valid.IValidator;
022import org.apache.tapestry.valid.ValidField;
023
024/**
025 * Backwards compatible version of the 1.0.7 DateField component. <table border=1>
026 * <tr>
027 * <td>Parameter</td>
028 * <td>Type</td>
029 * <td>Read / Write</td>
030 * <td>Required</td>
031 * <td>Default</td>
032 * <td>Description</td>
033 * </tr>
034 * <tr>
035 * <td>date</td>
036 * <td>java.util.Date</td>
037 * <td>R / W</td>
038 * <td>yes</td>
039 * <td>&nbsp;</td>
040 * <td>The date property to edit.</td>
041 * </tr>
042 * <tr>
043 * <td>required</td>
044 * <td>boolean</td>
045 * <td>R</td>
046 * <td>no</td>
047 * <td>no</td>
048 * <td>If true, then a value must be entered.</td>
049 * </tr>
050 * <tr>
051 * <td>minimum</td>
052 * <td>java.util.Date</td>
053 * <td>R</td>
054 * <td>no</td>
055 * <td>&nbsp;</td>
056 * <td>If provided, the date entered must be equal to or later than the provided minimum date.
057 * </td>
058 * </tr>
059 * <tr>
060 * <td>maximum</td>
061 * <td>java.util.Date</td>
062 * <td>R</td>
063 * <td>no</td>
064 * <td>&nbsp;</td>
065 * <td>If provided, the date entered must be less than or equal to the provided maximum date.</td>
066 * </tr>
067 * <tr>
068 * <td>displayName</td>
069 * <td>String</td>
070 * <td>R</td>
071 * <td>yes</td>
072 * <td>&nbsp;</td>
073 * <td>A textual name for the field that is used when formulating error messages.</td>
074 * </tr>
075 * <tr>
076 * <td>format</td>
077 * <td>{@link DateFormat}</td>
078 * <td>R</td>
079 * <td>no</td>
080 * <td>Default format <code>MM/dd/yyyy</code></td>
081 * <td>The format used to display and parse dates.</td>
082 * </tr>
083 * <tr>
084 * <td>displayFormat</td>
085 * <td>{@link String}</td>
086 * <td>R</td>
087 * <td>no</td>
088 * <td><code>MM/DD/YYYY</code></td>
089 * <td>The format string presented to the user if the date entered is in an incorrect format. e.g.
090 * the format object throws a ParseException.</td>
091 * </tr>
092 * </table>
093 * <p>
094 * Informal parameters are allowed. A body is not allowed.
095 * 
096 * @author Howard Lewis Ship, Richard Lewis-Shell
097 * @since 1.0.8
098 * @see ValidField
099 */
100
101public abstract class DateField extends ValidField
102{
103    public abstract Date getDate();
104
105    public abstract void setDate(Date date);
106
107    public abstract Date getMinimum();
108
109    public abstract Date getMaximum();
110
111    public abstract boolean isRequired();
112
113    public abstract DateFormat getFormat();
114
115    public abstract String getDisplayFormat();
116
117    /**
118     * Overrides {@link ValidField#getValidator()}to construct a validator on-the-fly.
119     */
120
121    public IValidator getValidator()
122    {
123        DateValidator validator = new DateValidator();
124
125        if (isParameterBound("minimum"))
126            validator.setMinimum(getMinimum());
127
128        if (isParameterBound("maximum"))
129            validator.setMaximum(getMaximum());
130
131        if (isParameterBound("required"))
132            validator.setRequired(isRequired());
133
134        if (isParameterBound("format"))
135            validator.setFormat(getFormat());
136
137        if (isParameterBound("displayFormat"))
138            validator.setDisplayFormat(getDisplayFormat());
139
140        return validator;
141    }
142
143    /**
144     * @see org.apache.tapestry.valid.ValidField#getValue()
145     */
146    public Object getValue()
147    {
148        return getDate();
149    }
150
151    /**
152     * @see org.apache.tapestry.valid.ValidField#setValue(java.lang.Object)
153     */
154    public void setValue(Object value)
155    {
156        setDate((Date) value);
157    }
158
159}