> 文档中心 > Eclipse插件(RCP)自定义工作台主菜单栏

Eclipse插件(RCP)自定义工作台主菜单栏

使用Eclipse plugin 向导创建的RCP项目有工作台菜单栏,并且很多时候,因为依赖不一样,菜单项也不一样,如下图所示。
在这里插入图片描述

需求

去掉RCP中不必要的菜单项,并且自定义自己的菜单项。

分析

根据Eclipse 向导(过程类似于Eclipse插件(RCP)项目搭建)创建的RCP项目,默认都会生成*Activator文件。这些文件对于构造工作台有很大的帮助,其中ApplicationActionBarAdvisor就是专门用来构建工作台菜单的。该类继承自ActionBarAdvisor

ActionBarAdvisor有三个空实现的方法fillMenuBarfillCoolBarfillStatusLine被设计子类可以重写,它们分别用来填充主菜单、快捷菜单(可拖动菜单)和状态栏(状态条,位于工作台最底部)。

本文需求将通过重写 fillMenuBar 而实现。ActionBarAdvisorfillMenuBar 的代码如下:

/** * Fills the menu bar with the main menus for the window. * 

* The default implementation does nothing. Subclasses may override. *

* * @param menuBar the menu manager for the menu bar */
protected void fillMenuBar(IMenuManager menuBar) {// do nothing}

实现

**ApplicationActionBarAdvisor ** 类实现了两个主菜单File和Edit,其下有多个常用的子菜单。

package com.xzbd.advisor;import org.eclipse.jface.action.IMenuManager;import org.eclipse.jface.action.MenuManager;import org.eclipse.jface.action.Separator;import org.eclipse.ui.IWorkbenchWindow;import org.eclipse.ui.actions.ActionFactory;import org.eclipse.ui.actions.ActionFactory.IWorkbenchAction;import org.eclipse.ui.application.ActionBarAdvisor;import org.eclipse.ui.application.IActionBarConfigurer;import com.xzbd.enums.MainMenu;/** * An action bar advisor is responsible for creating, adding, and disposing of * the actions added to a workbench window. Each window will be populated with * new actions. */public class ApplicationActionBarAdvisor extends ActionBarAdvisor {private IWorkbenchAction newAction;private IWorkbenchAction saveAction;private IWorkbenchAction saveAllAction;private IWorkbenchAction printAction;private IWorkbenchAction saveAsAction;private IWorkbenchAction closeAction;private IWorkbenchAction closeAllAction;// Actions - important to allocate these only in makeActions, and then use// them// in the fill methods. This ensures that the actions aren't recreated// when fillActionBars is called with FILL_PROXY.public ApplicationActionBarAdvisor(IActionBarConfigurer configurer) {super(configurer);}protected void makeActions(IWorkbenchWindow window) {makeAction(window, ActionFactory.UNDO);makeAction(window, ActionFactory.REDO);makeAction(window, ActionFactory.COPY);makeAction(window, ActionFactory.PASTE);makeAction(window, ActionFactory.CUT);makeAction(window, ActionFactory.SELECT_ALL);makeAction(window, ActionFactory.DELETE);makeAction(window, ActionFactory.FIND);makeAction(window, ActionFactory.IMPORT);makeAction(window, ActionFactory.EXPORT);newAction = ActionFactory.NEW.create(window);register(newAction);saveAction = ActionFactory.SAVE.create(window);register(saveAction);saveAllAction = ActionFactory.SAVE_ALL.create(window);register(saveAllAction);printAction = ActionFactory.PRINT.create(window);register(printAction);saveAsAction = ActionFactory.SAVE_AS.create(window);closeAction = ActionFactory.CLOSE.create(window);register(closeAction);closeAllAction = ActionFactory.CLOSE_ALL.create(window);register(closeAllAction);}protected IWorkbenchAction makeAction(IWorkbenchWindow window, ActionFactory af) {IWorkbenchAction action = af.create(window);register(action);return action;}@Overrideprotected void fillMenuBar(IMenuManager menuBarold) {IMenuManager menuBar = getActionBarConfigurer().getMenuManager();// File菜单MenuManager fileMenu = new MenuManager("&File",MainMenu.FILE.getId() );fileMenu.add(new Separator());fileMenu.add(closeAction);fileMenu.add(closeAllAction);fileMenu.add(new Separator());fileMenu.add(saveAction);fileMenu.add(saveAsAction);fileMenu.add(saveAllAction);fileMenu.add(new Separator());fileMenu.add(printAction);fileMenu.add(new Separator());fileMenu.add(getAction(ActionFactory.IMPORT.getId()));fileMenu.add(getAction(ActionFactory.EXPORT.getId()));menuBar.add(fileMenu);// Edit菜单MenuManager editMenu = new MenuManager("&Edit", MainMenu.EDIT.getId());editMenu.add(getAction("undo"));editMenu.add(getAction("redo"));editMenu.add(getAction("find"));editMenu.add(getAction("selectAll"));editMenu.add(getAction("delete"));menuBar.add(editMenu);}}

枚举类MainMenu实现如下:

package com.xzbd.enums;import com.xzbd.constants.MenuConst;public enum MainMenu {FILE(MenuConst.MAIN_MENU_ID_PREFIX.concat(".file")),EDIT(MenuConst.MAIN_MENU_ID_PREFIX.concat(".edit")),;private String Id ;public String getId() {return Id;}private MainMenu(String id) {Id = id;}}

常量MenuConst如下:

package com.xzbd.constants;public class MenuConst {public static final String MAIN_MENU_ID_PREFIX = "xzbd.epx.main.menu";}

隐藏不需要的菜单在ApplicationWorkbenchWindowAdvisor中重写``完成,暂时在窗口打开后将其隐藏,代码如下:

@Overridepublic void postWindowOpen() {IActionBarConfigurer actionBarConfigurer = getWindowConfigurer().getActionBarConfigurer();IContributionItem[] items = actionBarConfigurer.getMenuManager().getItems();// 将主菜单不是自定义ID的隐藏for (int i = 0; i < items.length; i++) {IContributionItem item = items[i];String id = item.getId();if (id == null) {item.setVisible(false);continue;}if(!id.startsWith(MenuConst.MAIN_MENU_ID_PREFIX)) {item.setVisible(false);}}}

效果展示

File 菜单

在这里插入图片描述

Edit 菜单

在这里插入图片描述

总结

多出来的菜单是由于依赖初始化时加的,本文采用隐藏的方式消除掉,若有更优雅的方式,请指正!

项目地址: epx