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

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