Paket för egna komponenter. Skapade en för datum och tid

git-svn-id: svn://svn.dsv.su.se/scipro/scipro/trunk@96 73ecded7-942e-4092-bab0-0e58ef0ee984
This commit is contained in:
dan-kjel 2011-02-11 08:00:24 +00:00
parent c2f9b631e6
commit 33746200a0
2 changed files with 282 additions and 0 deletions
src/main/java/se/su/dsv/scipro/components

@ -0,0 +1,7 @@
<wicket:panel>
<span style="white-space: nowrap;">
<input type="text" wicket:id="date" size="12" />
<input type="text" wicket:id="hours" size="2" />&nbsp;:
<input type="text" wicket:id="minutes" size="2" />
</span>
</wicket:panel>

@ -0,0 +1,275 @@
package se.su.dsv.scipro.components;
import java.util.Date;
import java.util.TimeZone;
import org.apache.wicket.Session;
import org.apache.wicket.datetime.DateConverter;
import org.apache.wicket.markup.html.form.FormComponentPanel;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.protocol.http.request.WebClientInfo;
import org.apache.wicket.request.ClientInfo;
import org.apache.wicket.util.convert.IConverter;
import org.apache.wicket.util.convert.converters.ZeroPaddingIntegerConverter;
import org.apache.wicket.validation.validator.RangeValidator;
import org.joda.time.DateTimeFieldType;
import org.joda.time.DateTimeZone;
import org.joda.time.MutableDateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.odlabs.wiquery.ui.datepicker.DatePicker;
/**
*
* @author Dan Kjellman
*
* Most of the code comes from the org.apache.wicket.extensions.yui.calendar.DateTimeField
*
*/
public class CustomDateTimeField extends FormComponentPanel<Date> {
private static final long serialVersionUID = 1L;
private static final IConverter MINUTES_CONVERTER = new ZeroPaddingIntegerConverter(2);
private MutableDateTime date;
private DatePicker<Date> dateField;
private Integer hours;
private TextField<Integer> hoursField;
private Integer minutes;
private TextField<Integer> minutesField;
public CustomDateTimeField(String id) {
this(id, null);
}
public CustomDateTimeField(String id, IModel<Date> model) {
super(id, model);
setType(Date.class);
PropertyModel<Date> dateFieldModel = new PropertyModel<Date>(this, "date");
dateField = new DatePicker<Date>("date", dateFieldModel){
private static final long serialVersionUID = 1L;
@Override
public final IConverter getConverter(Class<?> type){
return new DateConverter(true){
private static final long serialVersionUID = 1L;
@Override
public String getDatePattern() {
return "yy-dd-mm";
}
@Override
protected DateTimeFormatter getFormat() {
return DateTimeFormat.forPattern("YYYY-MM-dd");
}
};
}
};
dateField.setDateFormat("yy-mm-dd");
add(dateField);
add(hoursField = new TextField<Integer>("hours", new PropertyModel<Integer>(this, "hours"),
Integer.class));
hoursField.add(new HoursValidator());
hoursField.setLabel(new Model<String>("hours"));
add(minutesField = new TextField<Integer>("minutes", new PropertyModel<Integer>(this,
"minutes"), Integer.class) {
private static final long serialVersionUID = 1L;
public IConverter getConverter(Class<?> type) {
return MINUTES_CONVERTER;
}
});
minutesField.add(new RangeValidator<Integer>(0, 59));
minutesField.setLabel(new Model<String>("minutes"));
}
/**
* Gets date.
*
* @return date
*/
public Date getDate() {
return (date != null) ? date.toDate() : null;
}
/**
* Gets hours.
*
* @return hours
*/
public Integer getHours() {
return hours;
}
/**
* Gets minutes.
*
* @return minutes
*/
public Integer getMinutes() {
return minutes;
}
/**
* Sets hours.
*
* @param hours
* hours
*/
public void setHours(Integer hours) {
this.hours = hours;
}
/**
* Sets minutes.
*
* @param minutes
* minutes
*/
public void setMinutes(Integer minutes) {
this.minutes = minutes;
}
/**
* Sets date.
*
* @param date
* date
*/
public void setDate(Date date) {
if (date == null) {
this.date = null;
setDefaultModelObject(null);
setHours(null);
setMinutes(null);
return;
}
this.date = new MutableDateTime(date);
setDefaultModelObject(date);
Integer hours = getHours();
Integer minutes = getMinutes();
if (hours != null) {
this.date.set(DateTimeFieldType.hourOfDay(), hours.intValue());
this.date.setMinuteOfHour((minutes != null) ? minutes.intValue() : 0);
}
setDefaultModelObject(this.date.toDate());
}
/**
* Gets the client's time zone.
*
* @return The client's time zone or null
*/
protected TimeZone getClientTimeZone() {
ClientInfo info = Session.get().getClientInfo();
if (info instanceof WebClientInfo)
{
return ((WebClientInfo)info).getProperties().getTimeZone();
}
return null;
}
@Override
protected void convertInput() {
Object dateFieldInput = dateField.getConvertedInput();
if (dateFieldInput != null) {
MutableDateTime date = new MutableDateTime(dateFieldInput);
Integer hours = hoursField.getConvertedInput();
Integer minutes = minutesField.getConvertedInput();
try{
if (hours != null){
date.set(DateTimeFieldType.hourOfDay(), hours.intValue());
date.setMinuteOfHour((minutes != null) ? minutes.intValue() : 0);
}
TimeZone zone = getClientTimeZone();
if (zone != null){
date.setMillis(getMillis(zone, TimeZone.getDefault(), date.getMillis()));
}
// the date will be in the server's timezone
setConvertedInput(date.toDate());
}
catch (RuntimeException e){
CustomDateTimeField.this.error(e.getMessage());
invalid();
}
}
else{
setConvertedInput(null);
}
}
private long getMillis(TimeZone to, TimeZone from, long instant) {
return DateTimeZone.forTimeZone(from).getMillisKeepLocal(DateTimeZone.forTimeZone(to),
instant);
}
/**
* @see org.apache.wicket.Component#onBeforeRender()
*/
@Override
protected void onBeforeRender() {
dateField.setRequired(isRequired());
hoursField.setRequired(isRequired());
minutesField.setRequired(isRequired());
Date d = (Date)getDefaultModelObject();
if (d != null) {
date = new MutableDateTime(d);
} else {
date = null;
hours = null;
minutes = null;
}
if (date != null) {
// convert date to the client's time zone if we have that info
TimeZone zone = getClientTimeZone();
// instantiate with the previously set date
if (zone != null) {
date.setMillis(getMillis(TimeZone.getDefault(), zone, date.getMillis()));
}
hours = new Integer(date.get(DateTimeFieldType.hourOfDay()));
minutes = new Integer(date.getMinuteOfHour());
}
super.onBeforeRender();
}
private class HoursValidator extends RangeValidator<Integer> {
private static final long serialVersionUID = 1L;
/**
* Constructor
*/
public HoursValidator(){
setRange(0,23);
}
}
}