AttachFields called twice in Form

Hi,

I have a complex form with a grid. I followed the example in the demo and have the following issue:



ourLayout = new GridLayout(5, 9);
ourLayout.setMargin(true, false, false, true);
ourLayout.setSpacing(true);

setLayout(ourLayout);

setWriteThrough(false); // we want explicit 'apply'
setInvalidCommitted(false); // no invalid values in datamodel

// FieldFactory for customizing the fields and adding validators
setFormFieldFactory(new TenantFieldFactory());
setItemDataSource(tenantBeanItem);
setVisibleItemProperties(Arrays.asList( new String[] {
					"address1", "address2", "city", "stateName", "zip", 
					"phoneHome", "phoneWork", "phoneCell", "email"
}));

.....

@Override
protected void attachField(Object propertyId, Field field) {
	if (propertyId.equals("address1")) {
		ourLayout.addComponent(new Label("Address1"),0,0,0,0);
		ourLayout.addComponent(field, 1,0,2,0);
        }
....



  1. The first problem is that both setItemDataSource AND setVisibleItemProperties is calling attachField. As I understood it, it should only be called from setVisibleItemProperties. Is that not the case? If it is being called from both, the examples in the demo shouldn’t work as far as I understand it. You should be getting an exception indicating that the item is already in the grid.

I’ve looked at the API docs and combined the setItemDataSource to read:

 
setItemDataSource(tenantBeanItem, Arrays.asList( new String[] {
	"address1", "address2", "city", "stateName", "zip", 
	"phoneHome", "phoneWork", "phoneCell", "email"
}));

Is that equal to having both setItemDataSource and setVisibleItemProperties?

  1. I am setting the properties of the data source but the data is not being displayed. The data source object properties are being set AFTER the window and form are created. I thought the form was able to detect changes to the underlying data source (POJO) and update the form?

Thanks for your help.

Tom

  1. Umh, the damned setVisibleItemProperties()… Yes, I guess it does that. Using the two-parameter setItemDataSource(), as you did, should solve the problem so that it doesn’t run the factory twice, if I remember correctly. You could also have a reset method in the factory that you call before the setVisibleItemProperties(), but that doesn’t sound very pretty. Or reset it internally when the first field is attached.

  2. No, if you modify the bean directly, such as calling [tt]
    tenantBean.setAddress1(“Here”)
    [/tt], the form will not know about it. There’s unfortunately no mechanism to do that without having some notification mechanism in the bean itself. However, if you modify the bean through the BeanItem interface, for example by calling [tt]
    tenantBeanItem.getProperty(“address1”).setValue(“Here”)
    [/tt], the changes are relayed to the form.

If you really want that direct bean modifications are relayed to the form, you need to provide a notification mechanism for that, as is done in
this example
.

Thanks for your help. It was what I needed.

Tom