aboutsummaryrefslogtreecommitdiffstats
path: root/uitest/src/com/vaadin/tests/fieldgroup/AbstractBasicCrud.java
diff options
context:
space:
mode:
Diffstat (limited to 'uitest/src/com/vaadin/tests/fieldgroup/AbstractBasicCrud.java')
-rw-r--r--uitest/src/com/vaadin/tests/fieldgroup/AbstractBasicCrud.java191
1 files changed, 191 insertions, 0 deletions
diff --git a/uitest/src/com/vaadin/tests/fieldgroup/AbstractBasicCrud.java b/uitest/src/com/vaadin/tests/fieldgroup/AbstractBasicCrud.java
new file mode 100644
index 0000000000..30cc5676bb
--- /dev/null
+++ b/uitest/src/com/vaadin/tests/fieldgroup/AbstractBasicCrud.java
@@ -0,0 +1,191 @@
+/*
+ * Copyright 2000-2014 Vaadin Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.vaadin.tests.fieldgroup;
+
+import java.util.Iterator;
+
+import com.vaadin.annotations.Theme;
+import com.vaadin.data.Property.ValueChangeEvent;
+import com.vaadin.data.Property.ValueChangeListener;
+import com.vaadin.data.fieldgroup.BeanFieldGroup;
+import com.vaadin.data.fieldgroup.FieldGroup.CommitException;
+import com.vaadin.data.fieldgroup.PropertyId;
+import com.vaadin.data.util.BeanItem;
+import com.vaadin.data.util.BeanItemContainer;
+import com.vaadin.server.VaadinRequest;
+import com.vaadin.shared.util.SharedUtil;
+import com.vaadin.tests.components.AbstractTestUIWithLog;
+import com.vaadin.ui.Alignment;
+import com.vaadin.ui.Button;
+import com.vaadin.ui.Button.ClickEvent;
+import com.vaadin.ui.Button.ClickListener;
+import com.vaadin.ui.CheckBox;
+import com.vaadin.ui.ComboBox;
+import com.vaadin.ui.Field;
+import com.vaadin.ui.GridLayout;
+import com.vaadin.ui.HorizontalLayout;
+import com.vaadin.ui.TextField;
+
+@Theme("valo")
+public abstract class AbstractBasicCrud extends AbstractTestUIWithLog {
+
+ protected AbstractForm form;
+ protected static String[] columns = new String[] { "firstName", "lastName",
+ "gender", "birthDate", "age", "alive", "address.streetAddress",
+ "address.postalCode", "address.city", "address.country" };
+ protected BeanItemContainer<ComplexPerson> container = ComplexPerson
+ .createContainer(100);;
+ {
+ container.addNestedContainerBean("address");
+ }
+ protected ComboBox formType;
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see com.vaadin.tests.components.AbstractTestUI#setup(com.vaadin.server.
+ * VaadinRequest)
+ */
+ @Override
+ protected void setup(VaadinRequest request) {
+ getLayout().setSizeFull();
+ getLayout().setSpacing(true);
+ getContent().setSizeFull();
+ form = new CustomForm();
+
+ formType = new ComboBox();
+ formType.setNullSelectionAllowed(false);
+ formType.setWidth("300px");
+ formType.addItem(form);
+ formType.setValue(form);
+ formType.addItem(new AutoGeneratedForm(TextField.class));
+ formType.addItem(new AutoGeneratedForm(Field.class));
+ Iterator<?> iterator = formType.getItemIds().iterator();
+ formType.setItemCaption(iterator.next(), "TextField based form");
+ formType.setItemCaption(iterator.next(),
+ "Auto generated form (TextFields)");
+ formType.setItemCaption(iterator.next(),
+ "Auto generated form (Any fields)");
+ formType.addValueChangeListener(new ValueChangeListener() {
+ @Override
+ public void valueChange(ValueChangeEvent event) {
+ AbstractForm oldForm = form;
+ form = (AbstractForm) formType.getValue();
+ replaceComponent(oldForm, form);
+ }
+ });
+
+ addComponent(formType);
+
+ }
+
+ public class CustomForm extends AbstractForm {
+
+ private TextField firstName = new TextField("First name");
+ private TextField lastName = new TextField("Last name");
+ private TextField gender = new TextField("Gender");
+ private TextField birthDate = new TextField("Birth date");
+ private TextField age = new TextField("Age");
+ private CheckBox alive = new CheckBox("Alive");
+
+ @PropertyId("address.streetAddress")
+ private TextField address_streetAddress = new TextField(
+ "Street address");
+ @PropertyId("address.postalCode")
+ private TextField address_postalCode = new TextField("Postal code");
+ @PropertyId("address.city")
+ private TextField address_city = new TextField("City");
+ @PropertyId("address.country")
+ private TextField address_country = new TextField("Country");
+
+ public CustomForm() {
+ fieldGroup.bindMemberFields(this);
+
+ address_postalCode.setNullRepresentation("");
+ gender.setNullRepresentation("");
+ age.setNullRepresentation("");
+ address_country.setNullRepresentation("");
+ birthDate.setNullRepresentation("");
+
+ setDefaultComponentAlignment(Alignment.MIDDLE_LEFT);
+ addComponents(firstName, lastName, gender, birthDate, age, alive,
+ address_streetAddress, address_postalCode, address_city,
+ address_country);
+
+ HorizontalLayout hl = new HorizontalLayout(save, cancel);
+ hl.setSpacing(true);
+ addComponent(hl);
+
+ }
+
+ }
+
+ protected abstract void deselectAll();
+
+ public class AbstractForm extends GridLayout {
+ protected Button save = new Button("Save");
+ protected Button cancel = new Button("Cancel");
+
+ protected BeanFieldGroup<ComplexPerson> fieldGroup = new BeanFieldGroup<ComplexPerson>(
+ ComplexPerson.class);
+
+ public AbstractForm() {
+ super(5, 1);
+ setSpacing(true);
+ setId("form");
+ save.addClickListener(new ClickListener() {
+ @Override
+ public void buttonClick(ClickEvent event) {
+ try {
+ fieldGroup.commit();
+ log("Saved " + fieldGroup.getItemDataSource());
+ } catch (CommitException e) {
+ log("Commit failed: " + e.getMessage());
+ }
+ }
+ });
+ cancel.addClickListener(new ClickListener() {
+ @Override
+ public void buttonClick(ClickEvent event) {
+ log("Discarded " + fieldGroup.getItemDataSource());
+ deselectAll();
+ }
+ });
+ }
+
+ public void edit(BeanItem<ComplexPerson> item) {
+ fieldGroup.setItemDataSource(item);
+ }
+ }
+
+ public class AutoGeneratedForm extends AbstractForm {
+
+ public AutoGeneratedForm(Class<? extends Field> class1) {
+ for (String p : columns) {
+ Field f = fieldGroup.getFieldFactory().createField(
+ container.getType(p), class1);
+ f.setCaption(SharedUtil.propertyIdToHumanFriendly(p));
+ fieldGroup.bind(f, p);
+ addComponent(f);
+ }
+
+ HorizontalLayout hl = new HorizontalLayout(save, cancel);
+ hl.setSpacing(true);
+ addComponent(hl);
+ }
+
+ }
+}