简体   繁体   中英

Add popup action for Java projects in an Eclipse plugin

I want to add popup item for Java projects in my Eclipse plugin. My plugin.xml contains:

<extension point="org.eclipse.ui.popupMenus">
  <objectContribution
        objectClass="org.eclipse.jdt.core.IJavaProject"
        id="com.contribution1">
     <action
           label="Action"
           class="com.actions.NewAction"
           enablesFor="1"
           id="com.actions.newAction">
     </action>
  </objectContribution>

However, the menu item is not shown in the popup. If I use IPackageFragment instead, the menu item shows just fine. Please advise.

Ok, the problem was that a Java project is considered an IJavaProject in the Java perspective only and the Eclipse instance used to test the plugin starts with the Resources perspective. If I switch to the Java perspective everything works as expected.

If you want to display your menu in another explorer (for instance in the Resources perspective's Project explorer), you'll have to change your objectContribution definition as follows:

  1. Set the objectclass property to org.eclipse.core.resources.IProject .

  2. Add a visibility element to filter your menu/actions for projects that DO NOT have the Java nature .

Ie:

<objectContribution
      adaptable="true"
      objectClass="org.eclipse.core.resources.IProject">
...
   <visibility>
      <objectState
            name="projectNature"
            value="org.eclipse.jdt.core.javanature">
      </objectState>
   </visibility>
</objectContribution>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM