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:
- Select New > Other > Plug-in Project.
- Give it a name using standard package format e.g., jbs.eclipse.contribution.hello.
- Indicate that the wizard should create a Java project, including a separate source folder with a name such as src.
- Uncheck the option to generate a Java class to control the plugin's life cycle.
- When given the opportunity to base the project on an existing template, uncheck that option (see below).
- Open the project's MANIFEST.MF file under its META-INF folder
- There, select Dependencies tab and add the following Required Plug-ins:
- org.eclipse.core.runtime
- org.eclipse.ui
- org.eclipse.jdt.core
- Select the Extensions tab
- 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.
- Select Add from the All Extensions panel.
- Select the org.eclipse.up.poputMenus extension point and then, in the Available templates area, Popup Menu, as shown below
- Complete the wizard filling in reasonable values for package, class, and menu label values.
- 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>
- Create an Eclipse Test Application
- From the top menu bar, select run . . .
- Select Eclipse Application from the wizard and then new. This is analogous to creating a test server. Give it a name, such as HelloPluginTest.
- 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.
- In the test workbench, create a Java project and within its src folder, a package.
- 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.
- Close the test workbench and then open the ActionHello Java file. Look for the code that generated the message display.
- Replace the "Hello, World!" display code in the Action class's run method with code to determine the object selected.
- 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 ); }
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.
- Create a new package under the project's Java/src folder, such as jbs.eclipse.contribution. hello.plugin
- Add a Java class, such as PluginHello, that extends org.eclipse.core.runtime.Plugin.
- Add a run method.
- Include processing and notification such as the following: