How can we create a sub context menu for a treeviewer

296 Views Asked by At

I have created a context menu using right click mouse for a tree viewer and now i need to create a sub context menu for it so that i need to set the values for it .The code to create a context menu is as follows.

protected void createContextMenu(Viewer viewer) {
        MenuManager contextMenu = new MenuManager("#ViewerMenu"); //$NON-NLS-1$
        contextMenu.setRemoveAllWhenShown(true);
        contextMenu.addMenuListener(new IMenuListener() {
            @Override
            public void menuAboutToShow(IMenuManager mgr) {
                fillContextMenu(mgr);
            }
        });

        Menu menu = contextMenu.createContextMenu(viewer.getControl());
        viewer.getControl().setMenu(menu);
    }

    /**
     * Fill dynamic context menu
     *
     * @param contextMenu
     */

    protected void fillContextMenu(IMenuManager contextMenu) {
        //String nnn = null;
        //contextMenu.add(new GroupMarker(IWorkbenchActionConstants.MB_ADDITIONS));
        //contextMenu.add(new GroupMarker(nnn));

        contextMenu.add(new Action("set Iterations") {
            @Override
            public void run() {
                // implement this
            }


        });

        contextMenu.add(new Action("Set timeout") {
            @Override
            public void run() {
                // implement this
            }


        });
    }

so now for the context menu set timeout i need to create sub context menu and user can set the time out value in it.So how can this be done.

1

There are 1 best solutions below

8
On BEST ANSWER

Create a menu MenuManager for the sub-menu:

protected void fillContextMenu(IMenuManager contextMenu) {
    //String nnn = null;
    //contextMenu.add(new GroupMarker(IWorkbenchActionConstants.MB_ADDITIONS));
    //contextMenu.add(new GroupMarker(nnn));

    contextMenu.add(new Action("set Iterations") {
        @Override
        public void run() {
            // implement this
        }
    });

    contextMenu.add(new Action("Set timeout") {
        @Override
        public void run() {
            // implement this
        }
    });

   IMenuManager submenu = new MenuManager("Sub menu title");

   submenu.add(new Action("Sub menu item 1") {
        @Override
        public void run() {
            // implement this
        }
    });

  contextMenu.add(submenu);
}