Eclipse command framework core expression : Multiple handlers
Eclipse platform command framework supports core expression to control commands behavior runtime. In this series of posts I have explained different ways of using core expressions . This post explains having multiple handler for same command but activate only one of it at runtime based on some of the condition.
Eclipse command framework is based on MVC framework. Command declaration, handler declaration and contributing command to workbench helps to keep declaration separate from presentation. You can have multiple handlers for same command, but only one will be active at one moment.
Core expression provides “activeWhen” declarative clause to activate specific handler for given command based on specific condition. Here is how we use it . I have created a new eclipse plug-in project. I have extended “org.eclipse.ui.editors” extension point to define two simple text editors. Here I am going to define two different handlers for same commands but only one will be active based on editor instance.
1. Extend “org.eclipse.ui.commands” extension point to declare command
2. Extends “org.eclipse.ui.handlers” extension point to define handles for command
I have define two handlers for same command.
3. Use “activeWhen” clause, here I have used “instance of” expression to decide which handler will be active based on editor instance.
4. Define “org.eclipse.ui.menu” extension point to contribute command to text editor context menu.
The final plugin.xml will be
<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.4"?>
<plugin>
<extension
point="org.eclipse.ui.commands">
<command
id="com.example.advance.cmd.command1"
name="Command 1">
</command>
</extension>
<extension
point="org.eclipse.ui.editors">
<editor
class="com.example.advance.cmd.editor.Editor1"
default="true"
extensions=".txt"
id="com.example.advance.cmd.editor1"
name="Editor 1">
</editor>
<editor
class="com.example.advance.cmd.editor.Editor2"
default="false"
id="com.example.advance.cmd.editor2"
name="Editor 2">
</editor>
</extension>
<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">
</command>
</menuContribution>
</extension>
<extension
point="org.eclipse.ui.handlers">
<handler
class="com.example.advance.cmd.Cmd1Handler1"
commandId="com.example.advance.cmd.command1">
<activeWhen>
<with
variable="activeEditor">
<instanceof
value="com.example.advance.cmd.editor.Editor1">
</instanceof>
</with>
</activeWhen>
</handler>
<handler
class="com.example.advance.cmd.Cmd1Handler2"
commandId="com.example.advance.cmd.command1">
<activeWhen>
<with
variable="activeEditor">
<instanceof
value="com.example.advance.cmd.editor.Editor2">
</instanceof>
</with>
</activeWhen>
</handler>
</extension>
</plugin>
Things to be noticed here is
<handler
class="com.example.advance.cmd.Cmd1Handler2"
commandId="com.example.advance.cmd.command1">
<activeWhen>
<with
variable="activeEditor">
<instanceof
value="com.example.advance.cmd.editor.Editor2">
</instanceof>
</with>
</activeWhen> </handler>
This handler will be active only when Editor2 instance’s context menu. I have opened file.txt in two different editor “Editor1” & “Editor2”
As shown in above image when clicking on “Command One” option, you will see “Inside Cm1Handler1.execute()” message. It means Handler1 is active for Editor1 context menu (notice Editor name in file title “Editor1:file1.txt”)
Now I have opened same file in different editor “Editor2” and selected same command “Cammand One” from context menu. As shown in above image you will notice Handler2 has been invoked
Here is the complete source code.
Very good and nice spelling in the article that may reach every one reading. Better if with the levels may be a statistical images. All the way very good .
ReplyDeleteSTC Technologies