moe moe - 3 months ago 17
Java Question

Eclipse RCP: popup menu toggle command with object dependent states

I want have a context menu (right-click) that allows to toggle different states of the clicked object.

In the plugin.xml, I already have a popup menu with commands entries such as:

<command
commandId="...switchDistanceCommand"
label="30s"
style="toggle">
<parameter
name="...switchDistanceMillis"
value="30000">
</parameter>
</command>


and a command:

<extension
point="org.eclipse.ui.commands">
<command
id="....switchDistanceCommand"
name="Switch Distance">
<commandParameter
id="....switchDistanceMillis"
name="Seconds"
optional="false">
</commandParameter>
</command>
</extension>


The handler:

<handler
class="....SwitchDistanceHandler"
commandId="....switchDistanceCommand">
</handler>


The handler class
SwitchDistanceHandler
checks which objects are selected an calls a method on them to switch their state (adding or removing the parametrized value to a List).

So far so good...

However, I want to have my menu entries work as checkboxes (as
style="toggle"
) indicates. Every tutorial on this issue (such as this one) explains how to add a state to command by adding the following code to the plugin.xml:

<state
class="org.eclipse.ui.handlers.RegistryToggleState:true"
id="org.eclipse.ui.commands.toggleState">
</state>


But this will only give me only one global state for this command, I want to read the state(s) from the clicked objects? How can I do this?

Edit 1: Copied the wrong code snipped from the tutorial. Also I tried to implement an own class that extends the
State
class (as
RegistryToggleState
does). But I could not figure out how to return a state from this class.

Edit 2: I found a workaround. It does not solve the proposed problem but it works for me.

moe moe
Answer

Workaround

This is not exactly a solution to the problem because it does not use any state objects. But it works fine for me: Following the advice from this question I implemented IElementUpdater in my Handler. In the updateElement method, I fetched the selected element(s) from the element object and called element.setChecked()accordingly.

Comments