You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by Yevgeni Kovelman <ye...@kovelman.net> on 2007/12/08 23:11:27 UTC

dropdownchoice

I have the following problem, I have a dropdownchoice component.  The load
method returns a list of Broker objects which have an id and displayName
attributes.  Although currently not in this code, I will use the
iChoiceRenderer to populate the appropriate values.  The Request instance
object is a backing object which contains brokerId as an attribute.

 

My question is it possible to populate the instance.brokerId with the broker
id that gets rendered by iChoiceRenderer.  Currently I get an ClassCast
exception because the loaded model is of type Broker and the
instance.brokerId is long.  

 

     Request instance = new Request();

     // instance has broker as a long attribute

 

  Form form = new Form("entryForm", new CompoundPropertyModel(instance));

  add(form);                      

 

        DropDownChoice broker = 

            new DropDownChoice("broker",

                    new PropertyModel(instance, "broker"),

                    new LoadableDetachableModel()

                        {

                        @Override

                        protected Object load() 

                        {

                            return ((Application) getApplication())

                                 .getBrokerDao().getUniqueBrokers();

                        }

                    }

                );

        broker.setRequired(true);

        form.add(broker);


Re: dropdownchoice

Posted by Igor Vaynberg <ig...@gmail.com>.
here is the version that stores the id in the model instead...maybe at
some point it can make its way into extensions or core


import java.util.List;

import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.IChoiceRenderer;
import org.apache.wicket.model.IModel;

public class DropDownIdChoice extends DropDownChoice
{

    public DropDownIdChoice(String id, IModel model, IModel choices,
IChoiceRenderer renderer,
            Class<?> type)
    {
        super(id, model, choices, renderer);
        setType(type);
    }

    @Override
    public String getModelValue()
    {
        final Object id = getModelObject();
        if (id != null)
        {
            return getConverter(getType()).convertToString(id, getLocale());
        }
        else
        {
            return NO_SELECTION_VALUE;
        }
    }
}

-igor


On Dec 11, 2007 9:58 AM, Ivaylo Kovatchev <iv...@gmail.com> wrote:
>
> Fair question. Why return the Id, instead of just using a Property model to
> bind the member field of an object to a child component's model?
>
> Thanks for your insight.
>
> Yevgeni: If you do that you will have to have an object (can be the same one
> that extends DropDownChoice) implement IChoiceRenderer, so that you can see
> the field you are interested in on the dropdown. And also do not forget to
> say
> setChoiceRenderer(this);
> In the constructor.
>
>
> Johan Compagner wrote:
> >
> > yes this is true, you have to have a selected object that comes from the
> > choices
> > Why is this so hard to understand? I find that completely logical.
> >
> > Maybe with generics (1.4) this will be more clear
> >
> > *
> >
> > public* DropDownChoice(String id, IModel<T> model, IModel<List<T>>
> > choices,
> > IChoiceRenderer<T> renderer)
> >
> > So if your model object ins an integer, Then you also have to have a List
> > of
> > integers..
> >
> > Are you guys working with pojos like: Person.getAddressId() instead of
> > Person.getAddress() ??
> >
> > johan
> > On Dec 11, 2007 5:05 PM, Ivaylo Kovatchev <iv...@gmail.com>
> > wrote:
> >
>
> --
> View this message in context: http://www.nabble.com/dropdownchoice-tp14233301p14279580.html
> Sent from the Wicket - User mailing list archive at Nabble.com.
>
>
>
> ---------------------------------------------------------------------
> 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: dropdownchoice

Posted by Ivaylo Kovatchev <iv...@gmail.com>.
Fair question. Why return the Id, instead of just using a Property model to
bind the member field of an object to a child component's model?

Thanks for your insight.

Yevgeni: If you do that you will have to have an object (can be the same one
that extends DropDownChoice) implement IChoiceRenderer, so that you can see
the field you are interested in on the dropdown. And also do not forget to
say
setChoiceRenderer(this);
In the constructor.


Johan Compagner wrote:
> 
> yes this is true, you have to have a selected object that comes from the
> choices
> Why is this so hard to understand? I find that completely logical.
> 
> Maybe with generics (1.4) this will be more clear
> 
> *
> 
> public* DropDownChoice(String id, IModel<T> model, IModel<List<T>>
> choices,
> IChoiceRenderer<T> renderer)
> 
> So if your model object ins an integer, Then you also have to have a List
> of
> integers..
> 
> Are you guys working with pojos like: Person.getAddressId() instead of
> Person.getAddress() ??
> 
> johan
> On Dec 11, 2007 5:05 PM, Ivaylo Kovatchev <iv...@gmail.com>
> wrote:
> 

-- 
View this message in context: http://www.nabble.com/dropdownchoice-tp14233301p14279580.html
Sent from the Wicket - User mailing list archive at Nabble.com.


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


Re: dropdownchoice

Posted by Johan Compagner <jc...@gmail.com>.
yes this is true, you have to have a selected object that comes from the
choices
Why is this so hard to understand? I find that completely logical.

Maybe with generics (1.4) this will be more clear

*

public* DropDownChoice(String id, IModel<T> model, IModel<List<T>> choices,
IChoiceRenderer<T> renderer)

So if your model object ins an integer, Then you also have to have a List of
integers..

Are you guys working with pojos like: Person.getAddressId() instead of
Person.getAddress() ??

johan
On Dec 11, 2007 5:05 PM, Ivaylo Kovatchev <iv...@gmail.com>
wrote:

>
> I am not sure what the design decision was behind this and perhaps one of
> the
> Wicket Gurus can weigh in on this but I ran into something very similar
> recently, and here are my thoughts:
>
> I think you are mixing the model object that backs the Dropdown and the
> actual Broker object. They are not the same thing. For example, having
> Broker id's in the final rendered HTML is a potential security risk.
>
> That being said, I could not find a elegant solution to the same exact
> problem. I ended up subclassing DropdownChoice. Adapted to your code, it
> looked something like:
>
> private HashMap<String, Broker>map = new HashMap<String, Broker>();
> ...
> //get a list of the String that will be your choices
> for(Broker b : (Application)
> getApplication()).getBrokerDao().getUniqueBrokers())
> {
>   map.put(b.getName(), b);
> }
> setChoices(new ArrayList<String>(map.keySet()));
> ...
> public HashMap<String, Broker> getMap()
> {
>   return map;
> }
>
> Then, in your onSubmit() method for the containing component you can
> access
> the Id of each broker like this:
>
> brokerDropDown.getMap().get(modelObject.getBrokerName()).getId()
>
>
>
> I hate this approach. I would love to have someone point me in a better
> direction.
>
>
> Yevgeni Kovelman wrote:
> >
> > I have the following problem, I have a dropdownchoice component.  The
> load
> > method returns a list of Broker objects which have an id and displayName
> > attributes.  Although currently not in this code, I will use the
> > iChoiceRenderer to populate the appropriate values.  The Request
> instance
> > object is a backing object which contains brokerId as an attribute.
> >
> >
> >
> > My question is it possible to populate the instance.brokerId with the
> > broker
> > id that gets rendered by iChoiceRenderer.  Currently I get an ClassCast
> > exception because the loaded model is of type Broker and the
> > instance.brokerId is long.
> >
> >
> >
> >      Request instance = new Request();
> >
> >      // instance has broker as a long attribute
> >
> >
> >
> >   Form form = new Form("entryForm", new
> CompoundPropertyModel(instance));
> >
> >   add(form);
> >
> >
> >
> >         DropDownChoice broker =
> >
> >             new DropDownChoice("broker",
> >
> >                     new PropertyModel(instance, "broker"),
> >
> >                     new LoadableDetachableModel()
> >
> >                         {
> >
> >                         @Override
> >
> >                         protected Object load()
> >
> >                         {
> >
> >                             return ((Application) getApplication())
> >
> >                                  .getBrokerDao().getUniqueBrokers();
> >
> >                         }
> >
> >                     }
> >
> >                 );
> >
> >         broker.setRequired(true);
> >
> >         form.add(broker);
> >
> >
> >
>
> --
> View this message in context:
> http://www.nabble.com/dropdownchoice-tp14233301p14276903.html
> Sent from the Wicket - User mailing list archive at Nabble.com<http://nabble.com/>
> .
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>

Re: dropdownchoice

Posted by Ivaylo Kovatchev <iv...@gmail.com>.
I am not sure what the design decision was behind this and perhaps one of the
Wicket Gurus can weigh in on this but I ran into something very similar
recently, and here are my thoughts:

I think you are mixing the model object that backs the Dropdown and the
actual Broker object. They are not the same thing. For example, having
Broker id's in the final rendered HTML is a potential security risk.

That being said, I could not find a elegant solution to the same exact
problem. I ended up subclassing DropdownChoice. Adapted to your code, it
looked something like:

private HashMap<String, Broker>map = new HashMap<String, Broker>();
...
//get a list of the String that will be your choices
for(Broker b : (Application)
getApplication()).getBrokerDao().getUniqueBrokers())
{
   map.put(b.getName(), b);
}
setChoices(new ArrayList<String>(map.keySet()));
...
public HashMap<String, Broker> getMap()
{
   return map;
}

Then, in your onSubmit() method for the containing component you can access
the Id of each broker like this:

brokerDropDown.getMap().get(modelObject.getBrokerName()).getId()



I hate this approach. I would love to have someone point me in a better
direction.


Yevgeni Kovelman wrote:
> 
> I have the following problem, I have a dropdownchoice component.  The load
> method returns a list of Broker objects which have an id and displayName
> attributes.  Although currently not in this code, I will use the
> iChoiceRenderer to populate the appropriate values.  The Request instance
> object is a backing object which contains brokerId as an attribute.
> 
>  
> 
> My question is it possible to populate the instance.brokerId with the
> broker
> id that gets rendered by iChoiceRenderer.  Currently I get an ClassCast
> exception because the loaded model is of type Broker and the
> instance.brokerId is long.  
> 
>  
> 
>      Request instance = new Request();
> 
>      // instance has broker as a long attribute
> 
>  
> 
>   Form form = new Form("entryForm", new CompoundPropertyModel(instance));
> 
>   add(form);                      
> 
>  
> 
>         DropDownChoice broker = 
> 
>             new DropDownChoice("broker",
> 
>                     new PropertyModel(instance, "broker"),
> 
>                     new LoadableDetachableModel()
> 
>                         {
> 
>                         @Override
> 
>                         protected Object load() 
> 
>                         {
> 
>                             return ((Application) getApplication())
> 
>                                  .getBrokerDao().getUniqueBrokers();
> 
>                         }
> 
>                     }
> 
>                 );
> 
>         broker.setRequired(true);
> 
>         form.add(broker);
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/dropdownchoice-tp14233301p14276903.html
Sent from the Wicket - User mailing list archive at Nabble.com.


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