Great! Thanks a lot for this BeanDropDownChoice-Component ... iam a little bit dissapointed that something similiar is not include inside the standard-wicket framework....
One again: Great! :o) elygre wrote: > > > elygre wrote: >> >> It seems to me that it is a fairly hard requirement that the model and >> the choice list have identical types. I'll investigate a bit further, to >> see if I can build a generic mechanism for both a Map and a "bean"-style >> object. >> > > After looking deeper into this, I created two different specialized > DropDownChoices, with different properties. The class signatures > (interfaces) are as follows with complete code further below > > Now, of all the things I did in Wicket, this is the one thing that turned > out to be furthest from expectations. It wasn't the hardest, but it was > the easiest thing that turned out to be hard :-). If there is interest (or > at least no objections...), I'll create a jira issue and submit this code > for inclusion into 1.5. > > Class signatures: > > public class MapDropDownChoice<T> extends DropDownChoice<T> { > public MapDropDownChoice(String id, Map<T, ?> choices); > public MapDropDownChoice(String id, IModel<T> model, Map<T, ?> > choices); > } > > public class BeanDropDownChoice<T> extends MapDropDownChoice<T> { > public BeanDropDownChoice(String id, List choices, IChoiceRenderer > renderer); > public BeanDropDownChoice(String id, IModel<T> model, List choices, > IChoiceRenderer renderer); > } > > > Full code: > public class MapDropDownChoice<T> extends DropDownChoice<T> { > > public MapDropDownChoice(String id, Map<T, ?> choices) { > super(id, new ArrayList<T>(choices.keySet()), new > MapChoiceRenderer(choices)); > } > > public MapDropDownChoice(String id, IModel<T> model, Map<T, ?> > choices) { > super(id, model, new ArrayList<T>(choices.keySet()), new > MapChoiceRenderer(choices)); > } > > private static class MapChoiceRenderer extends ChoiceRenderer { > > private final Map map; > > public MapChoiceRenderer(Map map) { > super("value", "key"); > this.map = map; > } > > public Object getDisplayValue(Object object) { > Object value = map.get(object); > return value == null ? "" : value.toString(); > } > > public String getIdValue(Object object, int index) { > return object == null ? "" : object.toString(); > } > } > > } > > public class BeanDropDownChoice<T> extends MapDropDownChoice<T> { > > public BeanDropDownChoice(String id, List choices, IChoiceRenderer > renderer) { > super(id, createMap(choices, renderer)); > } > > public BeanDropDownChoice(String id, IModel<T> model, List choices, > IChoiceRenderer renderer) { > super(id, model, createMap(choices, renderer)); > } > > static Map createMap(Collection choices, IChoiceRenderer renderer) { > Map map = new HashMap(); > for (Object choice : choices) { > map.put(renderer.getIdValue(choice, -1), > renderer.getDisplayValue(choice)); > } > return map; > } > } > > > -- View this message in context: http://old.nabble.com/DropDown-where-%28type-of-model-property%29-%21%3D-%28type-of-choices%29-tp25052893p27714412.html Sent from the Wicket - User mailing list archive at Nabble.com. --------------------------------------------------------------------- To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org For additional commands, e-mail: users-h...@wicket.apache.org