Book Image

Eclipse 4 Plug-in Development by Example : Beginner's Guide

By : Dr Alex Blewitt
Book Image

Eclipse 4 Plug-in Development by Example : Beginner's Guide

By: Dr Alex Blewitt

Overview of this book

<p>As a highly extensible platform, Eclipse is used by everyone from independent software developers to NASA. Key to this is Eclipse’s plug-in ecosystem, which allows applications to be developed in a modular architecture and extended through its use of plug-ins and features.<br /><br />"Eclipse 4 Plug-in Development by Example Beginner's Guide" takes the reader through the full journey of plug-in development, starting with an introduction to Eclipse plug-ins, continued through packaging and culminating in automated testing and deployment. The example code provides simple snippets which can be developed and extended to get you going quickly.</p> <p>This book covers basics of plug-in development, creating user interfaces with both SWT and JFace, and interacting with the user and execution of long-running tasks in the background.</p> <p>Example-based tasks such as creating and working with preferences and advanced tasks such as well as working with Eclipse’s files and resources. A specific chapter on the differences between Eclipse 3.x and Eclipse 4.x presents a detailed view of the changes needed by applications and plug-ins upgrading to the new model. Finally, the book concludes on how to package plug-ins into update sites, and build and test them automatically.</p>
Table of Contents (19 chapters)
Eclipse 4 Plug-in Development by Example Beginner's Guide
Credits
About the Author
Acknowledgement
About the Reviewers
www.PacktPub.com
Preface
Index

Time for action – creating a plug-in


In the Plug-in Development Environment (PDE), every plug-in has its own individual project. A plug-in project is typically created with the New Project wizard, although it is possible to upgrade an existing Java project to a plug-in project by adding the PDE nature and the required files (by navigating to Configure | Convert to plug-in project).

  1. To create a Hello World plugin, navigate to File | New | Project:

  2. The project types shown may be different from this list, but should include Plug-in Project with Eclipse Classic. If nothing is shown under the File | New menu, navigate to Window | Open Perspective | Other | Plug-in Development first; the entries should then be seen under the File | New menu.

  3. Choose Plug-in Project and click on Next. Fill in the dialog as follows:

    • Project name: com.packtpub.e4.hello.ui

    • Select the checkbox for Use default location

    • Select the checkbox for Create a Java project

    • Target Eclipse Version: 3.5 or greater

  4. Click on Next again, and fill in the plug-in properties:

    • ID: com.packtpub.e4.hello.ui

    • Version: 1.0.0.qualifier

    • Name: Hello

    • Vendor: PacktPub

    • Execution Environment: Use the default (for example: JavaSE-1.6 or JavaSE-1.7)

    • Select the checkbox for Generate an Activator

    • Activator: com.packtpub.e4.hello.ui.Activator

    • Select the checkbox for This plug-in will make contributions to the UI

    • Rich client application: No

  5. Click on Next and a set of templates will be provided:

    • Select the checkbox for Create a plug-in using one of the templates

    • Choose the Hello World Command template

  6. Click on Next to customize the sample, including:

    • The Java package name, which defaults to the project's name

    • The handler class name, which is the code that gets invoked for the action

    • The message box text, which is the message supplied

  7. Finally, click on Finish and the project will be generated.

  8. If a dialog asks, click on Yes to show the plug-in development perspective.

What just happened?

Creating a plug-in project is the first step towards creating a plug-in for Eclipse. The New Plug-in Project wizard was used with one of the sample templates to create a project.

Plug-ins are typically named in reverse domain name format, so these examples will be prefixed with com.packtpub.e4. This helps to distinguish between many plug-ins; the stock Eclipse SDK comes with more than 440 individual plug-ins, for example, the Eclipse-developed ones start with org.eclipse.

Note

Conventionally, plug-ins which create additions to (or require) the use of the UI have .ui. in the name. This helps to distinguish from those that don't, which can often be used headlessly. Of the 440+ plug-ins that make up the Eclipse SDK, 120 of those are UI related and the rest are headless.

The project contains a number of files which are automatically generated, based on the content filled in the wizard. The key files in an Eclipse plug-in are:

  • META-INF/MANIFEST.MF: The OSGi manifest describes the plug-in's dependencies, version, and name. Double-clicking it will open a custom editor, which shows the information entered in the wizards; or it can be opened in a standard text editor.

    The Manifest follows standard Java conventions; continuations are represented by a new line followed by a single space character, and the file must end with a new line. (For example, the maximum line length is 72 characters, although many ignore this.)

  • plugin.xml: The plugin.xml file declares what extensions this plug-in provides to the Eclipse runtime. Not all plug-ins need a plugin.xml file; headless (non-UI) plug-ins often don't need to have one. Extension points will be covered in more detail later, but the sample project creates an extension for the commands, handlers, bindings, and menus extension points. (If the older Hello World template was chosen, present on 3.7 and older, only the actionSets extension will be used.)

    Text labels for the commands, actions, or menus are represented declaratively in the plugin.xml file, rather than programmatically; this allows Eclipse to show the menu before needing to load or execute any code.

    Note

    This is one of the reasons Eclipse starts so quickly; by not needing to load or execute classes, it can scale by showing what's needed at the time, and then load the class on demand when the user invokes the action. Java Swing's Actions provides labels and tool tips programmatically, which can result in a slower initialization of the user interface.

  • build.properties: This file is used by PDE at development time and at build time. Generally it can be ignored, but if resources are added that need to be made available to the plug-in (such as images, properties files, HTML content, and so on), an entry must be added here as otherwise it won't be found. Generally, the easiest way to do this is by going to the Build tab of the build.properties file, which will give a tree-like view of the project's contents.

This file is an archaic hangover from the days of Ant builds, and is generally useless when using more up-to-date builds such as Maven Tycho, which will be covered in Chapter 10, Automated Builds with Tycho.

Pop quiz – Eclipse workspaces and plug-ins

Q1. What is an Eclipse workspace?

Q2. What is the naming convention for Eclipse plug-in projects?

Q3. What are the names of the three key files in an Eclipse plug-in?