aboutsummaryrefslogtreecommitdiffstats
path: root/uitest/src/main/java/com/vaadin/tests/components/table/KeyControl.java
blob: ab9b51f485b65ddecb91c32b3a7b94eee3a6e49e (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
package com.vaadin.tests.components.table;

import java.util.Set;

import com.vaadin.tests.components.TestBase;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.v7.data.Container;
import com.vaadin.v7.data.Item;
import com.vaadin.v7.data.util.IndexedContainer;
import com.vaadin.v7.ui.Table;

@SuppressWarnings("serial")
public class KeyControl extends TestBase {

    private final Label selected1 = new Label("No selected items.");
    private final Table table1 = new Table("Read only table");

    private final Label selected2 = new Label("No selected items");
    private final Table table2 = new Table("Selectable table");

    private final Label selected3 = new Label("Not selected items");
    private final Table table3 = new Table("Multi-selectable table");

    @Override
    protected void setup() {
        HorizontalLayout layout = new HorizontalLayout();
        layout.setSpacing(true);
        addComponent(layout);

        // Create read only table
        VerticalLayout layout1 = new VerticalLayout();
        layout1.setSpacing(true);

        table1.setContainerDataSource(createContainer());
        table1.setWidth("300px");
        table1.setHeight("300px");
        table1.setImmediate(true);
        layout1.addComponent(table1);

        table1.addValueChangeListener(event -> {
            @SuppressWarnings("unchecked")
            Set<String> value = (Set<String>) table1.getValue();
            selected1.setValue(value + " TOTAL: " + value.size());
        });

        layout1.addComponent(selected1);
        layout.addComponent(layout1);

        // Create single select table
        VerticalLayout layout2 = new VerticalLayout();
        layout2.setSpacing(true);

        table2.setContainerDataSource(createContainer());
        table2.setSelectable(true);
        table2.setWidth("300px");
        table2.setHeight("300px");
        table2.setImmediate(true);
        layout2.addComponent(table2);

        table2.addValueChangeListener(event -> {
            String value = table2.getValue() == null ? "No selected items"
                    : table2.getValue().toString();
            selected2.setValue(value);
        });

        layout2.addComponent(selected2);
        layout.addComponent(layout2);

        // Create multi select table
        VerticalLayout layout3 = new VerticalLayout();
        layout3.setSpacing(true);

        table3.setContainerDataSource(createContainer());
        table3.setSelectable(true);
        table3.setMultiSelect(true);
        table3.setWidth("300px");
        table3.setHeight("300px");
        table3.setImmediate(true);
        layout3.addComponent(table3);

        table3.addValueChangeListener(event -> {
            @SuppressWarnings("unchecked")
            Set<String> value = (Set<String>) table3.getValue();
            selected3.setValue(value.isEmpty() ? "No selected items"
                    : value + ": Total " + value.size() + " items");
        });

        selected3.setWidth("300px");
        selected3.setHeight("500px");
        layout3.addComponent(selected3);
        layout.addComponent(layout3);
    }

    @Override
    protected String getDescription() {
        return "Add keyboard control to the Table";
    }

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

    private Container createContainer() {
        IndexedContainer container = new IndexedContainer();
        container.addContainerProperty("col1", String.class, "");
        container.addContainerProperty("col2", String.class, "");
        container.addContainerProperty("col3", String.class, "");
        container.addContainerProperty("col4", String.class, "");
        container.addContainerProperty("col5", String.class, "");
        container.addContainerProperty("col6", String.class, "");

        for (int i = 0; i < 100; i++) {
            Item item = container.addItem("item " + i);
            item.getItemProperty("col1").setValue("First column " + i);
            item.getItemProperty("col2").setValue("Second column " + i);
            item.getItemProperty("col3").setValue("Third column" + i);
            item.getItemProperty("col4").setValue("Fourth column" + i);
            item.getItemProperty("col5").setValue("Fifth column" + i);
            item.getItemProperty("col6").setValue("Sixth column" + i);
        }

        return container;
    }

}