| Actions and command handlers [message #333384] | 
Mon, 08 December 2008 10:18   | 
 
Eclipse User  | 
 | 
 | 
   | 
 
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? 
 
Dirk
 |  
 |  
  | 
| Re: Actions and command handlers [message #333388 is a reply to message #333384] | 
Mon, 08 December 2008 12:47    | 
 
Eclipse User  | 
 | 
 | 
   | 
 
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.eclips e.platform.doc.isv/guide/workbench.htm
 |  
 |  
  | 
| Re: Actions and command handlers [message #333461 is a reply to message #333388] | 
Fri, 12 December 2008 03:40    | 
 
Eclipse User  | 
 | 
 | 
   | 
 
Thanks Paul, 
 
that did the trick! But there is now one problem left. 
Using the org.eclipse.ui.menus extension point I succeded to contribute  
elements to the popupmenu which are directly linked to my commands. 
But I have problems contributing to submenus of the contextmenu. 
 
    <extension 
          point="org.eclipse.ui.menus"> 
       <menuContribution 
             locationURI="popup:my.view?after=startGroup"> 
          <menu 
                id="refactor.submenu" 
                label="Refactor"> 
          </menu> 
       </menuContribution> 
    </extension> 
 
If I define popup elements directly as command subelements of the menu  
definition, they are shown in the submenu. 
But I do not succeed to contribute from other plugins into that submenu. 
Maybe I am using the wrong locationURI? 
I tried 
 
locationURI="popup:my.view?after=refactor.submenu" 
 
I even tried the definition of a separator in the submenu and then used  
the identifier of the separator in the locationURI 
 
locationURI="popup:my.view?after=refactor.mySeparator" 
 
Any ideas? 
 
Dirk 
 
 
 
> 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 
>
 |  
 |  
  | 
 | 
Powered by 
FUDForum. Page generated in 0.02729 seconds