You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@wicket.apache.org by eh...@apache.org on 2007/04/06 00:37:53 UTC
svn commit: r525989 - in
/incubator/wicket/branches/wicket-1.x/jdk-1.4/wicket-datetime/src/main/java/wicket/datetime/markup/html/basic:
./ DateLabel.java
Author: ehillenius
Date: Thu Apr 5 15:37:52 2007
New Revision: 525989
URL: http://svn.apache.org/viewvc?view=rev&rev=525989
Log:
added DateLabel component
Added:
incubator/wicket/branches/wicket-1.x/jdk-1.4/wicket-datetime/src/main/java/wicket/datetime/markup/html/basic/
incubator/wicket/branches/wicket-1.x/jdk-1.4/wicket-datetime/src/main/java/wicket/datetime/markup/html/basic/DateLabel.java
Added: incubator/wicket/branches/wicket-1.x/jdk-1.4/wicket-datetime/src/main/java/wicket/datetime/markup/html/basic/DateLabel.java
URL: http://svn.apache.org/viewvc/incubator/wicket/branches/wicket-1.x/jdk-1.4/wicket-datetime/src/main/java/wicket/datetime/markup/html/basic/DateLabel.java?view=auto&rev=525989
==============================================================================
--- incubator/wicket/branches/wicket-1.x/jdk-1.4/wicket-datetime/src/main/java/wicket/datetime/markup/html/basic/DateLabel.java (added)
+++ incubator/wicket/branches/wicket-1.x/jdk-1.4/wicket-datetime/src/main/java/wicket/datetime/markup/html/basic/DateLabel.java Thu Apr 5 15:37:52 2007
@@ -0,0 +1,273 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package wicket.datetime.markup.html.basic;
+
+import java.text.SimpleDateFormat;
+
+import org.joda.time.DateTime;
+import org.joda.time.DateTimeZone;
+import org.joda.time.format.DateTimeFormat;
+
+import wicket.datetime.util.DateConverter;
+import wicket.datetime.util.PatternDateConverter;
+import wicket.datetime.util.StyleDateConverter;
+import wicket.markup.ComponentTag;
+import wicket.markup.MarkupStream;
+import wicket.markup.html.basic.Label;
+import wicket.model.IModel;
+import wicket.util.convert.IConverter;
+
+/**
+ * A label that is mapped to a <code>java.util.Date</code> object and that
+ * uses Joda time to format values.
+ * <p>
+ * You can provide a date pattern in two of the constructors. When not provided,
+ * {@link DateTimeFormat#shortDate()} will be used.
+ * </p>
+ * <p>
+ * A special option is applyTimeZoneDifference which is an option that says
+ * whether to correct for the difference between the client's time zone and
+ * server's time zone. This is true by default.
+ * </p>
+ *
+ * @see DateTime
+ * @see DateTimeFormat
+ * @see DateTimeZone
+ *
+ * @author eelcohillenius
+ */
+public class DateLabel extends Label
+{
+ private static final long serialVersionUID = 1L;
+
+ /**
+ * Creates a new DateLabel defaulting to using a short date pattern
+ *
+ * @param id
+ * The id of the text field
+ * @param model
+ * The model
+ * @param datePattern
+ * The pattern to use. Must be not null. See
+ * {@link SimpleDateFormat} for available patterns.
+ *
+ * @see wicket.markup.html.form.TextField
+ */
+ public static DateLabel forDatePattern(String id, IModel model, String datePattern)
+ {
+ return new DateLabel(id, model, new PatternDateConverter(datePattern, true));
+ }
+
+ /**
+ * Creates a new DateLabel defaulting to using a short date pattern
+ *
+ * @param id
+ * The id of the text field
+ * @param datePattern
+ * The pattern to use. Must be not null. See
+ * {@link SimpleDateFormat} for available patterns.
+ *
+ * @see wicket.markup.html.form.TextField
+ */
+ public static DateLabel forDatePattern(String id, String datePattern)
+ {
+ return forDatePattern(id, null, datePattern);
+ }
+
+ /**
+ * Creates a new DateLabel defaulting to using a short date pattern
+ *
+ * @param id
+ * The id of the text field
+ * @param model
+ * The model
+ * @param dateStyle
+ * style to use in case no pattern is provided. Must be two
+ * characters from the set {"S", "M", "L", "F", "-"}. Must be not
+ * null. See {@link DateTimeFormat#forStyle(String)} for options.
+ *
+ * @see wicket.markup.html.form.TextField
+ */
+ public static DateLabel forDateStyle(String id, IModel model, String dateStyle)
+ {
+ return new DateLabel(id, model, new StyleDateConverter(dateStyle, true));
+ }
+
+ /**
+ * Creates a new DateLabel defaulting to using a short date pattern
+ *
+ * @param id
+ * The id of the text field
+ * @param dateStyle
+ * style to use in case no pattern is provided. Must be two
+ * characters from the set {"S", "M", "L", "F", "-"}. Must be not
+ * null. See {@link DateTimeFormat#forStyle(String)} for options.
+ *
+ * @see wicket.markup.html.form.TextField
+ */
+ public static DateLabel forDateStyle(String id, String dateStyle)
+ {
+ return forDateStyle(id, null, dateStyle);
+ }
+
+ /**
+ * Creates a new DateLabel defaulting to using a short date pattern
+ *
+ * @param id
+ * The id of the text field
+ *
+ * @see wicket.markup.html.form.TextField
+ */
+ public static DateLabel forShortStyle(String id)
+ {
+ return forShortStyle(id, null);
+ }
+
+ /**
+ * Creates a new DateLabel defaulting to using a short date pattern
+ *
+ * @param id
+ * The id of the text field
+ * @param model
+ * The model
+ *
+ * @see wicket.markup.html.form.TextField
+ */
+ public static DateLabel forShortStyle(String id, IModel model)
+ {
+ return new DateLabel(id, model, new StyleDateConverter(true));
+ }
+
+ /**
+ * Creates a new DateLabel using the provided converter.
+ *
+ * @param id
+ * The id of the text field
+ * @param converter
+ * the date converter
+ *
+ * @see wicket.markup.html.form.TextField
+ */
+ public static DateLabel withConverter(String id, DateConverter converter)
+ {
+ return withConverter(id, null, converter);
+ }
+
+ /**
+ * Creates a new DateLabel using the provided converter.
+ *
+ * @param id
+ * The id of the text field
+ * @param model
+ * The model
+ * @param converter
+ * the date converter
+ *
+ * @see wicket.markup.html.form.TextField
+ */
+ public static DateLabel withConverter(String id, IModel model, DateConverter converter)
+ {
+ return new DateLabel(id, model, converter);
+ }
+
+ /** optionally prepend to label. */
+ private String after;
+
+ /** optionally append to label. */
+ private String before;
+
+ /**
+ * The converter for the Label
+ */
+ private final DateConverter converter;
+
+ /**
+ * Construct with a converter.
+ *
+ * @param The
+ * component id
+ * @param The
+ * model
+ * @param converter
+ * The converter to use
+ */
+ public DateLabel(String id, IModel model, DateConverter converter)
+ {
+ super(id, model);
+ if (converter == null)
+ {
+ throw new IllegalStateException("converter may not be null");
+ }
+ this.converter = converter;
+ }
+
+ /**
+ * @return after append to label or null
+ */
+ public String getAfter()
+ {
+ return after;
+ }
+
+ /**
+ * @return before prepend to label or null
+ */
+ public String getBefore()
+ {
+ return before;
+ }
+
+ /**
+ * Returns the specialized converter.
+ */
+ public IConverter getConverter(Class clazz)
+ {
+ return converter;
+ }
+
+ /**
+ * @param after
+ * append to label
+ */
+ public void setAfter(String after)
+ {
+ this.after = after;
+ }
+
+ /**
+ * @param before
+ * prepend to label
+ */
+ public void setBefore(String before)
+ {
+ this.before = before;
+ }
+
+ protected void onComponentTagBody(MarkupStream markupStream, ComponentTag openTag)
+ {
+ String s = getModelObjectAsString();
+ if (before != null)
+ {
+ s = before + s;
+ }
+ if (after != null)
+ {
+ s = s + after;
+ }
+ replaceComponentTagBody(markupStream, openTag, s);
+ }
+}