Automatic Form Generation

The JPAContainer FieldFactory is an implementation of the FormFieldFactory and TableFieldFactory interfaces that can generate fields based on JPA annotations in a POJO. It goes further than the DefaultFieldFactory, which only creates simple fields for the basic data types. This way, you can easily create forms to input entities or enable editing in tables.

The generated defaults are as follows:

AnnotationClass Mapping

@ManyToOne

NativeSelect

@OneToOne, @Embedded

Nested Form

@OneToMany, @ElementCollection

MasterDetailEditor (see below)

@ManyToMany

Selectable Table

The field factory is recursive, so that you can edit a complex object tree with one form.

Configuring the Field Factory

The FieldFactory is highly configurable with various configuration settings and by extending.

The setMultiSelectType() and setSingleSelectType() allow you to specify a selection component that is used instead of the default for a field with @ManyToMany and @ManyToOne annotation, respectively. The first parameter is the class type of the field, and the second parameter is the class type of a selection component. It must be a sub-class of AbstractSelect.

The setVisibleProperties() controls which properties (fields) are visible in generated forms, subforms, and tables. The first paramater is the class type for which the setting should be made, followed by the IDs of the visible properties.

The configuration should be done before binding the form to a data source as that is when the field generation is done.

Further configuration must be done by extending the many protected methods. Please see the API documentation for the complete list.

Using the Field Factory

The most basic use case for the JPAContainer FieldFactory is with a Form bound to a container item:

  1. // Have a persistent container
  2. final JPAContainer<Country> countries =
  3. JPAContainerFactory.make(Country.class, "book-examples");
  4. // For selecting an item to edit
  5. final ComboBox countrySelect =
  6. new ComboBox("Select a Country", countries);
  7. countrySelect.setItemCaptionMode(Select.ITEM_CAPTION_MODE_PROPERTY);
  8. countrySelect.setItemCaptionPropertyId("name");
  9. // Country Editor
  10. final Form countryForm = new Form();
  11. countryForm.setCaption("Country Editor");
  12. countryForm.addStyleName("bordered"); // Custom style
  13. countryForm.setWidth("420px");
  14. countryForm.setBuffered(true);
  15. countryForm.setEnabled(false);
  16. // When an item is selected from the list...
  17. countrySelect.addValueChangeListener(new ValueChangeListener(){
  18. @Override
  19. public void valueChange(ValueChangeEvent event) {
  20. // Get the item to edit in the form
  21. Item countryItem =
  22. countries.getItem(event.getProperty().getValue());
  23. // Use a JPAContainer field factory
  24. // - no configuration is needed here
  25. final FieldFactory fieldFactory = new FieldFactory();
  26. countryForm.setFormFieldFactory(fieldFactory);
  27. // Edit the item in the form
  28. countryForm.setItemDataSource(countryItem);
  29. countryForm.setEnabled(true);
  30. // Handle saves on the form
  31. final Button save = new Button("Save");
  32. countryForm.getFooter().removeAllComponents();
  33. countryForm.getFooter().addComponent(save);
  34. save.addClickListener(new ClickListener() {
  35. @Override
  36. public void buttonClick(ClickEvent event) {
  37. try {
  38. countryForm.commit();
  39. countryForm.setEnabled(false);
  40. } catch (InvalidValueException e) {
  41. }
  42. }
  43. });
  44. }
  45. });
  46. countrySelect.setImmediate(true);
  47. countrySelect.setNullSelectionAllowed(false);

See the on-line example.

This would create a form shown in Using FieldFactory with One-to-Many Relationship.

fieldfactory form

Using FieldFactory with One-to-Many Relationship

If you use Hibernate, you also need to pass an EntityManagerPerRequestHelper, either for the constructor or with setEntityManagerPerRequestHelper() , as described in “The EntityManager-Per-Request pattern” .

Master-Detail Editor

The MasterDetailEditor is a field component that allows editing an item property that has one-to-many relationship. The item can be a row in a table or bound to a form. It displays the referenced collection as an editable Table and allows adding and removing items in it.

You can use the MasterDetailEditor manually, or perhaps more commonly use a JPAContainer FieldFactory to create it automatically. As shown in the example in Using FieldFactory with One-to-Many Relationship, the factory creates a MasterDetailEditor for all properties with a @OneToMany or an @ElementCollection annotation.