aboutsummaryrefslogtreecommitdiffstats
path: root/uitest/src/main/java/com/vaadin/tests/components/combobox/ComboBoxSelectingNewItemValueChange.java
blob: a6f5e8e2970256a4e5e4e5bbaf916113753a86e3 (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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package com.vaadin.tests.components.combobox;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import com.vaadin.server.VaadinRequest;
import com.vaadin.shared.ui.ContentMode;
import com.vaadin.shared.ui.combobox.ComboBoxClientRpc;
import com.vaadin.ui.Button;
import com.vaadin.ui.CheckBox;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.Label;
import com.vaadin.ui.Notification;

public class ComboBoxSelectingNewItemValueChange extends ComboBoxSelecting {

    final class CustomComboBox extends ComboBox<String> {
        private CustomComboBox(String caption, Collection<String> options) {
            super(caption, options);
        }

        public ComboBoxClientRpc getComboBoxClientRpc() {
            return getRpcProxy(ComboBoxClientRpc.class);
        }
    }

    CustomComboBox comboBox;
    List<String> items = new ArrayList<>();
    int valueChangeEventCount = 0;
    int selectionChangeEventCount = 0;
    Label valueLabel = new Label();
    Label valueChangeLabel = new Label(null, ContentMode.HTML);
    CheckBox delay = new CheckBox("Slow adding process", false);
    CheckBox reject = new CheckBox("Reject new values", false);
    CheckBox noSelection = new CheckBox("Don't select new values", false);

    @Override
    protected void setup(VaadinRequest request) {
        initItems();
        comboBox = new CustomComboBox(null, items);
        comboBox.setTextInputAllowed(true);
        comboBox.setEmptySelectionAllowed(true);

        comboBox.addValueChangeListener(event -> {
            String value = event.getValue();
            if (value != null) {
                valueLabel.setValue(value);
            } else {
                valueLabel.setValue("null");
            }
        });

        configureNewItemHandling();

        comboBox.addValueChangeListener(e -> {
            ++valueChangeEventCount;
            updateLabel(e.isUserOriginated());
        });

        comboBox.addSelectionListener(e -> {
            ++selectionChangeEventCount;
            updateLabel(e.isUserOriginated());
        });

        Button checkButton = new Button("Check ComboBox value", e -> {
            Notification.show("selected: " + comboBox.getValue());
        });

        Button resetButton = new Button("Reset options", e -> {
            comboBox.setValue(null);
            initItems();
            comboBox.getDataProvider().refreshAll();
            valueLabel.setValue("");
            valueChangeLabel.setValue("Reset");
            valueChangeEventCount = 0;
            selectionChangeEventCount = 0;
        });

        valueLabel.setId("value");
        valueChangeLabel.setId("change");
        delay.setId("delay");
        reject.setId("reject");
        noSelection.setId("noSelection");
        resetButton.setId("reset");

        addComponents(comboBox, valueLabel, valueChangeLabel, checkButton,
                resetButton, delay, reject, noSelection);
    }

    @SuppressWarnings("deprecation")
    protected void configureNewItemHandling() {
        comboBox.setNewItemHandler(text -> {
            if (Boolean.TRUE.equals(delay.getValue())) {
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e1) {
                    e1.printStackTrace();
                }
            }
            if (Boolean.TRUE.equals(reject.getValue())) {
                valueChangeLabel.setValue("item " + text + " discarded");
                comboBox.getComboBoxClientRpc().newItemNotAdded(text);
            } else {
                items.add(text);
                Collections.sort(items);
                valueChangeLabel
                        .setValue("adding new item... count: " + items.size());
                if (Boolean.TRUE.equals(noSelection.getValue())) {
                    comboBox.getComboBoxClientRpc().newItemNotAdded(text);
                }
                comboBox.getDataProvider().refreshAll();
            }
        });
    }

    private void initItems() {
        items.clear();
        for (char c = 'a'; c <= 'z'; c++) {
            for (int i = 0; i < 100; i++) {
                items.add("" + c + i);
            }
        }
    }

    private void updateLabel(boolean userOriginated) {
        valueChangeLabel.setValue("Value change count: " + valueChangeEventCount
                + "\nSelection change count: " + selectionChangeEventCount
                + "\nuser originated: " + userOriginated);
    }

    @Override
    protected String getTestDescription() {
        return "New item should trigger value change when accepted "
                + "and restore the field to previous value when rejected.";
    }

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