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

相关文章

Java 虚拟线程的创建与使用深度解析

《Java虚拟线程的创建与使用深度解析》虚拟线程是Java19中以预览特性形式引入,Java21起正式发布的轻量级线程,本文给大家介绍Java虚拟线程的创建与使用,感兴趣的朋友一起看看吧... 目录一、虚拟线程简介1.1 什么是虚拟线程?1.2 为什么需要虚拟线程?二、虚拟线程与平台线程对比代码对比示例:三

k8s按需创建PV和使用PVC详解

《k8s按需创建PV和使用PVC详解》Kubernetes中,PV和PVC用于管理持久存储,StorageClass实现动态PV分配,PVC声明存储需求并绑定PV,通过kubectl验证状态,注意回收... 目录1.按需创建 PV(使用 StorageClass)创建 StorageClass2.创建 PV

Linux创建服务使用systemctl管理详解

《Linux创建服务使用systemctl管理详解》文章指导在Linux中创建systemd服务,设置文件权限为所有者读写、其他只读,重新加载配置,启动服务并检查状态,确保服务正常运行,关键步骤包括权... 目录创建服务 /usr/lib/systemd/system/设置服务文件权限:所有者读写js,其他

idea+spring boot创建项目的搭建全过程

《idea+springboot创建项目的搭建全过程》SpringBoot是Spring社区发布的一个开源项目,旨在帮助开发者快速并且更简单的构建项目,:本文主要介绍idea+springb... 目录一.idea四种搭建方式1.Javaidea命名规范2JavaWebTomcat的安装一.明确tomcat

Git打标签从本地创建到远端推送的详细流程

《Git打标签从本地创建到远端推送的详细流程》在软件开发中,Git标签(Tag)是为发布版本、标记里程碑量身定制的“快照锚点”,它能永久记录项目历史中的关键节点,然而,仅创建本地标签往往不够,如何将其... 目录一、标签的两种“形态”二、本地创建与查看1. 打附注标http://www.chinasem.cn

Spring创建Bean的八种主要方式详解

《Spring创建Bean的八种主要方式详解》Spring(尤其是SpringBoot)提供了多种方式来让容器创建和管理Bean,@Component、@Configuration+@Bean、@En... 目录引言一、Spring 创建 Bean 的 8 种主要方式1. @Component 及其衍生注解

MySQL 数据库表操作完全指南:创建、读取、更新与删除实战

《MySQL数据库表操作完全指南:创建、读取、更新与删除实战》本文系统讲解MySQL表的增删查改(CURD)操作,涵盖创建、更新、查询、删除及插入查询结果,也是贯穿各类项目开发全流程的基础数据交互原... 目录mysql系列前言一、Create(创建)并插入数据1.1 单行数据 + 全列插入1.2 多行数据

MySQL 临时表创建与使用详细说明

《MySQL临时表创建与使用详细说明》MySQL临时表是存储在内存或磁盘的临时数据表,会话结束时自动销毁,适合存储中间计算结果或临时数据集,其名称以#开头(如#TempTable),本文给大家介绍M... 目录mysql 临时表详细说明1.定义2.核心特性3.创建与使用4.典型应用场景5.生命周期管理6.注

MySQL的触发器全解析(创建、查看触发器)

《MySQL的触发器全解析(创建、查看触发器)》MySQL触发器是与表关联的存储程序,当INSERT/UPDATE/DELETE事件发生时自动执行,用于维护数据一致性、日志记录和校验,优点包括自动执行... 目录触发器的概念:创建触www.chinasem.cn发器:查看触发器:查看当前数据库的所有触发器的定

创建springBoot模块没有目录结构的解决方案

《创建springBoot模块没有目录结构的解决方案》2023版IntelliJIDEA创建模块时可能出现目录结构识别错误,导致文件显示异常,解决方法为选择模块后点击确认,重新校准项目结构设置,确保源... 目录创建spChina编程ringBoot模块没有目录结构解决方案总结创建springBoot模块没有目录