I’m not certain this is the best approach… only quick and easy. It’s also just the first pass…
I created a FieldOrder annotation…
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface FieldOrder {
int value();
}
And use it to annotate the fields of my pojo…
@FieldOrder(value = 0)
private String companyName = "";
@FieldOrder(value = 1)
private String salesPerson = "";
Then call the following method to populate the form:
public static void bindOrderedFieldGroup(FormLayout form, FieldGroup fieldGroup, Class<?> clazz) {
Collection<?> ids = fieldGroup.getItemDataSource().getItemPropertyIds();
List<Field> fieldList = orderFields(clazz);
for (Field field : fieldList ) {
for (Object id : ids) {
if (field.getName().equals(id.toString())) {
form.addComponent(fieldGroup.buildAndBind(id.toString()));
}
}
}
}
Like so…
// create a contact from the item
if (clazz == Contact.class) {
caption = "Contact Form";
fieldGroup = new BeanFieldGroup<Contact>(Contact.class);
fieldGroup.setItemDataSource(new BeanItem<Contact>(Contact.fromItem(item)));
fieldGroup.setBuffered(true);
VaadinUtils.bindOrderedFieldGroup(form, fieldGroup, Contact.class);
}
It allows me dynamic reuse of several components. I’m pretty new to Vaadin. If there is an easier way, or my approach is frowned upon then please let me know! I don’t want to reinvent the wheel.
PS
Contact.fromItem is another attempt to keep things DRY. This method is currently in my pojo. I am going to create a PropertyID annotation that will allow me to move it to my VaadinUtils class for dynamic reuse. All it does is return an instance of my pojo created from an item.
public static Contact fromItem(Item item) {
Contact contact = new Contact();
contact.setFirstName((String) item.getItemProperty("Last Name").getValue());
contact.setLastName((String) item.getItemProperty("First Name").getValue());
...
return contact;
}
The PropertyID annotation will also support a similar method to dynamically commit field groups.
My apologies to the thoroughly confused.