Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse 4 » ContextMenu and TreeItem data(How to get to a TreeItems data model from a popup menu)
ContextMenu and TreeItem data [message #1087973] Fri, 16 August 2013 11:30 Go to next message
Don Smyth is currently offline Don SmythFriend
Messages: 35
Registered: April 2013
Member
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?
Re: ContextMenu and TreeItem data [message #1088529 is a reply to message #1087973] Sat, 17 August 2013 07:22 Go to previous messageGo to next message
Don Smyth is currently offline Don SmythFriend
Messages: 35
Registered: April 2013
Member
I ended getting it all working using:
@Inject
private MApplication mApplication;
private static final String TREE_NODE_AT_MOUSE_CLICK = "tree_node_at_mouse_click";

viewer.getTree().addMouseListener(new MouseAdapter() {
@Override
public void mouseDown(MouseEvent e) {
if (e.button == 1)
return;
final TreeItem itemAtClick = viewer.getTree().getItem(new Point(e.x, e.y));
if (itemAtClick != null) {
final IStructuredSelection selection = (IStructuredSelection) viewer.getSelection();
if (selection.getFirstElement() instanceof TestNode) {
mApplication.getContext().set(TREE_NODE_AT_MOUSE_CLICK, (TestNode)selection.getFirstElement());
}
...


then in the popupmenu handler I did:
if (null != mApplication.getContext().get("tree_node_at_mouse_click")) {
final TreeNode node;
try {
node = (TreeNode) mApplication.getContext().get("tree_node_at_mouse_click");
if (node instanceof TestNode) {
....

and now I have node model access from the popup menu Smile
Re: ContextMenu and TreeItem data [message #1088574 is a reply to message #1087973] Sat, 17 August 2013 09:06 Go to previous messageGo to next message
Greg Pugh is currently offline Greg PughFriend
Messages: 15
Registered: July 2009
Junior Member
On 2013-08-16 11:30:53 +0000, Don Smyth said:

> 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?


Use a selection listener on the tree and call
ESelectionService.setSelection when the selection changes. Your hander
can then access the selection using
@Named(IServiceConstants.ACTIVE_SELECTION)
Re: ContextMenu and TreeItem data [message #1088681 is a reply to message #1088574] Sat, 17 August 2013 13:07 Go to previous message
Don Smyth is currently offline Don SmythFriend
Messages: 35
Registered: April 2013
Member
Thanks Greg,

Yes, looks like the ESelectionService is designed for UI selection state storage and is a more obvious place than the Workbench context to store such data.

Cheers

Don
Previous Topic:MenuItems always disabled with Kepler M4
Next Topic:the best practises with contextual menu and treeview
Goto Forum:
  


Current Time: Thu Apr 25 13:31:41 GMT 2024

Powered by FUDForum. Page generated in 0.03552 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top