You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

jpacontainer-fieldfactory.asciidoc 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. ---
  2. title: Automatic Form Generation
  3. order: 8
  4. layout: page
  5. ---
  6. [[jpacontainer.fieldfactory]]
  7. = Automatic Form Generation
  8. The JPAContainer [classname]#FieldFactory# is an implementation of the
  9. [interfacename]#FormFieldFactory# and [interfacename]#TableFieldFactory#
  10. interfaces that can generate fields based on JPA annotations in a POJO. It goes
  11. further than the [classname]#DefaultFieldFactory#, which only creates simple
  12. fields for the basic data types. This way, you can easily create forms to input
  13. entities or enable editing in tables.
  14. The generated defaults are as follows:
  15. [options="header"]
  16. |===============
  17. |Annotation|Class Mapping
  18. |[literal]#++@ManyToOne++#|[classname]#NativeSelect#
  19. |[literal]#++@OneToOne++#, [literal]#++@Embedded++#|Nested [classname]#Form#
  20. |[literal]#++@OneToMany++#, [literal]#++@ElementCollection++#|[classname]#MasterDetailEditor# (see below)
  21. |[literal]#++@ManyToMany++#|Selectable [classname]#Table#
  22. |===============
  23. The field factory is recursive, so that you can edit a complex object tree with one form.
  24. [[jpacontainer.fieldfactory.configuring]]
  25. == Configuring the Field Factory
  26. The [classname]#FieldFactory# is highly configurable with various configuration settings and by extending.
  27. // You need to make the configuration before ...
  28. The [methodname]#setMultiSelectType()# and [methodname]#setSingleSelectType()#
  29. allow you to specify a selection component that is used instead of the default
  30. for a field with [literal]#++@ManyToMany++# and [literal]#++@ManyToOne++#
  31. annotation, respectively. The first parameter is the class type of the field,
  32. and the second parameter is the class type of a selection component. It must be
  33. a sub-class of [classname]#AbstractSelect#.
  34. The [methodname]#setVisibleProperties()# controls which properties (fields) are
  35. visible in generated forms, subforms, and tables. The first paramater is the
  36. class type for which the setting should be made, followed by the IDs of the
  37. visible properties.
  38. The configuration should be done before binding the form to a data source as
  39. that is when the field generation is done.
  40. Further configuration must be done by extending the many protected methods.
  41. Please see the API documentation for the complete list.
  42. [[jpacontainer.fieldfactory.using]]
  43. == Using the Field Factory
  44. The most basic use case for the JPAContainer [classname]#FieldFactory# is with a
  45. [classname]#Form# bound to a container item:
  46. ----
  47. // Have a persistent container
  48. final JPAContainer<Country> countries =
  49. JPAContainerFactory.make(Country.class, "book-examples");
  50. // For selecting an item to edit
  51. final ComboBox countrySelect =
  52. new ComboBox("Select a Country", countries);
  53. countrySelect.setItemCaptionMode(Select.ITEM_CAPTION_MODE_PROPERTY);
  54. countrySelect.setItemCaptionPropertyId("name");
  55. // Country Editor
  56. final Form countryForm = new Form();
  57. countryForm.setCaption("Country Editor");
  58. countryForm.addStyleName("bordered"); // Custom style
  59. countryForm.setWidth("420px");
  60. countryForm.setBuffered(true);
  61. countryForm.setEnabled(false);
  62. // When an item is selected from the list...
  63. countrySelect.addValueChangeListener(new ValueChangeListener(){
  64. @Override
  65. public void valueChange(ValueChangeEvent event) {
  66. // Get the item to edit in the form
  67. Item countryItem =
  68. countries.getItem(event.getProperty().getValue());
  69. // Use a JPAContainer field factory
  70. // - no configuration is needed here
  71. final FieldFactory fieldFactory = new FieldFactory();
  72. countryForm.setFormFieldFactory(fieldFactory);
  73. // Edit the item in the form
  74. countryForm.setItemDataSource(countryItem);
  75. countryForm.setEnabled(true);
  76. // Handle saves on the form
  77. final Button save = new Button("Save");
  78. countryForm.getFooter().removeAllComponents();
  79. countryForm.getFooter().addComponent(save);
  80. save.addClickListener(new ClickListener() {
  81. @Override
  82. public void buttonClick(ClickEvent event) {
  83. try {
  84. countryForm.commit();
  85. countryForm.setEnabled(false);
  86. } catch (InvalidValueException e) {
  87. }
  88. }
  89. });
  90. }
  91. });
  92. countrySelect.setImmediate(true);
  93. countrySelect.setNullSelectionAllowed(false);
  94. ----
  95. See the http://demo.vaadin.com/book-examples-vaadin7/book#jpacontainer.fieldfactory.formonetoone[on-line example, window="_blank"].
  96. This would create a form shown in <<figure.jpacontainer.fieldfactory.using>>.
  97. [[figure.jpacontainer.fieldfactory.using]]
  98. .Using FieldFactory with One-to-Many Relationship
  99. image::img/fieldfactory-form.png[]
  100. If you use Hibernate, you also need to pass an
  101. [classname]#EntityManagerPerRequestHelper#, either for the constructor or with
  102. [methodname]#setEntityManagerPerRequestHelper()#
  103. ifdef::web[]
  104. , as described in
  105. <<dummy/../../../framework/jpacontainer/jpacontainer-hibernate#jpacontainer.hibernate.em-per-request,"The
  106. EntityManager-Per-Request
  107. pattern">>
  108. endif::web[]
  109. .
  110. [[jpacontainer.fieldfactory.masterdetaileditor]]
  111. == Master-Detail Editor
  112. The [classname]#MasterDetailEditor# is a field component that allows editing an
  113. item property that has one-to-many relationship. The item can be a row in a
  114. table or bound to a form. It displays the referenced collection as an editable
  115. [classname]#Table# and allows adding and removing items in it.
  116. You can use the [classname]#MasterDetailEditor# manually, or perhaps more
  117. commonly use a JPAContainer [classname]#FieldFactory# to create it
  118. automatically. As shown in the example in
  119. <<figure.jpacontainer.fieldfactory.using>>, the factory creates a
  120. [classname]#MasterDetailEditor# for all properties with a
  121. [literal]#++@OneToMany++# or an [literal]#++@ElementCollection++# annotation.