I'm trying to connect a Wicket component DateTextField
with a property of type java.time.LocalDate
.
Following the hints in this answer, I've tried to implement a custom CompoundPropertyModel
which contains a java.time.Localdate
property. I'm using Wicket 7.6 and JDK 8.
I'm new to Wicket and Java generics.
This is my code:
Class LocaldateModel
import java.time.*;
import java.util.Date;
import org.apache.wicket.model.IModel;
public class LocalDateModel implements IModel<Date>
{
private static final long serialVersionUID = 7262517323706786573L;
private IModel<LocalDate> localDateModel;
public LocalDateModel(IModel<LocalDate> localDateModel)
{
this.localDateModel = localDateModel;
}
@Override
public Date getObject()
{
return Date.from(localDateModel.getObject().atStartOfDay(ZoneId.systemDefault()).toInstant());
}
@Override
public void setObject(Date object)
{
localDateModel.setObject(
Instant.ofEpochMilli(object.getTime()).atZone(ZoneId.systemDefault()).toLocalDate());
}
@Override
public void detach()
{
localDateModel.detach();
}
}
Class LocalDateModelButAlsoWrapping
import java.time.Localdate;
import java.util.Date;
public class LocalDateModelButAlsoWrapping<T> extends LocalDateModel implements IWrapModel<Date>
{
private static final long serialVersionUID = -8539316259078354206L;
private IModel<Date> dateModel;
public LocalDateModelButAlsoWrapping(IModel<LocalDate> localDateModel)
{
super(localDateModel);
LocalDate localDate=localDateModel.getObject();
dateModel.setObject(Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant()));
}
@Override
public IModel<Date> getWrappedModel()
{
return dateModel;
}
@Override
public void setObject(Date object)
{
dateModel.setObject(object);
}
}
Class MyLocalDateCompoundPropertyModel
import java.time.Localdate;
import org.apache.wicket.Component;
import org.apache.wicket.model.*;
public class MyLocalDateCompoundPropertyModel<T> extends CompoundPropertyModel<T>
{
private static final long serialVersionUID = 7112056989820105247L;
public MyLocalDateCompoundPropertyModel(IModel<T> model)
{
super(model);
}
public MyLocalDateCompoundPropertyModel(T object)
{
super(object);
}
@Override
public <T> IWrapModel<T> wrapOnInheritance(Component component)
{
IWrapModel<T> actualModel;
actualModel = super.wrapOnInheritance(component);
if (actualModel.getObject() instanceof LocalDate)
{
return new LocalDateModelButAlsoWrapping(actualModel);
}
else
{
return actualModel;
}
}
}
Class with LocalDate property
import java.time.Localdate;
public class CustomerUI implements Serializable
{
private static final long serialVersionUID = -4071467669346190367L;
private CustomerDTO customer;
public LocalDate getActiveUntil()
{
return customer.getActiveUntil();
}
public void setActiveUntil(LocalDate activeUntil)
{
customer.setActiveUntil(activeUntil);
}
// more delegating getters and setters
}
Creating the component
detailsDialog = new MyCustomerDetailsDialog("createDialog",
new MyLocalDateCompoundPropertyModel<CustomerUI>(Model.of(new CustomerUI())))
These are the error messages:
Last cause: Property could not be resolved for class: class example.ui.customer.CustomerUI expression: dialog WicketMessage: Can't instantiate page using constructor 'public example.ui.customer.MyCustomerOverviewPage()'. An exception has been thrown during construction!
Stacktrace:
org.apache.wicket.WicketRuntimeException: Property could not be resolved for class: class example.ui.customer.CustomerUI expression: dialog
at org.apache.wicket.core.util.lang.PropertyResolver.getGetAndSet(PropertyResolver.java:393)
at org.apache.wicket.core.util.lang.PropertyResolver.getObjectWithGetAndSet(PropertyResolver.java:355)
at org.apache.wicket.core.util.lang.PropertyResolver.getObjectWithGetAndSet(PropertyResolver.java:261)
at org.apache.wicket.core.util.lang.PropertyResolver.getValue(PropertyResolver.java:111)
at org.apache.wicket.model.AbstractPropertyModel.getObject(AbstractPropertyModel.java:86)
at example.ui.models.MyLocalDateCompoundPropertyModel.wrapOnInheritance(MyLocalDateCompoundPropertyModel.java:35)
at org.apache.wicket.Component.initModel(Component.java:3841)
at org.apache.wicket.Component.getDefaultModel(Component.java:1625)
at org.apache.wicket.MarkupContainer$2.component(MarkupContainer.java:876)
at org.apache.wicket.MarkupContainer$2.component(MarkupContainer.java:872)
at org.apache.wicket.util.visit.Visits.visitChildren(Visits.java:144)
at org.apache.wicket.util.visit.Visits.visitChildren(Visits.java:123)
at org.apache.wicket.util.visit.Visits.visitChildren(Visits.java:192)
at org.apache.wicket.MarkupContainer.visitChildren(MarkupContainer.java:983)
at org.apache.wicket.MarkupContainer.setDefaultModel(MarkupContainer.java:871)
at example.ui.customer.MyCustomerDetailsDialog.<init>(MyCustomerDetailsDialog.java:44)
at example.ui.customer.MyCustomerOverviewPage$2.<init>(MyCustomerOverviewPage.java:95)
Line 35 is:
if (actualModel.getObject() instanceof LocalDate)
See Question&Answers more detail:os