All Downloads are FREE. Search and download functionalities are using the official Maven repository.

wicket.extensions.markup.html.form.DateTextField Maven / Gradle / Ivy

package wicket.extensions.markup.html.form;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import wicket.markup.html.form.TextField;
import wicket.model.IModel;
import wicket.util.convert.Converter;
import wicket.util.convert.IConverter;
import wicket.util.convert.converters.AbstractConverter;
import wicket.util.convert.converters.StringConverter;

/**
 * A TextField that is mapped to a java.util.Date object.
 * 
 * If you provide a SimpleDateFormat pattern, it will both parse
 * and validate the text field according to it.
 * 
 * If you don't, it is the same as creating a TextField with
 * java.util.Date as it's type (it will get the pattern
 * from the user's locale)
 * 
 * @author Stefan Kanev
 *
 */
public class DateTextField extends TextField
{

	private static final long serialVersionUID = 1L;

	/**
	 * The date pattern of the text field
	 */
	private String datePattern = null;
	
	/**
	 * The converter for the TextField
	 */
	private IConverter converter = null;
	
	/**
	 * Creates a new DateTextField, without a specified pattern. This
	 * is the same as calling new TextField(id, Date.class)
	 *  
	 * @param id The id of the text field
	 * 
	 * @see wicket.markup.html.form.TextField
	 */
	public DateTextField(String id)
	{
		super(id, Date.class);
	}

	/**
	 * Creates a new DateTextField, without a specified pattern. This
	 * is the same as calling new TextField(id, object, Date.class)
	 * 
	 * @param id The id of the text field
	 * @param object The model
	 * 
	 * @see wicket.markup.html.form.TextField
	 */
	public DateTextField(String id, IModel object)
	{
		super(id, object, Date.class);
	}

	/**
	 * Creates a new DateTextField bound with a specific 
	 * SimpleDateFormat pattern.
	 * 
	 * @param id The id of the text field
	 * @param datePattern A SimpleDateFormat pattern
	 * 
	 * @see wicket.markup.html.form.TextField
	 */
	public DateTextField(String id, String datePattern)
	{
		super(id, Date.class);
		this.datePattern = datePattern;
		this.converter = new DateTextFieldConverter();
	}
	
	/**
	 * Creates a new DateTextField bound with a specific 
	 * SimpleDateFormat pattern.
	 * 
	 * @param id The id of the text field
	 * @param object The model
	 * @param datePattern A SimpleDateFormat pattern
	 * 
	 * @see wicket.markup.html.form.TextField
	 */
	public DateTextField(String id, IModel object, String datePattern)
	{
		super(id, object, Date.class);
		this.datePattern = datePattern;
		this.converter = new DateTextFieldConverter();
	}

	/**
	 * Returns the default converter if created without pattern; otherwise it
	 * returns a pattern-specific converter.
	 * 
	 * @return A pattern-specific converter
	 * 
	 * @see wicket.markup.html.form.TextField
	 */
	public IConverter getConverter()
	{
		if (converter == null) 
		{ 
			return super.getConverter();
		} else 
		{
			return converter;
		}
	}
	
	/**
	 * Converts String to java.util.Date and back
	 * via the datePattern in the inner class
	 * 
	 * @author Stefan Kanev, [email protected]
	 *
	 */
	public final class DateTextFieldConverter extends Converter 
	{

		private static final long serialVersionUID = 1L;
		
		/**
		 * Creates an instance, setting 
		 * DateToStringPatternConverter and
		 * StringPatternToDateConverter as it is appropriate.
		 */
		public DateTextFieldConverter() 
		{
			super(getSession().getLocale());
			
			StringConverter stringConverter = new StringConverter();
			stringConverter.set(Date.class, new DateToStringPatternConverter());
			
			set(String.class, stringConverter);
			set(Date.class, new StringPatternToDateConverter());
		}
		
	}
	
	/**
	 * Converts a java.util.Date to String using
     * the the pattern in DateTextField
	 * 
	 * @author Stefan Kanev
	 *
	 */
	public final class DateToStringPatternConverter extends AbstractConverter
	{
		
		private static final long serialVersionUID = 1L;

		/**
		 * Converts a java.util.Date to String using
		 * the the pattern in DateTextField
		 * 
		 * @param value A java.util.Date object to parse
		 * @param locale The user locale (unused)
		 * @return The given value as string
		 */
		public Object convert(Object value, Locale locale)
		{
			if (!(value instanceof Date)) return null;
		
			SimpleDateFormat dateFormat = new SimpleDateFormat(datePattern);
			String result = dateFormat.format((Date) value);
			
			return result;
		}

		protected Class getTargetType()
		{
			return String.class;
		}
		
	}

	/**
	 * Parses a java.util.Date from a String
	 * 
	 * @author Stefan Kanev, [email protected]
	 *
	 */
	public final class StringPatternToDateConverter extends AbstractConverter 
	{

		private static final long serialVersionUID = 1L;

		/**
		 * Parses a java.util.Date from a String
		 * 
		 * @param value A date to parse
		 * @param locale User locale (rather unused)
		 * @return The date formatted as string according to the set pattern
		 */
		public Object convert(Object value, Locale locale)
		{
			SimpleDateFormat dateFormat = new SimpleDateFormat(datePattern);
			
			return parse(dateFormat, value);
		}

		protected Class getTargetType()
		{
			return Date.class;
		}
	}
	
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy