I have several plugins where commands need to be disabled when the user isn't logged in. This is done via an ISourceProvider. In one plugin.xml I have:
<extension
point="org.eclipse.ui.menus">
<menuContribution
allPopups="false"
locationURI="menu:org.eclipse.ui.main.menu">
<menu
id="menus.arm"
label="%menu.label"
mnemonic="%menu.mnemonic">
<command
commandId="ru.focusmedia.odp.arm.commands.login"
style="push">
<visibleWhen
checkEnabled="false">
<with
variable="arm.variables.loggedIn">
<equals
value="false">
</equals>
</with>
</visibleWhen>
</command>
<command
commandId="ru.focusmedia.odp.arm.commands.logout"
style="push">
<visibleWhen
checkEnabled="false">
<with
variable="arm.variables.loggedIn">
<equals
value="true">
</equals>
</with>
</visibleWhen>
</command>
<command
commandId="org.eclipse.ui.file.exit"
label="%command.label"
style="push"
tooltip="%command.tooltip">
</command>
</menu>
<menu
id="menus.help"
label="%menu.label.0"
mnemonic="%menu.mnemonic.0">
<command
commandId="org.eclipse.ui.help.aboutAction"
label="%command.label.0"
style="push"
tooltip="%command.tooltip.0">
</command>
</menu>
</menuContribution>
</extension>
<extension
point="org.eclipse.ui.commands">
<command
defaultHandler="ru.focusmedia.odp.arm.login.LogoutHandler"
id="ru.focusmedia.odp.arm.commands.logout"
name="%command.name.logout">
</command>
<command
defaultHandler="ru.focusmedia.odp.arm.login.LoginHandler"
id="ru.focusmedia.odp.arm.commands.login"
name="%command.name.login">
</command>
</extension>
<extension
point="org.eclipse.ui.services">
<sourceProvider
provider="ru.focusmedia.odp.arm.login.LoginStateSourceProvider">
<variable
name="arm.variables.loggedIn"
priorityLevel="workbench">
</variable>
</sourceProvider>
</extension>
This works fine; the correct command is visible all the time. In another plugin, which depends on this one, I have
<extension
point="org.eclipse.ui.commands">
<command
defaultHandler="ru.focusmedia.odp.arm.alarms.RequestMoreAlarmsHandler"
id="arm.alarms.commands.request_more_alarms"
name="Request more alarms">
</command>
</extension>
<extension
point="org.eclipse.ui.handlers">
<handler
class="ru.focusmedia.odp.arm.alarms.RequestMoreAlarmsHandler"
commandId="arm.alarms.commands.request_more_alarms">
<enabledWhen>
<with
variable="arm.variables.loggedIn">
<equals
value="true">
</equals>
</with>
</enabledWhen>
</handler>
</extension>
<extension
point="org.eclipse.ui.menus">
<menuContribution
all开发者_如何学PythonPopups="false"
locationURI="toolbar:arm.views.alarms">
<command
commandId="arm.alarms.commands.request_more_alarms"
style="push">
</command>
</menuContribution>
</extension>
And this command is disabled all the time. What's going wrong?
The extension point stuff looks fine.
The obvious question is then: how is the handler implemented? I have seen numerous cases - especially during Eclipse plug-in training, but also outside this - where the handler does not sub-class org.eclipse.core.commands.AbstractHandler
but is implemented bottom-up... The problematic method is isEnabled()
which by default returns false
- which of cause means the handler is never enabled. The inherited method from AbstractHandler
does the right thing...
There is a conflict between handler definitions in <command defaultHandler=...
and <handler ...
. After removing the defaultHandler
attribute, it works fine.
精彩评论