Eclipse JFace's Wizards

Manuel Selva picture Manuel Selva · Jun 5, 2009 · Viewed 8.8k times · Source

I need a wizard which second page content depends on the first page's selection. The first page asks the user the "kind" of filter he wants to create and the second one asks the user to create one filter instance of the selected "kind".

JFace's wizards pages contents (createControl(...) method) are all created when the wizard is open and not when a given page is displayed (this allow JFace to know the wizard size I guess ??).

Because of this, I have to create my second page content BEFORE the wizard is opened BUT I can't since the second page's content depends on the first page selection.

For now the cleaner solution I found consists in creating all (seconds) pages before the wizard is open (with their content) and override the getNextPage() method in the first page's implementation.

The main drawback of that solution is that it can be be expensive when there are many second pages to create.

What do you think about that solution ? How do you manage your wizard's pages ? Is there any cleaner solution I missed ?

Answer

VonC picture VonC · Jun 5, 2009

The approach is right if you are several other pages which are

  • completely different one with another
  • depends on the previous choices made in a previous page

Then you can add the next page dynamically (also as described here)

But if you have just a next page with a dynamic content, you should be able to create that content in the onEnterPage() method

public void createControl(Composite parent)
{
    //
    // create the composite to hold the widgets
    //
    this.composite = new Composite(parent, SWT.NONE);

    //
    // create the desired layout for this wizard page
    //
    GridLayout layout = new GridLayout();
    layout.numColumns = 4;
    this.composite.setLayout(layout);

    // set the composite as the control for this page
    setControl(this.composite);
}

void onEnterPage()
{
    final MacroModel model = ((MacroWizard) getWizard()).model;
    String selectedKey = model.selectedKey;
    String[] attrs = (String[]) model.macroMap.get(selectedKey);

    for (int i = 0; i < attrs.length; i++)
    {
        String attr = attrs[i];
        Label label = new Label(this.composite, SWT.NONE);
        label.setText(attr + ":");

        new Text(this.composite, SWT.NONE);
    }
    pack();
}

As shown in the eclipse corner article Creating JFace Wizards:

We can change the order of the wizard pages by overwriting the getNextPage method of any wizard page.Before leaving the page, we save in the model the values chosen by the user. In our example, depending on the choice of travel the user will next see either the page with flights or the page for travelling by car.

public IWizardPage getNextPage(){
   saveDataToModel();       
   if (planeButton.getSelection()) {
       PlanePage page = ((HolidayWizard)getWizard()).planePage;
     page.onEnterPage();
       return page;
   }
   // Returns the next page depending on the selected button
   if (carButton.getSelection()) { 
    return ((HolidayWizard)getWizard()).carPage;
   }
   return null;
}

We define a method to do this initialization for the PlanePage, onEnterPage() and we invoke this method when moving to the PlanePage, that is in the getNextPage() method for the first page.