ContextMenu and TreeItem data [message #1087973] |
Fri, 16 August 2013 07:30  |
Eclipse User |
|
|
|
I want to use a popup menu (contextmenu) on a treeview that can access the properties (name and database key) of the node on which the RMB selection was made.
In Eclipse 3.x we could do the following:
MenuManager menuManager = new MenuManager();
Menu menu = menuManager.createContextMenu(viewer.getTree());
menuMgr.addMenuListener(new IMenuListener() {
@Override
public void menuAboutToShow(IMenuManager manager) {
if (viewer.getSelection().isEmpty()) {
return;
}
if (viewer.getSelection() instanceof IStructuredSelection) {
IStructuredSelection selection = (IStructuredSelection) viewer.getSelection();
Object selectedNode = selection.getFirstElement();
if (selectedNode instanceof NodeModel) {
NodeModel nodeModel = (NodeModel) selectedNode;
String dataKey = nodeModel.getKey();
...
}
}
}
});
menuManager.setRemoveAllWhenShown(true);
viewer.getTree().setMenu(menu);
getSite().registerContextMenu(menuManager, viewer);
The 'advantage' of which one had direct access to a TreeViewers underlying data.
In e4 I have created a context menu using the Application.e4xmi and used a CoreExpression that only shows the popup menu when a node in the tree is selected. This works OK. However as far as I can see the handler for the context menu has no access to the node which was selected and thus cannot access the nodes data model.
A way around this is to use IEclipsePreferences and set a node key and name using a listener on the tree, then access the preference from the handler.
This all seems a bit complicated just to show some properties of a selected node.
1) is there a simpler way in e4 for a popup menu to access a treeviewer nodes data model?
2) is the best way to set preferences for the node in a SWT mouse button selected listner? - as a lot of magic is happening in the background, can we be sure the context menu handler will be able to access the correct IEclipsePreferences preferences in time?
Has anyone else had similar issues?
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.07994 seconds