Eclipse command framework core expression: enable/visible commands
In the previous post of Eclipse command framework core expression series, I have shown having multiple handler for single command and activating only one of then based on context. In this post you will see how to enable/disable command as well as make it visible/invisible.
Command framework core expression supports enabling/disabling commands using “enableWhen” phrase as well as make it visible/invisible to your RCP application using “visibleWhen”. You can play around with these phrases to enable/disable/visible/invisible commands. Here I am going to use same project from previous example.
Lets try “enableWhen” to enable command for particular editor instance.
<extension
point="org.eclipse.ui.handlers">
<handler
class="com.example.advance.cmd.Cmd1Handler1"
commandId="com.example.advance.cmd.command1">
<enabledWhen>
<with
variable="activeEditor">
<instanceof
value="com.example.advance.cmd.editor.Editor1">
</instanceof>
</with>
</enabledWhen>
</handler>
</extension>
As you see above image ‘CommandOne” command is enable for Editor1 and disabled for Editor2. You can see that user even see the command but can’t select as it is disable. You may want to make it disappear completely when it is disable. This can be achived by “visibleWhen” while contributing command to workbench/menu.
You can choose for be visible only when it is enable, or you can have any complex expression as well.
<extension
point="org.eclipse.ui.menus">
<menuContribution
allPopups="true"
locationURI="popup:#TextEditorContext?after=additions">
<command
commandId="com.example.advance.cmd.command1"
label="Command One"
style="push"
tooltip="Command 1">
<visibleWhen
checkEnabled="true">
</visibleWhen>
</command>
</menuContribution>
</extension>
In above image you can notice “Command One” option is disappeared from editor context menu, because it is not enabled for Editor2 reference.
Comments
Post a Comment