aboutsummaryrefslogtreecommitdiffstats
path: root/uitest/src/com/vaadin/tests/containers/sqlcontainer/CheckboxUpdateProblem.java
blob: 3af415217cb2dae96f768081b6bda09343bc9d7a (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
package com.vaadin.tests.containers.sqlcontainer;

import java.sql.SQLException;

import com.vaadin.Application;
import com.vaadin.data.Container.ItemSetChangeEvent;
import com.vaadin.data.Container.ItemSetChangeListener;
import com.vaadin.data.Item;
import com.vaadin.data.Property;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Form;
import com.vaadin.ui.HorizontalSplitPanel;
import com.vaadin.ui.UI.LegacyWindow;
import com.vaadin.ui.Table;

public class CheckboxUpdateProblem extends Application
        implements Property.ValueChangeListener {
    private final DatabaseHelper databaseHelper = new DatabaseHelper();
    private Table testList;
    private final HorizontalSplitPanel horizontalSplit = new HorizontalSplitPanel();

    private TestForm testForm = new TestForm();

    @Override
    public void init() {
        setMainWindow(new LegacyWindow("Test window"));
        horizontalSplit.setSizeFull();
        testList = new Table();

        horizontalSplit.setFirstComponent(testList);
        testList.setSizeFull();
        testList.setContainerDataSource(databaseHelper.getTestContainer());
        testList.setSelectable(true);
        testList.setImmediate(true);
        testList.addListener(this);

        databaseHelper.getTestContainer().addListener(
                new ItemSetChangeListener() {
                    @Override
                    public void containerItemSetChange(ItemSetChangeEvent event) {
                        Object selected = testList.getValue();
                        if (selected != null) {
                            testForm.setItemDataSource(testList
                                    .getItem(selected));
                        }
                    }
                });

        testForm = new TestForm();
        testForm.setItemDataSource(null);

        horizontalSplit.setSecondComponent(testForm);

        getMainWindow().setContent(horizontalSplit);
    }

    @Override
    public void valueChange(ValueChangeEvent event) {

        Property<?> property = event.getProperty();
        if (property == testList) {
            Item item = testList.getItem(testList.getValue());

            if (item != testForm.getItemDataSource()) {
                testForm.setItemDataSource(item);
            }
        }

    }

    private class TestForm extends Form implements Button.ClickListener {

        private final Button save;

        private TestForm() {
            setSizeFull();
            setBuffered(true);
            setInvalidCommitted(false);

            save = new Button("Save", this);
            getFooter().addComponent(save);
            getFooter().setVisible(false);
        }

        @Override
        public void buttonClick(ClickEvent event) {
            if (event.getSource() == save) {
                super.commit();

                try {
                    databaseHelper.getTestContainer().commit();
                    getMainWindow().showNotification("Saved");
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }

        @Override
        public void setItemDataSource(Item newDataSource) {
            super.setItemDataSource(newDataSource);

            if (newDataSource != null) {
                getFooter().setVisible(true);
            } else {
                getFooter().setVisible(false);
            }
        }

    }

}