"how can i bring wicket 7 to work with java.time from java 8?" Code Answer

2

you can wrap your localdate and etc. models in a imodel<java.util.date>, e.g.

public static class localdatemodel implements imodel<java.util.date> {
    private imodel<localdate> localdatemodel;
    public localdatemodel(imodel<localdate> localdatemodel){
        this.localdatemodel = localdatemodel;
    }


    @override
    public date getobject() {
        return convertlocaldatetoutildatesomehow(localdatemodel.getobject());
    }

    @override
    public void setobject(date object) {
        localdatemodel.setobject(convertutildatetolocaldatesomehow(object));
    }

    @override
    public void detach() {
        localdatemodel.detach();
    }
}

if you then feed models like this into the form components you want to use it should work just fine.

if you want your compoundpropertymodel to automatically provide such wrapping models, you need to extend it and override it's compoundpropertymodel#wraponinheritance(component component) method to infer that a wrapping model is needed. something like

@override
public <c> iwrapmodel<c> wraponinheritance(component component)
{
    iwrapmodel<c> actualmodel = super.wraponinheritance(component);
    if (actualmodel.getobject() instanceof localdate) {
        return new localdatemodelbutalsowrapping(actualmodel);
    } else {
        return actualmodel;
    }
}

where localdatemodelbutalsowrapping is unsurprisingly just an extension of localdatemodel example above but which also implements iwrapmodel<t>.

if you use this extension instead of your regular compoundpropertymodel it would detect when fields are localdate and provide models to components (like your datetextfield) that are wrapped to look like java.util.date models.

the code snippet i gave you is rather dirty though (you should probably not get the model object to infer its type) as i have only provided it to illustrate the general mechanism, so i suggest you devise your own way to infer the type of object expected (e.g. you can check if the component argument is a datetextfield), but this is the general direction of the solution that i can imagine.

By Thyagarajan C on September 2 2022

Answers related to “how can i bring wicket 7 to work with java.time from java 8?”

Only authorized users can answer the Search term. Please sign in first, or register a free account.