转载:http://www.ralfebert.de/blog/eclipsercp/togglehandler//
Toggle Handler for Eclipse Commands
Based on Moritz Posts blog post Toggling a Command contribution I built a base class for a command handler which automatically handles the toggle state. To use it add the base class to your project:
ToggleHandler.java
Add a state element to your command contribution:
<command id="somecommand" name="SomeCommand">
<state class="org.eclipse.jface.commands.ToggleState" id="STYLE"/>
</command>
Contribute your handler using the style="toggle" attribute:
<extension point="org.eclipse.ui.menus">
<menuContribution
locationURI="menu:de.ralfebert.somemenu">
<command commandId="somecommand" style="toggle"/>
</menuContribution>
</extension>
And use ToggleHandler to implement your Handler class:
public class SomeCommandHandler extends ToggleHandler {
@Override
protected void executeToggle(ExecutionEvent event, boolean newState) {
System.out.println(newState);
}
}
That’s everything required to get toggling commands:
转载于:http://eclipsesource.com/blogs/2009/01/15/toggling-a-command-contribution/
Every once in a while something just doesn’t happen to be as intiutive as you would have liked it to be. Lately I was trying to contribute a simple command based toggle button to the workbench. Although it is simple to actually provide the menu contribution and to put the button in visual “toggle” mode, it was so straight forward to actually obtain the state of the button in the UI.
To make the menu contribution show up as a toggleable button you have to provide the style value “toggle” on your commands menu contribution:
<extension point="org.eclipse.ui.menus">
<menuContribution locationURI="...">
<command commandId="org.eclipse.example.command.toggle"
style="toggle" />
</menuContribution>
</extension>
Next we have too keep track of our actual toggle state. Since it is possible to have multiple menu contributions for the same command, we have to keep track of the state in a central place. Imagine a toggle button triggerable from the main menu and a views toolbar. The state of these buttons are keept in sync by storing the state directly in the command. The key to this is the org.eclipse.jface.commands.ToggleState. This implementation of org.eclipse.core.commands.State is a wrapper for a boolean. To attach such a State object to a command, it is provided during the command declaration:
<command id="org.eclipse.example.command.toggle"
name="Toggle Me">
<state class="org.eclipse.jface.commands.ToggleState"
id="org.eclipse.example.command.toggleState" />
</command>
The state element takes the ToggleState class as a state provider and attaches itself to the command. The id of the state has to be unique to identfy the state. With the state attached to our command, how can we access it? In the Handler, reacting to the invocation of the menu contribution, we have to access the current state keept in the ToggleState. The following code demonstrates just that:
ICommandService service =
(ICommandService) PlatformUI.getWorkbench().getService(ICommandService.class);
Command command = service.getCommand("org.eclipse.example.command.toggle");
State state = command.getState("org.eclipse.example.command.toggleState");
state.setValue(!(Boolean) state.getValue());
We obtain our command from the ICommandService and ask it for our toggleState stored within the command. Next we just flip the boolean as to create the new state. Now we can just do whatever we want to do in our Handler using the new state. Great, isn’t it? Of course the ToggleState is only one possible implementation of State. One could also imagine multiple states, radio states, text states etc. Also the State class has a specialization (PersistableState), which can be persisted to the preferences as to keep track of the (toggle) states of your buttons.
But there is thing left to do: what happens to this other button which is also a menu contribution for our command? We need to toggle the pressed state of it, as to reflect the state of the first button. To do so, a Handler has to implement IElementUpdater providing the method updateElements(…). It gets an UIElement as a parementer, which can be used to trigger the toggle state:
public void updateElement(UIElement element, Map paramters) {
element.setChecked(isSelected);
}
To broadcast the refresh event to all menu contributions we use the ICommandService in our Handlers:
commandService.refreshElements(executionEvent.getCommand().getId(), null);
I hope you find the information here valuable and i am looking forward to your comments.
分享到:
相关推荐
"react-native-toggle-switch"是一个专门为React Native应用设计的可定制开关组件,它允许开发者轻松地在应用程序中添加开/关功能,同时可以根据项目需求进行样式调整。 1. **React Native基础**: React Native是...
使用 npm install bootstrap-show-password-toggle 使用安装yarn add bootstrap-show-password-toggle用法在 CSS 中包含show-password-toggle.css 将输入的密码包装在input-group div 中,如下所示: <...
【Laravel开发-laravel-feature-toggle】是一个专注于在Laravel框架中实现特性切换(Feature Toggle)的包,由Joshuaestes开发。特性切换是一种软件开发实践,它允许开发者在不部署新代码的情况下,通过控制台或后台...
Atom-atom-toggle是一款基于Atom文本编辑器的插件,它主要功能是实现关键字的快速切换。Atom是由GitHub开发的一款开源文本编辑器,采用Web技术构建,具有高度的可定制性和灵活性,深受开发者喜爱。这款插件是T9MD...
npm i react-dark-mode-toggle 使用 : yarn add react-dark-mode-toggle :sparkles: 用法 import React , { useState } from "react" ; import DarkModeToggle from "react-dark-mode-toggle" ; export default ...
Bootstrap Toggle Buttons是一款基于Bootstrap框架的插件,用于创建美观、交互性强的切换按钮。这款插件使得在网页设计中添加开关、选项等控制元素变得更加简便。Bootstrap Toggle Buttons提供了丰富的自定义选项,...
npm install vue-js-toggle-button --save 进口 导入插件: import ToggleButton from 'vue-js-toggle-button' Vue . use ( ToggleButton ) 要么 导入组件: import { ToggleButton } from 'vue-js-toggle-button...
开源项目-101Bas-hosts-toggle.zip,hosts-toggle - CLI application to easily toggle certain lines in your hostfile
<label class="mdl-icon-toggle mdl-js-icon-toggle mdl-js-ripple-effect"> <input class="mdl-icon-toggle__input" type="checkbox" value="0.9" checked="checked"/><i class="mdl-icon-toggle__...
exports = { modules : [ 'nuxt-feature-toggle' ] , featureToggle : { toggles : ( ) => { return Promise . resolve ( { 'my-unique-key' : true } ) } }}作为对象module . exports = { modules : [ 'nuxt-...
React-Native-Toggle元素安装yarn add react-native-toggle-element# or with npmnpm install react-native-toggle-element用法初始值import React , { useState } from 'react' ;import Toggle from 'react-native-...
React-Multi-Toggle是一种时尚轻巧的切换组件,扩展了复选框或单选按钮的功能 安装 可以作为安装 npm install react-multi-toggle 发展 示例可以在找到。 npm i npm start 在启用了“热模块重新加载”的...
本资源“QT-Multi-language-dymantic-toggle.7z”提供了一个完整的示例,展示了如何在QT应用中实现简体中文和英文的多语言动态切换。 在QT应用中实现多语言功能,主要依赖于QT的国际化(i18n)支持。i18n是...
安装jest (需要Jest 23.FIXME +)和jest-watch-toggle-verbosity yarn add --dev jest jest-watch-toggle-verbosity # or with NPM npm install --save-dev jest jest-watch-toggle-verbosity 将其添加到您的Jest...
link href =" videojs-captions-toggle.css " rel =" stylesheet " > < script src =" video.js " > </ script > < script src =" videojs-captions-toggle.js " > </ script > < ...
### Android开发——深入解析Eclipse调试与快捷键 #### 一、Eclipse Debugging详解 在Android开发过程中,使用Eclipse进行调试是非常重要的步骤之一。本文档将详细介绍Eclipse调试功能的基本使用方法,并提供一些...
在 GitBook 上切换章节使用以下方法安装它: $ npm install gitbook-plugin-toggle-chapters 要在您的书中使用它,请将其添加到 book.json: { "plugins": ["toggle-chapters"]}
import Toggle from 'react-styled-toggle'; render(){ return <Toggle> } 发展 yarn storybook 建造 yarn build 道具 财产 propType 必需的 默认 描述 已检查 布尔 -- -- 残障人士 布尔 -- -- onChange 功能...
pricing-component-with-toggle:前端导师挑战-带有Toggle的定价组件
WebExtension模块:浏览器操作上下文菜单,用于请求当前选项卡的权限。 Chrome,Firefox,Safari。 如果您还想在新域上注入内容脚本,那么在与配对时很好。 该存储库甚至包括可以告诉您的用户如何使用它。 在该...