转载: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.
分享到:
相关推荐
使用 npm install bootstrap-show-password-toggle 使用安装yarn add bootstrap-show-password-toggle用法在 CSS 中包含show-password-toggle.css 将输入的密码包装在input-group div 中,如下所示: <...
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 ...
Laravel开发-laravel-feature-toggle Laravel 5包包装Joshuaestes/功能切换
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
超好超棒的bootstrap-toggle-buttons用在网页很不错
Laravel开发-laravel-toggle-switch-button 为Laravel定制的拨动开关按钮发生器。
Atom-atom-toggle.zip,切换关键字切换,atom是一个用web技术构建的开源文本编辑器。
React原生切换开关用于 React Native 应用程序的可定制开关组件
<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 在启用了“热模块重新加载”的...
link href =" videojs-captions-toggle.css " rel =" stylesheet " > < script src =" video.js " > </ script > < script src =" videojs-captions-toggle.js " > </ script > < ...
安装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...
在 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。 如果您还想在新域上注入内容脚本,那么在与配对时很好。 该存储库甚至包括可以告诉您的用户如何使用它。 在该...
React-Bootstrap-切换 这是项目的react组件。 用法 您需要在应用程序中包含bootstrap css文件以及bootstrap2-toggle css文件。 如果您使用的是npm版本,则可以从这样的...import Toggle from 'react-bootstrap-toggle