Extending DefaultFieldFactory to Handle ListSelect

My bean to populate into form is something like this:

public class Foo {

    private Set<Boo> boos = new HashSet<Boo>();

    private Date creationDate;

    private String name;

... setter and getter skipped

Then I create my DataSource with

form.setItemDataSource(new BeanItem<Invito>(myPoplatedModel));

While String and Date fields comes with rigth Field Type for boos I prefer to have a ListSelect, so I extended my FieldFormFactory like this:

form.setFormFieldFactory(new DefaultFieldFactory() {
            public Field createField(Item item, Object propertyId, Component uiContext) {
                Field field = super.createField(item, propertyId, uiContext);
                if (propertyId.equals("boos")) {
                    field = new ListSelect();
                    field.setCaption("Boos Label);
                    // WorkAround Here
                    ((ListSelect) field).setMultiSelect(true);
                return (field);

The ListSelect appear correctly but it is empty, the only solution I found is this very bad workaround
replacing the string
// WorkAround Here

                    //FIXME Workaround
                    for (Boo boo : myPoplatedModel.getBoos) {
                        ((ListSelect) field).addItem(boo);

Now it works, but I prefer a more clean way.

Does someone may help me?


You need to populate the select with items yourself as nobody else can now what should be in the select. In your code you only add the items in your Set (you end up with a ListSelect with all items selected). What about if there is a Boo that is not in the Set? Typically you would add all possibilities of Boo to the select and the Form will take care of selecting only those in your Set.