Search code examples
javaeclipsehandler

How to activate Eclipse UI handler with multiple conditions


I am trying to activate the copy command using the "handler" in the extension point for multi conditions. If I add the condition to work for single view it's working correctly.

 <extension
         point="org.eclipse.ui.handlers">
      <handler class="example.xyz.CopyHandler"
            commandId="org.eclipse.ui.edit.copy">
         <activeWhen
           <with
               variable="activePartId">
        </with>
           <equals
          value="example.xyz.view1">
           </equals>
        </with> 
     </activeWhen>
  </handler>
   </extension>

But when i used with multi conditions like..
Conditions:

  • It should open when the "view1" or "view2" is active.
  • and the cont of the selection should be exactly 1 and the instance of the selection should be example.xyz.ICharacteristicValue.

I tried with this snippet, it's not working. what is wrong in this code?

 <extension
             point="org.eclipse.ui.handlers">
    <handler
            class="example.xyz.CopyHandler"
            commandId="org.eclipse.ui.edit.copy">
         <activeWhen>
                 <with
                     variable="activePartId">
                 <iterate
                       operator="or">
                    <equals
                 value="example.xyz.view1">
                    </equals>
                    <equals
                          value="example.xyz.view2">
                    </equals>
                 </iterate>
               </with>
               <with
                     variable="selection">
                  <count
                        value="1">
                  </count>
                  <iterate>
                     <instanceof
                           value="example.xyz.ICharacteristicValue">
                     </instanceof></iterate>
               </with> 
       </activeWhen>
</handler>
 </extension>

Solution

  • <activeWhen only accepts one child element - you have two. You need to combine them with an <and>:

    <activeWhen>
       <and>
          <with
             variable="activePartId">
           .....
          </with>
          <with
             variable="selection">
           .....
          </with>
      </and>
    </activeWhen>