Eclipse 插件开发——为TextViewer或SourceViewer添加右键弹出菜单

如果写成: Menu menu = popupMenuManager.createContextMenu( this.sourceViewer.getControl());
             this.sourceViewer.getControl().setMenu(menu);
   则无法弹出菜单!

   正确写法:

    protected void initializeHookContextMenu()
    {
        //创建菜单管理对象
        MenuManager popupMenuManager = new MenuManager();
        //add copy action
        popupMenuManager.add(ActionManager.INSTANCE.getAction(CopyAction.ACTION_ID));
        //add paste action
        popupMenuManager.add(ActionManager.INSTANCE.getAction(PasteAction.ACTION_ID));
        //add delete action
        popupMenuManager.add(ActionManager.INSTANCE.getAction(DeleteAction.ACTION_ID));
        popupMenuManager.add(new Separator());
        //add refresh action
        popupMenuManager.add(ActionManager.INSTANCE.getAction(RefreshAction.ACTION_ID));
        //为project树对象创建上下文菜单
        Menu menu = popupMenuManager.createContextMenu( this.sourceViewer.getTextWidget());
        //设置菜单
        this.sourceViewer.getTextWidget().setMenu(menu);
    }

你可能感兴趣的:(textview)