首先申明下,本文为笔者学习《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);
}
显示如下