Question

This is kind of newbie question. All tutorials show a way to add a command to a iFile object class. Can you show me an example of a plugin.xml file that registers a command for a function or class?

What I want to achieve is to right-click on a class or a function name in Outline view or in the code itself and have my new command in the context menu.

Was it helpful?

Solution

The key is to use visibleWhen part properly. This is an example of a command showing in Project Explorer which is visible only when a Java method or class is selected:

  <menuContribution
        locationURI="popup:org.eclipse.ui.navigator.ProjectExplorer#PopupMenu">
     <command
           commandId="__your.command.id__"
           id="your.contribution.id"
           label="Some Label"
           style="push">
        <visibleWhen
              checkEnabled="false">
           <iterate
                 ifEmpty="false"
                 operator="and">
              <or>
                 <instanceof
                       value="org.eclipse.jdt.core.IType">
                 </instanceof>
                 <instanceof
                       value="org.eclipse.jdt.core.IMethod">
                 </instanceof>
              </or>
           </iterate>
        </visibleWhen>
     </command>
  </menuContribution>

Don't forget to set commandId to something real.

You can find more info about property and selection testing here.

OTHER TIPS

Based on the examples here you need to change objectClass to IMethod for method or IType for class.

P.S:

In order to see what kind of object is represented you can use the Plugin SelectionSpy Menu. Select an object and click CtrlShift+F1.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top