RCP创建菜单栏工具栏(下拉)

2023-12-14 17:32
文章标签 创建 工具栏 菜单栏 rcp

本文主要是介绍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创建菜单栏工具栏(下拉)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/493422

相关文章

Macos创建python虚拟环境的详细步骤教学

《Macos创建python虚拟环境的详细步骤教学》在macOS上创建Python虚拟环境主要通过Python内置的venv模块实现,也可使用第三方工具如virtualenv,下面小编来和大家简单聊聊... 目录一、使用 python 内置 venv 模块(推荐)二、使用 virtualenv(兼容旧版 P

Linux lvm实例之如何创建一个专用于MySQL数据存储的LVM卷组

《Linuxlvm实例之如何创建一个专用于MySQL数据存储的LVM卷组》:本文主要介绍使用Linux创建一个专用于MySQL数据存储的LVM卷组的实例,具有很好的参考价值,希望对大家有所帮助,... 目录在Centos 7上创建卷China编程组并配置mysql数据目录1. 检查现有磁盘2. 创建物理卷3. 创

Java 如何创建和使用ExecutorService

《Java如何创建和使用ExecutorService》ExecutorService是Java中用来管理和执行多线程任务的一种高级工具,可以有效地管理线程的生命周期和任务的执行过程,特别是在需要处... 目录一、什么是ExecutorService?二、ExecutorService的核心功能三、如何创建

使用Python创建一个功能完整的Windows风格计算器程序

《使用Python创建一个功能完整的Windows风格计算器程序》:本文主要介绍如何使用Python和Tkinter创建一个功能完整的Windows风格计算器程序,包括基本运算、高级科学计算(如三... 目录python实现Windows系统计算器程序(含高级功能)1. 使用Tkinter实现基础计算器2.

CentOS和Ubuntu系统使用shell脚本创建用户和设置密码

《CentOS和Ubuntu系统使用shell脚本创建用户和设置密码》在Linux系统中,你可以使用useradd命令来创建新用户,使用echo和chpasswd命令来设置密码,本文写了一个shell... 在linux系统中,你可以使用useradd命令来创建新用户,使用echo和chpasswd命令来设

使用Python和Pyecharts创建交互式地图

《使用Python和Pyecharts创建交互式地图》在数据可视化领域,创建交互式地图是一种强大的方式,可以使受众能够以引人入胜且信息丰富的方式探索地理数据,下面我们看看如何使用Python和Pyec... 目录简介Pyecharts 简介创建上海地图代码说明运行结果总结简介在数据可视化领域,创建交互式地

idea中创建新类时自动添加注释的实现

《idea中创建新类时自动添加注释的实现》在每次使用idea创建一个新类时,过了一段时间发现看不懂这个类是用来干嘛的,为了解决这个问题,我们可以设置在创建一个新类时自动添加注释,帮助我们理解这个类的用... 目录前言:详细操作:步骤一:点击上方的 文件(File),点击&nbmyHIgsp;设置(Setti

Spring 中使用反射创建 Bean 实例的几种方式

《Spring中使用反射创建Bean实例的几种方式》文章介绍了在Spring框架中如何使用反射来创建Bean实例,包括使用Class.newInstance()、Constructor.newI... 目录1. 使用 Class.newInstance() (仅限无参构造函数):2. 使用 Construc

C#原型模式之如何通过克隆对象来优化创建过程

《C#原型模式之如何通过克隆对象来优化创建过程》原型模式是一种创建型设计模式,通过克隆现有对象来创建新对象,避免重复的创建成本和复杂的初始化过程,它适用于对象创建过程复杂、需要大量相似对象或避免重复初... 目录什么是原型模式?原型模式的工作原理C#中如何实现原型模式?1. 定义原型接口2. 实现原型接口3

Python中conda虚拟环境创建及使用小结

《Python中conda虚拟环境创建及使用小结》本文主要介绍了Python中conda虚拟环境创建及使用小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们... 目录0.前言1.Miniconda安装2.conda本地基本操作3.创建conda虚拟环境4.激活c