aboutsummaryrefslogtreecommitdiffstats
path: root/uitest/src/com/vaadin/tests/components/abstractfield/AbstractComponentDataBindingTest.java
blob: 327ed0f86b3dd5e8d9eebfed87f0afdbe537575e (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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package com.vaadin.tests.components.abstractfield;

import java.util.HashSet;
import java.util.Locale;
import java.util.Set;

import com.vaadin.Application;
import com.vaadin.data.Container;
import com.vaadin.data.Item;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.Property.ValueChangeListener;
import com.vaadin.tests.components.TestBase;
import com.vaadin.tests.util.Log;
import com.vaadin.ui.AbstractField;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.Component;

public abstract class AbstractComponentDataBindingTest extends TestBase
        implements ValueChangeListener {
    private static final Object CAPTION = "CAPTION";
    private Log log = new Log(5);
    private ComboBox localeSelect;

    @Override
    protected void setup() {
        addComponent(log);
        localeSelect = createLocaleSelect();
        addComponent(localeSelect);

        // Causes fields to be created
        localeSelect.setValue(Locale.US);
    }

    private ComboBox createLocaleSelect() {
        ComboBox cb = new ComboBox("Locale");
        cb.addContainerProperty(CAPTION, String.class, "");
        cb.setItemCaptionPropertyId(CAPTION);
        cb.setNullSelectionAllowed(false);
        for (Locale l : Locale.getAvailableLocales()) {
            Item i = cb.addItem(l);
            i.getItemProperty(CAPTION).setValue(
                    l.getDisplayName(Locale.ENGLISH));
        }
        ((Container.Sortable) cb.getContainerDataSource()).sort(
                new Object[] { CAPTION }, new boolean[] { true });
        cb.setImmediate(true);
        cb.addListener(new ValueChangeListener() {

            @Override
            public void valueChange(ValueChangeEvent event) {
                updateLocale((Locale) localeSelect.getValue());
            }
        });
        return cb;
    }

    protected void updateLocale(Locale locale) {
        Application.getCurrent().setLocale(locale);
        for (Component c : fields) {
            removeComponent(c);
        }
        fields.clear();
        createFields();
    }

    protected abstract void createFields();

    private Set<Component> fields = new HashSet<Component>();

    @Override
    protected void addComponent(Component c) {
        super.addComponent(c);
        if (c instanceof AbstractField) {
            configureField((AbstractField<?>) c);
            if (c != localeSelect) {
                fields.add(c);
            }
        }
    }

    protected void configureField(AbstractField<?> field) {
        field.setImmediate(true);
        field.addListener(this);
    }

    @Override
    protected String getDescription() {
        return "";
    }

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

    @Override
    public void valueChange(ValueChangeEvent event) {
        AbstractField field = (AbstractField) event.getProperty();
        // if (field == localeSelect) {
        // return;
        // }

        Object newValue = field.getValue();
        if (newValue != null) {
            newValue = newValue + " (" + newValue.getClass().getName() + ")";
        }

        String message = "Value of " + field.getCaption() + " changed to "
                + newValue + ".";
        if (field.getPropertyDataSource() != null) {
            Object dataSourceValue = field.getPropertyDataSource().getValue();
            message += "Data model value is " + dataSourceValue;
            message += " (" + field.getPropertyDataSource().getType().getName()
                    + ")";
        }
        log.log(message);

    }

}