Hello, World!
Plugin
Triggered by Popup Menu

 

The goal of this lesson is to begin building an understanding of the Eclipse architecture with respect to extensions and extension points.  More specifically, it will look at the use of extension points and extensions as they may be used to link a user's action with an action class that, in turn, invokes an Eclipse plugin.  This sequence appears to be a fundamental construct within the Eclipse architecture since plugins are the basic building blocks of Eclipse systems and since they provide the context within Eclipse for more complex computing, such as JET applications, analogous to a Java applications with a main method.

The specific task that will serve as an illustration is :

The strategy that will be followed to accomplish this task includes the following steps:

  1. Create a blank plugin project
  2. Create popup menu item and action class tht will be notified
  3. Create a test workbench and text popup menu with it
  4. Revise the action class to pass control and parameters to a plugin class
  5. Create the plugin class that will be called by the action class
     


Create a Blank Plugin Project

  1. Select New > Other > Plug-in Project.
  2. Give it a name using standard package format e.g., jbs.eclipse.contribution.hello.
  3. Indicate that the wizard should create a Java project, including a separate source folder with a name such as src.
  4. Uncheck the option to generate a Java class to control the plugin's life cycle.

  5. When given the opportunity to base the project on an existing template, uncheck that option (see below).

Create Popup Menu and an Action Class

  1. Open the project's MANIFEST.MF file under its META-INF folder
  2. There, select Dependencies tab and add the following Required Plug-ins:
    • org.eclipse.core.runtime
    • org.eclipse.ui
    • org.eclipse.jdt.core
  3. Select the Extensions tab
  4. The goal here is to select an extension point for the popup menu and create the support structure for it in a plugin.xml file that will be created for you.
  5. Select Add from the All Extensions panel. 
  6. Select the org.eclipse.up.poputMenus extension point and then, in the Available templates area, Popup Menu, as shown below


     
  7. Complete the wizard filling in reasonable values for package, class, and menu label values.
  8. Clean up the plugin.xml definition by selecting that tag and manually editing the xml.  Get rid of more detailed components that are not needed for a Hello, World menu, thereby reducing it to something that approximates that shown below.  Also, change the objectClass, as shown below, so that the will be shown when you right-click on a package name.  As you make these changes, move back and forth from the Extensions tab to the plugin.xml tab and note how changes in the plugin.xml file result in changes in the extension properties view.
  
  <plugin>

   <extension point="org.eclipse.ui.popupMenus">
      <objectContribution
            id="jbs.eclipse.contribution.hello.action.ActionHello"
            nameFilter="plugin.xml"
            objectClass="org.eclipse.jdt.core.IPackageFragment">
         <action
               class="jbs.eclipse.contribution.hello.action.ActionHello"
               enablesFor="1"
               id="jbs.eclipse.contribution.hello.action.ActionHello"
               label="Popup Action"
               />
      </objectContribution>

   </extension>

  </plugin>

  

Test the Popup menu

  1. Create an Eclipse Test Application
  2. From the top menu bar, select run . . .
  3. Select Eclipse Application from the wizard and then new.  This is analogous to creating a test server.  Give it a name, such as HelloPluginTest.
  4. Select the hello plugin project, right click, and then the Run as option.  Select the newly created Eclipse test application.  This will bring up another instance of the Eclipse workbench.
  5. In the test workbench, create a Java project and within its src folder, a package.
  6. Right click on the new test package and select your Hello Plugin popup menu item from the larger popup.  You should see a message display come up.
  7. Close the test workbench and then open the ActionHello Java file.  Look for the code that generated the message display.
     

Add logic to Action class to process selected object

  1. Replace the "Hello, World!" display code in the Action class's run method with code to determine the object selected.
  2. Call the Plugin class's run method (created in the next step) and pass it the object selected (for eventual processing of that object), as illustrated in the following example code:
    public void run(IAction action) {
    		
    	//  test for Structured object selected
    	if (! (selection instanceof IStructuredSelection))  {
    		System.out.println("NOT a structured selection");
    		return;
    	}
    		
    	IStructuredSelection structuredSelection = (IStructuredSelection) selection;		
    	IPackageFragment packageSelection = (IPackageFragment) structuredSelection.getFirstElement();
    		
    	PluginHello plugin = new PluginHello();
    	plugin.run( packageSelection );
    		
    }
    

 

Create Plugin Class and add notification mechanism

Since many tools, such as JET, require a plugin context for execution, the goal for this step is to create a plugin class that will be called from the action class when the popup menu is selected.

  1. Create a new package under the project's Java/src folder, such as jbs.eclipse.contribution. hello.plugin
  2. Add a Java class, such as PluginHello, that extends org.eclipse.core.runtime.Plugin.
  3. Add a run method.
  4. Include processing and notification such as the following: