summaryrefslogtreecommitdiffstats
path: root/uitest/src/com/vaadin/tests/components/NoLayoutUpdateWhichNeedsLayout.java
blob: 9f63970f85365b725ecc59b38619d73dbd4abd84 (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
package com.vaadin.tests.components;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import com.vaadin.annotations.Theme;
import com.vaadin.server.VaadinRequest;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.GridLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.ProgressBar;
import com.vaadin.ui.UI;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;

@SuppressWarnings("serial")
@Theme("valo")
public class NoLayoutUpdateWhichNeedsLayout extends UI {

    private ProgressBar progressBar;
    private Window w;

    @Override
    protected void init(VaadinRequest request) {
        final VerticalLayout layout = new VerticalLayout();
        layout.setMargin(true);
        setContent(layout);

        setPollInterval(1000);

        Button button = new Button(
                "1. Execute scheduled task and show progress in a window");
        button.setId("openWindow");
        button.addClickListener(new Button.ClickListener() {

            private Window w2;

            @Override
            public void buttonClick(ClickEvent event) {
                GridLayout glo = new GridLayout();
                progressBar = new ProgressBar();
                progressBar.setIndeterminate(false);
                progressBar.setId("progress");
                glo.addComponent(progressBar);

                w2 = new Window("test");
                w2.setWidth("600px");
                w2.setHeight("200px");
                w2.setContent(glo);
                w2.center();
                Button closeB = new Button(
                        "2. Click to close after the progress is updated");
                closeB.setId("closeWindow");
                closeB.addClickListener(new ClickListener() {
                    @Override
                    public void buttonClick(ClickEvent event) {
                        w2.close();
                        w2 = null;
                    }
                });
                glo.addComponent(closeB);
                addWindow(w2);

                scheduleTask();
            }
        });

        Button openWin = new Button("3. Finally, Click to open a new Window");
        openWin.addClickListener(new ClickListener() {
            @Override
            public void buttonClick(ClickEvent event) {
                w = new Window("test");
                w.setWidth("300px");
                w.setHeight("300px");
                w.setContent(new VerticalLayout(new Label(
                        "simple test label component")));
                w.center();
                getUI().addWindow(w);
            }
        });

        Button stopPolling = new Button("Stop polling", new ClickListener() {

            @Override
            public void buttonClick(ClickEvent event) {
                setPollInterval(-1);
            }
        });
        layout.addComponents(button, openWin, stopPolling);
    }

    protected void scheduleTask() {
        Thread t = new Thread() {

            @Override
            public void run() {
                getUI().access(new Runnable() {
                    @Override
                    public void run() {
                        updateProgresBar(50);
                    }
                });
            }
        };
        ScheduledExecutorService worker = Executors
                .newSingleThreadScheduledExecutor();
        worker.schedule(t, 2, TimeUnit.SECONDS);
    }

    public void updateProgresBar(int pc) {
        progressBar.setValue((float) (pc / 100.0));
    }

}