Reputation: 1788
I'm writing a RCP App with target-platform 3.7. I like to enable a menuItem only if a specific view is active otherwise it should be disabled. I try it via expression like shown in the plugin.xml below, but the menuItem is always active.
<extension
point="org.eclipse.ui.commands">
<command
defaultHandler="pgui.handler.SaveHandler"
id="pgui.rcp.command.save"
name="Save">
</command>
</extension>
<extension
point="org.eclipse.ui.views">
<view
allowMultiple="true"
class="pgui.view.LogView"
id="pgui.view.LogView"
name="logview"
restorable="true">
</view>
</extension>
<extension
point="org.eclipse.ui.menus">
<menuContribution
allPopups="false"
locationURI="menu:org.eclipse.ui.main.menu">
<menu
id="fileMenu"
label="File">
<command
commandId="pgui.rcp.command.save"
label="Save"
style="push"
tooltip="Save the active log file.">
</command>
</menu>
</menuContribution>
</extension>
<extension
point="org.eclipse.ui.handlers">
<handler
commandId="pgui.rcp.command.save">
<activeWhen>
<with
variable="activePart">
<instanceof
value="pgui.view.LogView">
</instanceof>
</with>
</activeWhen>
</handler>
</extension>
Upvotes: 1
Views: 2840
Reputation: 1023
First, remove the defaultHandler from your command.
Next, add your handler class to your handler extension point instead.
Basically, the mechanism allows you to define multiple handlers for the same command, using different activeWhen expressions to have the command handled by different handler classes in different circumstances.
If all of the activeWhen expressions on all of the defined handlers for a command evaluate to false, and there is a defaultHandler defined for the command itself, then that default handler will be used for the command. The command will, essentially, always be active, since there's always a default handler around to handle it.
For example, if you had both your existing LogView, and another view full of unicorns, and you wanted to use the same pgui.rcp.command.save command to handle the saving of items from either view:
<extension point="org.eclipse.ui.commands">
<command
id="pgui.rcp.command.save"
name="Save">
</command>
</extension>
:
<extension point="org.eclipse.ui.handlers">
<handler
class="pgui.handler.SaveLogHandler"
commandId="pgui.rcp.command.save">
<activeWhen>
<with variable="activePart">
<instanceof value="pgui.view.LogView">
</instanceof>
</with>
</activeWhen>
</handler>
</extension>
:
<extension point="org.eclipse.ui.handlers">
<handler
class="pgui.handler.SaveUnicornHandler"
commandId="pgui.rcp.command.save">
<activeWhen>
<with variable="activePart">
<instanceof value="pgui.view.UnicornView">
</instanceof>
</with>
</activeWhen>
</handler>
</extension>
Upvotes: 3