Interesting. The SimpleSelection can be used on any DropDownChoice, this 
is nice. I extended DropDownChoice for all the cases where I want to 
specify the choices directly:


public static class SimpleDropDownChoice extends DropDownChoice {
   private static class Choice {
     private String label;
     private Object value;

     public Choice(String choiceLabel, Object choiceValue) {
       label = choiceLabel;
       value = choiceValue;
     }
     public String getLabel() {
       return label;
     }

     public Object getValue() {
       return value;
     }
   }
   public SimpleDropDownChoice(String id, IModel model) {
     this(id);
     setModel(model);
   }
   public SimpleDropDownChoice(String id) {
     super(id);
     setChoiceRenderer(new IChoiceRenderer() {
       public String getDisplayValue(Object object) {
         Choice e = (Choice) object;
         return e.getLabel();
       }
       public String getIdValue(Object object, int index) {
         return String.valueOf(index);
       }
     });
     setChoices(new ArrayList());
   }
   public SimpleDropDownChoice addChoice(String label, Object value) {
     List choices = getChoices();
     choices.add(new Choice(label, value));
     setChoices(choices);
     return this;
   }
}


Usage:

   SimpleDropDownChoice c = new SimpleDropDownChoice("gender");
   c.addChoice("Male", new Integer(1));
   c.addChoice("Female", new Integer(2));
   c.addChoice("Unisex", new Integer(3));


Timo


Matej Knopp schrieb:
> It's doable, but you have to write your own choice renderer.
> I've made a SimpleSelection class that might help you. Usage is
> 
> SimpleSelectionItem items[] = {
>   new SimpleSelectionItem(1, "Male"),
>   new SimpleSelectionItem(2, "Female"),
>   new SimpleSelectionItem(4, "Unisex")
> };
> 
> SimpleSelection selection = new SimpleSelection(items);
> 
> add(new DropDownChoice("gender",
>                        new PropertyModel(this, "gender"),
>                        selection.getItemsModel(),
>                        selection.getChoiceRenderer());
> 
> void setGender(int) {
>   ...
> }
> 
> int getGender() {
>   ...
> }
> 
> -Matej
> 
> Johan Compagner wrote:
>> that is a problem. Because currently the Choice/ChoiceRender solutions 
>> require
>> a one<->one relation ship between whats in the list and whats in the 
>> model.
>> if you want to map that then the fastest way that i can think of right 
>> now is to use
>> a model that sits between it. That maps CategoryVO <-> int back and 
>> forward.
>>
>> Why do you work with ints in the model objects? Why not just full 
>> blown java objects?
>>
>> johan
>>
>>
>> On 6/2/06, *Ralf Ebert* <[EMAIL PROTECTED] 
>> <mailto:[EMAIL PROTECTED]>> wrote:
>>
>>     Hi,
>>
>>     quite often I have the problem that my model objects (which are bound
>>     using compound property models) contain a field like categoryId (as
>>     int), but when building pages, I would like to use objects like
>>     CategoryVO. Simplest example is choosing from a list, the
>>     dropdownchoices would be a List<CategoryVO> while the model object
>>     itself would be an simple integer. I'm looking for an elegant way to
>>     handle these things in a model based way. I would like to throw in a
>>     little class which allows me to implement a mapping between both 
>> types
>>     (id -> object, object -> id, a bit like a converter). I couldn't find
>>     an elegant way to do things like these, is there some best practice
>>     for handling such cases?
>>
>>     thx,
>>     Ralf
>>
>>
>>     -------------------------------------------------------
>>     All the advantages of Linux Managed Hosting--Without the Cost and 
>> Risk!
>>     Fully trained technicians. The highest number of Red Hat
>>     certifications in
>>     the hosting industry. Fanatical Support. Click to learn more
>>     
>> http://sel.as-us.falkag.net/sel?cmd=lnk&kid=107521&bid=248729&dat=121642
>>     
>> <http://sel.as-us.falkag.net/sel?cmd=lnk&kid=107521&bid=248729&dat=121642> 
>>
>>     _______________________________________________
>>     Wicket-user mailing list
>>     Wicket-user@lists.sourceforge.net
>>     <mailto:Wicket-user@lists.sourceforge.net>
>>     https://lists.sourceforge.net/lists/listinfo/wicket-user
>>
>>
> 



_______________________________________________
Wicket-user mailing list
Wicket-user@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/wicket-user

Reply via email to