Add a Class Template Using an Eclipse Plug-in

By Rob Gravelle

A little while back I noticed that my version of Eclipse only had a JUnit Suite class template for version 3. In case you were unaware, JUnit 4 differs significantly from JUnit 3 because it uses annotations. In looking for a way to change the class template, I discovered that these are dynamically rendered through code and cannot easily be changed; certainly far less easily than code templates, which are quite painless to manage by comparison. While someone beat me to the punch with that particular plugin, I was intrigued enough with the concept to look into it further. Sadly, I was disappointed to find that there is precious little documentation on Eclipse plugin development, other than the usual API docs. It took me a while, but I managed to cobble together a simple plugin that extends the New menu to let me choose my own class type and open a basic skeleton in the editor. It's certainly a good start, and the subject of today's article.

About the Eclipse Plug-in Model

An Eclipse plug-in is a component that provides a service within the context of the Eclipse workbench. Eclipse's extensible integrated development environment (IDE) allows anyone to build tools that integrate seamlessly with the environment and other tools. With the exception of a small runtime kernel, everything in Eclipse is a plug-in. We can develop our own plug-ins using the "Plug-in Project" type. Plug-ins that you develop integrate with Eclipse in exactly the same way as other plug-ins.

Requirements

In order to be able to develop plug-ins you have two choices. First, you can download a recent version of Eclipse such as 4.2.1 or above or you can install the required plug-in on your existing Eclipse. Here's how:

Open the Eclipse Update Manager by selecting Help > Install new Software… from the main menu.

Install General Purpose Tools > Eclipse Plug-in Development Environment > Eclipse RCP Plug-in Developer Resources from the Eclipse update site for your release. This would be for example http://download.eclipse.org/releases/kepler for the Eclipse 4.3. release.

If you're not sure if you have it, just take a look on the Installed Software tab:

software_updates_and_addons_dialog_with_plugin_environment_highlighted (74K)

Creating a Plug-in Project from a Template

Open the New Project Wizard from the main menu, and start typing the word "plugin" in the Select a Wizard screen until you see the Plug-in Project in the list. Select it and click the Next> button.

On the Plug-in Project screen, provide a name for the project such as "CreateClass". Accept all the defaults and click Next>.

The Plug-in Content wizard page allows you to customize the plug-in data such as the ID, version, name, provider, and others. Generally, you can accept the defaults here, unless you want to change something.

The Templates screen provides you with a number of templates to start from. Considering the number of actions that are achievable via a plug-in, it's nice to get a head start.

If you choose the first - custom plug-in wizard - item and click Next, you will get a list of more fine-grained templates to choose from. They are all selected by default, so you should click on the Deselect All button and then choose which items you'd like to include in your plug-in. For our purposes here today, the closest choice is the "New File Wizard". Select it with the mousepointer to see the description. As you can see, the plug-in will create a menu group and item under File > New > Other… from the main menu.

Clicking Next from the Template Selection screen will summarize the plug-in properties, including the package name, wizard names, as well as the file name and extension. These properties are too generic to be of much use, so I would recommend that you change them as follows:

new_wizard_options_dialog (40K)

Click the Finish button to create the project. At this point Eclipse will ask you if you'd like to switch the the Plug-in Development Perspective. Unless you have a good reason not to, I would suggest accepting the suggestion.

Customizing and Running Your Plug-in

Upon creating the project, Eclipse will open the Manifest.MF file in the editor. It's really the home base for all non-code-related aspects of your plug-in. Besides the number of properties on the main page, there are several tabs on the bottom including the Manifest source, as well as extension points (declared in plugin.xml), and build configuration properties:

CreateClass_manifest_overview (60K)

The Extensions tab defines how our plug-in is hooked into the IDE. Our plug-in project creates a menu group and item which we named "Create Class Wizards" and "Create Class Wizard" respectively. You can see the names and other properties for each by clicking on them in the All Extensions list:

The CreateClass Wizards Group Properties CreateClass_wizards_group (24K)

 

The CreateClass Wizard Item Properties CreateClass_wizard_item (40K)

When you select File > New > Other… from the main menu of your test application (more on that in a bit), your new group and item should appear in the list:

select_a_wizard_dialog (40K)

Notice that the icon beside the item is listed in the icon property from the CreateClass Wizard Item Properties page.

Conclusion

That concludes the setting up of the project and properties. In the next installment, we'll get to the real fun stuff: the coding! That's where we'll write code that outputs code to the file.



Make a Comment

Loading Comments...

  • Web Development Newsletter Signup

    Invalid email
    You have successfuly registered to our newsletter.
  •  
  •