currently i am wondering if Vaadin Forms support creation of beans with a list of nested beans.
I found multiple threads that suggest it isn’t possible since there are no replies containing a solution.
I have the following Beans:
public class Post {
String content;
Set<Tag> tags;
}
public class Tag {
String text;
}
I create a Form for the Post class.
For the tag field, i both tried the TokenField and CustomField addons.
But i didn’t get the Field to return a Set to the Main Form.
Mainly i get a ClassCast or ConversionException, since for example PropertyFormatter tries to cast the dataSource value to String which is not possible for a Set of Tags:
(line 237 of PropertyFormatter.class)
...
dataSource.setValue(parse((String) newValue));
...
Is there anyone who can give me pointer what else i can try??
Continuing from
this reply : Where does the PropertyFormatter come from? Did you put it there explicitly? Does TokenField put it there somehow?
PropertyFormatter converts between a String that is shown to the user and an underlying data type. It has some other limitations as well. The class PropertyConverter in the CustomField add-on does provide more flexibility about the data types to convert, and should be easy to use especially with the FieldWrapper class from the same add-on if conversions are needed - you can wrap a TokenField in a FieldWrapper that provides the conversions, creating the field wrapper and its contained TokenField in your FormFieldFactory.
Also, do make sure getType() for your item returns the correct data type for the property id. What kind of an item are you using? BeanItem?
TokenField uses CustomField and returns a Set as Type.
It works for me with this solution:
public class TagField extends TokenField {
public TagField() {
setContainerDataSource(getTagContainer());
setWidth("30em");
setFilteringMode(ComboBox.FILTERINGMODE_CONTAINS);
setTokenCaptionPropertyId("text");
setInputPrompt("type or select tags here");
setNewTokensAllowed(true);
setRememberNewTokens(true);
}
@SuppressWarnings("unchecked")
@Override
protected void onTokenInput(Object tokenId) {
Tag newTag = new Tag(tokenId.toString());
if (!cb.containsId(newTag)) {
((BeanItemContainer<Tag>) getContainerDataSource()).addBean(newTag);
}
addToken(newTag);
}
}
All other methods are already fully sufficient implemented by TokenField.
It holds a Set of Objects and uses the toString() method for displaying them.
I guess i went off the tracks a little too far by trying to mess with PropertyFormatters, which wasn’t necessary.
There were several problems with my code, one was that my Bean was holding a List of Tags, not a Set…
So thank you again and by the way, real good job with the CustomField addon which i do use also, it is very usefull.
I’m looking to do similar things with my form. I have a parent bean with nested child bean lists. I want to expose all the properties of the parent beans and list the main properties (e.g,. name and description) for each child list. I have no idea how to do that using Vaadin Form. Given that you have successfully implemented such functionality, can you provide snippets of your code so that I can model after. I’m sure others will find it useful. I appreciate it!
Anyone with examples, please feel free to chime in…