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
	@Override
	protected 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);

	}

	// 创建菜单,菜单项
	@Override
	protected 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);
	@Override
	protected 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);
	@Override
	protected void fillStatusLine(IStatusLineManager statusLine) {
		// TODO Auto-generated method stub
		super.fillStatusLine(statusLine);
		statusLine.add(new1);
	}

	class NewAction extends Action {
		NewAction() {
			// 设置Action的样式
			super("", Action.AS_PUSH_BUTTON);
			// 设置label
			this.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){
			//
			// }
		}

		@Override
		public 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");
		}

		@Override
		public void run() {
			System.out.println("testtest");
		}

	}

	// 为工具栏中的选项增加下拉菜单
	class TestMenuCreator implements IMenuCreator {
		private MenuManager dropDownMenuMgr;

		@Override
		public Menu getMenu(Menu parent) {// 在菜单栏被调用
			return null;
		}

		@Override
		public Menu getMenu(Control parent) {// 在工具栏被 调用
			createDropDownMenuMgr();
			return dropDownMenuMgr.createContextMenu(parent);
		}

		@Override
		public 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;
		}

		@Override
		public 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);
		}

		@Override
		public void run() {
			System.out.println("pulldownbar");
		}

		@Override
		public void dispose() {
			if (null != dropDownMenuMgr) {
				dropDownMenuMgr.dispose();
				dropDownMenuMgr = null;
			}
		}

		// parent为工具栏时调用
		@Override
		public Menu getMenu(Control parent) {
			createDropDownMenuMgr();
			return dropDownMenuMgr.createContextMenu(parent);
		}

		// /parent为菜单栏时调用
		@Override
		public 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"));
			}
		}
	}

}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值