首页 > 代码库 > Rcp 中添加eclipse中的菜单栏
Rcp 中添加eclipse中的菜单栏
自己创建的Eclipse rcp 插件中需要添加像Eclipse中的菜单栏和工具栏,实现该功能可以通过两种方式:1种是通过扩展点,2种是通过在ApplicationActionBarAdvisor 类中编写代码实现。
1、通过扩展点实现:org.eclipse.ui.menus 用于扩展菜单栏,该例子实现eclipse中的window菜单栏中的部分Action
ViewsShortlistContributionItem 源码:
package com.workflow.menu; import java.util.ArrayList; import java.util.List; import org.eclipse.jface.action.IContributionItem; import org.eclipse.ui.IWorkbenchWindow; import org.eclipse.ui.PlatformUI; import org.eclipse.ui.actions.CompoundContributionItem; import org.eclipse.ui.actions.ContributionItemFactory; /** * 添加show view 的动态菜单项 * @author lww * */ public class ViewsShortlistContributionItem extends CompoundContributionItem { @Override protected IContributionItem[] getContributionItems() { IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); IContributionItem item = ContributionItemFactory.VIEWS_SHORTLIST.create(window);//创建viewshortlist List<IContributionItem> menuContributionList = new ArrayList<IContributionItem>(); menuContributionList.add(item); return menuContributionList.toArray(new IContributionItem[0]); } }
PerspectiveContributionItem 源码:
package com.workflow.menu; import java.util.ArrayList; import java.util.List; import org.eclipse.jface.action.IContributionItem; import org.eclipse.ui.IWorkbenchWindow; import org.eclipse.ui.PlatformUI; import org.eclipse.ui.actions.CompoundContributionItem; import org.eclipse.ui.actions.ContributionItemFactory; /** * 创建透视图二级菜单 * @author lww * */ public class PerspectiveContributionItem extends CompoundContributionItem{ @Override protected IContributionItem[] getContributionItems() { IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); IContributionItem item = ContributionItemFactory.PERSPECTIVES_SHORTLIST.create(window); List<IContributionItem> menuContributionList = new ArrayList<IContributionItem>(); menuContributionList.add(item); return menuContributionList.toArray(new IContributionItem[0]); } }
plugin.xml 中的内容如下:
这样就实现了自己的rcp的菜单中添加了window菜单栏,效果如下:
另一种方式是通过代码实现:
package mydesigner; import org.eclipse.jface.action.IContributionItem; import org.eclipse.jface.action.ICoolBarManager; import org.eclipse.jface.action.IMenuManager; import org.eclipse.jface.action.MenuManager; import org.eclipse.jface.action.Separator; import org.eclipse.jface.action.ToolBarManager; import org.eclipse.ui.IWorkbenchActionConstants; import org.eclipse.ui.IWorkbenchWindow; import org.eclipse.ui.actions.ActionFactory; import org.eclipse.ui.actions.ContributionItemFactory; import org.eclipse.ui.actions.ActionFactory.IWorkbenchAction; import org.eclipse.ui.application.ActionBarAdvisor; import org.eclipse.ui.application.IActionBarConfigurer; public class ApplicationActionBarAdvisor extends ActionBarAdvisor { private IWorkbenchAction exitAction;//退出按钮 private IWorkbenchAction aboutAction;//关于按钮 private IWorkbenchAction newAction;//新建按钮 private IWorkbenchAction saveProjectAction;// 保存工程文件 private IWorkbenchAction saveAllAction;// 保存全部 private IWorkbenchAction newWindowAction; private IContributionItem item; //文件菜单栏中的action private IWorkbenchAction importAction; private IWorkbenchAction exportAction; public ApplicationActionBarAdvisor(IActionBarConfigurer configurer) { super(configurer); } //创建Action protected void makeActions(IWorkbenchWindow window) { exitAction = ActionFactory.QUIT.create(window); register(exitAction); aboutAction = ActionFactory.ABOUT.create(window); register(aboutAction); newAction = ActionFactory.NEW.create(window); register(newAction);//新建按钮 saveProjectAction = ActionFactory.SAVE.create(window); saveAllAction = ActionFactory.SAVE_ALL.create(window); this.getActionBarConfigurer().registerGlobalAction(saveProjectAction); // ctrl+s,保存工程文件。 this.getActionBarConfigurer().registerGlobalAction(saveAllAction);// 激活快捷键,ctrl+shift+s,全部保存。 newWindowAction = ActionFactory.OPEN_NEW_WINDOW.create(window); register(newWindowAction);//注册成全局Action item = ContributionItemFactory.VIEWS_SHORTLIST.create(window);//创建viewshortlist importAction = ActionFactory.IMPORT.create(window); register(importAction); exportAction = ActionFactory.EXPORT.create(window); register(exportAction); } //菜单栏 protected void fillMenuBar(IMenuManager menuBar) { //File菜单栏 MenuManager fileMenu = new MenuManager("&File", IWorkbenchActionConstants.M_FILE); fileMenu.add(exitAction); fileMenu.add(new Separator());//添加分隔栏 fileMenu.add(newAction); fileMenu.add(new Separator()); fileMenu.add(importAction);//导入 fileMenu.add(exportAction);//导出 //Help菜单栏 MenuManager helpMenu = new MenuManager("&Help", IWorkbenchActionConstants.M_HELP); helpMenu.add(aboutAction); //Window菜单栏 MenuManager windowMenu = new MenuManager("&Window", IWorkbenchActionConstants.M_WINDOW); windowMenu.add(newWindowAction); MenuManager showView = new MenuManager("Show View"); showView.add(item);//添加ContributionItem windowMenu.add(showView); menuBar.add(fileMenu);//把菜单栏添加到工作空间 menuBar.add(helpMenu); menuBar.add(windowMenu); } //添加到工具栏 @Override protected void fillCoolBar(ICoolBarManager coolBar) { ToolBarManager barManager = new ToolBarManager(); barManager.add(saveProjectAction); barManager.add(saveAllAction); coolBar.add(barManager); } }效果如下图:
若想添加其它的Action,可以在类:
org.eclipse.ui.IWorkbenchActionConstants
org.eclipse.ui.actions.ActionFactory
org.eclipse.ui.actions.ContributionItemFactory
org.eclipse.ui.IWorkbenchCommandConstants 查找到对应Action的实现,eclipse实现。
org.eclipse.ui.actions.ActionFactory
org.eclipse.ui.actions.ContributionItemFactory
org.eclipse.ui.IWorkbenchCommandConstants 查找到对应Action的实现,eclipse实现。
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。