I want to show a popup menu in JFace's TreeViewer
.
The menu should contain 3 constant menu items that never change, and additional item that varies depending to a tree node that was clicked on (selected).
One option is to use setRemoveAllWhenShown(true)
, but this will delete all menu items each time including the constant items.
I want to avoid that.
So to conclude my task:
- If use right clicks on tree without selecting any node, show just the constant items.
- If use right clicks on specific node, show the constant items (remove the previous additional item if exists) and add additional item for this node (it also can be replaced if this option is available).
My code so far:
//Add Some Actions
menuManager.add(..);
menuManager.add(..);
menuManager.add(..);
menuManager.add(new Separator());
//This will delete all items inluding the constant, I want to avoid that
//menuManager.setRemoveAllWhenShown(true);
menuManager.addMenuListener(new IMenuListener() {
public void menuAboutToShow(IMenuManager manager) {
IStructuredSelection selection = (IStructuredSelection) mTreeViewer.getSelection();
if (!selection.isEmpty()) {
BaseItm selected = (BaseItm) selection.getFirstElement();
if (selected instanceof sometype) {
//Remove additional item IF exists
manager.add(sepcificActionForThisNode);
}
}
}
});
add all Actions and use
javax.swing.JComponent#setVisible(boolean)