本文主要是介绍RCP创建菜单栏工具栏(下拉),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
如图:
1
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.ActionContributionItem;
import org.eclipse.jface.action.IContributionItem;
import org.eclipse.jface.action.ICoolBarManager;
import org.eclipse.jface.action.IMenuCreator;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.IStatusLineManager;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.Separator;
import org.eclipse.jface.action.ToolBarManager;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
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;public class ApplicationActionBarAdvisor extends ActionBarAdvisor {private NewAction new1;private IWorkbenchAction exitAction;private IWorkbenchAction helpopen;private IWorkbenchAction refOpen;private PullDownAction pd;public ApplicationActionBarAdvisor(IActionBarConfigurer configurer) {super(configurer);}// 创建并注册action@Overrideprotected void makeActions(IWorkbenchWindow window) {new1 = new NewAction();register(new1);exitAction = ActionFactory.QUIT.create(window);register(exitAction);// 帮助菜单,需要增加帮助扩展点helpopen = ActionFactory.HELP_CONTENTS.create(window);register(helpopen);// 首选项菜单,需要增加首选项扩展点refOpen = ActionFactory.PREFERENCES.create(window);register(refOpen);// 设置不可用exitAction.setEnabled(false);}// 创建菜单,菜单项@Overrideprotected void fillMenuBar(IMenuManager menuBar) {MenuManager code = new MenuManager("code(&c)");code.add(new1);code.add(exitAction);code.add(helpopen);code.add(refOpen);// 增加到菜单栏menuBar.add(code);MenuManager help = new MenuManager("help(&h)");// 添加二级菜单MenuManager second = new MenuManager("second");second.add(new1);pd = new PullDownAction();help.add(second);help.add(new1);// 添加分割线help.add(new Separator());help.add(pd);help.add(helpopen);menuBar.add(help);}// 创建工具栏,需在ApplicationWorkbenchWindowAdvisor中设置setShowCoolBar(true);@Overrideprotected void fillCoolBar(ICoolBarManager coolBar) {IToolBarManager toolbar = new ToolBarManager(coolBar.getStyle());toolbar.add(new1);toolbar.add(new Separator());toolbar.add(exitAction);coolBar.add(toolbar);IToolBarManager toolbar2 = new ToolBarManager(coolBar.getStyle());pd = new PullDownAction();// 设置工具栏下拉菜单pd.setMenuCreator(new TestMenuCreator());toolbar2.add(new1);toolbar2.add(pd);toolbar2.add(new PullDownBar());toolbar2.add(exitAction);coolBar.add(toolbar2);}// 状态栏, 需在ApplicationWorkbenchWindowAdvisor中设置setShowStatusLine(true);@Overrideprotected void fillStatusLine(IStatusLineManager statusLine) {// TODO Auto-generated method stubsuper.fillStatusLine(statusLine);statusLine.add(new1);}class NewAction extends Action {NewAction() {// 设置Action的样式super("", Action.AS_PUSH_BUTTON);// 设置labelthis.setText("new(&n)");// 设置ID,便于管理this.setId("NewAction");// 设置提示this.setToolTipText("testnew");// 添加图标,菜单栏、工具栏、状态栏共用// 使用绝对路径// ImageData data = new// ImageData("D:\\workspace\\RCPTest\\icons\\alt_window_16.gif");// ImageDescriptor imageData = ImageDescriptor.createFromImageData(// data );// this.setImageDescriptor(imageData);// 使用相对路径// try{// URL u = new// URL(Activator.getDefault().getBundle().getEntry("/"),"icons/sample.gif");// this.setImageDescriptor(ImageDescriptor.createFromURL(u));// }catch(Exception e){//// }}@Overridepublic void run() {// 动态设置不可用// 得到MenuBar上的菜单,菜单索引从0开始MenuItem menu = Activator.getDefault().getWorkbench().getActiveWorkbenchWindow().getShell().getMenuBar().getItem(1);// 得到菜单中的菜单项MenuItem item = menu.getMenu().getItem(3);// 菜单项的具体设置item.setEnabled(false);System.out.println(menu.getText());System.out.println(menu.getMenu().getItem(3).getText());}}class PullDownAction extends Action {PullDownAction() {super("", Action.AS_DROP_DOWN_MENU);this.setText("PullDown(&P)");this.setId("PullDownAction");this.setToolTipText("PullDownAction");}@Overridepublic void run() {System.out.println("testtest");}}// 为工具栏中的选项增加下拉菜单class TestMenuCreator implements IMenuCreator {private MenuManager dropDownMenuMgr;@Overridepublic Menu getMenu(Menu parent) {// 在菜单栏被调用return null;}@Overridepublic Menu getMenu(Control parent) {// 在工具栏被 调用createDropDownMenuMgr();return dropDownMenuMgr.createContextMenu(parent);}@Overridepublic void dispose() {if (null != dropDownMenuMgr) {dropDownMenuMgr.dispose();dropDownMenuMgr = null;}}private void createDropDownMenuMgr() {if (dropDownMenuMgr == null) {dropDownMenuMgr = new MenuManager();dropDownMenuMgr.add(new InnerAction("One"));dropDownMenuMgr.add(new InnerAction("Two"));}}}class InnerAction extends Action {private final String text;InnerAction(String text) {super(text);this.text = text;}@Overridepublic void run() {super.run();if ("One".equals(text)) {System.out.println("click one");} else {// do two something}}}// 工具栏下拉菜单实现class PullDownBar extends Action implements IMenuCreator {private MenuManager dropDownMenuMgr;PullDownBar() {super("", Action.AS_DROP_DOWN_MENU);this.setId("PullDownBar");this.setText("PullDownBar");this.setToolTipText("pulldownbar");setMenuCreator(this);}@Overridepublic void run() {System.out.println("pulldownbar");}@Overridepublic void dispose() {if (null != dropDownMenuMgr) {dropDownMenuMgr.dispose();dropDownMenuMgr = null;}}// parent为工具栏时调用@Overridepublic Menu getMenu(Control parent) {createDropDownMenuMgr();return dropDownMenuMgr.createContextMenu(parent);}// /parent为菜单栏时调用@Overridepublic Menu getMenu(Menu parent) {createDropDownMenuMgr();Menu menu = new Menu(parent);IContributionItem[] items = dropDownMenuMgr.getItems();for (int i = 0; i < items.length; i++) {IContributionItem item = items[i];IContributionItem newItem = item;if (item instanceof ActionContributionItem) {newItem = new ActionContributionItem(((ActionContributionItem) item).getAction());}newItem.fill(menu, -1);}return menu;}private void createDropDownMenuMgr() {if (dropDownMenuMgr == null) {dropDownMenuMgr = new MenuManager();dropDownMenuMgr.add(new InnerAction("One"));dropDownMenuMgr.add(new InnerAction("two"));}}}}
这篇关于RCP创建菜单栏工具栏(下拉)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!