View RSS Feed

Eclipse

Start writing a plug-in for Eclipse

Rate this Entry
by , 02-06-2012 at 05:13 AM (2488 Views)
The Eclipse IDE has an open architecture which allows third party developers to extend or add new functionalities to the Eclipse’s core platform. This architecture follows the OSGi framework which is a specification for application and component life cycle management.

This article will guide you to develop a sample plug-in from creating fundamental blocks to testing and installing as a feature to the IDE. The version of Eclipse targeted by this article is the 3.7 (Indigo).

Before getting started, let’s get familiar with some fundamental concepts regarding the plug-in development in Eclipse.


Fundamental Concepts about plug-ins for Eclipse

  • PDE: stands for Plug-in Development Environment, PDE provides necessary tools to help developers creating, developing, debugging, testing and deploying components for Eclipse IDE such as plug-ins, features, update sites… The PDE provided by Eclipse is very powerful, flexible and easy to use.
  • Plug-in: A plug-in is a smallest unit that represents a group of code which is modular, extendable and shareable. Plug-ins are the basic parts that make up of an Eclipse based program or product. Think a plug-in as a modular part of a program.
  • Extensions and Extension points: An extension is a way for a plug-in to extend a functionality of another plug-in through its extension point. An extension point is declared by a plug-in to allow another plug-in extends its functionality. Think extensions as interfaces that allow plug-ins to work together.
  • Feature: A feature is used to group some plug-ins together into a single unit which is installable and updatable. A product or program can contain several features.
  • Update Site: An update site is used to organize features so that they can be installed into Eclipse IDE. Update sites can be on a local computer on a remote server and it is the widely used way to deliver Eclipse based products.



There are other terms such as fragment, product, and target platform but for the purpose of this article, the terms described above is enough for you to start. For other terms, refer to Eclipse’s documentation.


Activate the PDE

The PDE is provided as a perspective in the IDE. Select Window -> Open Perspective -> Other… from Eclipse’s main menu, and choose Plug-in Development from the Open Perspective dialog:

Name:  open perspective.PNG
Views: 460
Size:  11.3 KB
Figure: Open the Plug-in Development perspective

The Plug-in Development perspective has the following views:

Name:  views.PNG
Views: 455
Size:  10.9 KB
Figure: The views of Plug-in Development perspective

The New menu is also customized for creating plug-in related items:

Name:  new menu.PNG
Views: 471
Size:  24.6 KB
Figure: New menu is customized for PDE

The following screenshot illustrates the Plug-in Development perspective for a plug-in project:

Name:  pde.PNG
Views: 475
Size:  142.9 KB
Figure: The Plug-in Development perspective


Creating a Eclipse plug-in project

We are going to develop a plug-in project that adds a custom menu to the main menu.

Select File -> New -> Plug-in Project from the main menu. In the Plug-in Project screen, type MyPlugin as the Project name. In the Target Platform section, select Eclipse version and select 3.6 from the dropdown list. The plug-in we are going to create will be run on Eclipse 3.6 or later:

Name:  Plugin project 1.PNG
Views: 467
Size:  25.8 KB
Figure: Create a new Plug-in project

Click Next. In the Content screen:

  • Keep 1.0.0 as Version (delete the word qualifier).
  • Type My Company as Provider.
  • In the dropdown list Execution Environment, select the JavaSE version available on your computer.


Name:  Plugin project 2.PNG
Views: 473
Size:  24.5 KB
Figure: Specify content for new plug-in project

Leave others as default, and click Next. In the Templates screen, uncheck the option Create a plug-in using one of the templates, as we will add an extension later.

Click Finish, Eclipse creates some skeleton stuff for the project, note the Activator.java class is created under src -> myplugin directory. This class is to control the plug-in life cycle:

Name:  the plugin project.PNG
Views: 477
Size:  83.1 KB
Figure: The generated Activator class

Eclipse created the necessary skeleton code, and normally we don’t need to modify this class.
And note the new editor page:

Name:  plugin editor.PNG
Views: 604
Size:  50.8 KB
Figure: The plug-in editor

This editor allows developers to control everything about a plug-in which is separated into individual pages such as Overview, Dependencies, Runtime, Extensions…This makes configuring and managing the plug-in easily.

Creating an extension for custom menu

Now let’s add something interesting to the plug-in. Select page Extensions in the MyPlugin tab, and click on the Add button:

Name:  add extension 1.PNG
Views: 458
Size:  15.6 KB
Figure: Add an extension

In the New Extension dialog, select the tab Extension Wizards, and choose the “Hello, World” action set on the right:

Name:  new extension 1.PNG
Views: 451
Size:  29.7 KB
Figure: Select an extension wizard

Click Next, in the next screen, type the following information:

  • MyAction for Action Class Name.
  • Greetings, thank you for using my plugin for Message Box Text.


Name:  new extension 2.PNG
Views: 445
Size:  16.0 KB
Figure: Specify information for the action

Click Finish. Eclipse has created two things:

  • The plugin.xml file: holds configuration for the plug-in.
  • The MyAction.java class: implements the behavior when the menu is clicked. This class implements the IWorkbenchWindowActionDelegate interface and overrides some methods. The code to display the message is in the run() method.


Select the Sample Menu (menu) under the Sample Action Set (actionSet) in the All Extensions section.

In the section Extension Element Details, type myMenu for id, and My Menu for label, like the following screenshot:

Name:  edit sample menu.PNG
Views: 449
Size:  21.4 KB
Figure: Configure menu

Select the Sample Action (action), type myAction for id and My Action for label. Press Ctrl + S to save the changes to the project.


Testing the plug-in in Eclipse

The PDE allows developers to test their plug-in project with a single mouse click. Select page Overview, click Launch Eclipse application under the Testing section:

Name:  testing.PNG
Views: 467
Size:  47.1 KB
Figure: Click Launch an Eclipse application

Eclipse will launch a separate instance for testing. You will see a custom menu My Menu is added to the main menu, with a menu item My Action:

Name:  test menu.PNG
Views: 443
Size:  30.2 KB
Figure: Test the action of the plug-in

Select My Menu -> My Action, a message box appears:

Name:  message box.PNG
Views: 413
Size:  6.7 KB
Figure: The message dialog

It is great, right? So far you don’t have to write any code at all. Eclipse automatically generates almost the code for you. Have a look at the plugin.xml file to see how Eclipse generates the XML declaration for the plug-in:

Name:  plugin xml.PNG
Views: 474
Size:  25.5 KB
Figure: Content of the plugin.xml

And if you want to modify some code, look at the generated classes MyAction.java and Activator.java.


Creating a feature project

Till now we have a plug-in project that works. Next, we need to create a feature project that covers the plug-in and turns it into an installable unit.
Select File -> New -> Feature Project from the main menu. In the New Feature dialog:

  • Type MyFeature as Project name.
  • Keep 1.0.0 as Feature Version (delete the word qualifier).
  • Type My Company as Feature Provider.


Name:  new feature 1.PNG
Views: 440
Size:  21.6 KB
Figure: Create a Feature project

Click Next. In the next screen, select option Initialize from the plug-ins list, then select the item MyPlugin:

Name:  new feature 2.PNG
Views: 440
Size:  27.6 KB
Figure: Add a plug-in to the feature

Click Finish, Eclipse creates the MyFeature project:

Name:  my feature project.PNG
Views: 445
Size:  63.4 KB
Figure: Feature editor

There are only two files created for this project: build.properties and feature.xml. The feature.xml file contains various definitions for the feature such as the plug-in it wraps, copyright and license information.

Select the feature.xml page, type some information for the description, copyright and license, as in the screenshot below:

Name:  feature xml.PNG
Views: 452
Size:  23.3 KB
Figure: Content of feature.xml

Press Ctrl + S to save the project.

The Plug-ins page allows managing which plug-ins are included in the feature:

Name:  feature plugins.PNG
Views: 442
Size:  28.6 KB
Figure: Plug-ins added to the feature

Here we see the MyPlugin is included because we selected it previously in the New Feature wizards.


Creating an update site project

This is the last step to create a deployable and installable unit. In the editor for MyFeature, select Overview page, then click Create an Update Site Project, in the Publishing section:

Name:  create update site project.PNG
Views: 447
Size:  41.9 KB
Figure: Click Create an Update Site Project

The New Update Site dialog appears, type MyUpdateSite as Project name, then click Finish:

Name:  new update site.PNG
Views: 452
Size:  18.4 KB
Figure: Create an Update Site project

The project MyUpdateSite is created with a site.xml file and an editor Update Site Map:

Name:  update site project.PNG
Views: 454
Size:  18.6 KB
Figure: Update Site Map editor

Click Add Feature, the Feature Selection dialog appears, select MyFeature in the drop-down list, and click OK:

Name:  feature selection.PNG
Views: 447
Size:  15.8 KB
Figure: Add a feature to the site

The feature MyFeature is added under the section Managing the Site. Click Build All button to build the projects MyFeature and MyPlugin. Some artifacts are created under MyUpdateSite project, as shown in the following screenshot:

Name:  after build.PNG
Views: 444
Size:  22.5 KB
Figure: Artifacts created after build

The JAR files created, along with the site.xml file, are ready for installing.


Install the feature to Eclipse

Now close all the projects, exit the IDE and re-launch Eclipse. We are going to install the plug-in created as a feature.

Select Help -> Install New Software from the main menu. In the Install dialog, click Add button. In the Add Repository dialog, type My Company into the Name field and click Local button. Select the directory of the MyUpdateSite project, and click OK:

Name:  install feature.PNG
Views: 444
Size:  39.7 KB
Figure: Install feature from local site

Click OK in the Add Repository dialog. Click Select All in the Install dialog to choose the feature to be installed, and click Next.

In the next screen, Install Details, click Next. In the Review Licenses screen, select I accept the terms of the license agreement, and click Finish. Eclipse is now installing the software:

Name:  install software.PNG
Views: 436
Size:  9.9 KB
Figure: The feature is being installed

Eclipse may display a security warning dialog, click OK. Finally Eclipse asks to restart or apply change, click Apply Changes Now.

Immediately a new menu is added to the Eclipse’s main menu, My Menu. Select My Menu -> My Action, a message dialog appears:

Name:  run plugin.PNG
Views: 458
Size:  41.8 KB
Figure: Running the installed feature

So that our plug-in is installed and working properly!

We installed the plug-in from a local site. You can also copy the artifacts to a remote server and install it via an Update Site URL.

Uninstall the feature from Eclipse

To uninstall the plug-in, select Help -> About Eclipse. In the About Eclipse dialog, click Installation Details. In the Eclipse Installation Details dialog, select MyFeature from the list and click Uninstall:

Name:  uninstall.PNG
Views: 437
Size:  15.2 KB
Figure: Uninstall the feature

In the Uninstall Details dialog, click Finish. Eclipse is now uninstalling the software:

Name:  uninstall progres.PNG
Views: 434
Size:  10.1 KB
Figure: The feature is being uninstalled

Click Apply Changes Now when Eclipse asks, you will see the My Menu is immediately removed from the main menu. Check the Installation Details to see the MyFeature is no longer there. The plug-in is completely uninstalled.

Submit "Start writing a plug-in for Eclipse" to Facebook Submit "Start writing a plug-in for Eclipse" to Digg Submit "Start writing a plug-in for Eclipse" to del.icio.us Submit "Start writing a plug-in for Eclipse" to StumbleUpon Submit "Start writing a plug-in for Eclipse" to Google

Categories
Plug-ins

Comments