基于选定节点--SWT的TreeViewer的上下文菜单 [英] Context menu for TreeViewer based on selected node - SWT

查看:226
本文介绍了基于选定节点--SWT的TreeViewer的上下文菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在Eclipse插件项目中为TreeViewer创建一个上下文菜单。但是,菜单不应该包含常量项,它们应该根据所选节点的类型而有所不同。例如,我的treeViewer具有以下层次结构:

I need to create a context menu for a TreeViewer in an Eclipse plugin project. But, the menu should not contain constant items, they should vary depending on the type of the node that is selected. For example, my treeViewer has the following hierarchy:

Node A
 |
 --Node B
  |
   --Node C

对于节点A - 我想显示一个带有动作的菜单,但对于节点B和CI不想显示任何内容(无菜单)。
我设法为节点A创建菜单,但是当选择其他类型的节点时,我无法摆脱它。我的代码看起来像:

For node A - I want to show a menu with an action, but for nodes B and C I don't want to show anything (no menu). I managed to create the menu for node A, but then I can't get rid of it when some other type of node is selected. My code looks like:

treeViewer.addSelectionChangedListener(
            new ISelectionChangedListener(){
                public void selectionChanged(SelectionChangedEvent event) {
                    if(event.getSelection() instanceof IStructuredSelection) {
                        IStructuredSelection selection = (IStructuredSelection)event.getSelection();            
                        Object o = selection.getFirstElement();     

                        MenuManager menuMgr = new MenuManager();

                        if (o instanceof NodeA){

                            Menu menu = menuMgr.createContextMenu(treeViewer.getControl());
                            treeViewer.getControl().setMenu(menu);
                            getSite().registerContextMenu(menuMgr, treeViewer);

                            menuMgr.add(new SomeAction());

                        }else {
                            //what ?
                        }
                    }

                }
            }   
    );

在其他分支上,我试图调用 dispose() removeAll()在MenuManager ...没有任何工作!

On the else branch I tried to call dispose(),removeAll() on the MenuManager...nothing works!

任何帮助,谢谢,谢谢

推荐答案

正如@jeeeyul所说,你只应该在你的视图中创建一个MenuManager。

As @jeeeyul mentioned, you should only create one MenuManager to use within your view.

您可以使用新建>插件项目和视图模板,使用查看器获取视图中的上下文菜单的示例,但基本上在您的 createPartControl(Composite) 方法,你将连接你的上下文管理器。

You can use New>Plug-in Project and the view template to get an example of a context menu in a view using a viewer, but basically in your createPartControl(Composite) method you would hook up your context manager.

    MenuManager menuMgr = new MenuManager();
    menuMgr.setRemoveAllWhenShown(true);
    menuMgr.addMenuListener(new IMenuListener() {
        public void menuAboutToShow(IMenuManager manager) {
            SampleView.this.fillContextMenu(manager);
        }
    });
    Menu menu = menuMgr.createContextMenu(viewer.getControl());
    viewer.getControl().setMenu(menu);
    getSite().registerContextMenu(menuMgr, viewer);

您的 fillContextMenu(MenuManager)访问您的浏览器,所以您可以从中获取当前的选择。您可以添加所需的任何操作,甚至在使用当前选择更新后再添加操作。

Your fillContextMenu(MenuManager) method will have access to your viewer, so you can get the current selection from that. You can add whatever actions you want, even re-add actions after updating them with the current selection.

registerContextMenu(*)调用允许像org.eclipse.ui.popupMenus和org.eclipse.ui.menus这样的扩展点向上下文菜单提供项目。

The registerContextMenu(*) call allows extension points like org.eclipse.ui.popupMenus and org.eclipse.ui.menus to contribute items to your context menu.

这篇关于基于选定节点--SWT的TreeViewer的上下文菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆