aboutsummaryrefslogtreecommitdiffstats
path: root/uitest/src/main/java/com/vaadin/tests/actions/ActionsOnDetachedComponents.java
blob: e1f194d32fc8eb01a2a92d18bb060090202a6354 (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
package com.vaadin.tests.actions;

import java.util.concurrent.atomic.AtomicInteger;

import com.vaadin.event.Action;
import com.vaadin.event.ShortcutAction;
import com.vaadin.server.VaadinRequest;
import com.vaadin.tests.components.AbstractTestUIWithLog;
import com.vaadin.ui.Button;
import com.vaadin.ui.Label;
import com.vaadin.ui.Panel;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.v7.ui.Table;

/**
 * @author Vaadin Ltd
 */
public class ActionsOnDetachedComponents extends AbstractTestUIWithLog {

    private final AtomicInteger clickCounter = new AtomicInteger();
    private Panel mainLayout;

    @Override
    protected void setup(VaadinRequest request) {
        clickCounter.set(0);
        mainLayout = new Panel();
        mainLayout.setSizeFull();
        mainLayout.setContent(new ShortCutALayer());
        addComponent(mainLayout);

    }

    private Table tableWithActions(final LayerSwitcher switcher) {
        Table table = new Table();
        table.addContainerProperty("id", Integer.class, 0);
        table.addItems(1, 2, 3, 4);
        table.addActionHandler(new Action.Handler() {

            Action action = new Action("Table action");

            @Override
            public Action[] getActions(Object target, Object sender) {
                return new Action[] { action };
            }

            @Override
            public void handleAction(Action action, Object sender,
                    Object target) {
                if (action == this.action) {
                    log("Fired action for tableAction");
                    switcher.switchLayers();
                }
            }
        });
        return table;
    }

    private interface LayerSwitcher {
        void switchLayers();
    }

    private class ShortCutALayer extends VerticalLayout
            implements LayerSwitcher {
        public ShortCutALayer() {
            setId("layer-A");
            Label l = new Label(getClass().getSimpleName());
            Button b = new Button("click here or press 'a'");
            b.setId("btn-A");
            b.setClickShortcut(ShortcutAction.KeyCode.A);
            b.addClickListener(event -> {
                log("Fired action for btn-A");
                switchLayers();
            });
            addComponents(l, b);
            Table table = tableWithActions(this);
            addComponent(table);
        }

        @Override
        public void switchLayers() {
            try {
                Thread.sleep(1000); // do something important

            } catch (InterruptedException e) {
            }
            mainLayout.setContent(new ShortCutBLayer());
        }
    }

    private class ShortCutBLayer extends VerticalLayout
            implements LayerSwitcher {
        public ShortCutBLayer() {
            setId("layer-B");
            Label l = new Label(getClass().getSimpleName());
            Button b = new Button("click here or press 'b'");
            b.setId("btn-B");
            b.setClickShortcut(ShortcutAction.KeyCode.B);
            b.addClickListener(event -> {
                log("Fired action for btn-B");
                switchLayers();
            });
            addComponents(l, b);
            Table table = tableWithActions(this);
            addComponent(table);
        }

        @Override
        public void switchLayers() {
            try {
                Thread.sleep(1000); // do something important
            } catch (InterruptedException e) {
            }
            mainLayout.setContent(new ShortCutALayer());
        }
    }
}