Chapter 8. Eclipse Development

In the previous chapters of this book, we have seen how to use Eclipse in various ways, such as developing code for Java, managing projects with Git, and developing web applications. In this chapter, we'll switch the focus and learn how we can use Eclipse to develop code for Eclipse. Since Eclipse is developed in Java, good knowledge of the language is a requisite for this chapter.

One of the Eclipse's features that allowed it to become one of the most popular IDEs around is its pluggable architecture. It contains a runtime platform, which provides the necessary infrastructure for the IDE to run, such as the primary UI elements, a registry of the loaded plugins, a logging infrastructure, among others, and basically everything else is plugins. You can check that by taking a look at the Eclipse's folder in your machine. Inside the plugins folder there's a number of JAR files and folders, each one relating to a plugin. Plugins can be packed either in a simple folder or in a JAR file (which is simply a renamed zip file). Each of these plugins contribute to some aspect of Eclipse. The org.eclipse.jdt.* ones, for example, contribute with the Java development tools, such as the Java debugging, the Java editor, and the Java project type.

In Eclipse's home folder, you will also find a folder called features. A feature is basically a set of plugins. You can use features to group plugins that only make sense when they are bundled together. Take a look at the feature.xml file inside the features/org.eclipse.jdt_<version> folder. It contains a list of all the plugins this feature contains.

Features and plugins can have dependencies between them. The org.eclipse.pde feature, for example, which provides the tools for plugin development, depends on the org.eclipse.jdt feature, since all Eclipse plugins are written in Java language.

You can see a list of all features and plugins installed in your Eclipse instance by navigating to Help | About Eclipse SDK, and clicking on Installation Details in the window that appears. The Installed Software tab lists all the feature groups installed, and the Installation History tab displays when each additional plugin or feature was installed. The Features and Plug-ins tabs contain information about the plugins and features provider, version, and ID. You can also check under which license the plugins have been released by clicking on the Legal Info button, or the License button in the Features tab.

By the end of this chapter, you will be able to do the following:

  • Create a new plugin project
  • Run and debug a plugin project
  • Declare and implement extension points
  • Create custom entries for Eclipse toolbars and menus
  • Use the Plugin Spy feature
  • Create new Eclipse views
  • Export your plugins

Creating your first plugin

Since most of the features that Eclipse contains are provided by plugins, a good way of starting to study Eclipse development is to learn how to create your own plugin. The best way of doing so is by using the Eclipse's new plugin project wizard. Select File | New | Other to see a list of wizards. Select the Plug-in Project option and click on Next. The first window of the wizard contains basic project configuration. The default options are just fine for us. Pick a nice name for your project, such as "HelloWorldPlugin", and click on Next.

The next page contains more plugin properties. Again, default options will do, so just click on Next. The following page allows you to build your plugin from a template instead of starting it from scratch. Let's select the Hello, World template, which creates the plugin's basic file structure and adds a button to the toolbar that displays Hello, Eclipse World when pressed. Click on Finish. You will be asked if you want to open the Plugin Development perspective. This perspective is very similar to the Java one, with some views specific to plugin development, such as the plug-ins' view.

Before we try running this project, let's take a look at the file structure that was generated. The following screenshot displays the Package Explorer view of the HelloWorldPlugin project:

Creating your first plugin

The MANIFEST.MF file contains basic information about the plugin. It contains, among others, the plugin's name and version, and information regarding what the plugin provides and on what it depends. This file's format and what it provides are determined by the OSGI specification.

The plugin.xml file is another important file in the plugin's basic file structure. It contains information about which extension points the plugin provides and which ones it implements. Extension points are, in general lines, the mechanism through which plugins "talk" to each other. We'll talk more about extension points in this chapter.

The build.properties file contains information about which folders contain source files, and which ones should be included in binary and source builds.

By double-clicking any of these three files, Eclipse will open the Plugin Manifest Editor instead of a plain text editor with the file's contents. This editor can be used to manage every aspect of the plugin: dependencies, extension points, exported packages, among others. The editor will, under the hood, edit the MANIFEST.MF, build.properties, and plugin.xml files to add the information that you're providing through its forms. Some Eclipse development guides prefer to present plugin management by showing how to edit these files directly. This chapter, however, will always use the editor's forms, since they are more friendly for the newcomer Eclipse developer.

The following screenshot shows how the Plugin Manifest Editor looks:

Creating your first plugin

As you can see, there are horizontal tabs in the lower part of the editor. Each one will allow you to control a different aspect of your plugin. The first tab contains basic information about the plugin, as well as links to the other tabs and to plugin development wizards. Now take a look at the Dependencies tab, for example. We can see that our plugin depends on org.eclipse.ui and org.eclipse.runtime, since they are in the list of required plugins. We will describe each of these tabs as we utilize them. The last three tabs allow you to see the actual content of the three plugin's configuration files.

The src folder contains two source files: Activator.java and SampleAction.java, which is the class that effectively displays the message when the button is pressed.

The activator class manages the lifecycle of the plugin. It contains code that is executed when the plugin is started or stopped in the Eclipse platform, among other lifecycle-related actions. The Activator.java class generated by the wizard contains everything that we need.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset