4

I have created a TreeViewer using JFace, but now I have to add a right click listener to the nodes. When the right click is done it has to show a menu like:

  1. Do something
  2. Do Nothing
  3. Delete

I am trying to do this as follows, but it is throwing a null pointer exception.

MenuManager menuMgr = new MenuManager();
        menuMgr.setRemoveAllWhenShown(true);

        menuMgr.addMenuListener(new IMenuListener() {
            @Override
            public void menuAboutToShow(IMenuManager menuManager) {

                IContributionManager menu = null;
                MenuItem[] items = (MenuItem[]) menu.getItems();
                for (int i = 0; i < items.length; i++)
                    items[i].dispose();

                MenuItem itemCollectionFolder = new MenuItem((Menu) menu, SWT.NONE);
                itemCollectionFolder.setText("Add Something" );

                MenuItem itemNewTestCase = new MenuItem((Menu) menu, SWT.NONE);
                itemNewTestCase.setText("Do Nothing" );

            }
        });

        Control tree = treeViewer.getControl();

        Menu menu = menuMgr.createContextMenu(tree);

        tree.setMenu(menu);
flavio.donze
  • 7,432
  • 9
  • 58
  • 91
vinod raj
  • 69
  • 2
  • 10

1 Answers1

9

Try this, don't forget to call: createContextMenu(viewer);

/**
 * Creates the context menu
 *
 * @param viewer
 */
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) {
    contextMenu.add(new GroupMarker(IWorkbenchActionConstants.MB_ADDITIONS));

    contextMenu.add(new Action("Do Something") {
        @Override
        public void run() {
            // implement this
        }
    });
    contextMenu.add(new Action("Do Nothing") {
        @Override
        public void run() {
            // don't do anything here
        }
    });
    contextMenu.add(new Action("Delete") {
        @Override
        public void run() {
            // implement this
        }
    });
}

To get the selected element of the treeviewer, do this:

IStructuredSelection selection = (IStructuredSelection) viewer.getSelection();
selection.getFirstElement();
selection.toList(); // or if you handle multi selection
flavio.donze
  • 7,432
  • 9
  • 58
  • 91
  • when i import org.eclipse.ui to the imported package for IWorkbenchActionConstants and run the code it throws error as no application id has been found so which is the suitable package for that as i am using e4 application @flavio.donze – vinod raj Dec 08 '14 at 10:10
  • You can actually delete this line, it is used to extend your menu with actions/commands through extension points. I don't think you need this feature. – flavio.donze Dec 08 '14 at 10:28
  • can you show me how can we add sub contextmenu for this context menu and for that sub context menu the user should be able to enter some value to it .So how can this be done @flavio.donze – vinod raj Dec 08 '14 at 12:16
  • best if you create a new question – flavio.donze Dec 08 '14 at 13:38
  • when i press any button in the submenu how can i get the name of the submenu which is pressed – vinod raj Dec 16 '14 at 13:12