RSS

Search Engine

Monday, June 14, 2010

Wizards

12.1. Overview

Wizards provide a flexible ways to systematically gather user input and perform input validation. A wizard guides a user step by step through a process.

Eclipse implements a Wizard via class WizardDialog. The WizardDialog controls the process (Navigation, process bar, area for error and information messages). The Wizard content and is provided by the class Wizard and the pages are provided via class WizardPages.

12.2. Example

Create a new project "de.vogella.rcp.intro.wizards". Use the "Hello RCP" as a template.

Create the command "de.vogella.rcp.intro.wizards.showWizard" with the default handler "de.vogella.rcp.intro.wizards.handler.ShowWizard". Add the command to the menu.

Create a package "de.vogella.rcp.intro.wizards.wizard". Create the classes "MyPageOne" and "MyPageTwo" MyWizard which extends org.eclipse.jface.wizard.WizardPage.

Maintain the following code for the classes.

    
package de.vogella.rcp.intro.wizards.wizard;

import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

public class MyPageOne extends WizardPage {
private Text text1;
private Composite container;

public MyPageOne() {
super("First Page");
setTitle("First Page2");
setDescription("This wizard does not really do anything. But this is the first page");
}

@Override
public void createControl(Composite parent) {
container = new Composite(parent, SWT.NULL);
GridLayout layout = new GridLayout();
container.setLayout(layout);
layout.numColumns = 2;
Label label1 = new Label(container, SWT.NULL);
label1.setText("Put here a value");

text1 = new Text(container, SWT.BORDER | SWT.SINGLE);
text1.setText("");
text1.addKeyListener(new KeyListener() {

@Override
public void keyPressed(KeyEvent e) {
}

@Override
public void keyReleased(KeyEvent e) {
if (!text1.getText().isEmpty()) {
setPageComplete(true);

}
}

});
GridData gd = new GridData(GridData.FILL_HORIZONTAL);
text1.setLayoutData(gd);
// Required to avoid an error in the system
setControl(container);
setPageComplete(false);

}

public String getText1() {
return text1.getText();
}
}

    
package de.vogella.rcp.intro.wizards.wizard;

import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

public class MyPageTwo extends WizardPage {
private Text text1;
private Composite container;

public MyPageTwo() {
super("Second Page");
setTitle("Second Page");
setDescription("Now this is the second page");
setControl(text1);
}

@Override
public void createControl(Composite parent) {
container = new Composite(parent, SWT.NULL);
GridLayout layout = new GridLayout();
container.setLayout(layout);
layout.numColumns = 2;
Label label1 = new Label(container, SWT.NULL);
label1.setText("Say hello to Fred");

text1 = new Text(container, SWT.BORDER | SWT.SINGLE);
text1.setText("");
text1.addKeyListener(new KeyListener() {

@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub

}

@Override
public void keyReleased(KeyEvent e) {
if (!text1.getText().isEmpty()) {
setPageComplete(true);
}
}

});
GridData gd = new GridData(GridData.FILL_HORIZONTAL);
text1.setLayoutData(gd);
Label labelCheck = new Label(container, SWT.NONE);
labelCheck.setText("This is a check");
Button check = new Button(container, SWT.CHECK);
check.setSelection(true);
// Required to avoid an error in the system
setControl(container);
setPageComplete(false);
}

public String getText1() {
return text1.getText();
}

}

Create a new class MyWizard which extends org.eclipse.jface.wizard.Wizard.

>

    
package de.vogella.rcp.intro.wizards.wizard;

import org.eclipse.jface.wizard.Wizard;

public class MyWizard extends Wizard {

private MyPageOne one;
private MyPageTwo two;

public MyWizard() {
super();
setNeedsProgressMonitor(true);
}

@Override
public void addPages() {
one = new MyPageOne();
two = new MyPageTwo();
addPage(one);
addPage(two);
}

@Override
public boolean performFinish() {

// just put the result to the console, imagine here much more
// intelligent stuff.
System.out.println(one.getText1());
System.out.println(two.getText1());

return true;
}
}

Implement the handler for the command.

    
package de.vogella.rcp.intro.wizards.handler;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.jface.wizard.WizardDialog;
import org.eclipse.ui.handlers.HandlerUtil;

import de.vogella.rcp.intro.wizards.wizard.MyWizard;

public class ShowWizard extends AbstractHandler {

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
MyWizard wizard = new MyWizard();
WizardDialog dialog = new WizardDialog(HandlerUtil
.getActiveShell(event), wizard);
dialog.open();
return null;
}

}

If you run your application and select the menu entry the wizard should get displayed and after pressing "Finish" you should see the result on the console.

1 comments:

Estevex said...

This works on Android?

Post a Comment