I am trying to hide/unhide a submenu item (menumanager) from the context (popup menu)
when specific node is selected/not selected in the tree.
Although the setVisible methods triggered as expected it doesn’t effect.
The Code:
TreeViewer tViewer;
tViewer = new TreeViewer(parent, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL);
tViewer.setLabelProvider(new WorkbenchLabelProvider());
tViewer.setContentProvider(new BaseWorkbenchContentProvider());
tViewer.setInput(viewFactory.getInstance().getRoot());
final MenuManager menuMain = new MenuManager("Main",null);
menuMain.add(mActionClose);
MenuManager menuManager = new MenuManager("#PopupMenu", "contextMenu");
menuManager.add(menuMain);
menuManager.addMenuListener(new IMenuListener() {
public void menuAboutToShow(IMenuManager manager) {
//Get the selected node in tree
IStructuredSelection selection = (IStructuredSelection) tViewer.getSelection();
if (!selection.isEmpty()) { //If something selected
NNodeBase ob = (NNodeBase) selection.getFirstElement(); //Get the base class of node
if (!(ob instanceof NTMModel)) {
menuMain.setVisible(false);
}
else
menuMain.setVisible(true);
}
}
});
Menu menu = menuManager.createContextMenu(tViewer.getControl());
tViewer.getControl().setMenu(menu);
You must set the manager to recreate the menu before it is shown:
Then, in
menuAboutToShow()you add the items, testing the condition you need:You don’t need
menuMainanymore.