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.

FormExample.java 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. package com.vaadin.automatedtests.featurebrowser;
  2. import java.io.Serializable;
  3. import com.vaadin.data.Item;
  4. import com.vaadin.data.Validator;
  5. import com.vaadin.data.util.BeanItem;
  6. import com.vaadin.ui.BaseFieldFactory;
  7. import com.vaadin.ui.Button;
  8. import com.vaadin.ui.Component;
  9. import com.vaadin.ui.CustomComponent;
  10. import com.vaadin.ui.Field;
  11. import com.vaadin.ui.Form;
  12. import com.vaadin.ui.HorizontalLayout;
  13. import com.vaadin.ui.TextField;
  14. import com.vaadin.ui.VerticalLayout;
  15. import com.vaadin.ui.Button.ClickEvent;
  16. /**
  17. * This example demonstrates the most important features of the Form component:
  18. * binding Form to a JavaBean so that form fields are automatically generated
  19. * from the bean properties, creation of custom field editors using a
  20. * FieldFactory, customizing the form without FieldFactory, buffering
  21. * (commit/discard) and validation. Please note that the example is quite a bit
  22. * more complex than real use, as it tries to demonstrate more features than
  23. * needed in general case.
  24. */
  25. @SuppressWarnings("serial")
  26. public class FormExample extends CustomComponent {
  27. static final String cities[] = { "Amsterdam", "Berlin", "Helsinki",
  28. "Hong Kong", "London", "Luxemburg", "New York", "Oslo", "Paris",
  29. "Rome", "Stockholm", "Tokyo", "Turku" };
  30. /** Compose the demo. */
  31. public FormExample() {
  32. // Example data model
  33. final Address dataModel = new Address();
  34. Button peekDataModelState = new Button("Show the data model state",
  35. new Button.ClickListener() {
  36. public void buttonClick(ClickEvent event) {
  37. getWindow().showNotification(
  38. dataModel.getAddressAsText());
  39. }
  40. });
  41. // Example form
  42. final AddressForm form = new AddressForm("Contact Information");
  43. form.setDataSource(dataModel);
  44. form
  45. .setDescription("Please enter valid name and address. Fields marked with * are required. "
  46. + "If you try to commit with invalid values, a form error message is displayed. "
  47. + "(Address is required but failing to give it a value does not display an error.)");
  48. // Layout the example
  49. VerticalLayout root = new VerticalLayout();
  50. root.setMargin(true);
  51. root.setSpacing(true);
  52. root.addComponent(form);
  53. root.addComponent(peekDataModelState);
  54. setCompositionRoot(root);
  55. }
  56. public static class AddressForm extends Form {
  57. public AddressForm(String caption) {
  58. setCaption(caption);
  59. // Use custom field factory to modify the defaults on how the
  60. // components are created
  61. setFieldFactory(new MyFieldFactory());
  62. // Add Commit and Discard controls to the form.
  63. Button commit = new Button("Save", this, "commit");
  64. Button discard = new Button("Reset", this, "discard");
  65. HorizontalLayout footer = new HorizontalLayout();
  66. footer.addComponent(commit);
  67. footer.addComponent(discard);
  68. setFooter(footer);
  69. }
  70. public void setDataSource(Address dataModel) {
  71. // Set the form to edit given datamodel by converting pojo used as
  72. // the datamodel to Item
  73. setItemDataSource(new BeanItem(dataModel));
  74. // Ensure that the fields are shown in correct order as the
  75. // datamodel does not force any specific order.
  76. setVisibleItemProperties(new String[] { "name", "streetAddress",
  77. "postalCode", "city" });
  78. // For examples sake, customize some of the form fields directly
  79. // here. The alternative way is to use custom field factory as shown
  80. // above.
  81. getField("name").setRequired(true);
  82. getField("name").setRequiredError("Name is missing");
  83. getField("streetAddress").setRequired(true); // No error message
  84. getField("postalCode").setRequired(true); // No error message
  85. replaceWithSelect("city", cities, cities).setNewItemsAllowed(true);
  86. // Set the form to act immediately on user input. This is
  87. // automatically transports data between the client and the server
  88. // to do server-side validation.
  89. setImmediate(true);
  90. // Enable buffering so that commit() must be called for the form
  91. // before input is written to the data. (Form input is not written
  92. // immediately through to the underlying object.)
  93. setWriteThrough(false);
  94. }
  95. }
  96. /**
  97. * This is example on how to customize field creation. Any kind of field
  98. * components could be created on the fly.
  99. */
  100. static class MyFieldFactory extends BaseFieldFactory implements
  101. Serializable {
  102. @Override
  103. public Field createField(Item item, Object propertyId,
  104. Component uiContext) {
  105. Field field = super.createField(item, propertyId, uiContext);
  106. if ("postalCode".equals(propertyId)) {
  107. ((TextField) field).setColumns(5);
  108. field.addValidator(new PostalCodeValidator());
  109. }
  110. return field;
  111. }
  112. }
  113. /**
  114. * This is an example of how to create a custom validator for automatic
  115. * input validation.
  116. */
  117. static class PostalCodeValidator implements Validator {
  118. public boolean isValid(Object value) {
  119. if (value == null || !(value instanceof String)) {
  120. return false;
  121. }
  122. return ((String) value).matches("[0-9]{5}");
  123. }
  124. public void validate(Object value) throws InvalidValueException {
  125. if (!isValid(value)) {
  126. throw new InvalidValueException(
  127. "Postal code must be a five digit number.");
  128. }
  129. }
  130. }
  131. /**
  132. * Contact information data model created as POJO. Note that in many cases
  133. * it would be a good idea to implement Item -interface for the datamodel to
  134. * make it directly bindable to form (without BeanItem wrapper)
  135. */
  136. public static class Address implements Serializable {
  137. String name = "";
  138. String streetAddress = "";
  139. String postalCode = "";
  140. String city;
  141. public String getAddressAsText() {
  142. return name + "\n" + streetAddress + "\n" + postalCode + " "
  143. + (city == null ? "" : city);
  144. }
  145. public void setName(String name) {
  146. this.name = name;
  147. }
  148. public String getName() {
  149. return name;
  150. }
  151. public void setStreetAddress(String address) {
  152. streetAddress = address;
  153. }
  154. public String getStreetAddress() {
  155. return streetAddress;
  156. }
  157. public void setPostalCode(String postalCode) {
  158. this.postalCode = postalCode;
  159. }
  160. public String getPostalCode() {
  161. return postalCode;
  162. }
  163. public void setCity(String city) {
  164. this.city = city;
  165. }
  166. public String getCity() {
  167. return city;
  168. }
  169. }
  170. }