treeViewer.getTree().addMenuDetectListener(new MenuDetectListener() {
@Override
public void menuDetected(MenuDetectEvent e) {
// get the selected items
ISelection selection = (IStructuredSelection) treeViewer.getSelection();
// define menus - menu 1 if right click on parent tree item
Menu menu1 = new Menu(treeViewer.getTree());
MenuItem item = new MenuItem(menu1, SWT.PUSH);
item.setText("Test 1");
item.addSelectionListener(new Test1Listener());
// menu 2 if right click on child tree item
Menu menu2 = new Menu(procedureTreeViewerICD9.getTree());
MenuItem item1 = new MenuItem(menu2, SWT.PUSH);
item1.setText("Test2");
item1.addSelectionListener(new Test2Listener());
// menu 3 if right click on neither the parent tree item or child tree item. ex - menu for headers.
Menu menu3 = new Menu(procedureTreeViewerICD9.getTree());
MenuItem item1 = new MenuItem(menu3, SWT.PUSH);
item1.setText("Test3");
item1.addSelectionListener(new Test3Listener());
// check if the tree item selected is parent or child and set the menu accordingly
if (selection.getFirstElement() instanceof <ParentClass> ) {
treeViewer.getControl().setMenu(menu1);
} else if (selection.getFirstElement() instanceof <ChildClass> )
treeViewer.getControl().setMenu(menu2);
} else {
treeViewer.getControl().setMenu(menu3);
}
// clear the selection
treeViewer.setSelection(null);
}
});
@Override
public void menuDetected(MenuDetectEvent e) {
// get the selected items
ISelection selection = (IStructuredSelection) treeViewer.getSelection();
// define menus - menu 1 if right click on parent tree item
Menu menu1 = new Menu(treeViewer.getTree());
MenuItem item = new MenuItem(menu1, SWT.PUSH);
item.setText("Test 1");
item.addSelectionListener(new Test1Listener());
// menu 2 if right click on child tree item
Menu menu2 = new Menu(procedureTreeViewerICD9.getTree());
MenuItem item1 = new MenuItem(menu2, SWT.PUSH);
item1.setText("Test2");
item1.addSelectionListener(new Test2Listener());
// menu 3 if right click on neither the parent tree item or child tree item. ex - menu for headers.
Menu menu3 = new Menu(procedureTreeViewerICD9.getTree());
MenuItem item1 = new MenuItem(menu3, SWT.PUSH);
item1.setText("Test3");
item1.addSelectionListener(new Test3Listener());
// check if the tree item selected is parent or child and set the menu accordingly
if (selection.getFirstElement() instanceof <ParentClass> ) {
treeViewer.getControl().setMenu(menu1);
} else if (selection.getFirstElement() instanceof <ChildClass> )
treeViewer.getControl().setMenu(menu2);
} else {
treeViewer.getControl().setMenu(menu3);
}
// clear the selection
treeViewer.setSelection(null);
}
});