Hi,
I’ve a very simple enum and a bean class that has it as a member field:
public enum State{
OFFLINE(0), ONLINE(1);
private int id;
private State(int anId){
id = anId;
}
}
//...
public MyBean{
State state;
//getters,setters and other code
}
Now I’m creating the corresponding Form by binding it to the bean using a BeanFieldGroup. The following code works:
MyBean bean = //some initialization here
final BeanFieldGroup<MyBean> binder = new BeanFieldGroup<MyBean>(MyBean.class);
binder.setItemDataSource(bean);
OptionGroup stateGroup = new OptionGroup();
stateGroup.setCaption("State");
stateGroup.addItem(State.OFFLINE);
stateGroup.addItem(State.ONLINE);
binder.bind(stateGroup, "state");
mainLayout.addComponent(stateGroup);
//other bindings
However, I found it too complicated. I’d like to follow the “buildAndBind” way, which seems simpler. Something like that:
mainLayout.addComponent(binder.buildAndBind("State","state"));
But it doesn’t work. Vaadin doesn’t know how to handle the enum in a Field. This is the error:
Caused by: com.vaadin.data.fieldgroup.FieldGroup$BindException: Unable to build a field of type com.vaadin.ui.Field for editing com.acme.State
at com.vaadin.data.fieldgroup.FieldGroup.build(FieldGroup.java:1067)
at com.vaadin.data.fieldgroup.FieldGroup.buildAndBind(FieldGroup.java:1039)
at com.vaadin.data.fieldgroup.FieldGroup.buildAndBind(FieldGroup.java:1017)
at com.vaadin.data.fieldgroup.BeanFieldGroup.buildAndBind(BeanFieldGroup.java:170)
I think the editing of enum using OptionGroup items should be the default behaviour in this case. It’s so strange that the binder can’t guess automagically the enum values by calling a simple enum.values() and putting them into an OptionGroup.
What do you think?