aboutsummaryrefslogtreecommitdiffstats
path: root/uitest/src/com/vaadin/tests/components/customfield/AbstractNestedFormExample.java
blob: 07c740a96dddc0ecd22266cd1bf7c57f2fd81951 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package com.vaadin.tests.components.customfield;

import com.vaadin.data.Item;
import com.vaadin.data.Property;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.tests.components.TestBase;
import com.vaadin.tests.util.Person;
import com.vaadin.ui.Table;

/**
 * Demonstrate the use of a form as a custom field within another form.
 */
public abstract class AbstractNestedFormExample extends TestBase {
    private NestedPersonForm personForm;
    private boolean embeddedAddress;

    public void setup(boolean embeddedAddress) {
        this.embeddedAddress = embeddedAddress;

        addComponent(getPersonTable());
    }

    /**
     * Creates a table with two person objects
     */
    public Table getPersonTable() {
        Table table = new Table();
        table.setPageLength(5);
        table.setSelectable(true);
        table.setImmediate(true);
        table.setNullSelectionAllowed(true);
        table.addContainerProperty("Name", String.class, null);
        table.addListener(getTableValueChangeListener());
        Person person = new Person("Teppo", "Testaaja",
                "teppo.testaaja@example.com", "", "Ruukinkatu 2–4", 20540,
                "Turku");
        Person person2 = new Person("Taina", "Testaaja",
                "taina.testaaja@example.com", "", "Ruukinkatu 2–4", 20540,
                "Turku");
        Item item = table.addItem(person);
        item.getItemProperty("Name").setValue(
                person.getFirstName() + " " + person.getLastName());
        item = table.addItem(person2);
        item.getItemProperty("Name").setValue(
                person2.getFirstName() + " " + person2.getLastName());
        return table;
    }

    /**
     * Creates value change listener for the table
     */
    private Property.ValueChangeListener getTableValueChangeListener() {
        return new Property.ValueChangeListener() {

            @Override
            public void valueChange(ValueChangeEvent event) {
                if (personForm != null) {
                    removeComponent(personForm);
                }
                if (event.getProperty().getValue() != null) {
                    personForm = new NestedPersonForm((Person) event
                            .getProperty().getValue(), embeddedAddress);
                    personForm.setWidth("350px");
                    addComponent(personForm);
                }
            }

        };
    }

    @Override
    protected Integer getTicketNumber() {
        return null;
    }

}