Context menu for TreeViewer based on selected node - SWT

前端 未结 4 1152
逝去的感伤
逝去的感伤 2020-12-31 13:51

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

相关标签:
4条回答
  • 2020-12-31 14:37

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

    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);
    

    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.

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

    0 讨论(0)
  • 2020-12-31 14:37

    Thats the way I do it:

    MenuManager menuMgr = new MenuManager();
    
            Menu menu = menuMgr.createContextMenu(viewer.getControl());
            menuMgr.addMenuListener(new IMenuListener() {
                @Override
                public void menuAboutToShow(IMenuManager manager) {
                    // IWorkbench wb = PlatformUI.getWorkbench();
                    // IWorkbenchWindow win = wb.getActiveWorkbenchWindow();
                    if (viewer.getSelection().isEmpty()) {
                        return;
                    }
    
                    if (viewer.getSelection() instanceof IStructuredSelection) {
                        IStructuredSelection selection = (IStructuredSelection) viewer.getSelection();
                        Node object = (Node)selection.getFirstElement();
    
                        if (object.getModel() instanceof NodeA) {
                            manager.add(new Action();
                        } else if (object.getModel() instanceof NodeB) {
    
                            manager.add(new OtherAction());
    
                        }
                    }
                }
            });
            menuMgr.setRemoveAllWhenShown(true);
            viewer.getControl().setMenu(menu);
    

    I hope this helps ;)

    It is important to set removeAllWhenShown property of menu manager to false, in order to hide all the other nodes actions ;)

    0 讨论(0)
  • 2020-12-31 14:44

    Just use single Menu Manager. Do not make Menu Manager dynamically.

    in theory, It's possible you tried, but it's inefficient and it's not general way.

    Just make a Menu Manager and add all actions which you needs.

    when selection has been changed, call Action#setVisible(true|false)to hide or show menu items. You can also use Action#setEnable to enable/disable menu item.

    ps. Menu Manager is not a menu GUI(likes TreeViewer is a not tree)

    It contributes Actions(business logic) to Menu(SWT). And It also manage visibility and enablement. We call this Contribution Manager. We can create a SWT menu very easy with this. (even we don't know about SWT, we just have to know only our business logic:Action) It's fundamental idea in JFace.

    When you add an action into manu manager, action will be wrapped with ActionContributionItem. It hooks action's state to update UI(visibility, enablement for menu, button, toolbar and so on). It also hooks UI to launch action when it pressed.

    If you are new to eclipse, It is easy to confuse role of SWT and JFace.

    0 讨论(0)
  • 2020-12-31 14:44

    Suppose that you know how to create Action and you are only interested in context menu following example worked for me hope this bunch of code will help you

     private void hookContextMenu() {
        MenuManager contextMenu = new MenuManager();
        contextMenu.setRemoveAllWhenShown(true);
        contextMenu.addMenuListener(new IMenuListener() {
    
            @Override
            public void menuAboutToShow(IMenuManager manager) {
    
    
    
                IStructuredSelection sSelection = (IStructuredSelection) treeViewer.getSelection();
    
                }
                if(selectedObject instanceof A){
                manager.add(action);
                }
    
            }
        });
    
    0 讨论(0)
提交回复
热议问题