Search code examples
javaeclipseswteclipse-rcp

SWT - TreeViewer - Single Select vs Multi-Select


Please note Eclipse RCP is used.

I have enabled Multi-select using CTRL+Mouse Click for an existing Tree Viewer. Now I need to provide two menus for Right Click:

 One menu item on Single Selection.
 A different menu item for Multiple Selection.

Currently extension definition through plugin.xml is available for Single Selection where the selected object is checked if it is an instanceof some value.

How to identify multiple select? What needs to be checked for multi-select in extension definition.

   <definition
        id="com.sample.rightclickmenu.singleselect.id.expression">
     <with
           variable="org.eclipse.ui.selection">
        <iterate
              ifEmpty="false"
              operator="and">
           <or>
              <instanceof
                    value="com.sample.ExampleNGroup"> -> Where N=1,2,.. 
              </instanceof>
           </or>
        </iterate>
     </with>
  </definition>

When I multi-select between different N, what should be the extension definition.

   <definition
        id="com.sample.rightclickmenu.multiselect.id.expression">
     <with
           variable="org.eclipse.ui.selection">
        <iterate
              ifEmpty="false"
              operator="and">
           <or>
              <instanceof
                    value=??>  -> what needs to be the value here.
              </instanceof>
           </or>
        </iterate>
     </with>
  </definition>

I hope I have explained the problem properly.


Solution

  • You can use the count element to test the selection size

    Single item selected:

     <with variable="org.eclipse.ui.selection">
        <count value="1" />
        <iterate ifEmpty="false">
              ....
        </iterate>
     </with>
    

    Two or more items selected

     <with variable="org.eclipse.ui.selection">
        <count value="(2-" />
        <iterate ifEmpty="false">
              ....
        </iterate>
     </with>