Hi,
I’m new to Vaadin, and I’d like to hear some thoughts on my approach to creating reusable forms. My approach is quite straight forward: factory class with method returning Form object, filled in basing on backing bean passed as parameter. Additionally factory method takes two listeners as parameters one for commit action , other for cancel action. This way the form user does not need to retrieve buttons references to subscribe to the ‘onClick’ events.
See the code below:
public final class ErrorReportFormFactory {
private static class ErrorReportFormFieldFactory implements FormFieldFactory {
private final boolean hideId;
public ErrorReportFormFieldFactory(boolean hideId) {
this.hideId = hideId;
}
@Override
public Field createField(Item item, Object propertyId, Component uiContext) {
String pid = (String) propertyId;
TextField textField = new TextField(pid.substring(0, 1).toUpperCase() + pid.substring(1).toLowerCase());
// special treatment for "description" multicolumn
if (pid.equals("descritpion")) {
textField.setRows(10);
}
// special treatment for "id" - always read-only, sometimes hide
if (pid.equalsIgnoreCase("id")) {
textField.setReadOnly(true);
textField.setVisible(!hideId);
}
return textField;
}
}
public static Form createErrorReportForm(ErrorReportBean errorReportBean, boolean hideId, Button.ClickListener commitButtonListener,
Button.ClickListener cancelClickListener) {
// create text fields
final Form form = new Form(new FormLayout(), new ErrorReportFormFieldFactory(hideId));
BeanItem<ErrorReportBean> beanItem = new BeanItem<ErrorReportBean>(errorReportBean);
form.setItemDataSource(beanItem);
// default order
form.setVisibleItemProperties(new String[] { "id", "title", "description" });
// default property values
form.setWriteThrough(false);
// add commit, clear and cancel button
final Button commitButton = new Button("Commit", commitButtonListener);
final Button clearButton = new Button("Discard", form, "discard");
final Button cancelButton = new Button("Cancel", cancelClickListener);
form.getFooter().addComponent(commitButton);
form.getFooter().addComponent(clearButton);
form.getFooter().addComponent(cancelButton);
//
return form;
}
}
The example above represents form for abstract error raporting. ErrorReportBean class used as BeanItem is simple:
public final class ErrorReportBean implements Serializable {
/**
*
*/
private static final long serialVersionUID = -593970513853035906L;
private int id=0;
private String title ="";
private String description="";
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
The only customizations done in the form factory is that Id field can not be modified by the form (as it’d be auto-assigned) and also sometimes we don’t want to present it to user (when creating new ER for example as it is not assigned any value yet).
Anyway what do you think about this approach guys? Does that make sense to you or should it be done completely differently?
thanks
Lukasz