Binding a BeanItem that contains other beans

I apologize in advance for this question as I know there is a simple answer, I just can’t seem to find it.

I have a class that extends Form and binds the form to a BeanItem of lets say NewEntry where NewEntry contains a Submitter like follows:

public class NewEntry implements Serializable {

    private Date dateOfEntry;
    private String comments;
    private Submitter submitter;

...

}

and Submitter:


public class Submitter implements Serializable {
    private firstName;
    private lastName;

....
} 

From within my form I then have my attachField to layout the form with a GridLayout. When I inspect the propertyId and field in attachField I can see that Submitter is in fact a propertyId, I just don’t understand how to get it to be bound properly to show up in the form as part of the NewEntry.

NewEntry in the form class is bound to the form:


public class MyForm extends Form {

    public MyForm() {
        setItemDataSource(new BeanItem(new NewEntry()));
        ....
    }
}

So simply put, how do I get the Submitter within NewEntry to process as part of the BeanItem? I know this is a simple thing I’m missing, thanks for any pointers to RTFM.

Hi Mike!

This is, as you thought, quite a common problem and a limitation in the way Forms are handled in Vaadin. We’ll be sure to fix this for Vaadin 7.

For now, however, the
CustomField add-on
should do the trick for you. The demo/example should contain just the case you’re looking for IIRC.

HTH,
/Jonatan

If all you want is to “pull” some sub-item fields to the top level, you can also take a look at
the POJO Container add-on
.

However,
CustomField
will give you more flexibility in terms of how to handle the subobjects.

As a practical approach (if you are not having millions of NewEntry objects) I would suggest the following.

Wrap and “flatten” the objects - i.e. Create a simple (inner) class ‘NewEntryUI’ that extends the NewEntry, but adds two delegate functions to Submitter. Then loop through the collection before binding it to Form or Table.

Very easy to generate delegate functions in Eclipse IDE for example and this add extra flexibility to formatting too.

I have used this pattern whenever there is the UI needs a sightly modified version of data. Easy to understand what is happening and makes rest of your code a lot cleaner.

Hi Jonathan,

Is there any more infomation or a roadmap for these Form changes? We would like to jump the wagon at the right time - and form configurablity is high on our priority list.

Hi Albert,

The form changes will most probably be released in Vaadin 7.0. I don’t think theres currently a date set, but we’re aiming for sometime next summer.

/Jonatan