summaryrefslogtreecommitdiffstats
path: root/src/com/vaadin/tests/tickets/Ticket1598.java
blob: ca2da6d0101949101ec865f403c50e9187f5c4b5 (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
package com.vaadin.tests.tickets;

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

import com.vaadin.Application;
import com.vaadin.terminal.ThemeResource;
import com.vaadin.ui.MenuBar;
import com.vaadin.ui.Window;
import com.vaadin.ui.MenuBar.Command;
import com.vaadin.ui.MenuBar.MenuItem;

public class Ticket1598 extends Application {

    Window main = new Window("MenuBar test");

    final MenuBar menuBar = new MenuBar();

    @Override
    public void init() {
        setMainWindow(main);
        setTheme("default");

        List<MenuItem> itemList = new ArrayList<MenuItem>();
        // Populate the menu bar
        for (int i = 0; i < 6; i++) {
            itemList.add(menuBar.addItem(new String("Menu " + i), null, null));
        }

        MenuItem first = itemList.get(0);

        for (int i = 0; i < 5; i++) {
            first.addItem(new String("Submenu item" + i), null, new Command() {

                public void menuSelected(MenuItem selected) {
                    main.showNotification("Action " + selected.getText());
                }
            });
        }

        MenuItem firstSubItem1 = first.getChildren().get(1);

        for (int i = 0; i < 3; i++) {
            firstSubItem1.addItem(new String("Subsubmenu item" + i), null,
                    new Command() {

                        public void menuSelected(MenuItem selected) {
                            main.showNotification("Action "
                                    + selected.getText());
                        }
                    });
        }
        MenuItem firstSubItem2 = first.getChildren().get(3);

        for (int i = 0; i < 3; i++) {
            firstSubItem2.addItem(new String("Subsubmenu item" + i), null,
                    new Command() {

                        public void menuSelected(MenuItem selected) {
                            main.showNotification("Action "
                                    + selected.getText());
                        }
                    });
        }

        MenuItem second = menuBar.getItems().get(1);

        for (int i = 0; i < 5; i++) {
            second.addItem(new String("Second submenu item" + i), null,
                    new Command() {

                        public void menuSelected(MenuItem selected) {
                            main.showNotification("Action "
                                    + selected.getText());
                        }
                    });
        }

        MenuItem third = menuBar.getItems().get(2);
        third.setIcon(new ThemeResource("icons/16/document.png"));

        for (int i = 2; i <= 3; i++) {
            ((MenuItem) menuBar.getItems().get(i)).setCommand(new Command() {

                public void menuSelected(MenuItem selectedItem) {
                    main.showNotification("Action " + selectedItem.getText());
                }
            });
        }

        final MenuItem fourth = menuBar.getItems().get(3);
        fourth.setText("Add new item");

        fourth.setCommand(new Command() {
            public void menuSelected(MenuItem selected) {
                menuBar.addItem("Newborn", null, null);
            }
        });

        final MenuItem fifth = menuBar.getItems().get(4);
        for (int i = 0; i < 5; i++) {
            fifth.addItem("Another subitem " + i, null);
        }

        final MenuItem last = menuBar.getItems().get(menuBar.getSize() - 1);
        last.setText("Remove me!");

        // A command for removing the selected menuitem
        Command removeCommand = new Command() {

            public void menuSelected(MenuItem selected) {
                MenuItem parent = selected.getParent();
                if (parent != null) {
                    parent.removeChild(selected);
                } else {
                    menuBar.removeItem(selected);
                }
            }
        };

        last.setCommand(removeCommand);

        main.addComponent(menuBar);

    }
}