0

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.

vinod raj
  • 69
  • 2
  • 10

1 Answers1

1

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);
}
greg-449
  • 109,219
  • 232
  • 102
  • 145
  • Can you tell in which function i need to create the menu manager for the sub menu @greg-449 – vinod raj Dec 08 '14 at 11:06
  • If i add that part of code to fillContextMenu function it is throwing error @greg-449 – vinod raj Dec 08 '14 at 11:27
  • the code what i have added is contextMenu.add(new Action("Set timeout") { IMenuManager submenu = new MenuManager("Sub menu title"); contextMenu.add(submenu); @Override public void run() { })}; – vinod raj Dec 08 '14 at 12:02
  • in the submenu it displays the static string but i need the user to enter the value inplace of static .so how can it be made – vinod raj Dec 08 '14 at 12:41
  • Menus don't support inputing values – greg-449 Dec 08 '14 at 13:01
  • ok thanks for the info . Can we generate the sub context menu in sucha way that it lists all the contents in the list.for eg the list contains [a,b,c,d,e] so now main menu has menu A and the submenu must contain all the elements in the list can this be done – vinod raj Dec 08 '14 at 13:08
  • Just loop through the list adding an action for each entry – greg-449 Dec 08 '14 at 13:20
  • when i press any button in the submenu how can i get the name of the submenu for ex the name/title given for submenu item which is pressed. – vinod raj Dec 16 '14 at 13:10