001 /*
002 * Databinder: a simple bridge from Wicket to Hibernate
003 * Copyright (C) 2006 Nathan Hamblen nathan@technically.us
004
005 * This library is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU Lesser General Public
007 * License as published by the Free Software Foundation; either
008 * version 2.1 of the License, or (at your option) any later version.
009 *
010 * This library is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013 * Lesser General Public License for more details.
014 *
015 * You should have received a copy of the GNU Lesser General Public
016 * License along with this library; if not, write to the Free Software
017 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
018 */
019 package net.databinder.components;
020
021 import java.text.DateFormat;
022 import java.text.SimpleDateFormat;
023 import java.util.Date;
024 import java.util.Locale;
025
026 import org.apache.wicket.model.IModel;
027 import org.apache.wicket.util.convert.converters.AbstractConverter;
028
029 /**
030 * Label with a specific date format (rather than a globally defined one).
031 * @deprecated use DateLabel from wicket-datetime
032 * @author Nathan Hamblen
033 */
034 public class DateLabel extends CustomLabel {
035 /**
036 * @param id Wicket id (must map to a Date property)
037 * @param formatString SimpleDateFormat format string
038 * @see SimpleDateFormat
039 */
040 public DateLabel(String id, String formatString) {
041 super(id, new DateConverter(new SimpleDateFormat(formatString)));
042 }
043
044 public DateLabel(String id, DateFormat format) {
045 super(id, new DateConverter(format));
046 }
047
048 /**
049 * @param id Wicket id
050 * @param model source model (must be a Date)
051 * @param formatString SimpleDateFormat format string
052 * @see SimpleDateFormat
053 */
054 public DateLabel(String id, IModel model, String formatString) {
055 super(id, model, new DateConverter(new SimpleDateFormat(formatString)));
056 }
057
058 public DateLabel(String id, IModel model, DateFormat dateFormat) {
059 super(id, model, new DateConverter(dateFormat));
060
061 }
062 /**
063 * Format Date as a String determined by its configuration.
064 */
065 protected static class DateConverter extends AbstractConverter {
066 private DateFormat df;
067 public DateConverter(DateFormat df) {
068 this.df = df;
069 }
070 public Object convertToObject(final String value, Locale locale)
071 {
072 return parse(df, value, locale);
073 }
074 @Override
075 protected Class getTargetType() {
076 return Date.class;
077 }
078 public String convertToString(Object source, Locale locale) {
079 if (source instanceof Date)
080 try{
081 return df.format((Date) source);
082 } catch (Exception e) {
083 throw new RuntimeException(e);
084 }
085 throw new UnsupportedOperationException("Can only convert Dates to Strings");
086 }
087 }
088 }