Sadık
Sadık

Reputation: 4419

Submenu for Context Menu in SWT Text

I want to have a pop-up menu with a simple submenu. On right-click on the SWT Text (commandText). What I want to achieve is this:

A -> D
     E
     F
B
C

So there should be Actions "D,E,F" under the Action "A". "B" and "C" are actions on top level, just like "A". My try is:

private void addCommandTextContextMenu() {
    MenuManager popupMenu = new MenuManager("#PopupMenu");
    popupMenu.setRemoveAllWhenShown(true);

    popupMenu.addMenuListener(new IMenuListener() {
        public void menuAboutToShow(IMenuManager manager) {
            Action aAction = new Action("A") {};
            Action bAction = new Action("B") {};
            Action cAction = new Action("C") {};

            manager.add(aAction);
            manager.add(bAction);
            manager.add(cAction);
        }
    });

    MenuManager subMenu = new MenuManager("#SubMenu");
    subMenu.setRemoveAllWhenShown(true);
    subMenu.addMenuListener(new IMenuListener() {
        public void menuAboutToShow(IMenuManager manager) {
            Action dAction = new Action("D") {};
            Action eAction = new Action("E") {};
            Action fAction = new Action("F") {};

            manager.add(dAction);
            manager.add(eAction);
            manager.add(fAction);
        }
    });

    popupMenu.add(subMenu);

    final Menu menu2 = popupMenu.createContextMenu(commandText);
    commandText.setMenu(menu2);
}

I can only see A, B, C.

I try to add this pop-up menu for an eclipse plugin with Java only because I thought it should be easier than defining a menu in the plugin.xml with commands and handlers.

Upvotes: 0

Views: 488

Answers (1)

greg-449
greg-449

Reputation: 111217

Just create the sub-menu and add the sub menu actions directy to the sub-menu:

    public void menuAboutToShow(final IMenuManager manager) {

        final Action bAction = new Action("B") {};
        final Action cAction = new Action("C") {};
        final Action dAction = new Action("D") {};
        final Action eAction = new Action("E") {};
        final Action fAction = new Action("F") {};

        final MenuManager subMenu = new MenuManager("A");

        subMenu.add(dAction);
        subMenu.add(eAction);
        subMenu.add(fAction);

        manager.add(subMenu);

        manager.add(bAction);
        manager.add(cAction);
    }

Add the sub-menu manager to the top level manager. The name of the sub-menu manager is used for the top level menu item.

Upvotes: 2

Related Questions