Eclipse RCP平添菜单
Eclipse RCP添加菜单
一、单级菜单
1.ApplicationWorkbenchWindowAdvisor.java添加configurer.setShowMenuBar(true);
public void preWindowOpen() { IWorkbenchWindowConfigurer configurer = getWindowConfigurer(); configurer.setInitialSize(new Point(400, 300)); configurer.setShowMenuBar(true); configurer.setShowCoolBar(false); configurer.setShowStatusLine(false); configurer.setTitle("Hello RCP"); //$NON-NLS-1$ }
2.新建Action
package com.dianbo.first.actions; import org.eclipse.jface.action.Action; import org.eclipse.jface.dialogs.MessageDialog; import org.eclipse.jface.resource.ImageDescriptor; import org.eclipse.ui.IWorkbenchWindow; import org.eclipse.ui.internal.IWorkbenchGraphicConstants; import org.eclipse.ui.internal.WorkbenchImages; @SuppressWarnings("restriction") public class MyFirstMenuAction extends Action { private IWorkbenchWindow window; public MyFirstMenuAction(IWorkbenchWindow window) { this.window = window; this.setText("&导航@Ctrl+Alt+N"); setToolTipText("open new Action!"); ImageDescriptor imageDes = WorkbenchImages .getImageDescriptor(IWorkbenchGraphicConstants.IMG_ETOOL_EXPORT_WIZ); this.setImageDescriptor(imageDes); } public void run(){ MessageDialog.openInformation(window.getShell(), "Memo", "Mr.Taxi By 소녀시대"); } }
3.ApplicationActionBarAdvisor.java
private MyFirstMenuAction newOpenAction; public ApplicationActionBarAdvisor(IActionBarConfigurer configurer) { super(configurer); } protected void makeActions(IWorkbenchWindow window) { newOpenAction = new MyFirstMenuAction(window); } protected void fillMenuBar(IMenuManager menuBar) { MenuManager firstMenuManager = new MenuManager("&HiFisrt", IWorkbenchActionConstants.M_WINDOW); firstMenuManager.add(newOpenAction); menuBar.add(firstMenuManager); ; }
二、多级菜单
ApplicationActionBarAdvisor.java
protected void fillMenuBar(IMenuManager menuBar) { MenuManager firstMenuManager = new MenuManager("&Fisrt", IWorkbenchActionConstants.M_WINDOW); MenuManager secondMenuManager = new MenuManager("&Second", IWorkbenchActionConstants.M_WINDOW); secondMenuManager.add(newOpenAction); firstMenuManager.add(secondMenuManager); menuBar.add(firstMenuManager); ; }