aboutsummaryrefslogtreecommitdiffstats
path: root/uitest/src/main/java/com/vaadin/tests/components/panel/PanelShouldRemoveActionHandler.java
blob: 0c0b26cf87676b842fc6e6afbb278f4bd6a560e3 (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
package com.vaadin.tests.components.panel;

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

import com.vaadin.event.Action;
import com.vaadin.event.Action.Handler;
import com.vaadin.event.ShortcutAction;
import com.vaadin.event.ShortcutAction.ModifierKey;
import com.vaadin.tests.components.TestBase;
import com.vaadin.ui.Button;
import com.vaadin.ui.Panel;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.v7.ui.TextField;

public class PanelShouldRemoveActionHandler extends TestBase {

    private Panel panel;

    @Override
    protected String getDescription() {
        return "Adding action handlers to the panel should make them appear on the client side. Removing the action handlers should remove them also from the client side, also if all action handlers are removed.";
    }

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

    @Override
    protected void setup() {
        VerticalLayout layout = new VerticalLayout();
        layout.setMargin(true);
        panel = new Panel("A panel", layout);
        layout.addComponent(new TextField());
        Button add = new Button("Add an action handler", event -> add());
        Button addAnother = new Button("Add another action handler",
                event -> addAnother());
        Button remove = new Button("Remove an action handler",
                event -> remove());
        addComponent(panel);
        addComponent(add);
        addComponent(addAnother);
        addComponent(remove);
    }

    public void remove() {
        panel.setCaption(panel.getCaption() + " - Removed handler");
        panel.removeActionHandler(
                actionHandlers.remove(actionHandlers.size() - 1));
    }

    private List<Handler> actionHandlers = new ArrayList<>();

    public void add() {
        panel.setCaption(panel.getCaption() + " - Added handler");
        Handler actionHandler = new Handler() {

            @Override
            public Action[] getActions(Object target, Object sender) {
                return new Action[] { new ShortcutAction("Ctrl+Left",
                        ShortcutAction.KeyCode.ARROW_LEFT,
                        new int[] { ModifierKey.CTRL }) };
            }

            @Override
            public void handleAction(Action action, Object sender,
                    Object target) {
                getMainWindow().showNotification(
                        "Handling action " + action.getCaption());
            }

        };

        addHandler(actionHandler);
    }

    public void addAnother() {
        Handler actionHandler = new Handler() {

            @Override
            public Action[] getActions(Object target, Object sender) {
                return new Action[] { new ShortcutAction("Ctrl+Right",
                        ShortcutAction.KeyCode.ARROW_RIGHT,
                        new int[] { ModifierKey.CTRL }) };
            }

            @Override
            public void handleAction(Action action, Object sender,
                    Object target) {
                getMainWindow().showNotification(
                        "Handling action " + action.getCaption());
            }

        };

        addHandler(actionHandler);
    }

    private void addHandler(Handler actionHandler) {
        actionHandlers.add(actionHandler);
        panel.addActionHandler(actionHandler);
        panel.setCaption(
                "A panel with " + actionHandlers.size() + " action handlers");

    }
}