標籤:eclipse rcp 功能表列 file window
自己建立的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 {@Overrideprotected 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{@Overrideprotected 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;//檔案功能表列中的actionprivate IWorkbenchAction importAction;private IWorkbenchAction exportAction;public ApplicationActionBarAdvisor(IActionBarConfigurer configurer) {super(configurer);}//建立Actionprotected 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);}//添加到工具列@Overrideprotected void fillCoolBar(ICoolBarManager coolBar) {ToolBarManager barManager = new ToolBarManager();barManager.add(saveProjectAction);barManager.add(saveAllAction);coolBar.add(barManager);}}
效果如:
若想添加其它的Action,可以在類:
org.eclipse.ui.IWorkbenchActionConstantsorg.eclipse.ui.actions.ActionFactoryorg.eclipse.ui.actions.ContributionItemFactory
org.eclipse.ui.IWorkbenchCommandConstants 尋找到對應Action的實現,eclipse實現。