You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by Gianni Doe <gd...@yahoo.it> on 2008/10/28 20:27:53 UTC

LoadableDetachableModel for drop-down menu selection

I'm starting out with Wicket and following the Wicket In Action  
examples through and I'm a bit stuck on how to make use of a  
LoadableDetachableModel in a drop-down menu situation.

I've got a drop-down menu of countries which is populated from a  
LoadableDetachableModel, this works perfectly but my selected Country  
is then stored in the page as a complete object, I'd like to be able  
to make use of LoadableDetachableModel also for the selectedCountry  
but I'm not clear on where and how I should instantiate it.
i.e. for the example below where do I put the "new  
LoadableRefDataModel<Country>(selectedCountry);".

I'd appreciate some suggestions, I think I'm getting the hang of the  
Model idea but can't see the wood for the trees at the moment.



public class LocationSearchPage extends WebPage {

     private DropDownChoice<Country> countryChoice;

     private DropDownChoice<Location> locationChoice;

     private Country selectedCountry;

     private List<Location> locations = new ArrayList<Location>();

     private Location selectedLocation;

     @SpringBean
     private RefDataService refDataService;

     public LocationSearchPage() {

         Form searchForm = new Form("searchForm") {
             @Override
             protected void onSubmit() {
                 //...
             }
         };
         add(searchForm);

         ChoiceRenderer countryRenderer = new  
ChoiceRenderer("description", "id");
         IModel<List<? extends Country>> countryModel = new  
LoadableDetachableModel<List<? extends Country>>() {
             @Override
             protected List<Country> load() {
                 return refDataService.getCountries();
             }
         };

         countryChoice = new DropDownChoice<Country>(
                 "country", selectedCountry, countryModel,  
countryRenderer);

         countryChoice.add(new  
AjaxFormComponentUpdatingBehavior("onchange") {
             protected void onUpdate(AjaxRequestTarget target) {
                 this.selectedLocation = null;
                 locationChoice.setChoices(new  
ArrayList<Country>(selectedCountry.getLocations()));
                 target.addComponent(locationChoice);
             }
         });

         ChoiceRenderer locationRenderer = new  
ChoiceRenderer("description", "id");
         locationChoice = new DropDownChoice<Location>(
                 "location", new PropertyModel<Location>(this,  
"selectedLocation"), locations, locationRenderer);
     }
}


public class LoadableRefDataModel<T extends RefData> extends  
LoadableDetachableModel {

     @SpringBean
     private RefDataService refDataService;

     private Class<T> type;

     private Serializable id;

     public LoadableRefDataModel(Class<T> type, Serializable id) {
         InjectorHolder.getInjector().inject(this);
         this.type = type;
         this.id = id;
     }

     public LoadableRefDataModel(T domainObject) {
         super(domainObject);
         InjectorHolder.getInjector().inject(this);
         this.type = (Class<T>) domainObject.getClass();
         this.id = domainObject.getCode();
     }

     @Override
     protected T load() {
         return refDataService.load(type, id);
     }
}


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: LoadableDetachableModel for drop-down menu selection

Posted by Igor Vaynberg <ig...@gmail.com>.
ah, i thought that article had the version that supported setobject()
as well. anyways it is trivial to implement

setobject(identifiable entity) {
 clazz=entity.getclass();
 id=entity.getid();
 this.entity=entity;
}

and now you have an entity model that ddc can call setobject on.

-igor

On Tue, Oct 28, 2008 at 10:52 PM, Gianni <gd...@yahoo.it> wrote:
> Thanks Igor, I've already got my entity model LoadableRefDataModel which is
> similar to your example and I can see how to make use of this when I've
> actually got the entity object or id. My problem is I'm not clear on how I
> go about creating the entity model when a value is selected from my
> drop-down menu.
>
>>>  private Country selectedCountry;
>
> ....
>>>
>>> countryChoice = new DropDownChoice<Country>(
>>>              "country", selectedCountry, countryModel, countryRenderer);
>
>
> In the above example selectedCountry is populated when a value is selected
> from the drop-down, what I'd like to do is replace selectedCountry with a
> LoadableDetachableModel.
> i.e.
> private LoadableRefDataModel <Country> country;
>
> But I can't work out where to put the instantiation code. Is there some
> method of DropDownChoice I can override to do this or would I wrap my entity
> model in another model, overriding setObject() to instantiate my entity
> model.
> What would be the recommended way?
> -Gianni
>
>
>
> On 28/ott/08, at 21:06, Igor Vaynberg wrote:
>
>> this should give you an idea
>>
>> http://wicketinaction.com/2008/09/building-a-smart-entitymodel/
>>
>> -igor
>>
>> On Tue, Oct 28, 2008 at 12:27 PM, Gianni Doe <gd...@yahoo.it> wrote:
>>>
>>> I'm starting out with Wicket and following the Wicket In Action examples
>>> through and I'm a bit stuck on how to make use of a
>>> LoadableDetachableModel
>>> in a drop-down menu situation.
>>>
>>> I've got a drop-down menu of countries which is populated from a
>>> LoadableDetachableModel, this works perfectly but my selected Country is
>>> then stored in the page as a complete object, I'd like to be able to make
>>> use of LoadableDetachableModel also for the selectedCountry but I'm not
>>> clear on where and how I should instantiate it.
>>> i.e. for the example below where do I put the "new
>>> LoadableRefDataModel<Country>(selectedCountry);".
>>>
>>> I'd appreciate some suggestions, I think I'm getting the hang of the
>>> Model
>>> idea but can't see the wood for the trees at the moment.
>>>
>>>
>>>
>>> public class LocationSearchPage extends WebPage {
>>>
>>>  private DropDownChoice<Country> countryChoice;
>>>
>>>  private DropDownChoice<Location> locationChoice;
>>>
>>>  private Country selectedCountry;
>>>
>>>  private List<Location> locations = new ArrayList<Location>();
>>>
>>>  private Location selectedLocation;
>>>
>>>  @SpringBean
>>>  private RefDataService refDataService;
>>>
>>>  public LocationSearchPage() {
>>>
>>>      Form searchForm = new Form("searchForm") {
>>>          @Override
>>>          protected void onSubmit() {
>>>              //...
>>>          }
>>>      };
>>>      add(searchForm);
>>>
>>>      ChoiceRenderer countryRenderer = new ChoiceRenderer("description",
>>> "id");
>>>      IModel<List<? extends Country>> countryModel = new
>>> LoadableDetachableModel<List<? extends Country>>() {
>>>          @Override
>>>          protected List<Country> load() {
>>>              return refDataService.getCountries();
>>>          }
>>>      };
>>>
>>>      countryChoice = new DropDownChoice<Country>(
>>>              "country", selectedCountry, countryModel, countryRenderer);
>>>
>>>      countryChoice.add(new AjaxFormComponentUpdatingBehavior("onchange")
>>> {
>>>          protected void onUpdate(AjaxRequestTarget target) {
>>>              this.selectedLocation = null;
>>>              locationChoice.setChoices(new
>>> ArrayList<Country>(selectedCountry.getLocations()));
>>>              target.addComponent(locationChoice);
>>>          }
>>>      });
>>>
>>>      ChoiceRenderer locationRenderer = new ChoiceRenderer("description",
>>> "id");
>>>      locationChoice = new DropDownChoice<Location>(
>>>              "location", new PropertyModel<Location>(this,
>>> "selectedLocation"), locations, locationRenderer);
>>>  }
>>> }
>>>
>>>
>>> public class LoadableRefDataModel<T extends RefData> extends
>>> LoadableDetachableModel {
>>>
>>>  @SpringBean
>>>  private RefDataService refDataService;
>>>
>>>  private Class<T> type;
>>>
>>>  private Serializable id;
>>>
>>>  public LoadableRefDataModel(Class<T> type, Serializable id) {
>>>      InjectorHolder.getInjector().inject(this);
>>>      this.type = type;
>>>      this.id = id;
>>>  }
>>>
>>>  public LoadableRefDataModel(T domainObject) {
>>>      super(domainObject);
>>>      InjectorHolder.getInjector().inject(this);
>>>      this.type = (Class<T>) domainObject.getClass();
>>>      this.id = domainObject.getCode();
>>>  }
>>>
>>>  @Override
>>>  protected T load() {
>>>      return refDataService.load(type, id);
>>>  }
>>> }
>>>
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
>>> For additional commands, e-mail: users-help@wicket.apache.org
>>>
>>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
>> For additional commands, e-mail: users-help@wicket.apache.org
>>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: LoadableDetachableModel for drop-down menu selection

Posted by Gianni <gd...@yahoo.it>.
Thanks Igor, I've already got my entity model LoadableRefDataModel  
which is similar to your example and I can see how to make use of this  
when I've actually got the entity object or id. My problem is I'm not  
clear on how I go about creating the entity model when a value is  
selected from my drop-down menu.

>>   private Country selectedCountry;
....
>> countryChoice = new DropDownChoice<Country>(
>>               "country", selectedCountry, countryModel,  
>> countryRenderer);


In the above example selectedCountry is populated when a value is  
selected from the drop-down, what I'd like to do is replace  
selectedCountry with a LoadableDetachableModel.
i.e.
private LoadableRefDataModel <Country> country;

But I can't work out where to put the instantiation code. Is there  
some method of DropDownChoice I can override to do this or would I  
wrap my entity model in another model, overriding setObject() to  
instantiate my entity model.
What would be the recommended way?
-Gianni



On 28/ott/08, at 21:06, Igor Vaynberg wrote:

> this should give you an idea
>
> http://wicketinaction.com/2008/09/building-a-smart-entitymodel/
>
> -igor
>
> On Tue, Oct 28, 2008 at 12:27 PM, Gianni Doe <gd...@yahoo.it>  
> wrote:
>> I'm starting out with Wicket and following the Wicket In Action  
>> examples
>> through and I'm a bit stuck on how to make use of a  
>> LoadableDetachableModel
>> in a drop-down menu situation.
>>
>> I've got a drop-down menu of countries which is populated from a
>> LoadableDetachableModel, this works perfectly but my selected  
>> Country is
>> then stored in the page as a complete object, I'd like to be able  
>> to make
>> use of LoadableDetachableModel also for the selectedCountry but I'm  
>> not
>> clear on where and how I should instantiate it.
>> i.e. for the example below where do I put the "new
>> LoadableRefDataModel<Country>(selectedCountry);".
>>
>> I'd appreciate some suggestions, I think I'm getting the hang of  
>> the Model
>> idea but can't see the wood for the trees at the moment.
>>
>>
>>
>> public class LocationSearchPage extends WebPage {
>>
>>   private DropDownChoice<Country> countryChoice;
>>
>>   private DropDownChoice<Location> locationChoice;
>>
>>   private Country selectedCountry;
>>
>>   private List<Location> locations = new ArrayList<Location>();
>>
>>   private Location selectedLocation;
>>
>>   @SpringBean
>>   private RefDataService refDataService;
>>
>>   public LocationSearchPage() {
>>
>>       Form searchForm = new Form("searchForm") {
>>           @Override
>>           protected void onSubmit() {
>>               //...
>>           }
>>       };
>>       add(searchForm);
>>
>>       ChoiceRenderer countryRenderer = new  
>> ChoiceRenderer("description",
>> "id");
>>       IModel<List<? extends Country>> countryModel = new
>> LoadableDetachableModel<List<? extends Country>>() {
>>           @Override
>>           protected List<Country> load() {
>>               return refDataService.getCountries();
>>           }
>>       };
>>
>>       countryChoice = new DropDownChoice<Country>(
>>               "country", selectedCountry, countryModel,  
>> countryRenderer);
>>
>>       countryChoice.add(new  
>> AjaxFormComponentUpdatingBehavior("onchange") {
>>           protected void onUpdate(AjaxRequestTarget target) {
>>               this.selectedLocation = null;
>>               locationChoice.setChoices(new
>> ArrayList<Country>(selectedCountry.getLocations()));
>>               target.addComponent(locationChoice);
>>           }
>>       });
>>
>>       ChoiceRenderer locationRenderer = new  
>> ChoiceRenderer("description",
>> "id");
>>       locationChoice = new DropDownChoice<Location>(
>>               "location", new PropertyModel<Location>(this,
>> "selectedLocation"), locations, locationRenderer);
>>   }
>> }
>>
>>
>> public class LoadableRefDataModel<T extends RefData> extends
>> LoadableDetachableModel {
>>
>>   @SpringBean
>>   private RefDataService refDataService;
>>
>>   private Class<T> type;
>>
>>   private Serializable id;
>>
>>   public LoadableRefDataModel(Class<T> type, Serializable id) {
>>       InjectorHolder.getInjector().inject(this);
>>       this.type = type;
>>       this.id = id;
>>   }
>>
>>   public LoadableRefDataModel(T domainObject) {
>>       super(domainObject);
>>       InjectorHolder.getInjector().inject(this);
>>       this.type = (Class<T>) domainObject.getClass();
>>       this.id = domainObject.getCode();
>>   }
>>
>>   @Override
>>   protected T load() {
>>       return refDataService.load(type, id);
>>   }
>> }
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
>> For additional commands, e-mail: users-help@wicket.apache.org
>>
>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>



---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: LoadableDetachableModel for drop-down menu selection

Posted by Igor Vaynberg <ig...@gmail.com>.
this should give you an idea

http://wicketinaction.com/2008/09/building-a-smart-entitymodel/

-igor

On Tue, Oct 28, 2008 at 12:27 PM, Gianni Doe <gd...@yahoo.it> wrote:
> I'm starting out with Wicket and following the Wicket In Action examples
> through and I'm a bit stuck on how to make use of a LoadableDetachableModel
> in a drop-down menu situation.
>
> I've got a drop-down menu of countries which is populated from a
> LoadableDetachableModel, this works perfectly but my selected Country is
> then stored in the page as a complete object, I'd like to be able to make
> use of LoadableDetachableModel also for the selectedCountry but I'm not
> clear on where and how I should instantiate it.
> i.e. for the example below where do I put the "new
> LoadableRefDataModel<Country>(selectedCountry);".
>
> I'd appreciate some suggestions, I think I'm getting the hang of the Model
> idea but can't see the wood for the trees at the moment.
>
>
>
> public class LocationSearchPage extends WebPage {
>
>    private DropDownChoice<Country> countryChoice;
>
>    private DropDownChoice<Location> locationChoice;
>
>    private Country selectedCountry;
>
>    private List<Location> locations = new ArrayList<Location>();
>
>    private Location selectedLocation;
>
>    @SpringBean
>    private RefDataService refDataService;
>
>    public LocationSearchPage() {
>
>        Form searchForm = new Form("searchForm") {
>            @Override
>            protected void onSubmit() {
>                //...
>            }
>        };
>        add(searchForm);
>
>        ChoiceRenderer countryRenderer = new ChoiceRenderer("description",
> "id");
>        IModel<List<? extends Country>> countryModel = new
> LoadableDetachableModel<List<? extends Country>>() {
>            @Override
>            protected List<Country> load() {
>                return refDataService.getCountries();
>            }
>        };
>
>        countryChoice = new DropDownChoice<Country>(
>                "country", selectedCountry, countryModel, countryRenderer);
>
>        countryChoice.add(new AjaxFormComponentUpdatingBehavior("onchange") {
>            protected void onUpdate(AjaxRequestTarget target) {
>                this.selectedLocation = null;
>                locationChoice.setChoices(new
> ArrayList<Country>(selectedCountry.getLocations()));
>                target.addComponent(locationChoice);
>            }
>        });
>
>        ChoiceRenderer locationRenderer = new ChoiceRenderer("description",
> "id");
>        locationChoice = new DropDownChoice<Location>(
>                "location", new PropertyModel<Location>(this,
> "selectedLocation"), locations, locationRenderer);
>    }
> }
>
>
> public class LoadableRefDataModel<T extends RefData> extends
> LoadableDetachableModel {
>
>    @SpringBean
>    private RefDataService refDataService;
>
>    private Class<T> type;
>
>    private Serializable id;
>
>    public LoadableRefDataModel(Class<T> type, Serializable id) {
>        InjectorHolder.getInjector().inject(this);
>        this.type = type;
>        this.id = id;
>    }
>
>    public LoadableRefDataModel(T domainObject) {
>        super(domainObject);
>        InjectorHolder.getInjector().inject(this);
>        this.type = (Class<T>) domainObject.getClass();
>        this.id = domainObject.getCode();
>    }
>
>    @Override
>    protected T load() {
>        return refDataService.load(type, id);
>    }
> }
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org