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.basic;
018:
019: import java.text.SimpleDateFormat;
020:
021: import org.apache.wicket.datetime.DateConverter;
022: import org.apache.wicket.datetime.PatternDateConverter;
023: import org.apache.wicket.datetime.StyleDateConverter;
024: import org.apache.wicket.markup.ComponentTag;
025: import org.apache.wicket.markup.MarkupStream;
026: import org.apache.wicket.markup.html.basic.Label;
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 label that is mapped to a <code>java.util.Date</code> object and that
035: * uses Joda time to format values.
036: * <p>
037: * You can provide a date pattern in two of the constructors. When not provided,
038: * {@link DateTimeFormat#shortDate()} will be used.
039: * </p>
040: * <p>
041: * A special option is applyTimeZoneDifference which is an option that says
042: * whether to correct for the difference between the client's time zone and
043: * server's time zone. This is true by default.
044: * </p>
045: *
046: * @see DateTime
047: * @see DateTimeFormat
048: * @see DateTimeZone
049: *
050: * @author eelcohillenius
051: */
052: public class DateLabel extends Label {
053: private static final long serialVersionUID = 1L;
054:
055: /**
056: * Creates a new DateLabel defaulting to using a short date pattern
057: *
058: * @param id
059: * The id of the text field
060: * @param model
061: * The model
062: * @param datePattern
063: * The pattern to use. Must be not null. See
064: * {@link SimpleDateFormat} for available patterns.
065: *
066: * @see org.apache.wicket.markup.html.form.TextField
067: */
068: public static DateLabel forDatePattern(String id, IModel model,
069: String datePattern) {
070: return new DateLabel(id, model, new PatternDateConverter(
071: datePattern, true));
072: }
073:
074: /**
075: * Creates a new DateLabel defaulting to using a short date pattern
076: *
077: * @param id
078: * The id of the text field
079: * @param datePattern
080: * The pattern to use. Must be not null. See
081: * {@link SimpleDateFormat} for available patterns.
082: *
083: * @see org.apache.wicket.markup.html.form.TextField
084: */
085: public static DateLabel forDatePattern(String id, String datePattern) {
086: return forDatePattern(id, null, datePattern);
087: }
088:
089: /**
090: * Creates a new DateLabel defaulting to using a short date pattern
091: *
092: * @param id
093: * The id of the text field
094: * @param model
095: * The model
096: * @param dateStyle
097: * style to use in case no pattern is provided. Must be two
098: * characters from the set {"S", "M", "L", "F", "-"}. Must be not
099: * null. See {@link DateTimeFormat#forStyle(String)} for options.
100: *
101: * @see org.apache.wicket.markup.html.form.TextField
102: */
103: public static DateLabel forDateStyle(String id, IModel model,
104: String dateStyle) {
105: return new DateLabel(id, model, new StyleDateConverter(
106: dateStyle, true));
107: }
108:
109: /**
110: * Creates a new DateLabel defaulting to using a short date pattern
111: *
112: * @param id
113: * The id of the text field
114: * @param dateStyle
115: * style to use in case no pattern is provided. Must be two
116: * characters from the set {"S", "M", "L", "F", "-"}. Must be not
117: * null. See {@link DateTimeFormat#forStyle(String)} for options.
118: *
119: * @see org.apache.wicket.markup.html.form.TextField
120: */
121: public static DateLabel forDateStyle(String id, String dateStyle) {
122: return forDateStyle(id, null, dateStyle);
123: }
124:
125: /**
126: * Creates a new DateLabel defaulting to using a short date pattern
127: *
128: * @param id
129: * The id of the text field
130: *
131: * @see org.apache.wicket.markup.html.form.TextField
132: */
133: public static DateLabel forShortStyle(String id) {
134: return forShortStyle(id, null);
135: }
136:
137: /**
138: * Creates a new DateLabel defaulting to using a short date pattern
139: *
140: * @param id
141: * The id of the text field
142: * @param model
143: * The model
144: *
145: * @see org.apache.wicket.markup.html.form.TextField
146: */
147: public static DateLabel forShortStyle(String id, IModel model) {
148: return new DateLabel(id, model, new StyleDateConverter(true));
149: }
150:
151: /**
152: * Creates a new DateLabel using the provided converter.
153: *
154: * @param id
155: * The id of the text field
156: * @param converter
157: * the date converter
158: *
159: * @see org.apache.wicket.markup.html.form.TextField
160: */
161: public static DateLabel withConverter(String id,
162: DateConverter converter) {
163: return withConverter(id, null, converter);
164: }
165:
166: /**
167: * Creates a new DateLabel using the provided converter.
168: *
169: * @param id
170: * The id of the text field
171: * @param model
172: * The model
173: * @param converter
174: * the date converter
175: *
176: * @see org.apache.wicket.markup.html.form.TextField
177: */
178: public static DateLabel withConverter(String id, IModel model,
179: DateConverter converter) {
180: return new DateLabel(id, model, converter);
181: }
182:
183: /** optionally prepend to label. */
184: private String after;
185:
186: /** optionally append to label. */
187: private String before;
188:
189: /**
190: * The converter for the Label
191: */
192: private final DateConverter converter;
193:
194: /**
195: * Construct with a converter.
196: *
197: * @param The
198: * component id
199: * @param The
200: * model
201: * @param converter
202: * The converter to use
203: */
204: public DateLabel(String id, IModel model, DateConverter converter) {
205: super (id, model);
206: if (converter == null) {
207: throw new IllegalStateException("converter may not be null");
208: }
209: converter.setComponent(this );
210: this .converter = converter;
211: }
212:
213: /**
214: * @return after append to label or null
215: */
216: public String getAfter() {
217: return after;
218: }
219:
220: /**
221: * @return before prepend to label or null
222: */
223: public String getBefore() {
224: return before;
225: }
226:
227: /**
228: * Returns the specialized converter.
229: */
230: public IConverter getConverter(Class clazz) {
231: return converter;
232: }
233:
234: /**
235: * @param after
236: * append to label
237: */
238: public void setAfter(String after) {
239: this .after = after;
240: }
241:
242: /**
243: * @param before
244: * prepend to label
245: */
246: public void setBefore(String before) {
247: this .before = before;
248: }
249:
250: protected void onComponentTagBody(MarkupStream markupStream,
251: ComponentTag openTag) {
252: String s = getModelObjectAsString();
253: if (before != null) {
254: s = before + s;
255: }
256: if (after != null) {
257: s = s + after;
258: }
259: replaceComponentTagBody(markupStream, openTag, s);
260: }
261: }
|