- 浏览: 25760 次
- 性别:
- 来自: 重庆
文章分类
最新评论
转载于:http://blog.eclipse-tips.com/2009/03/commands-part-6-toggle-radio-menu.html
Commands Part 6: Toggle & Radio menu contributions
In the previous parts of the series, we saw how Commands contribute to push style menu items. But commands allow you to contribute menu items with 'toggle' and 'radio' style as well. Let us see how to contribute the familiar "Format" menu thru Commands:
First we will look at the toggle style menu contribution. Commands can have states associated with it. A state id known by its id and it can have any value, which is stored in a subclass of org.eclipse.core.commands.State. To know whether a command is checked or not, the Command Framework looks for a state with the id 'org.eclipse.ui.commands.toggleState'. Lets specify the default checked state of Bold to true and Italic to false:
<command
defaultHandler="com.eclipse_tips.commandstate.BoldHandler"
id="com.eclipse-tips.commandState.boldCommand"
name="Bold">
<state
class="org.eclipse.ui.handlers.RegistryToggleState:true"
id="org.eclipse.ui.commands.toggleState">
</state>
</command>
<command
defaultHandler="com.eclipse_tips.commandstate.ItalicHandler"
id="com.eclipse-tips.commandState.italicCommand"
name="Italic">
<state
class="org.eclipse.ui.handlers.RegistryToggleState:false"
id="org.eclipse.ui.commands.toggleState">
</state>
</command>
The Framework expects a Boolean value from the state. It doesn't care about which class that holds the state. So why should we use the RegistryToggleState instead of our own state class? Two reasons:
It implements IExecutableExtension. So you can specify the default values in the plugin.xml as I've done above.
It extends PersistentState, so it can remember the value between Eclipse sessions. So if the user had checked/unchecked the menu, restarts Eclipse, he will get the same state as he left before - all this, you get without even your plugin being loaded.
So how should the handler work for this command?
public Object execute(ExecutionEvent event) throws ExecutionException {
Command command = event.getCommand();
boolean oldValue = HandlerUtil.toggleCommandState(command);
// use the old value and perform the operation
return null;
}
Basically, its the Handler's responsibility to update the Command's state. The HandlerUtil has a convenient method which toggles the state and gives you the old value of the state. You can use the value to perform the operation.
The radio state is also similar, which expects the state with a predefined id. Since the same command is contributed for various option, its should be a parameterized command. The id of the parameter which denotes the radio state is also predefined:
<command
defaultHandler="com.eclipse_tips.commandstate.AlignHandler"
id="com.eclipse-tips.commandState.alignCommand"
name="Align Command">
<commandParameter
id="org.eclipse.ui.commands.radioStateParameter"
name="State"
optional="false">
</commandParameter>
<state
class="org.eclipse.ui.handlers.RadioState:left"
id="org.eclipse.ui.commands.radioState">
</state>
</command>
Just like toggle state, the state must have the id and the class could be anything that stores the value as String. The RadioState class provides initializing from plugin.xml and also persists the value across sessions.
The menu contribution would specify the parameter value:
<menuContribution
locationURI="menu:org.eclipse.ui.main.menu?after=additions">
<menu
label="Format">
... other contributions here
<command
commandId="com.eclipse-tips.commandState.alignCommand"
label="Align Left"
style="radio">
<parameter
name="org.eclipse.ui.commands.radioStateParameter"
value="left">
</parameter>
</command>
<command
commandId="com.eclipse-tips.commandState.alignCommand"
label="Align Center"
style="radio">
<parameter
name="org.eclipse.ui.commands.radioStateParameter"
value="center">
</parameter>
</command>
<command
commandId="com.eclipse-tips.commandState.alignCommand"
label="Align Right"
style="radio">
<parameter
name="org.eclipse.ui.commands.radioStateParameter"
value="right">
</parameter>
</command>
</menu>
</menuContribution>
The Command Framework understands this parameter and sets the UI contributions according to this value. Again its the handler's job to set the correct state from the command's parameter during execution. You have helper methods in the HandlerUtil to perform this:
public Object execute(ExecutionEvent event) throws ExecutionException {
if(HandlerUtil.matchesRadioState(event))
return null; // we are already in the updated state - do nothing
String currentState = event.getParameter(RadioState.PARAMETER_ID);
// perform task for current state
if(currentState.equals("left"))
// perform left alignment
else if(currentState.equals("center"))
// perform center alignment
// and so on ...
// and finally update the current state
HandlerUtil.updateRadioState(event.getCommand(), currentState);
return null;
}
Bonus:
Q: I want the initial value always loaded from the plugin.xml, the state should not be remembered between sessions. Should I write my own State class?
A: Not necessary. You can specify both the default value of the state and whether to persist or not in the plugin.xml itself:
<command
defaultHandler="com.eclipse_tips.commandstate.AlignHandler"
id="com.eclipse-tips.commandState.alignCommand"
name="Align Command">
<commandParameter
id="org.eclipse.ui.commands.radioStateParameter"
name="State"
optional="false">
</commandParameter>
<state
id="org.eclipse.ui.commands.radioState">
<class
class="org.eclipse.ui.handlers.RadioState">
<parameter
name="default"
value="left">
</parameter>
<parameter
name="persisted"
value="false">
</parameter>
</class>
</state>
</command>
This applies for toggle state as well.
Q: I want to know the state of the command elsewhere in the code. How do I get it?
A: Just use Command.getState(<state id>).getValue(). The state ids are available as constants at RegistryToggleState.STATE_ID and RadioState.STATE_ID
My patch for this feature has just been checked in and should be available from 3.5 M6 onwards. If you are curious, pick up a recent Nightly build and play with this.
Commands Part 6: Toggle & Radio menu contributions
In the previous parts of the series, we saw how Commands contribute to push style menu items. But commands allow you to contribute menu items with 'toggle' and 'radio' style as well. Let us see how to contribute the familiar "Format" menu thru Commands:
First we will look at the toggle style menu contribution. Commands can have states associated with it. A state id known by its id and it can have any value, which is stored in a subclass of org.eclipse.core.commands.State. To know whether a command is checked or not, the Command Framework looks for a state with the id 'org.eclipse.ui.commands.toggleState'. Lets specify the default checked state of Bold to true and Italic to false:
<command
defaultHandler="com.eclipse_tips.commandstate.BoldHandler"
id="com.eclipse-tips.commandState.boldCommand"
name="Bold">
<state
class="org.eclipse.ui.handlers.RegistryToggleState:true"
id="org.eclipse.ui.commands.toggleState">
</state>
</command>
<command
defaultHandler="com.eclipse_tips.commandstate.ItalicHandler"
id="com.eclipse-tips.commandState.italicCommand"
name="Italic">
<state
class="org.eclipse.ui.handlers.RegistryToggleState:false"
id="org.eclipse.ui.commands.toggleState">
</state>
</command>
The Framework expects a Boolean value from the state. It doesn't care about which class that holds the state. So why should we use the RegistryToggleState instead of our own state class? Two reasons:
It implements IExecutableExtension. So you can specify the default values in the plugin.xml as I've done above.
It extends PersistentState, so it can remember the value between Eclipse sessions. So if the user had checked/unchecked the menu, restarts Eclipse, he will get the same state as he left before - all this, you get without even your plugin being loaded.
So how should the handler work for this command?
public Object execute(ExecutionEvent event) throws ExecutionException {
Command command = event.getCommand();
boolean oldValue = HandlerUtil.toggleCommandState(command);
// use the old value and perform the operation
return null;
}
Basically, its the Handler's responsibility to update the Command's state. The HandlerUtil has a convenient method which toggles the state and gives you the old value of the state. You can use the value to perform the operation.
The radio state is also similar, which expects the state with a predefined id. Since the same command is contributed for various option, its should be a parameterized command. The id of the parameter which denotes the radio state is also predefined:
<command
defaultHandler="com.eclipse_tips.commandstate.AlignHandler"
id="com.eclipse-tips.commandState.alignCommand"
name="Align Command">
<commandParameter
id="org.eclipse.ui.commands.radioStateParameter"
name="State"
optional="false">
</commandParameter>
<state
class="org.eclipse.ui.handlers.RadioState:left"
id="org.eclipse.ui.commands.radioState">
</state>
</command>
Just like toggle state, the state must have the id and the class could be anything that stores the value as String. The RadioState class provides initializing from plugin.xml and also persists the value across sessions.
The menu contribution would specify the parameter value:
<menuContribution
locationURI="menu:org.eclipse.ui.main.menu?after=additions">
<menu
label="Format">
... other contributions here
<command
commandId="com.eclipse-tips.commandState.alignCommand"
label="Align Left"
style="radio">
<parameter
name="org.eclipse.ui.commands.radioStateParameter"
value="left">
</parameter>
</command>
<command
commandId="com.eclipse-tips.commandState.alignCommand"
label="Align Center"
style="radio">
<parameter
name="org.eclipse.ui.commands.radioStateParameter"
value="center">
</parameter>
</command>
<command
commandId="com.eclipse-tips.commandState.alignCommand"
label="Align Right"
style="radio">
<parameter
name="org.eclipse.ui.commands.radioStateParameter"
value="right">
</parameter>
</command>
</menu>
</menuContribution>
The Command Framework understands this parameter and sets the UI contributions according to this value. Again its the handler's job to set the correct state from the command's parameter during execution. You have helper methods in the HandlerUtil to perform this:
public Object execute(ExecutionEvent event) throws ExecutionException {
if(HandlerUtil.matchesRadioState(event))
return null; // we are already in the updated state - do nothing
String currentState = event.getParameter(RadioState.PARAMETER_ID);
// perform task for current state
if(currentState.equals("left"))
// perform left alignment
else if(currentState.equals("center"))
// perform center alignment
// and so on ...
// and finally update the current state
HandlerUtil.updateRadioState(event.getCommand(), currentState);
return null;
}
Bonus:
Q: I want the initial value always loaded from the plugin.xml, the state should not be remembered between sessions. Should I write my own State class?
A: Not necessary. You can specify both the default value of the state and whether to persist or not in the plugin.xml itself:
<command
defaultHandler="com.eclipse_tips.commandstate.AlignHandler"
id="com.eclipse-tips.commandState.alignCommand"
name="Align Command">
<commandParameter
id="org.eclipse.ui.commands.radioStateParameter"
name="State"
optional="false">
</commandParameter>
<state
id="org.eclipse.ui.commands.radioState">
<class
class="org.eclipse.ui.handlers.RadioState">
<parameter
name="default"
value="left">
</parameter>
<parameter
name="persisted"
value="false">
</parameter>
</class>
</state>
</command>
This applies for toggle state as well.
Q: I want to know the state of the command elsewhere in the code. How do I get it?
A: Just use Command.getState(<state id>).getValue(). The state ids are available as constants at RegistryToggleState.STATE_ID and RadioState.STATE_ID
My patch for this feature has just been checked in and should be available from 3.5 M6 onwards. If you are curious, pick up a recent Nightly build and play with this.
发表评论
-
CoolBar CoolBarManager应用到任何ViewForm
2012-07-03 10:44 1556ViewForm viewForm = new ViewFor ... -
eclipse 透视图切换监听 和 视图激活监听
2012-06-28 16:24 2001<extension point=&q ... -
动态更新或者删除菜单
2012-06-08 12:42 907@Override public void postWind ... -
网址收集
2012-06-07 15:05 607http://wiki.eclipse.org/Platfor ... -
ActionHandler.java
2012-06-07 15:00 6631: /*************************** ... -
eclilpse check 菜单 --Toggle Handler for Eclipse Commands_1
2012-06-07 14:59 1145转载:http://eclipsesource.com/blo ... -
eclilpse check 菜单 --Toggle Handler for Eclipse Commands
2012-06-07 14:55 1291转载:http://www.ralfebert.de/blog ... -
RCP3.5版本 help所需的依赖包
2012-05-15 15:05 8081. org.apache.lucene 2. ...
相关推荐
Unity单选菜单(ToggleMenu)
"收展式二级菜单toggleMenu"是一种常见的导航结构,尤其适用于内容层次丰富的网站。这个插件的设计目标是通过点击第一级菜单项来动态显示或隐藏二级菜单,同时为用户提供平滑的3D旋转过渡效果,以增加视觉吸引力和...
"toggleMenu.rar" 是一个压缩包文件,其中包含了一个用于构建网站后台模板的资源集合。这个模板设计的主要目的是为了提供一个高效、用户友好的管理界面,使得网站管理员或开发人员在进行后台操作时能有一个清晰、...
标题 "LEDTOGGLE_ledtoggle_" 暗示我们讨论的主题是关于STM32F411 Nucleo开发板上实现LED灯闪烁的程序。STM32F411 Nucleo是一款基于ARM Cortex-M4内核的微控制器开发板,广泛应用于嵌入式系统设计。"LED TOGGLE"通常...
Bootstrap Toggle Buttons是一款基于Bootstrap框架的插件,用于创建美观、交互性强的切换按钮。这款插件使得在网页设计中添加开关、选项等控制元素变得更加简便。Bootstrap Toggle Buttons提供了丰富的自定义选项,...
Toggle是unity中常用的控件之一, 该项目是Unity3D中的Toggle使用事件的方式控制交互方式,简单实用。
在Unity引擎中,Toggle组件是UI系统的一部分,用于创建用户界面中的开关或复选框功能。Toggle组件在很多场合都非常实用,例如设置选项、选择开关等。本学习笔记将深入探讨Toggle组件及其相关组件ToggleGroup的一些...
$ yarn add custom-element-password-toggle < script type =" module " defer > import 'custom-element-password-toggle' </ script > 基本用法 只需在您的输入字段上添加is="password-toggle" : ...
STM32 Led Toggle STM32 Led Toggle STM32 Led Toggle STM32 Led Toggle
三星Toggle Mode DDR NAND规格说明书是关于三星公司生产的Toggle Mode DDR NAND闪存的一种详细技术文档。Toggle Mode DDR NAND是一种高速、高性能的非易失性存储技术,广泛应用于移动设备、嵌入式系统和固态硬盘等...
纯CSS切换(Toggle)按钮是一种常见的交互元素,用于在两种状态之间切换,例如打开/关闭、显示/隐藏等。它不依赖JavaScript,仅通过CSS实现,降低了代码复杂性,提高了页面加载速度。本教程将深入探讨如何创建这种可...
这个教程主要聚焦于三种常用的GUI控件:Radio Button、Check Box和Toggle Button,它们都是用户界面设计中的重要元素,用于接收用户的输入和设定。 Radio Button,通常成对或成组出现,用户只能选择其中的一个。在...
本案例主要探讨的是jQuery中的`toggle()`方法,它主要用于元素的显示和隐藏,是一个非常实用的功能。 `toggle()`函数是jQuery的核心方法之一,它的基本用法有两种:一种是无参数的,另一种是接受两个函数作为参数。...
本资源“S7-200SMART_库文件_翻转输出状态_toggle_单按钮启停功能.rar”提供了一种巧妙的方法,通过库指令实现了一个简单的单按钮启停控制逻辑,这对于许多工业应用来说非常实用。 首先,我们需要了解S7-200SMART ...
ToggleButtons是针对Android平台的一个开源项目,专门设计用于实现符合材料设计规范的切换按钮。材料设计是Google推出的一种设计语言,旨在提供一致、直观且富有表现力的用户体验,而ToggleButtons则是这一设计理念...
Toggle切换按钮,通常被称作Toggle Switch或Switch Control,在用户界面设计中扮演着重要的角色,尤其是在移动应用中。它是用户进行二元选择的一种直观方式,允许用户在两种状态之间快速切换,例如开启或关闭某个...
Toggle Comment 1.5(VS最好的注释代码插件), Ctrl+/ 快速进行代码注释,方便好用!
2. 使用jQuery插件:为了更方便地实现复杂的美化效果,开发者可以选择使用已经存在的jQuery插件,如iCheck、uniform.js、Bootstrap Toggle等。这些插件提供了丰富的预设样式和选项,只需简单的配置就能快速实现美观...