Okay, here's what worked for me:
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import wicket.markup.html.form.DropDownChoice;
import wicket.markup.html.form.FormComponent;
import wicket.markup.html.form.IChoiceRenderer;
import wicket.model.Model;
import java.sql.Time;
import java.util.Arrays;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;
public class TimeSelector extends FormComponent implements IChoiceRenderer {
static Log log = LogFactory.getLog(TimeSelector.class);
private static final List<String> hourList =
Arrays.asList(new
String[]{"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11",
"12"});
private static final List<String> minuteList =
Arrays.asList(new
String[]{"00", "05", "10", "15", "20", "25", "30", "35", "40", "45",
"50", "55"});
private static final List<String> amPmList =
Arrays.asList(new String[]{"AM", "PM"});
private DropDownChoice hours;
private DropDownChoice minutes;
private DropDownChoice amPm;
private Calendar c = Calendar.getInstance();
public TimeSelector(final String id, Time t) {
super(id);
Model hourModel = new Model(getValueFromModel(t, Calendar.HOUR));
hours = new DropDownChoice("hour", hourModel, hourList, this);
add(hours);
Model minuteModel = new Model(getValueFromModel(t, Calendar.MINUTE));
minutes = new DropDownChoice("minute", minuteModel, minuteList, this);
add(minutes);
Model amPmModel = new Model(getValueFromModel(t, Calendar.AM_PM));
amPm = new DropDownChoice("amPm", amPmModel, amPmList, this);
add(amPm);
}
private String getValueFromModel(Time t, int component) {
if (t == null) {
return "";
}
c.setTime(t);
switch (component) {
case Calendar.HOUR:
return Integer.toString(c.get(Calendar.HOUR));
case Calendar.MINUTE:
return Integer.toString(c.get(Calendar.MINUTE));
default:
return (c.get(Calendar.AM_PM) == Calendar.AM) ? "AM" : "PM";
}
}
public void updateModel() {
GregorianCalendar gc = new GregorianCalendar();
String hourValue = hours.getInput();
String minuteValue = minutes.getInput();
String amPmValue = amPm.getInput();
gc.set(Calendar.HOUR, Integer.parseInt(hourValue));
gc.set(Calendar.MINUTE, Integer.parseInt(minuteValue));
if (amPmValue.equals("AM")) {
gc.set(Calendar.AM_PM, Calendar.AM);
}
else {
gc.set(Calendar.AM_PM, Calendar.PM);
}
setModelObject(new Time(gc.getTime().getTime()));
}
public String getDisplayValue(Object object) {
return object.toString();
}
public String getIdValue(Object object, int i) {
return object.toString();
}
}
Adding it to the page:
add(new TimeSelector("endTime", s.getEndTime()));
And the markup:
<span wicket:id="endTime">
<select wicket:id="hour"></select>:<select wicket:id="minute"></select>
<select wicket:id="amPm"></select>
</span>
- [Wicket-user] Time Selection Component COMPLETE Nick Heudecker
- Re: [Wicket-user] Time Selection Component COMPLETE Michael Jouravlev
- Re: [Wicket-user] Time Selection Component COMP... Nick Heudecker
