The Sun BabelFish Blog

Don't panic !

Thursday Apr 06, 2006

writing complex wizards made easy

I have recently been tasked to write some increasingly complex wizards for Sun. The simplest wizard I started off with had about 5 steps, and the last one I wrote had a relatively complex branching structure, one of the steps containing a panel with 6 options to choose from, selecting any one of these leading the user down a separate branch, a couple of these branches having themselves a number of choice points. The longest route from beginning to end may be close to 9 steps, which is quite long for a wizard, whose purpose after all is to make life easy for the user.

Using Garrett IA notation we get a wizard that can be described like this

Each of the squares represents a step in the wizard, and the steps collected on a green background form a branch. Branches can be ended with a choice point where a decision is made as to which step should be taken next.

Luckily for me Tim Boudreau had written an very good open source wizard library, that closely follows the Java Look and Feel Guidelines as descibed in chapter 7, so most of the work had allready been done. The only thing missing was a clear abstraction for Steps and Branch. After adding this abstraction on the CVS EZBranch, the code to write a simple one page wizard is as simple as found in this Main main class:

public class Main {
    public static void main(String[] ignored) {
        //All we do here is assemble the list of WizardPage subclasses we
        //want to show:
        Branch branch = new Branch (
            new EZStep(AnimalTypePage.class),
            new EZStep(LocomotionPage.class),
            new EZStep(OtherAttributesPage.class),
            new EZStep(FinalPage.class)
        );
        
        //Use the utility method to compose a Wizard
        Wizard wizard = new SimpleProvider("Simple Wizard",branch).createWizard();
        
        //And show it onscreen
        WizardDisplayer.showWizard (wizard);
    }

A more complex wizard with a branching point just requires code such as in this NewPetWizard class I rewrote a little rashly:

public static void main (String[] ignored) throws Exception {
        Branch initial = new Branch(
                new EZStep(AnimalLoverPanel.class),
                new EZStep(SpeciesPanel.class)
                );
        final Branch catloverBr = new Branch(
                new EZStep(CatHairLengthPanel.class),
                new EZStep(CatBreedPanel.class)
                );
        final Branch dogLoverBr = new Branch(
                new EZStep(DogTempermentPanel.class),
                new EZStep(DogSizePanel.class)
                );
        initial.setDecison(new Branch.Decision() {
            public Branch selectBranch(Map info) {
                     Object species = info.get(SpeciesPanel.KEY_SPECIES);
                    if (SpeciesPanel.VALUE_CAT.equals(species)) {
                        return catloverBr;
                    } else if (SpeciesPanel.VALUE_DOG.equals(species)) {
                        return dogLoverBr;
                     } else if (SpeciesPanel.VALUE_GERBIL.equals(species)) {
                         return null;//new GerbilSteps();
                    } else {
                        return null;
                    }
             }
            
        });
            
        Wizard wis = new BranchController("Pet Wizard",initial).createWizard();
        WizardDisplayer.showWizard (wis);

The only work to be done then, is to write the Panels, which is super easy when using Matisse with Netbeans 5.

Comments:

Note on comments:

Post a Comment:
Comments are closed for this entry.

Search

Recent Entries

Navigation

Referers