Freitag, 28. Mai 2010

Chained Commands Execution

Chained Commands Execution

What are commands?

The eclipse command API [1] allows for declaratively contributing UI parts (menu/toolbar items, window controls, ...) to the UI anywhere in your workbench window.
In general, this requires to
  1. create a command definition
  2. create a handler or handlers (which are conditionally enabled upon a specific expression)
  3. define a menu contribution to be shown in the UI which the user can use to trigger the execution of the command

As the command API does exist since eclipse 3.3, there are plenty of (re-)usable commands:



What are chained commands?

As you might have noticed, the core API just allows to call one specific command per menu contribution (menu/toolbar item, ...).
Often, there is a need to execute several commands by just one UI button. To allow this, you have to define a composite command that is manually written to call command X, Y, and Z.
If you want to execute command A, B, and C in a sequence, you have to provide another composite command.
Things will become more complicated if those wrapped commands require parameters to be passed to them.

To fix this, we just define a chain of commands.


A chain of commands consists of an arbitrary number of commands tied together to be executed (conditionally) in a sequence.

Example: Imagine you have a web application based on RAP [2]. Once the user accesses the web application, he is given a login form where he has to provide his login data. Once he presses the login button, he is redirected to a home perspective. If the login fails, an error message will occur.

The plugin.xml may contain this extension:

<extension

         point="org.eclipse.ui.menus">

      <menuContribution

            allPopups="false"

            locationURI="chain:chains.demo.login">

         <command

               commandId="myapp.login"

               id="command.login"

               style="push">

         </command>

         <command

               commandId="ui.showMessage"

               style="push">

            <parameter

                  name="type"

                  value="error">

            </parameter>

            <parameter

                  name="message"

                  value="Login denied using: user={login.user}">

            </parameter>

            <visibleWhen

                  checkEnabled="false">

               <with

                     variable="previous">

                  <equals

                        value="false">

                  </equals>

               </with>

            </visibleWhen>

         </command>

         <command

               commandId="org.eclipse.ui.perspectives.showPerspective"

               style="push">

            <parameter

                  name="org.eclipse.ui.perspectives.showPerspective.perspectiveId"

                  value="myapp.myHomePerspective">

            </parameter>

            <visibleWhen

                  checkEnabled="false">

               <with

                     variable="command.login">

                  <equals

                        value="true">

                  </equals>

               </with>

            </visibleWhen>

         </command>

      </menuContribution>

   </extension>


This is the menu contribution extension point.
Our contributions will not go into a menu/toolbar/..., instead they will be added to a chain (locationURI="chain:chains.demo.login" where the chain's id is chains.demo.login).
Next, we define three commands to be executed by the chain:

1. the myapp.login command will authenticate the user
2. the ui.showMessage will show an error message if the user could not be authenticated
    - this will only happen if the previous command has been executed successfully, you can check this by comparing the "previous" variable's boolean value
3. the last command will change the active perspective, if the first command succeeded

Next, we have to define the chain execution menu item:

<extension

         point="org.eclipse.ui.menus">

      <menuContribution

            allPopups="false"

            locationURI="toolbar:org.eclipse.ui.main.toolbar">

         <command

               commandId="platform.executeChain"

               style="push">

            <parameter

                  name="chainID"

                  value="chains.demo.login">

            </parameter>

         </command>

      </menuContribution>

   </extension>


This command menu item will execute the chain defined by the id "platform.executeChain". That is our composite command that handles chain executions.

Source

The source of the chain command is available on google code at

https://erdalkaraca.googlecode.com/svn/trunk/org.eclipselabs.chains

Use a subversion client to checkout.

Links

[1] http://wiki.eclipse.org/index.php/Platform_Command_Framework
[2] http://eclipse.org/rap/