1 package com.vaadin.tests.components.abstractfield;
3 import java.util.HashSet;
4 import java.util.Locale;
7 import com.vaadin.server.VaadinSession;
8 import com.vaadin.tests.components.TestBase;
9 import com.vaadin.tests.util.Log;
10 import com.vaadin.ui.Component;
11 import com.vaadin.v7.data.Container;
12 import com.vaadin.v7.data.Item;
13 import com.vaadin.v7.data.Property.ValueChangeEvent;
14 import com.vaadin.v7.data.Property.ValueChangeListener;
15 import com.vaadin.v7.ui.AbstractField;
16 import com.vaadin.v7.ui.ComboBox;
18 public abstract class AbstractComponentDataBindingTest extends TestBase
19 implements ValueChangeListener {
20 private static final Object CAPTION = "CAPTION";
21 private Log log = new Log(5);
22 private ComboBox localeSelect;
25 protected void setup() {
27 localeSelect = createLocaleSelect();
28 addComponent(localeSelect);
30 // Causes fields to be created
31 localeSelect.setValue(Locale.US);
34 private ComboBox createLocaleSelect() {
35 ComboBox cb = new ComboBox("Locale");
36 cb.addContainerProperty(CAPTION, String.class, "");
37 cb.setItemCaptionPropertyId(CAPTION);
38 cb.setNullSelectionAllowed(false);
39 for (Locale l : Locale.getAvailableLocales()) {
40 Item i = cb.addItem(l);
41 i.getItemProperty(CAPTION)
42 .setValue(l.getDisplayName(Locale.ENGLISH));
44 ((Container.Sortable) cb.getContainerDataSource())
45 .sort(new Object[] { CAPTION }, new boolean[] { true });
46 cb.setImmediate(true);
47 cb.addListener(new ValueChangeListener() {
50 public void valueChange(ValueChangeEvent event) {
51 updateLocale((Locale) localeSelect.getValue());
57 protected void updateLocale(Locale locale) {
58 VaadinSession.getCurrent().setLocale(locale);
59 for (Component c : fields) {
66 protected abstract void createFields();
68 private Set<Component> fields = new HashSet<Component>();
71 protected void addComponent(Component c) {
72 super.addComponent(c);
73 if (c instanceof AbstractField) {
74 configureField((AbstractField<?>) c);
75 if (c != localeSelect) {
81 protected void configureField(AbstractField<?> field) {
82 field.setImmediate(true);
83 field.addListener(this);
87 protected String getDescription() {
92 protected Integer getTicketNumber() {
97 public void valueChange(ValueChangeEvent event) {
98 AbstractField field = (AbstractField) event.getProperty();
99 // if (field == localeSelect) {
103 Object newValue = field.getValue();
104 if (newValue != null) {
105 newValue = newValue + " (" + newValue.getClass().getName() + ")";
108 String message = "Value of " + field.getCaption() + " changed to "
110 if (field.getPropertyDataSource() != null) {
111 Object dataSourceValue = field.getPropertyDataSource().getValue();
112 message += "Data model value is " + dataSourceValue;
113 message += " (" + field.getPropertyDataSource().getType().getName()