You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by hill180 <hi...@gmail.com> on 2009/06/08 21:21:34 UTC

Time Field

I understand there is the Datetimefield, but is there a field for just  
time.

-jose

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Time Field

Posted by Vladimir K <ko...@gmail.com>.
Does anyone know the converter for org.joda.time.Duration?
It is not trivial one taking into account the user's locale.
-- 
View this message in context: http://www.nabble.com/Time-Field-tp23930465p23931514.html
Sent from the Wicket - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Time Field

Posted by Martin Makundi <ma...@koodaripalvelut.com>.
Actually there is also timeconverter:

public class SqlTimeConverter extends AbstractConverter
{

	private static final long serialVersionUID = 1L;

	/** @see org.apache.wicket.util.convert.converters.DateConverter#convertToObject(java.lang.String,java.util.Locale)
*/
	public Time convertToObject(String value, Locale locale)
	{
		if (value == null)
		{
			return null;
		}
		if (locale == null)
		{
			locale = Locale.getDefault();
		}
		DateFormat format = DateFormat.getTimeInstance(DateFormat.SHORT, locale);
		try
		{
			Date date = format.parse(value);
			return new Time(date.getTime());
		}
		catch (ParseException e)
		{
			throw new ConversionException("Cannot parse '" + value + "' using
format " + format).setSourceValue(
				value)
				.setTargetType(getTargetType())
				.setConverter(this)
				.setLocale(locale);
		}
	}

	@Override
	public String convertToString(final Object value, Locale locale)
	{
		if (value == null)
		{
			return null;
		}
		if (locale == null)
		{
			locale = Locale.getDefault();
		}
		Time time = (Time)value;
		DateFormat format = DateFormat.getTimeInstance(DateFormat.SHORT, locale);
		return format.format(time);
	}

	@Override
	protected Class<Time> getTargetType()
	{
		return Time.class;
	}
}



2009/6/8 Martin Makundi <ma...@koodaripalvelut.com>:
> There used to be... the one for timestamp.. but in 1.4-rc4 you must
> make one yourself (by tweaking the converter).
>
> public class SqlTimestampTextField extends TextField<Timestamp> {
>
>  /**
>   * @param id
>   * @param modelObject
>   * @param property
>   * @param datePattern
>   */
>  public SqlTimestampTextField(String id, Object modelObject, String property,
>      String datePattern) {
>    this(id, new PropertyModel<Timestamp>(modelObject, property), datePattern);
>  }
>
>  /**
>   * @param id
>   * @param model
>   * @param datePattern
>   */
>  public SqlTimestampTextField(String id, IModel<Timestamp> model,
>      String datePattern) {
>    super(id, model, Timestamp.class);
>  }
>
>  /**
>   * @see org.apache.wicket.markup.html.form.FormComponent#getInput()
>   */
>  @Override
>  public String getInput() {
>    String value = super.getInput();
>
>    if (Utils.isEmptyWithTrim(value)) {
>      value = null;
>    }
>
>    if (value != null) {
>      value = value.replace('.', ':');
>      if ((!Utils.isEmpty(value)) && (value.indexOf(':') < 0)) {
>        value = new StringBuilder(value).append(":00").toString();
>      }
>    }
>    return value;
>  }
>
>  /**
>   * @see org.apache.wicket.Component#getConverter(java.lang.Class)
>   */
>  @Override
>  public IConverter getConverter(Class<?> type) {
>    if (type == Timestamp.class) {
>      return ParametrizedConverterLocator.get(type);
>    }
>
>    return super.getConverter(type);
>  }
>
>  /**
>   * @see org.apache.wicket.markup.html.form.AbstractTextComponent#isInputNullable()
>   */
>  @Override
>  public boolean isInputNullable() {
>    return true;
>  }
> }
>
>
> public class TimeToSqlTimestampConverter extends AbstractConverter
> {
>  private static final long serialVersionUID = 1L;
>
>  private final int dateFormat;
>
>  /**
>   * Construct.
>   */
>  public TimeToSqlTimestampConverter()
>  {
>    dateFormat = DateFormat.SHORT;
>  }
>
>  /**
>   * Construct.
>   *
>   * @param dateFormat
>   *            See java.text.DateFormat for details. Defaults to
> DateFormat.SHORT
>   */
>  public TimeToSqlTimestampConverter(int dateFormat)
>  {
>    this.dateFormat = dateFormat;
>  }
>
>  /**
>   *
>   * @see org.apache.wicket.util.convert.IConverter#convertToObject(java.lang.String,
>   *      java.util.Locale)
>   */
>  public Timestamp convertToObject(final String value, Locale locale)
>  {
>    if (value == null)
>    {
>      return null;
>    }
>
>    if (locale == null)
>    {
>      locale = Locale.getDefault();
>    }
>
>    DateFormat format = DateFormat.getTimeInstance(dateFormat, locale);
>    try
>    {
>      Date date = format.parse(value);
>      return new Timestamp(date.getTime());
>    }
>    catch (ParseException e)
>    {
>      throw newConversionException("Cannot parse '" + value + "' using
> format " + format,
>        value, locale);
>    }
>  }
>
>  /**
>   *
>   * @see org.apache.wicket.util.convert.converters.AbstractConverter#convertToString(java.lang.Object,
>   *      java.util.Locale)
>   */
>  @Override
>  public String convertToString(final Object value, Locale locale)
>  {
>    if (value == null)
>    {
>      return null;
>    }
>
>    if (locale == null)
>    {
>      locale = Locale.getDefault();
>    }
>
>    Timestamp timestamp = (Timestamp)value;
>    DateFormat format = DateFormat.getTimeInstance(dateFormat, locale);
>    return format.format(timestamp);
>  }
>
>  /**
>   *
>   * @see org.apache.wicket.util.convert.converters.AbstractConverter#getTargetType()
>   */
>  @Override
>  protected Class<Timestamp> getTargetType()
>  {
>    return Timestamp.class;
>  }
> }
>
>
> You can easily change the Timestamp to time..
>
> **
> Martin
>
> 2009/6/8 hill180 <hi...@gmail.com>:
>> I understand there is the Datetimefield, but is there a field for just time.
>>
>> -jose
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
>> For additional commands, e-mail: users-help@wicket.apache.org
>>
>>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Time Field

Posted by Martin Makundi <ma...@koodaripalvelut.com>.
There used to be... the one for timestamp.. but in 1.4-rc4 you must
make one yourself (by tweaking the converter).

public class SqlTimestampTextField extends TextField<Timestamp> {

  /**
   * @param id
   * @param modelObject
   * @param property
   * @param datePattern
   */
  public SqlTimestampTextField(String id, Object modelObject, String property,
      String datePattern) {
    this(id, new PropertyModel<Timestamp>(modelObject, property), datePattern);
  }

  /**
   * @param id
   * @param model
   * @param datePattern
   */
  public SqlTimestampTextField(String id, IModel<Timestamp> model,
      String datePattern) {
    super(id, model, Timestamp.class);
  }

  /**
   * @see org.apache.wicket.markup.html.form.FormComponent#getInput()
   */
  @Override
  public String getInput() {
    String value = super.getInput();

    if (Utils.isEmptyWithTrim(value)) {
      value = null;
    }

    if (value != null) {
      value = value.replace('.', ':');
      if ((!Utils.isEmpty(value)) && (value.indexOf(':') < 0)) {
        value = new StringBuilder(value).append(":00").toString();
      }
    }
    return value;
  }

  /**
   * @see org.apache.wicket.Component#getConverter(java.lang.Class)
   */
  @Override
  public IConverter getConverter(Class<?> type) {
    if (type == Timestamp.class) {
      return ParametrizedConverterLocator.get(type);
    }

    return super.getConverter(type);
  }

  /**
   * @see org.apache.wicket.markup.html.form.AbstractTextComponent#isInputNullable()
   */
  @Override
  public boolean isInputNullable() {
    return true;
  }
}


public class TimeToSqlTimestampConverter extends AbstractConverter
{
  private static final long serialVersionUID = 1L;

  private final int dateFormat;

  /**
   * Construct.
   */
  public TimeToSqlTimestampConverter()
  {
    dateFormat = DateFormat.SHORT;
  }

  /**
   * Construct.
   *
   * @param dateFormat
   *            See java.text.DateFormat for details. Defaults to
DateFormat.SHORT
   */
  public TimeToSqlTimestampConverter(int dateFormat)
  {
    this.dateFormat = dateFormat;
  }

  /**
   *
   * @see org.apache.wicket.util.convert.IConverter#convertToObject(java.lang.String,
   *      java.util.Locale)
   */
  public Timestamp convertToObject(final String value, Locale locale)
  {
    if (value == null)
    {
      return null;
    }

    if (locale == null)
    {
      locale = Locale.getDefault();
    }

    DateFormat format = DateFormat.getTimeInstance(dateFormat, locale);
    try
    {
      Date date = format.parse(value);
      return new Timestamp(date.getTime());
    }
    catch (ParseException e)
    {
      throw newConversionException("Cannot parse '" + value + "' using
format " + format,
        value, locale);
    }
  }

  /**
   *
   * @see org.apache.wicket.util.convert.converters.AbstractConverter#convertToString(java.lang.Object,
   *      java.util.Locale)
   */
  @Override
  public String convertToString(final Object value, Locale locale)
  {
    if (value == null)
    {
      return null;
    }

    if (locale == null)
    {
      locale = Locale.getDefault();
    }

    Timestamp timestamp = (Timestamp)value;
    DateFormat format = DateFormat.getTimeInstance(dateFormat, locale);
    return format.format(timestamp);
  }

  /**
   *
   * @see org.apache.wicket.util.convert.converters.AbstractConverter#getTargetType()
   */
  @Override
  protected Class<Timestamp> getTargetType()
  {
    return Timestamp.class;
  }
}


You can easily change the Timestamp to time..

**
Martin

2009/6/8 hill180 <hi...@gmail.com>:
> I understand there is the Datetimefield, but is there a field for just time.
>
> -jose
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org