aboutsummaryrefslogtreecommitdiffstats
path: root/uitest/src/main/java/com/vaadin/tests/components/menubar/MenuBarsWithNesting.java
blob: 2cdc386004f36f3ccb891df40f7b0049e54db58c (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
package com.vaadin.tests.components.menubar;

import com.vaadin.icons.VaadinIcons;
import com.vaadin.server.Resource;
import com.vaadin.server.ThemeResource;
import com.vaadin.server.VaadinRequest;
import com.vaadin.tests.components.AbstractReindeerTestUI;
import com.vaadin.ui.Label;
import com.vaadin.ui.MenuBar;
import com.vaadin.ui.MenuBar.MenuItem;

/**
 * A UI for testing VMenuBar.getSubPartElement(String). The UI contains two
 * MenuBars, one without icons and one containing items with and without icons.
 * Some of the icons are textual (using VaadinIcons) and should behave like
 * items with image icons: the icon should not be considered to be a part of the
 * item's caption.
 *
 * @author Vaadin
 */
@SuppressWarnings("serial")
public class MenuBarsWithNesting extends AbstractReindeerTestUI {

    // The label displays the last selection.
    private final Label label = new Label("Initial content");

    // The captions and icons used in the second MenuBar.
    public static final String[] itemNames = { "Icon item", "Arrow down",
            "Arrow up", "Warning" };
    private static final Resource[] itemIcons = {
            new ThemeResource("window/img/restore.png"), VaadinIcons.ARROW_DOWN,
            VaadinIcons.ARROW_UP, VaadinIcons.WARNING };

    // The last menu item is nested with the following submenu items.
    public static final String[] nestedItemnames = { "No icon", "Font icon",
            "Image icon" };
    private static final Resource[] nestedItemIcons = { null, VaadinIcons.LINK,
            new ThemeResource("window/img/restore.png") };

    private MenuBar.Command selectionCommand;

    @Override
    protected void setup(VaadinRequest request) {
        selectionCommand = new MenuBar.Command() {
            @Override
            public void menuSelected(MenuItem selectedItem) {
                label.setValue(selectedItem.getText());
            }
        };
        addComponent(createFirstMenuBar());
        addComponent(createSecondMenuBar());
        addComponent(label);
    }

    /*
     * Returns a menu bar with three levels of nesting but no icons.
     */
    private MenuBar createFirstMenuBar() {
        MenuBar menuBar = new MenuBar();
        MenuItem file = menuBar.addItem("File", null);
        file.addItem("Open", selectionCommand);
        file.addItem("Save", selectionCommand);
        file.addItem("Save As..", selectionCommand);
        file.addSeparator();
        MenuItem export = file.addItem("Export..", null);
        export.addItem("As PDF...", selectionCommand);
        export.addItem("As Doc...", selectionCommand);
        file.addSeparator();
        file.addItem("Exit", selectionCommand);

        MenuItem edit = menuBar.addItem("Edit", null);
        edit.addItem("Copy", selectionCommand);
        edit.addItem("Cut", selectionCommand);
        edit.addItem("Paste", selectionCommand);

        menuBar.addItem("Help", selectionCommand);

        MenuItem disabled = menuBar.addItem("Disabled", null);
        disabled.setEnabled(false);
        disabled.addItem("Can't reach", selectionCommand);

        return menuBar;
    }

    /*
     * Returns a menu bar containing items with icons. The last menu item is
     * nested and its submenu contains items with and without icons.
     */
    private MenuBar createSecondMenuBar() {
        MenuBar menuBar = new MenuBar();
        int n = itemNames.length;
        for (int i = 0; i < n - 1; i++) {
            menuBar.addItem(itemNames[i], itemIcons[i], selectionCommand);
        }
        MenuItem last = menuBar.addItem(itemNames[n - 1], itemIcons[n - 1],
                null);
        for (int i = 0; i < nestedItemnames.length; i++) {
            last.addItem(nestedItemnames[i], nestedItemIcons[i],
                    selectionCommand);
        }
        return menuBar;
    }

    @Override
    protected String getTestDescription() {
        return "This UI is used for testing subpart functionality of MenuBar. The "
                + "functionality is used in TestBench tests.";
    }

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