package com.subhajit.eclipse.swt;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.progress.IProgressService;
import com.subhajit.common.util.IProgress;
public abstract class SwtTaskRunner {
public SwtTaskRunner() throws Throwable {
super();
final List<Throwable> exceptions = new ArrayList<Throwable>();
IWorkbench wb = PlatformUI.getWorkbench();
IProgressService ps = wb.getProgressService();
try {
ps.busyCursorWhile(new IRunnableWithProgress() {
public void run(final IProgressMonitor progressMonitor) {
try {
runTask(new SwtProgressImpl(progressMonitor));
} catch (InterruptedException exc) {
Thread.currentThread().interrupt();
return;
} catch (Exception exc) {
exc.printStackTrace();
exceptions.add(exc);
} finally {
progressMonitor.done();
}
}
});
} catch (InterruptedException exc) {
Thread.currentThread().interrupt();
} catch (InvocationTargetException exc) {
exc.getTargetException().printStackTrace(System.out);
}
if (!exceptions.isEmpty()) {
throw exceptions.get(0);
}
}
protected abstract void runTask(final IProgress progress) throws Exception;
}