Eclipse RCP:具有对象相关状态的弹出菜单切换命令
Eclipse RCP: popup menu toggle command with object dependent states
我想要一个允许切换所单击对象的不同状态的上下文菜单(右键单击)。
在 plugin.xml 中,我已经有一个带有命令条目的弹出菜单,例如:
<command
commandId="...switchDistanceCommand"
label="30s"
style="toggle">
<parameter
name="...switchDistanceMillis"
value="30000">
</parameter>
</command>
和一个命令:
<extension
point="org.eclipse.ui.commands">
<command
id="....switchDistanceCommand"
name="Switch Distance">
<commandParameter
id="....switchDistanceMillis"
name="Seconds"
optional="false">
</commandParameter>
</command>
</extension>
负责人:
<handler
class="....SwitchDistanceHandler"
commandId="....switchDistanceCommand">
</handler>
处理程序 class SwitchDistanceHandler
检查选择了哪些对象并调用它们的方法来切换它们的状态(向列表添加或删除参数化值)。
到目前为止一切顺利...
但是,我想让我的菜单条目像复选框(如 style="toggle"
)指示的那样工作。每个关于这个问题的教程(例如 this one)都解释了如何通过将以下代码添加到 plugin.xml 来为命令添加状态:
<state
class="org.eclipse.ui.handlers.RegistryToggleState:true"
id="org.eclipse.ui.commands.toggleState">
</state>
但这只会给我这个命令的一个全局状态,我想从单击的对象中读取状态?我怎样才能做到这一点?
编辑 1: 复制了从教程中截取的错误代码。我还尝试实现自己的 class 来扩展 State
class(就像 RegistryToggleState
那样)。但是我无法弄清楚如何从这个 class.
return 一个状态
编辑 2: 我找到了解决方法。它没有解决提出的问题,但对我有用。
解决方法
这不完全是问题的解决方案,因为它不使用任何状态对象。但它对我来说很好用:
根据 this question 的建议,我在 Handler 中实现了 IElementUpdater
。
在 updateElement
方法中,我从 element
对象中获取选定的元素并相应地调用 element.setChecked()
。
我想要一个允许切换所单击对象的不同状态的上下文菜单(右键单击)。
在 plugin.xml 中,我已经有一个带有命令条目的弹出菜单,例如:
<command
commandId="...switchDistanceCommand"
label="30s"
style="toggle">
<parameter
name="...switchDistanceMillis"
value="30000">
</parameter>
</command>
和一个命令:
<extension
point="org.eclipse.ui.commands">
<command
id="....switchDistanceCommand"
name="Switch Distance">
<commandParameter
id="....switchDistanceMillis"
name="Seconds"
optional="false">
</commandParameter>
</command>
</extension>
负责人:
<handler
class="....SwitchDistanceHandler"
commandId="....switchDistanceCommand">
</handler>
处理程序 class SwitchDistanceHandler
检查选择了哪些对象并调用它们的方法来切换它们的状态(向列表添加或删除参数化值)。
到目前为止一切顺利...
但是,我想让我的菜单条目像复选框(如 style="toggle"
)指示的那样工作。每个关于这个问题的教程(例如 this one)都解释了如何通过将以下代码添加到 plugin.xml 来为命令添加状态:
<state
class="org.eclipse.ui.handlers.RegistryToggleState:true"
id="org.eclipse.ui.commands.toggleState">
</state>
但这只会给我这个命令的一个全局状态,我想从单击的对象中读取状态?我怎样才能做到这一点?
编辑 1: 复制了从教程中截取的错误代码。我还尝试实现自己的 class 来扩展 State
class(就像 RegistryToggleState
那样)。但是我无法弄清楚如何从这个 class.
编辑 2: 我找到了解决方法。它没有解决提出的问题,但对我有用。
解决方法
这不完全是问题的解决方案,因为它不使用任何状态对象。但它对我来说很好用:
根据 this question 的建议,我在 Handler 中实现了 IElementUpdater
。
在 updateElement
方法中,我从 element
对象中获取选定的元素并相应地调用 element.setChecked()
。