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
Pages: 1, 2, 3, 4, 5, 6

Coding the New Wizard Page

Next, we need to write the InvokatronWizardPage2. This whole class is new:

public class InvokatronWizardPage2 extends WizardPage {
    private Text packageText;
    private Text superclassText;
    private Text interfacesText;

    private ISelection selection;

    public InvokatronWizardPage2(ISelection selection) {
        super("wizardPage2");
        setTitle("Invokatron Wizard");
        setDescription("This wizard creates a new"+
            " file with *.invokatron extension.");
        this.selection = selection;
    }

    private void updateStatus(String message) {
        setErrorMessage(message);
        setPageComplete(message == null);
    }

    public String getPackage() {
        return packageText.getText();
    }
    public String getSuperclass() {
        return superclassText.getText();
    }
    public String getInterfaces() {
        return interfacesText.getText();
    }

The constructor above sets the title of the page (which appears highlighted below the title bar) and the description (which appears below the page title). We also have a few helper methods. updateStatus takes care of displaying page-specific error messages. If there are no error messages, it means the page is completed; therefore, the Next button will become available. There are also getter methods for the data fields' contents. Next is the createControl() method, which builds all of the visual components of the page:

    public void createControl(Composite parent) {
        Composite controls =
            new Composite(parent, SWT.NULL);
        GridLayout layout = new GridLayout();
        controls.setLayout(layout);
        layout.numColumns = 3;
        layout.verticalSpacing = 9;

        Label label =
            new Label(controls, SWT.NULL);
        label.setText("&Package:");

        packageText = new Text(
            controls,
            SWT.BORDER | SWT.SINGLE);
        GridData gd = new GridData(
            GridData.FILL_HORIZONTAL);
        packageText.setLayoutData(gd);
        packageText.addModifyListener(
            new ModifyListener() {
                public void modifyText(
                        ModifyEvent e) {
                    dialogChanged();
                }
        });

        label = new Label(controls, SWT.NULL);
        label.setText("Blank = default package");

        label = new Label(controls, SWT.NULL);
        label.setText("&Superclass:");

        superclassText = new Text(
            controls,
            SWT.BORDER | SWT.SINGLE);
        gd = new GridData(
            GridData.FILL_HORIZONTAL);
        superclassText.setLayoutData(gd);
        superclassText.addModifyListener(
            new ModifyListener() {
                public void modifyText(
                        ModifyEvent e) {
                    dialogChanged();
            }
        });

        label = new Label(controls, SWT.NULL);
        label.setText("Blank = Object");

        label = new Label(controls, SWT.NULL);
        label.setText("&Interfaces:");

        interfacesText = new Text(
            controls,
            SWT.BORDER | SWT.SINGLE);
        gd = new GridData(
            GridData.FILL_HORIZONTAL);
        interfacesText.setLayoutData(gd);
        interfacesText.addModifyListener(
            new ModifyListener() {
                public void modifyText(
                        ModifyEvent e) {
                    dialogChanged();
            }
        });

        label = new Label(controls, SWT.NULL);
        label.setText("Separated by ','");

        dialogChanged();
        setControl(controls);
    }

You need to know SWT in order to write this code. If you don't, at the bottom of this article there are links to places where you can learn it. Basically this method creates labels and fields and places them in a grid layout. Every time a field is changed, its data is validated by a call to dialogChanged():

    private void dialogChanged() {
        String aPackage = getPackage();
        String aSuperclass = getSuperclass();
        String interfaces = getInterfaces();

        String status = new PackageValidator().isValid(aPackage);
        if(status != null) {
            updateStatus(status);
            return;
        }

        status = new SuperclassValidator().isValid(aSuperclass);
        if(status != null) {
            updateStatus(status);
            return;
        }

        status = new InterfacesValidator().isValid(interfaces);
        if(status != null) {
            updateStatus(status);
            return;
        }

        updateStatus(null);
    }

}

This work is done with three utility classes: PackageValidator, SuperclassValidator, and InterfacesValidator. We will write those classes next.

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

Next Pagearrow