Search code examples
eclipsepopupe4

Eclipse 4.x popup menu visibility based on tree viewer selection


I'm creating an eclipse 4 plugin project. I have a tree viewer that shows model elements.

Problem: I need to show a popup menu based on the selection that I'm doing on the tree viewer. I tired to use core expression as explained in the link:

http://www.vogella.com/tutorials/EclipseRCP/article.html#menuadvanced_popup

But whenever I attach the core expression with my popup menu it disappears.

The popup menu appears for all the elements if i don't attach the core expression.

Is there something else that i'm supposed to do to get things right ? or Should i use a different approach ?

Please find the below snap of my Application.e4xmi file

enter image description here

My plugin.xml file definition for the core expression

      <definition
            id="xxx.abc.project.addchilddefinition">
        <with variable="org.eclipse.ui.selection">
         <iterate
               ifEmpty="false"
               operator="or">
            <instanceof
                  value="xxx.abc.project.model.ObjectName">
            </instanceof>
         </iterate>
       </with>
      </definition>

I've registered my popup menu using the below code snippet:

menuService.registerContextMenu(treeviewer.getControl(), "xxx.abc.project.popupmenu.addchild");

menuService is the EMenuService object and "xxx.abc.project.popupmenu.addchild" is my popupmenu id


Solution

  • You need to use a with element to specify that the selection should be used:

    <definition
        id="xxx.abc.project.addchilddefinition">
      <with
         variable="org.eclipse.ui.selection">
        <iterate
             ifEmpty="false"
             operator="or">
          <instanceof
                value="xxx.abc.project.model.ObjectName">
          </instanceof>
       </iterate>
    </with>
    

    You must also call the ESelectionService setSelection(Object) method when the tree selection changes.

    The selection variable org.eclipse.ui.selection is defined in IServiceConstants.ACTIVE_SELECTION