aboutsummaryrefslogtreecommitdiffstats
path: root/uitest/src/main/java/com/vaadin/tests/push/MakeComponentVisibleWithPush.java
blob: 82e1594f294b67a969ac3dbb854b4900fd341a9f (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
package com.vaadin.tests.push;

import java.util.ArrayList;
import java.util.List;

import com.vaadin.annotations.Push;
import com.vaadin.server.VaadinRequest;
import com.vaadin.tests.components.AbstractTestUI;
import com.vaadin.ui.Button;
import com.vaadin.ui.Grid;
import com.vaadin.ui.UI;
import com.vaadin.ui.VerticalLayout;

@Push
public class MakeComponentVisibleWithPush extends AbstractTestUI {

    private VerticalLayout rootLayout;
    private Grid<Person> grid;
    private SearchThread searchThread;
    private List<Person> data;

    @Override
    protected void setup(VaadinRequest request) {
        data = new ArrayList<>();

        rootLayout = new VerticalLayout();
        setContent(rootLayout);

        grid = new Grid<Person>();
        grid.addColumn(Person::getName);
        grid.setVisible(false);
        rootLayout.addComponent(grid);

        Button doUpdateButton = new Button("Do Update", event -> {
            try {
                doUpdate();
            } catch (InterruptedException e) {
            }
        });

        rootLayout.addComponent(doUpdateButton);
    }

    private void doUpdate() throws InterruptedException {

        cancelSuggestThread();

        grid.setVisible(false);
        grid.setItems(data);

        UI ui = UI.getCurrent();
        searchThread = new SearchThread(ui);
        searchThread.start();

    }

    class SearchThread extends Thread {
        private UI ui;

        public SearchThread(UI ui) {
            this.ui = ui;
        }

        @Override
        public void run() {
            data = new ArrayList<Person>(data);
            data.add(new Person("Person " + (data.size() + 1)));

            if (!searchThread.isInterrupted()) {
                ui.access(() -> {
                    grid.setItems(data);
                    grid.setVisible(true);
                });
            }
        }

    }

    private void cancelSuggestThread() {

        if ((searchThread != null) && !searchThread.isInterrupted()) {
            searchThread.interrupt();
            searchThread = null;
        }
    }

    class Person {

        private String name;

        public Person(String name) {
            this.name = name;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }

}