[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [Newsgroup Home]
[news.eclipse.platform] Re: Actions and command handlers

Dirk Wenke wrote:
Hi,

I encoutered a problem while working with actions and commands/handlers associated with it in Eclipse 3.4.
In my application I have a TreeViewer with a context menu. Depending on the selected object in the tree, I have different "new" actions to create a new element of the given type.
I tried now to add the key accelerator "CTRL+N" to all these new actions (at any time, only one action can be associated with this key binding), by defining a command with a keybinding:


   <extension
         point="org.eclipse.ui.commands">
      <command
            categoryId="myCategory"
            description="Create new element"
            id="first.test.newCommand"
            name="Create element">
      </command>
   </extension>
   <extension
         point="org.eclipse.ui.bindings">
      <key
            commandId="first.test.newCommand"
            contextId="contextOfMyView"
            schemeId="org.eclipse.ui.defaultAcceleratorConfiguration"
            sequence="CTRL+N">
      </key>
   </extension>

and the following handlers:

   <extension
         point="org.eclipse.ui.handlers">
      <handler
            class="NewElementAHandler"
            commandId="first.test.newCommand">
         <activeWhen>
            <iterate>
               <instanceof value="ElementA">
               </instanceof>
            </iterate>
         </activeWhen>
      </handler>

      <handler
            class="NewElementBHandler"
            commandId="first.test.newCommand">
         <activeWhen>
            <iterate>
               <instanceof value="ElementB">
               </instanceof>
            </iterate>
         </activeWhen>
      </handler>
   </extension>

The actions in the context menu are objectContributions:

   <extension
       point="org.eclipse.ui.popupMenus">
      <objectContribution
          objectClass="ElementA"
          id="ElementA_Actions">
         <action
             enablesFor="1"
             label="New Element A"
             class="NewElementAAction"
             menubarPath="navigator.new"
             definitionId="first.test.newCommand"
         </action>
      </objectContribution>
      <objectContribution
          objectClass="ElementB"
          id="ElementB_Actions">
         <action
             enablesFor="1"
             label="New Element B"
             class="NewElementBAction"
             menubarPath="navigator.new"
             definitionId="first.test.newCommand"
         </action>
      </objectContribution>
   </extension>

So far, everything works fine. If an element of type ElementA is selected, the accelerator key invokes NewElementAHandler and the same for elements of type ElementB. But in the logfile I find many log entries that inform about conflicting handlers.
It seems to me, that eclipse itself generates Handlers to wrap actions with a given definitionId. And these generated handlers seem to cause these conflicts.
Is there any way to solve this problem?

Don't mix commands and actions. If you have already created commands and handlers, simply use org.eclipse.ui.menus to place your command in the appropriate popup menu.


Then executing the menu item will execute the command, which will point to the appropriate handler.

PW

--
Paul Webster
http://wiki.eclipse.org/Platform_Command_Framework
http://wiki.eclipse.org/Command_Core_Expressions
http://wiki.eclipse.org/Menu_Contributions
http://wiki.eclipse.org/Menus_Extension_Mapping
http://help.eclipse.org/ganymede/index.jsp?topic=/org.eclipse.platform.doc.isv/guide/workbench.htm