Free tutorials for Java, Eclipse and Web programming



Follow me on twitter

2. Blocking the UI and providing feedback

Before looking at Jobs and background processing we present a simple way of giving the user feedback. The easiest is to change the cursor via BusyIndicator.showWhile(Display, Runnable);

Create a new Eclipse RCP project "de.vogella.rcp.background.feedback" with the "RCP with a view" template. Change the view code to the following. If you press the included button then the cursor will change until the job is done. While this is not background processing it gives the user some feedback.

			
package de.vogella.rcp.background.feedback;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.BusyIndicator;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.part.ViewPart;

public class View extends ViewPart {
	public void createPartControl(Composite parent) {
		Button button = new Button(parent, SWT.PUSH);
		button.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				Runnable runnable = new Runnable() {
					@Override
					public void run() {
						// Imagine something useful here
						try {
							Thread.sleep(10000);
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
						
					}
				};
				BusyIndicator.showWhile(getSite().getShell().getDisplay(), runnable);
			}
		});
	}

	/**
	 * Passing the focus request to the viewer's control.
	 */
	public void setFocus() {
	}
}
		

A more advanced approach is to use a "ProgressMonitorDialog" together with a "IRunnableWithProgress".