com.databasesandlife.util.wicket.TimeTextField Maven / Gradle / Ivy
Show all versions of java-common Show documentation
package com.databasesandlife.util.wicket;
import java.sql.Time;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.Locale;
import org.apache.wicket.markup.ComponentTag;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.IModel;
import org.apache.wicket.util.convert.ConversionException;
import org.apache.wicket.util.convert.IConverter;
/**
* <input type="time">
field.
*
* Model may be {@link java.sql.Time} or Java 8 {@link java.time.LocalTime}.
* (Jodatime is not supported as this library is Java 8 and thus Java 8 time should be used in preference.)
*
*
For those browsers which do not support <input type="time">
,
* which display a normal <input type="text">
* instead, for example Firefox at the time of writing, we show a placeholder text, and display an error in case the format is wrong.
*
* @author This source is copyright Adrian Smith and licensed under the LGPL 3.
* @see Project on GitHub
*/
@SuppressWarnings("serial")
public class TimeTextField extends TextFieldWithType {
public TimeTextField(String id, IModel model) {
super(id, "time", model);
}
protected LocalTime parse(String str) {
try {
if (str == null) return null;
else return LocalTime.parse(str, DateTimeFormatter.ofPattern("HH:mm"));
}
catch (DateTimeParseException e) {
throw new ConversionException(e).setResourceKey("TimeTextField.invalid").setSourceValue(str);
}
}
@SuppressWarnings("unchecked")
@Override
public IConverter getConverter(Class type) {
if (type.equals(Time.class)) return (IConverter) new IConverter