RSS

Search Engine

Showing posts with label Eclipse Job API in RCP - Tutorial. Show all posts
Showing posts with label Eclipse Job API in RCP - Tutorial. Show all posts

Wednesday, June 30, 2010

Overview

The Eclipse Jobs API provides background capabilities to Eclipse with can also be used in Eclipse RCP.

The important parts of the Job API are:

  • JobManager - Schedules the jobs

  • Job - The individual task to perform

  • IProgresMonitor - UI for Job information

Using the Jobs API in Eclipse RCP is the same as in Eclipse plug-in development therefore I just give a small example. Check the appendix for a general introduction to the Eclipse Jobs API.

Example

The following assumes that you are familiar with Eclipse RCP development .

Create a new Eclipse RCP project "de.vogella.jobs.first" with the "Hello RCP" example. Create one command "de.vogella.jobs.first.runJob1" with the following default handler.

   
package de.vogella.jobs.first.handler;


import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.ui.handlers.HandlerUtil;

public class RunJob1 extends AbstractHandler {

@Override
public Object execute(final ExecutionEvent event) throws ExecutionException {
Job job = new Job("First Job"){
@Override
protected IStatus run(IProgressMonitor monitor) {

for (int i=0; i<10;>

Add the command to the menu.

Run your application to see the Job API in action.

Progress report

If you perform long running operations you should provide the user some information about the long running job. A good way of doing this is to show a progress indicator or a progress dialog.

Create a new project "de.vogella.rcp.intro.progress" with "Hello RCP" as a template. Create the command "de.vogella.rcp.intro.progress.showDialog" with the default handler "de.vogella.rcp.intro.progress.handler.ShowDialog". Create the handler class.

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

import java.lang.reflect.InvocationTargetException;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.dialogs.ProgressMonitorDialog;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.ui.handlers.HandlerUtil;

public class ShowDialog extends AbstractHandler {

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
ProgressMonitorDialog dialog = new ProgressMonitorDialog(HandlerUtil
.getActiveShell(event).getShell());
try {
dialog.run(true, true, new IRunnableWithProgress() {
@Override
public void run(IProgressMonitor monitor) {
monitor
.beginTask("Doing something timeconsuming here",
100);
for (int i = 0; i < 10; i++) {
if (monitor.isCanceled())
return;
monitor.subTask("I'm doing something here " + i);
sleep(1000);
monitor.worked(i);
}
monitor.done();
}
});
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}

return null;
}

private void sleep(Integer waitTime) {
try {
Thread.sleep(waitTime);
} catch (Throwable t) {
System.out.println("Wait time interrupted");
}
}

}

Add this command to the menu.

Run the application. Your command should open a dialog which show the process.

Thank you

Thank you for practicing with this tutorial.

I maintain this tutorial in my private time. If you like the information please help me by donating or by recommending this tutorial to other people.