List type in CustomField

Hello everybody.

(This post is about CustomField usage but the main purpose is to use data model in Form field. Should I move this post to Vaadin’s add-ons category?)

I needed custom form field for editing user’s addresses. I used for that CustomField add-on which contains table and 3 buttons for add/edit/delete actions (see the picture). Everything works fine…heh, almost :).

In regular fields like TextField, according to Vaadin’s data model and setWriteThrough(false) functionality, value under edit is buffered and finally set to the bean after commit() method invokation. And this works for List - List is commited after calling commit() - but not the way I want.
What I want to achieve is to commit not exactly the List, but the objects (Address objects) this list contains. E.g. when I press delete button:

List< Address > addresses = (List< Address >)( [b]
this.addressField.getPropertyDataSource().getValue()
[/b] );
		if ( addresses == null ) {
			addresses = new ArrayList< Address >( 1 );
			this.addressField.getPropertyDataSource().setValue( addresses );
		}
addresses.remove( address );

I remove Address object from the List in the field what is “original” List object. It works ok, but when I invoke discard() on the form, original List with addresses has changed. This logically expected behaviour but not the one I want.

I see this solution:

When setInternalValue(…) is invoked on Addresses custom field, I have to clone the List AND objects that this List contains, and use them as “buffered” values. After that, when commit() will be invoked - new List will be set to the bean.

But I’m in doubt of this solution because of implement clone() method or some custom “Cloner” class that does that. Also Address objects are JPA entities and I am not sure how this affects JPA-DB operations when commit() sets newly (buffered) created List without any changes.

Any ideas how to solve this problem?

This problem doesn’t occure when custom field is created for “regular” types like String, Integer etc becasue these types are desined immutable so “buffering” in this case means “create new object and put same value there”. This doesn’t work for Collections and other mutable classes.
11491.png