001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.wicket.datetime.markup.html.form;
018:
019: import java.text.SimpleDateFormat;
020: import java.util.Date;
021:
022: import org.apache.wicket.datetime.DateConverter;
023: import org.apache.wicket.datetime.PatternDateConverter;
024: import org.apache.wicket.datetime.StyleDateConverter;
025: import org.apache.wicket.markup.html.form.TextField;
026: import org.apache.wicket.markup.html.form.AbstractTextComponent.ITextFormatProvider;
027: import org.apache.wicket.model.IModel;
028: import org.apache.wicket.util.convert.IConverter;
029: import org.joda.time.DateTime;
030: import org.joda.time.DateTimeZone;
031: import org.joda.time.format.DateTimeFormat;
032:
033: /**
034: * A TextField that is mapped to a <code>java.util.Date</code> object and that
035: * uses Joda time to parse and format values.
036: * <p>
037: * You should use on of the factory methods to construct the kind you want or
038: * use the public constructor and pass in the converter to use.
039: * </p>
040: * <p>
041: * This component tries to apply the time zone difference between the client and
042: * server. See the
043: * {@link DateConverter#getApplyTimeZoneDifference() date converter} of this
044: * package for more information on that.
045: * </p>
046: *
047: * @see StyleDateConverter
048: * @see DateTime
049: * @see DateTimeFormat
050: * @see DateTimeZone
051: *
052: * @author eelcohillenius
053: */
054: public class DateTextField extends TextField implements
055: ITextFormatProvider {
056:
057: private static final long serialVersionUID = 1L;
058:
059: /**
060: * Creates a new DateTextField defaulting to using a short date pattern
061: *
062: * @param id
063: * The id of the text field
064: * @param model
065: * The model
066: * @param datePattern
067: * The pattern to use. Must be not null. See
068: * {@link SimpleDateFormat} for available patterns.
069: *
070: * @see org.apache.wicket.markup.html.form.TextField
071: */
072: public static DateTextField forDatePattern(String id, IModel model,
073: String datePattern) {
074: return new DateTextField(id, model, new PatternDateConverter(
075: datePattern, true));
076: }
077:
078: /**
079: * Creates a new DateTextField defaulting to using a short date pattern
080: *
081: * @param id
082: * The id of the text field
083: * @param datePattern
084: * The pattern to use. Must be not null. See
085: * {@link SimpleDateFormat} for available patterns.
086: *
087: * @see org.apache.wicket.markup.html.form.TextField
088: */
089: public static DateTextField forDatePattern(String id,
090: String datePattern) {
091: return forDatePattern(id, null, datePattern);
092: }
093:
094: /**
095: * Creates a new DateTextField using the provided date style.
096: *
097: * @param id
098: * The id of the text field
099: * @param model
100: * The model
101: * @param dateStyle
102: * Date style to use. The first character is the date style, and
103: * the second character is the time style. Specify a character of
104: * 'S' for short style, 'M' for medium, 'L' for long, and 'F' for
105: * full. A date or time may be ommitted by specifying a style
106: * character '-'. See {@link DateTimeFormat#forStyle(String)}.
107: *
108: * @see org.apache.wicket.markup.html.form.TextField
109: */
110: public static DateTextField forDateStyle(String id, IModel model,
111: String dateStyle) {
112: return new DateTextField(id, model, new StyleDateConverter(
113: dateStyle, true));
114: }
115:
116: /**
117: * Creates a new DateTextField using the provided date style.
118: *
119: * @param id
120: * The id of the text field
121: * @param dateStyle
122: * Date style to use. The first character is the date style, and
123: * the second character is the time style. Specify a character of
124: * 'S' for short style, 'M' for medium, 'L' for long, and 'F' for
125: * full. A date or time may be ommitted by specifying a style
126: * character '-'. See {@link DateTimeFormat#forStyle(String)}.
127: *
128: * @see org.apache.wicket.markup.html.form.TextField
129: */
130: public static DateTextField forDateStyle(String id, String dateStyle) {
131: return forDateStyle(id, null, dateStyle);
132: }
133:
134: /**
135: * Creates a new DateTextField defaulting to using a short date pattern
136: *
137: * @param id
138: * The id of the text field
139: *
140: * @see org.apache.wicket.markup.html.form.TextField
141: */
142: public static DateTextField forShortStyle(String id) {
143: return forShortStyle(id, null);
144: }
145:
146: /**
147: * Creates a new DateTextField defaulting to using a short date pattern
148: *
149: * @param id
150: * The id of the text field
151: * @param model
152: * The model
153: *
154: * @see org.apache.wicket.markup.html.form.TextField
155: */
156: public static DateTextField forShortStyle(String id, IModel model) {
157: return new DateTextField(id, model,
158: new StyleDateConverter(true));
159: }
160:
161: /**
162: * Creates a new DateTextField using the provided converter.
163: *
164: * @param id
165: * The id of the text field
166: * @param converter
167: * the date converter
168: *
169: * @see org.apache.wicket.markup.html.form.TextField
170: */
171: public static DateTextField withConverter(String id,
172: DateConverter converter) {
173: return withConverter(id, null, converter);
174: }
175:
176: /**
177: * Creates a new DateTextField using the provided converter.
178: *
179: * @param id
180: * The id of the text field
181: * @param model
182: * The model
183: * @param converter
184: * the date converter
185: *
186: * @see org.apache.wicket.markup.html.form.TextField
187: */
188: public static DateTextField withConverter(String id, IModel model,
189: DateConverter converter) {
190: return new DateTextField(id, model, converter);
191: }
192:
193: /**
194: * The converter for the TextField
195: */
196: private final DateConverter converter;
197:
198: /**
199: * Construct with a converter.
200: *
201: * @param The
202: * component id
203: * @param The
204: * model
205: * @param converter
206: * The converter to use
207: */
208: public DateTextField(String id, IModel model,
209: DateConverter converter) {
210: super (id, model, Date.class);
211: if (converter == null) {
212: throw new IllegalStateException("converter may not be null");
213: }
214: converter.setComponent(this );
215: this .converter = converter;
216: }
217:
218: /**
219: * @return The specialized converter.
220: * @see org.apache.wicket.Component#getConverter(java.lang.Class)
221: */
222: public final IConverter getConverter(Class clazz) {
223: return converter;
224: }
225:
226: /**
227: * @see org.apache.wicket.markup.html.form.AbstractTextComponent.ITextFormatProvider#getTextFormat()
228: */
229: public final String getTextFormat() {
230: return converter.getDatePattern();
231: }
232: }
|