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.

AbstractBasicCrud.java 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /*
  2. * Copyright 2000-2016 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.tests.fieldgroup;
  17. import java.util.Iterator;
  18. import java.util.Map;
  19. import com.vaadin.annotations.PropertyId;
  20. import com.vaadin.server.VaadinRequest;
  21. import com.vaadin.shared.ui.label.ContentMode;
  22. import com.vaadin.shared.util.SharedUtil;
  23. import com.vaadin.tests.components.AbstractTestUIWithLog;
  24. import com.vaadin.ui.Alignment;
  25. import com.vaadin.ui.Button;
  26. import com.vaadin.ui.Button.ClickEvent;
  27. import com.vaadin.ui.Button.ClickListener;
  28. import com.vaadin.ui.CheckBox;
  29. import com.vaadin.ui.Component;
  30. import com.vaadin.ui.GridLayout;
  31. import com.vaadin.ui.HorizontalLayout;
  32. import com.vaadin.ui.Label;
  33. import com.vaadin.ui.Notification;
  34. import com.vaadin.ui.Notification.Type;
  35. import com.vaadin.ui.themes.ValoTheme;
  36. import com.vaadin.v7.data.Property.ValueChangeEvent;
  37. import com.vaadin.v7.data.Property.ValueChangeListener;
  38. import com.vaadin.v7.data.Validator.InvalidValueException;
  39. import com.vaadin.v7.data.fieldgroup.BeanFieldGroup;
  40. import com.vaadin.v7.data.fieldgroup.FieldGroup.CommitException;
  41. import com.vaadin.v7.data.util.BeanItem;
  42. import com.vaadin.v7.data.util.BeanItemContainer;
  43. import com.vaadin.v7.data.validator.IntegerRangeValidator;
  44. import com.vaadin.v7.ui.ComboBox;
  45. import com.vaadin.v7.ui.Field;
  46. import com.vaadin.v7.ui.TextField;
  47. public abstract class AbstractBasicCrud extends AbstractTestUIWithLog {
  48. protected AbstractForm form;
  49. protected static String[] columns = new String[] { "firstName", "lastName",
  50. "gender", "birthDate", "age", "alive", "address.streetAddress",
  51. "address.postalCode", "address.city", "address.country" };
  52. protected BeanItemContainer<ComplexPerson> container = ComplexPerson
  53. .createContainer(100);;
  54. {
  55. container.addNestedContainerBean("address");
  56. }
  57. protected ComboBox formType;
  58. /*
  59. * (non-Javadoc)
  60. *
  61. * @see com.vaadin.tests.components.AbstractTestUI#setup(com.vaadin.server.
  62. * VaadinRequest)
  63. */
  64. @Override
  65. protected void setup(VaadinRequest request) {
  66. getLayout().setSizeFull();
  67. getLayout().setSpacing(true);
  68. getContent().setSizeFull();
  69. form = new CustomForm();
  70. formType = new ComboBox();
  71. formType.setNullSelectionAllowed(false);
  72. formType.setWidth("300px");
  73. formType.addItem(form);
  74. formType.setValue(form);
  75. formType.addItem(new AutoGeneratedForm(TextField.class));
  76. formType.addItem(new AutoGeneratedForm(Field.class));
  77. Iterator<?> iterator = formType.getItemIds().iterator();
  78. formType.setItemCaption(iterator.next(), "TextField based form");
  79. formType.setItemCaption(iterator.next(),
  80. "Auto generated form (TextFields)");
  81. formType.setItemCaption(iterator.next(),
  82. "Auto generated form (Any fields)");
  83. formType.addValueChangeListener(new ValueChangeListener() {
  84. @Override
  85. public void valueChange(ValueChangeEvent event) {
  86. AbstractForm oldForm = form;
  87. form = (AbstractForm) formType.getValue();
  88. replaceComponent(oldForm, form);
  89. }
  90. });
  91. addComponent(formType);
  92. }
  93. public class CustomForm extends AbstractForm {
  94. private TextField firstName = new TextField("First name");
  95. private TextField lastName = new TextField("Last name");
  96. private TextField gender = new TextField("Gender");
  97. private TextField birthDate = new TextField("Birth date");
  98. private TextField age = new TextField("Age");
  99. private CheckBox alive = new CheckBox("Alive");
  100. private Label errorLabel = new Label((String) null, ContentMode.HTML);
  101. @PropertyId("address.streetAddress")
  102. private TextField address_streetAddress = new TextField(
  103. "Street address");
  104. @PropertyId("address.postalCode")
  105. private TextField address_postalCode = new TextField("Postal code");
  106. @PropertyId("address.city")
  107. private TextField address_city = new TextField("City");
  108. @PropertyId("address.country")
  109. private TextField address_country = new TextField("Country");
  110. public CustomForm() {
  111. fieldGroup.bindMemberFields(this);
  112. address_postalCode.setNullRepresentation("");
  113. gender.setNullRepresentation("");
  114. age.setNullRepresentation("");
  115. address_country.setNullRepresentation("");
  116. // Last name editing is disabled through property readonly.
  117. // Postal code editing is disabled through disabling field.
  118. /*
  119. * Currently only sets the initial state because of
  120. * https://dev.vaadin.com/ticket/17847
  121. *
  122. * Must set lastName state initially as BeanFieldGroup can't tell it
  123. * should be read-only before setting an item data source
  124. */
  125. lastName.setReadOnly(true);
  126. address_postalCode.setEnabled(false);
  127. birthDate.setNullRepresentation("");
  128. age.addValidator(new IntegerRangeValidator(
  129. "Must be between 0 and 100", 0, 100));
  130. setDefaultComponentAlignment(Alignment.MIDDLE_LEFT);
  131. addComponents(firstName, lastName, gender, birthDate, age, alive,
  132. address_streetAddress, address_postalCode, address_city,
  133. address_country);
  134. errorLabel.addStyleName(ValoTheme.LABEL_COLORED);
  135. setRows(3);
  136. addComponent(errorLabel, 0, 2, getColumns() - 1, 2);
  137. HorizontalLayout hl = new HorizontalLayout(save, cancel);
  138. hl.setSpacing(true);
  139. addComponent(hl);
  140. }
  141. @Override
  142. protected void handleCommitException(CommitException e) {
  143. String message = "";
  144. // Produce error message in the order in which the fields are in the
  145. // layout
  146. for (Component c : this) {
  147. if (!(c instanceof Field)) {
  148. continue;
  149. }
  150. Field<?> f = (Field<?>) c;
  151. Map<Field<?>, InvalidValueException> exceptions = e
  152. .getInvalidFields();
  153. if (exceptions.containsKey(f)) {
  154. message += f.getCaption() + ": "
  155. + exceptions.get(f).getLocalizedMessage()
  156. + "<br/>\n";
  157. }
  158. }
  159. errorLabel.setValue(message);
  160. }
  161. @Override
  162. protected void discard() {
  163. super.discard();
  164. errorLabel.setValue(null);
  165. }
  166. }
  167. protected abstract void deselectAll();
  168. public class AbstractForm extends GridLayout {
  169. protected Button save = new Button("Save");
  170. protected Button cancel = new Button("Cancel");
  171. protected BeanFieldGroup<ComplexPerson> fieldGroup = new BeanFieldGroup<ComplexPerson>(
  172. ComplexPerson.class) {
  173. @Override
  174. protected void configureField(com.vaadin.v7.ui.Field<?> field) {
  175. super.configureField(field);
  176. if (field.getCaption().equals("Postal code")) {
  177. // Last name editing is disabled through property.
  178. // Postal code editing is disabled through field.
  179. /*
  180. * This is needed because of
  181. * https://dev.vaadin.com/ticket/17847
  182. */
  183. field.setEnabled(false);
  184. }
  185. };
  186. };
  187. public AbstractForm() {
  188. super(5, 1);
  189. setSpacing(true);
  190. setId("form");
  191. save.addClickListener(new ClickListener() {
  192. @Override
  193. public void buttonClick(ClickEvent event) {
  194. try {
  195. fieldGroup.commit();
  196. log("Saved " + fieldGroup.getItemDataSource());
  197. } catch (CommitException e) {
  198. handleCommitException(e);
  199. log("Commit failed: " + e.getMessage());
  200. }
  201. }
  202. });
  203. cancel.addClickListener(new ClickListener() {
  204. @Override
  205. public void buttonClick(ClickEvent event) {
  206. log("Discarded " + fieldGroup.getItemDataSource());
  207. discard();
  208. }
  209. });
  210. }
  211. protected void discard() {
  212. deselectAll();
  213. }
  214. protected void handleCommitException(CommitException e) {
  215. String message = "";
  216. for (Object propertyId : e.getInvalidFields().keySet()) {
  217. Field<?> f = e.getFieldGroup().getField(propertyId);
  218. message += f.getCaption() + ": "
  219. + e.getInvalidFields().get(propertyId);
  220. }
  221. if (!message.isEmpty()) {
  222. Notification.show(message, Type.ERROR_MESSAGE);
  223. }
  224. }
  225. public void edit(BeanItem<ComplexPerson> item) {
  226. fieldGroup.setItemDataSource(item);
  227. }
  228. }
  229. public class AutoGeneratedForm extends AbstractForm {
  230. public AutoGeneratedForm(Class<? extends Field> class1) {
  231. for (String p : columns) {
  232. Field f = fieldGroup.getFieldFactory()
  233. .createField(container.getType(p), class1);
  234. f.setCaption(SharedUtil.propertyIdToHumanFriendly(p));
  235. fieldGroup.bind(f, p);
  236. addComponent(f);
  237. }
  238. HorizontalLayout hl = new HorizontalLayout(save, cancel);
  239. hl.setSpacing(true);
  240. addComponent(hl);
  241. }
  242. }
  243. }