Aug 05 2008
Long-running operation in a wizard page
Some time you may need to do some long running operation in one of the pages in your wizard. Probably the best solution would be to redesign the flow of an application in such way that this operation can be performed outside the wizard as a background task (using Eclipse Jobs API). If you really need to place this inside wizard than in order to not freeze your application completely with such operation you can use IRunnableWithProgress. Sample usage of this interface is presented below.
import java.lang.reflect.InvocationTargetException; import java.util.logging.Logger; import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.jface.operation.IRunnableWithProgress; import org.eclipse.jface.wizard.WizardPage; import org.eclipse.swt.SWT; import org.eclipse.swt.events.SelectionAdapter; import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Composite; /** * Wizard Page with long running operation * */ public class DummyWizardPage extends WizardPage { private Logger logger = Logger.getLogger(DummyWizardPage.class.getName()); protected DummyWizardPage(String pageName) { super(pageName); } /** * {@inheritDoc} */ public void createControl(Composite parent) { Composite composite = new Composite(parent, SWT.NONE); Button button = new Button(composite, SWT.PUSH); button.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent event) { try { // Invoking long running operation getContainer().run(true, false, new IRunnableWithProgress() { public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException { // Some time consuming operation } }); } catch (InvocationTargetException e) { logger.warning(e.getMessage()); } catch (InterruptedException e) { logger.warning(e.getMessage()); } } }); // Setting control for Wizard Page setControl(composite); } }
Do you think it would be easilly possible while the long job is running to add a button or change the grayed “finish” button by a “Run in background” button dyanmically. If this button where clicked then the wizard dialog box would be reduced in the job bar like a Job and bring the wizard back again to show next page for instance when the long task is over ?
SeB.