Aug 05 2008

Long-running operation in a wizard page

Published by at 2:07 am under Eclipse,IT,Java

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);
    }
}

3 responses so far

3 Responses to “Long-running operation in a wizard page”

  1. SeBon 10 Mar 2009 at 1:07 am

    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.

  2. Velvet Wine Bagson 20 Aug 2010 at 5:19 pm

    Ever woundered how microsoft create the nice longrunning process windows works in SharePoint 2007?

    SPLongOperation is the class to use. It has 2 important methods

    Begin and End;

    All your code that runs for a long time is placed between begin and end.

    Below is a sample class.

    It is almost to simple and just works 🙂

    using System;
    using System.Web;
    using System.Web.UI.WebControls;
    using Microsoft.SharePoint;
    namespace CreateLongOperation
    {
    public class LongRun : System.Web.UI.Page
    {
    protected Button buttonOk;

    protected void Page_Load(object sender, EventArgs e)
    {
    buttonOk.Click += new EventHandler(buttonOk_Click);
    }

    void buttonOk_Click(object sender, EventArgs e)
    {
    SPLongOperation operation = new SPLongOperation(this.Page);

    operation.Begin();

    // do long operation code here…
    System.Threading.Thread.Sleep(6000);

    operation.End(“http://sps/_layouts/Mynewpage.aspx”);
    }
    }
    }

  3. Sebon 09 Apr 2011 at 4:08 pm

    It seems that to display the wizard progress monitor control you also have to call :

    ((Wizard) getWizard()).setNeedsProgressMonitor(true);

Trackback URI | Comments RSS

Leave a Reply

You must be logged in to post a comment.