首先申明下,本文为笔者学习《Eclipse插件开发学习笔记》的笔记,并加入笔者自己的理解和归纳总结。
1. 视图菜单
扩展点【org.eclipse.ui.viewActions】
在【Extensions】中添加【org.eclipse.ui.viewActions】。
在【org.eclipse.ui.viewActions】中添加【viewContribution】,【targetID】指定对应的视图DemoViewPart
。
在【viewContribution】中添加相应的【menu】和【action】。
<extension
point="org.eclipse.ui.viewActions">
<viewContribution
id="com.plugin.blog.demo.viewContribution.DemoViewPart"
targetID="com.plugin.blog.demo.view.DemoViewPart">
<menu
id="com.plugin.blog.demo.DemoViewPart.menu"
label="下拉菜单"
path="additions">
<separator
name="DemoViewPart.menu.separator">
</separator>
</menu>
<action
class="com.plugin.blog.demo.actions.UserInfoAction"
icon="icons/icon_selection.png"
id="com.plugin.blog.demo.actions.UserInfoAction"
label="用户信息"
menubarPath="com.plugin.blog.demo.DemoViewPart.menu/DemoViewPart.menu.separator"
style="push"
toolbarPath="additions">
</action>
</viewContribution>
</extension>
【action】配置
在【class】中选择对应的Action
,必须继承IViewActionDelegate
类。
在【menubarPath】和【toolbarPath】中分别定义了下拉菜单和工具栏菜单的路径。
UserSelectionAction
类,显示被选择列表信息
public class UserInfoAction implements IViewActionDelegate {
private IViewPart mView;
private User mUser;
@Override
public void run(IAction action) {
if (mView != null && mUser != null) {
MessageBox mb = new MessageBox(mView.getSite().getShell());
mb.setText("消息");
mb.setMessage("用户" + mUser.getName() + "被选择!");
mb.open();
}
}
@Override
public void selectionChanged(IAction action, ISelection selection) {
if (selection != null) {
IStructuredSelection ss = (IStructuredSelection) selection;
if (!ss.isEmpty()) {
mUser = (User) ss.getFirstElement();
action.setEnabled(true);
} else {
action.setEnabled(false);
}
} else {
action.setEnabled(false);
}
}
@Override
public void init(IViewPart view) {
this.mView = view;
}
}
显示如下
2. 视图Popup菜单
扩展点【org.eclipse.ui.popupMenus】
在【Extensions】中添加【org.eclipse.ui.popupMenus】
在【org.eclipse.ui.popupMenus】中添加【viewContribution】,【targetID】指定对应的视图
在【viewContribution】中添加相应的【menu】和【action】。
<extension
point="org.eclipse.ui.popupMenus">
<viewerContribution
id="com.plugin.blog.demo.viewerContribution.DemoViewPart"
targetID="com.plugin.blog.demo.view.DemoViewPart">
<menu
id="com.plugin.blog.demo.popup.view.menu"
label="PopupViewMenu"
path="additions">
</menu>
<action
class="com.plugin.blog.demo.actions.AddUserAction"
icon="icons/icon_add.png"
id="com.plugin.blog.demo.actions.AddUserAction"
label="新增用户"
menubarPath="com.plugin.blog.demo.popup.view.menu">
</action>
</viewerContribution>
</extension>
【action】配置
在【class】中选择对应的Action
,必须继承IWorkbenchWindowActionDelegate
类。
AddUserAction
类,添加一个新的用户,使用InputDialog
来输入用户名。
public class AddUserAction implements IViewActionDelegate {
private IViewPart mViewPart;
@Override
public void run(IAction action) {
if (mViewPart != null) {
InputDialog dialog = new InputDialog(mViewPart.getViewSite().getShell(),
"添加用户", "输入用户名:", "", null);
dialog.open();
String value = dialog.getValue();
if (value != null && value.length() > 0) {
UserManager.getInstance().addObject(
new User(value, User.CATEGORY_ARRAY[0]));
}
}
}
@Override
public void selectionChanged(IAction action, ISelection selection) {
}
@Override
public void init(IViewPart view) {
mViewPart = view;
}
}
在DemoViewPart
类中注册右键菜单,添加到createPartControl()
方法中。
private void addMenu() {
MenuManager menuManager = new MenuManager();
menuManager.setRemoveAllWhenShown(true);
Menu menu = menuManager.createContextMenu(mTable);
mTable.setMenu(menu);
getSite().registerContextMenu(menuManager, mTableViewer);
}
显示如下
3. 对象Popup菜单
扩展点【org.eclipse.ui.popupMenus】
在【org.eclipse.ui.popupMenus】中添加【objectContribution】,在【objectClass】中指定对象User
。
在【objectContribution】中添加相应的【menu】和【action】
<extension
point="org.eclipse.ui.popupMenus">
<objectContribution
adaptable="false"
id="com.plugin.blog.demo.objectContribution.User"
objectClass="com.plugin.blog.demo.data.User">
<menu
id="com.plugin.blog.demo.popup.object.menu"
label="PopupUserMenu"
path="additions">
</menu>
<action
class="com.plugin.blog.demo.actions.RemoveUserAction"
icon="icons/icon_remove.png"
id="com.plugin.blog.demo.actions.RemoveUserAction"
label="删除用户"
menubarPath="com.plugin.blog.demo.popup.object.menu">
</action>
</objectContribution>
</extension>
RemoveUserAction
类
public class RemoveUserAction implements IObjectActionDelegate {
private IWorkbenchPart mWorkbenchPart;
private ISelection mSelection;
@Override
public void run(IAction action) {
if (mWorkbenchPart != null && mSelection != null) {
IStructuredSelection ss = (IStructuredSelection) mSelection;
if (!ss.isEmpty()) {
User user = (User) ss.getFirstElement();
boolean result = MessageDialog.openConfirm(
mWorkbenchPart.getSite().getShell(),
"删除用户", "您是否确认删除" + user.getName() + "?");
if (result) {
UserManager.getInstance().removeObject(user);
}
}
}
}
@Override
public void selectionChanged(IAction action, ISelection selection) {
this.mSelection = selection;
}
@Override
public void setActivePart(IAction action, IWorkbenchPart targetPart) {
this.mWorkbenchPart = targetPart;
}
}
显示如下
4. 视图代码中添加菜单
MessageAction
类,继承Action
类,实现功能
public class WarnAction extends Action {
private IViewPart mPart;
public WarnAction(IViewPart targetPart) {
super("警告", ImageKeys.getImageDescriptor(ImageKeys.IMAGE_WARN));
this.mPart = targetPart;
}
@Override
public void run() {
MessageBox mb = new MessageBox(mPart.getSite().getShell());
mb.setText("警告");
mb.setMessage("警告");
mb.open();
}
}
ImageKeys
类,加载图片
public class ImageKeys {
public static final String IMAGE_WARN = "icons/icon_warn.png";
public static ImageDescriptor getImageDescriptor(String path) {
return Activator.imageDescriptorFromPlugin(Activator.PLUGIN_ID, path);
}
}
DemoViewPart
类,把MessageAction
操作添加到视图的菜单栏、工具栏和右键菜单中。
private WarnAction mWarnAction;
private void addMenu() {
mWarnAction = new WarnAction(this);
MenuManager menuManager = new MenuManager();
menuManager.setRemoveAllWhenShown(true);
menuManager.addMenuListener(new IMenuListener() {
@Override
public void menuAboutToShow(IMenuManager manager) {
addMenu(manager);
}
});
Menu menu = menuManager.createContextMenu(mTable);
mTable.setMenu(menu);
getSite().registerContextMenu(menuManager, mTableViewer);
IActionBars actionBar = getViewSite().getActionBars();
addMenu(actionBar.getMenuManager());
addMenu(actionBar.getToolBarManager());
}
private void addMenu(IContributionManager manager) {
manager.add(mWarnAction);
}
显示如下