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
------------------------------------------------------------------------
package sk.eea.cbk.web.components;
import java.io.Serializable;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import sk.eea.framework.collections.CollectionCreator;
import wicket.markup.html.form.IChoiceRenderer;
import wicket.model.IModel;
import wicket.model.LoadableDetachableModel;
/**
* Helper class for simple choices. Simple choice is a selection
* that consists of associated integer id and value. E.g. <br>
* <code>
* Gender <br>
* 1 - Male <br>
* 3 - Female <br>
* </code>
* Usage: <br>
* <code>
* SimpleSelectionItem items[] = { <br>
* new SimpleSelectionItem(1, "Male"), <br>
* new SimpleSelectionItem(2, "Female"), <br>
* new SimpleSelectionItem(4, "Unisex") <br>
* }; <br>
* <br>
* SimpleSelection selection = new SimpleSelection(items); <br>
* <br>
* add(new DropDownChoice("gender", new PropertyModel(this, "gender"), <br>
* selection.getItemsModel(), <br>
* selection.getChoiceRenderer()); <br>
* <br>
* void setGender(int) { <br>
* ... <br>
* } <br>
* <br>
* int getGender() { <br>
* ... <br>
* } <br>
*
* </code>
*
* @author Matej Knopp
*
*/
public class SimpleSelection implements Serializable {
List<Integer> ids;
List<SimpleSelectionItem> items = CollectionCreator.createArrayList();
protected void rebuildIds() {
ids = CollectionCreator.createArrayList(items.size());
for (Iterator i = items.iterator(); i.hasNext(); ) {
SimpleSelectionItem item = (SimpleSelectionItem)
i.next();
ids.add(Integer.valueOf(item.getId()));
}
}
public List getIds() {
if (ids == null || items.size() != ids.size())
rebuildIds();
return ids;
}
/**
* Get the name associated with this id.
* @param id
* @return name associated with id
*/
public String getName(int id) {
for (Iterator i = items.iterator(); i.hasNext();) {
SimpleSelectionItem item = (SimpleSelectionItem)
i.next();
if (item.getId() == id)
return item.getName();
}
return null;
}
/**
* Get the model that can be used as items in Choice components.
* @return model
*/
public IModel getItemsModel() {
return new LoadableDetachableModel() {
protected Object load() {
return getIds();
};
};
}
/**
* Get the choice renderer for Choice components.
* @return choice renderer
*/
public IChoiceRenderer getChoiceRenderer() {
return new IChoiceRenderer() {
public String getDisplayValue(Object obj) {
return getName(((Integer) obj).intValue());
}
public String getIdValue(Object obj, int index) {
return obj.toString();
}
};
}
/**
* Creates a new simple selection according to items of type SimpleSelectionItem
* in the given list.
* @param items
*/
public SimpleSelection(List<SimpleSelectionItem> items) {
this.items = items;
}
/**
* Creates a new simple selection according to the given array.
* @param items
*/
public SimpleSelection(SimpleSelectionItem items[]) {
Collections.addAll(this.items, items);
}
}
------------------------------------------------------------------------
package sk.eea.cbk.web.components;
import java.io.Serializable;
public class SimpleSelectionItem implements Serializable {
int id;
String name;
public SimpleSelectionItem(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
}