user1808408
user1808408

Reputation:

Trouble deciding how to add popup menu to eclipse plugin using either action ObjectContributions or command handlers

The issue is this: Most things I read on the web says that we should avoid actions when creating popup menus because the command framework allows for more decoupling.

Example: http://wiki.eclipse.org/FAQ_What_is_the_difference_between_a_command_and_an_action%3F

Fair enough.

But I am having a heck of a time getting the command framework to add my menu when I right click on a .java file inside the editor, and only when I click inside the editor. I can get the menu to show (using 'with' and the activeEditor variable), but it also shows when I right click on the java file inside the package explorer, which I don't want. I suspect it's because the file is already open inside the editor.

I also have a menu that gets added when I right click on the java file inside the package explorer, using IComplilationUnit. That works fine.

So my problem is solved using a popup menu action for when I click inside the file. I also get access to to all the ISelection stuff there. But the coupling is to high and I lose the flexability of using handlers.

I'm looking for either: Tell me I'm doing it the only way possible; or Tell me how to have my popup appear only when I right click on the java file editor.

Regards

Upvotes: 1

Views: 1335

Answers (1)

user1808408
user1808408

Reputation:

In the end it was really straight forward. The example below uses the command framework. It doesn't have the handlers, so just click the class hyperlink whenever you need a class generated.

Create a new eclipse plugin project called com.test.plugin.project

In the dependency tab of the plugin.xml file add the following dependencies

  • org.eclipse.jdt
  • org.eclipse.jdt.core
  • org.eclipse.jdt.ui
  • org.eclipse.jface.text

Put this in the plugin.xml tab section:

<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.4"?>
<plugin>
   <extension
         point="org.eclipse.ui.menus">
      <menuContribution
            allPopups="true"
            locationURI="popup:org.eclipse.ui.popup.any">
         <menu
               label="Test Project Sub Menu">
            <command
                  commandId="com.test.plugin.project.command.packageexplorer"
                  id="packageexplorerId"
                  style="push">
               <visibleWhen
                     checkEnabled="false">
                  <iterate
                        ifEmpty="false"
                        operator="or">
                     <instanceof
                           value="org.eclipse.jdt.internal.core.CompilationUnit">
                     </instanceof>
                  </iterate>
               </visibleWhen>
            </command>
            <command
                  commandId="com.test.plugin.project.command.classfile"
                  id="classfileId"
                  style="push">
               <visibleWhen
                     checkEnabled="false">
                  <iterate
                        ifEmpty="false"
                        operator="or">
                     <and>
                        <with
                              variable="selection">
                           <instanceof
                                 value="org.eclipse.jface.text.TextSelection">
                           </instanceof>
                        </with>
                        <with
                              variable="activeEditorId">
                           <equals
                                 value="org.eclipse.jdt.ui.CompilationUnitEditor">
                           </equals>
                        </with>
                     </and>
                  </iterate>
               </visibleWhen>
            </command>
         </menu>
      </menuContribution>
   </extension>
   <extension
         point="org.eclipse.ui.commands">
      <command
            id="com.test.plugin.project.command.packageexplorer"
            name="Only Show In Package Explorer">
      </command>
      <command
            id="com.test.plugin.project.command.classfile"
            name="Only Show In Class File">
      </command>
   </extension>

</plugin>

What this does

  • It creates one popup menu in a sub menu when you right click on a java file inside the package explorer (and only when you click on a java file).
  • It create a different popup menu in a sub menu when you right click on a java file.

Upvotes: 2

Related Questions