Eclipse Plug-in 编辑器菜单(六)

首先申明下,本文为笔者学习《Eclipse插件开发学习笔记》的笔记,并加入笔者自己的理解和归纳总结。

1. 编辑器菜单

扩展点【org.eclipse.ui.editorActions】
在【extensions】中添加【org.eclipse.ui.editorActions】
在【org.eclipse.ui.editorActions】中添加【editorContribution】,【targetID】指定对应的编辑器DemoEditorView
在【editorContribution】中添加相应的【menu】和【action】

<extension
      point="org.eclipse.ui.editorActions">
    <editorContribution
          id="com.plugin.blog.demo.editorContribution.DemoEditorPart"
          targetID="com.plugin.blog.demo.edit.DemoEditorPart">
        <menu
              id="com.plugin.blog.demo.menu.DemoEditorPart"
              label="编辑器菜单"
              path="additions">
            <separator
                  name="menu.DemoEditorPart.separator">
            </separator>
        </menu>
        <action
              class="com.plugin.blog.demo.actions.DemoEditorAction"
              icon="icons/icon_notice.png"
              id="com.plugin.blog.demo.actions.DemoEditorAction"
              label="编辑器Action"
              menubarPath="com.plugin.blog.demo.menu.DemoEditorPart/menu.DemoEditorPart.separator"
              style="push"
              toolbarPath="additions">
        </action>
    </editorContribution>
</extension>

【action】配置
在【class】中选择对应的Action,必须继承IEditorActionDelegate类。
在【menubarPath】和【toolbarPath】中分别定义了菜单栏和工具栏的路径。
DemoEditorAction类,继承IEditorActionDelegate

public class DemoEditorAction implements IEditorActionDelegate {
    private IEditorPart mEditorPart;

    @Override
    public void run(IAction action) {
        MessageBox mb = new MessageBox(mEditorPart.getSite().getShell());
        mb.setText("消息");
        mb.setMessage("编辑器菜单");
        mb.open();
    }

    @Override
    public void selectionChanged(IAction action, ISelection selection) {
    }

    @Override
    public void setActiveEditor(IAction action, IEditorPart targetEditor) {
        this.mEditorPart = targetEditor;
    }

}

显示如下
在这里插入图片描述

2. 编辑器Popup菜单

在编辑器中,同样可以在【org.eclipse.ui.popupMenus】中添加【objectContribution】或【viewerContribution】。

<extension
      point="org.eclipse.ui.popupMenus">
    <objectContribution
          adaptable="false"
          id="com.plugin.blog.demo.objectContribution.UserInfo"
          objectClass="com.plugin.blog.demo.edit.UserInfo">
        <menu
              id="com.plugin.blog.demo.menu.objectContribution.UserInfo"
              label="编辑器对象菜单"
              path="additions">
        </menu>
        <action
              class="com.plugin.blog.demo.actions.EditorObjectAction"
              icon="icons/icon_editor.png"
              id="com.plugin.blog.demo.actions.EditorObjectAction"
              label="编辑器对象Action"
              menubarPath="com.plugin.blog.demo.menu.objectContribution.UserInfo">
        </action>
    </objectContribution>
</extension>

EditorObjectAction类,继承IObjectActionDelegate

public class EditorObjectAction implements IObjectActionDelegate {
    private IWorkbenchPart mWorkbenchPart;
    private ISelection mSelection;

    @Override
    public void run(IAction action) {
        if (mWorkbenchPart != null && mSelection != null) {
            IStructuredSelection ss = (IStructuredSelection) mSelection;
            UserInfo userInfo = (UserInfo) ss.getFirstElement();

            MessageBox mb = new MessageBox(mWorkbenchPart.getSite().getShell());
            mb.setText("消息");
            mb.setMessage("用户信息:\n" + userInfo.getText());
            mb.open();
        }
    }

    @Override
    public void selectionChanged(IAction action, ISelection selection) {
        this.mSelection = selection;
    }	

    @Override
    public void setActivePart(IAction action, IWorkbenchPart targetPart) {
        this.mWorkbenchPart = targetPart;
    }

}

UserInfoBlock类中添加右键菜单

@Override
protected void createMasterPart(IManagedForm managedForm, Composite parent) {
    ... ...

    MenuManager menuManager = new MenuManager();
    menuManager.setRemoveAllWhenShown(true);

    Menu menu = menuManager.createContextMenu(table);
    table.setMenu(menu);
    getEditor().getSite().registerContextMenu(menuManager, tableViewer);
}

显示如下
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值