Wednesday, February 11, 2009

Eclipse : How to create Wizards

Eclipse provides easy ways to create wizards and connect them to standard eclipse actions like New, Import,Export and so on. If you want to add a Wizard to file->New menu, you can use the newWizards extension point.


point="org.eclipse.ui.newWizards">
class="com.blog.sample.loginapp.SampleWizard"
id="com.blog.sample.loginApp.wizard3"
name="Sample Wizard">




This adds a Sample Wizard to the File New action. Now the SampleWizard has to include all the pages in the wizard. First of all, the SampleWizard has to implement the IWizard interface ( can extend the abstract implementation Wizard ).


import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.ui.INewWizard;
import org.eclipse.ui.IWorkbench;

public class SampleWizard extends Wizard implements INewWizard {

public SampleWizard() {

}

@Override
public boolean performFinish() {

return false;
}

public void init(IWorkbench workbench, IStructuredSelection selection) {


}

}


This creates a Wizard as shown below.



Now to add a Page to the SampleWizard , create a WizardPage and add it.


public class SamplePage extends WizardPage {

Text text = null;

public SamplePage() {
super("SamplePage1");
setTitle("Page1");
setDescription("Page1 Description");
}

public void createControl(Composite parent) {
Composite comp = new Composite(parent, SWT.NONE);
comp.setLayout(new GridLayout(2, false));
Label label = new Label(comp, SWT.NONE);
label.setText("Text ");
text = new Text(comp, SWT.BORDER);
text.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
setControl(parent);
}

@Override
public boolean isPageComplete() {
return super.isPageComplete();
}

}


Now the SamplePage has to be added to the SampleWizard via the addPages method as follows.


@Override
public void addPages() {
super.addPages();
addPage(samplePage);
}


This adds the page as below.



The isPageComplete() method from the SamplePage enables/disables the Next button if the page is complete. Once all the data is taken from the page. The isPageComplete returns true to the SampleWizard. At the end, the SampleWizard enables the finish the Button by looking at the canFinish return value. The canFinish can return true if the mandatory values are entered.

Have fun !.

No comments: