ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button

Eclipse Plugins Exposed, Part 3: Customizing a Wizard

by Emmanuel Proulx
07/27/2005

A while ago, I had a tiny portable electronic address book. I took it for granted until the day it stopped working. The salesperson who sold it to me couldn't retrieve my contacts, but offered to replace it. That day I learned that data is important. This shiny gizmo was worth nothing compared to the bits stored on it.

In part one of this series, we introduced Eclipse's plugin development environment and developed a simple plugin. In part two, we added a toolbar button, a menu item, and dialogs. The result was a shiny gizmo that didn't do much for us. It simply displayed sample text using a font. Now we need to make it manage actual data. We will massage the plugin so that it does what we need it to do. This article discusses editor documents and shows how to customize a wizard.

Invokatron Story

But first, let's elaborate on Invokatron itself. As discussed in the previous articles, Invokatron is an graphical tool that generates Java code. You can code up a class' methods simply by dragging and dropping. The dragged-in method is "invoked" by the edited method; thus, the name of the plugin. We will let the data drive the design of our application. In a later article, we're going to develop this GUI. For now, all we need to do is to figure out the important data our plugin will input and store. This is often referred to as the model of the application. Here are the things we'll need to worry about when designing our system:

Related Reading

Eclipse Cookbook
By Steve Holzner

  • What specific pieces of data need to be saved?
  • What will be the in-memory representation of this data? A POJO, a JavaBean, an EJB?
  • What will be the persisted format of this data? A database table, an XML file, a properties file, a serialized binary file?
  • What are the different ways the data can be entered? The New File wizard or some other wizard, using a pop-up dialog, graphically using an editor, typing in a text editor, in a document properties page?

These decisions have to be resolved before we go on. There is no right answer that is good for all projects; it all depends on your needs. In our case, I made arbitrary, questionable decisions as follows:

  • A Java class contains a class name, a package, a superclass, and implemented interfaces. Let's start with these, and add some more data in a later article.
  • I will represent the data as a class extending the Properties class. This constitutes the "document class" of our editor.
  • The format I will use is a properties file that is very easy to parse with the Properties class.
  • I will ask for the data initially in the New File wizard, and then I will let the user change the data in the Properties window or in a text editor. This step will be done in the next article.

Document Class

The next step is to write the document class. Create a new package named invokatron.model, and a new class named InvokatronDocument. Here's the first shot at our document class:

public class InvokatronDocument
        extends Properties
{
    public static final String PACKAGE = "package";
    public static final String SUPERCLASS = "superclass";
    public static final String INTERFACES = "interfaces";
}

Using the Properties class allows for simple parsing and saving of our data. The getter and setter methods are not necessary, but you could add them if you want to. This class is not finished; we will add the interfaces that Eclipse requires later on.

With this class, getting a property is as simple as:

String package =
    document.getProperty(InvokatronDocument.PACKAGE);

Customizing a Wizard

Have a look at our wizard as it was in the previous article (get the source code if you don't have it already). Remember, you can access it by clicking on the toolbar button or menu item we added. Here it is in figure 1:

Old Wizard

Figure 1. Old wizard

It has only one page, with no graphic in the upper-right corner. We'd like to enter more information and have a nice graphic. In other words, we'd like to customize this wizard.

Let's dissect our wizard. Open the file InvokatronWizard.java. Notice how this class extends Wizard and implements INewWizard. These have many methods you should know about. To customize the wizard, we simply invoke or override some of these methods. Here are a few important ones:

Pages: 1, 2, 3, 4, 5, 6

Next Pagearrow