PropertysetItem vs BeanItem in Forms

The book of vaadin as well as the sampler examples shows how to use BeanItem with a Form, but not PropertysetItem.

The reason I wish to use PropertysetItem rather than BeanItem is two fold.

  • Primarily, I wish to use a more general way of writing properties by using a map, rather than hard-coded get/set methods.
  • secondly, I am avoiding reflection at all costs, and BeanItem must be using reflection (don’t wish to debate on this preference).

I took FormPojoExample.java and changed the line

BeanItem personItem = new BeanItem(person);

to

PropertysetItem personItem = new PropertysetItem();

When I did that, the form no longer generated any fields. The form had no fields, only the buttons.

So how should I use PropertysetItem?

Treat PropertysetItem as a Map!

By doing

PropertysetItem personItem = new PropertysetItem();

You are generating an empty map, with no properties - which is different to a bean with null values for those properties. The FieldFactory will generated a field for each property in the Item - as the item is empty, no fields are generated.

Here’s some (untested) code to create an empty PropertysetItem that represents a Person as in the FormPojoExample


   /* Create an empty "Person" item */
    PropertysetItem item = new PropertysetItem();
    item.addItemProperty("firstName", new ObjectProperty(null, String.class));
    item.addItemProperty("lastName", new ObjectProperty(null, String.class));
    item.addItemProperty("birthDate", new ObjectProperty(null, Date.class));
    item.addItemProperty("password", new ObjectProperty(null, String.class));
    item.addItemProperty("countryCode", new ObjectProperty(null, String.class));


    /* Change some properties */
    item.getItemProperty("firstName").setValue("Blessed R.");
    item.getItemProperty("lastName").setValue("Geek");
    
    
    /* Retrieve a property */
    Date birthDate = (Date)  item.getItemProperty("birthDate").getValue();

Cheers,

Charles

Wow, thanks for the quick response.

Therefore, using it as an empty map, is this line of any use anymore? Seems redundant to me.

    personForm.setVisibleItemProperties(Arrays.asList(new String[] {
            "firstName", "lastName", "countryCode", "password",
            "birthdate", "shoesize", "uuid" }));

You can hide some properties that are in the container, but that you don’t want to show in form. IDs and such. You can also rearrange the form’s fields by specifying an order to that array.

No problem - happened to be passing by!

Yes : you can set it to be a subset of the list of properties, so that you are not editing all properties on the item. E.g.

personForm.setVisibleItemProperties(Arrays.asList(new String {
“firstName”, “lastName” }));

Would mean that the form would only show the firstName and lastName properties. You can also change the order in which the properties are displayed; in this particular instance, we are setting all of the properties as visible - but, as Jens points out, we are ensuring that shoeSize and uuid come last. I would imagine that without specifiying the order, the order is “jvm-defined”, i.e. pretty useless.

Cheers,

Charles