/*******************************************************************************
 * Copyright (c) 2012, 2016 Pivotal Software, Inc.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 * Pivotal Software, Inc. - initial API and implementation
 *******************************************************************************/
package org.springsource.ide.eclipse.commons.frameworks.core.util;

import java.lang.reflect.InvocationTargetException;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.SubMonitor;
import org.eclipse.core.runtime.jobs.ISchedulingRule;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.jface.operation.IRunnableContext;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.progress.UIJob;
import org.springsource.ide.eclipse.commons.frameworks.core.FrameworkCoreActivator;

public class JobUtil {

	/**
	 * Should ideally not be used except for testing. Eclipse UI should provide a runnable context like
	 * a progress service via the workbench.
	 */
	public static final IRunnableContext DEFAULT_BACKGROUND_RUNNABLE_CONTEXT = new IRunnableContext() {

		@Override
		public void run(boolean arg0, boolean arg1, final IRunnableWithProgress runnableWithProgress)
				throws InvocationTargetException, InterruptedException {
			if (runnableWithProgress != null) {
				Job job = new Job("Running a background job.") {

					@Override
					public IStatus run(IProgressMonitor monitor) {
						try {
							runnableWithProgress.run(monitor);
						} catch (InvocationTargetException | InterruptedException e) {
							return UIJob.errorStatus(e);
						}
						return Status.OK_STATUS;
					}
				};
				job.setSystem(true);
				job.schedule();

			}
		}
	};

	/**
	 * Create a scheduling rule that conflicts only with itself and only
	 * contains itself. Jobs that want to have a 'light' impact on blocking
	 * other jobs but still some guarantee that they won't trample over other
	 * things that require access to some internal shared resource that only
	 * they can access should use this rule to protect the resource.
	 */
	public static ISchedulingRule lightRule(final String name) {
		return new ISchedulingRule() {
			public boolean contains(ISchedulingRule rule) {
				return rule == this;
			}

			public boolean isConflicting(ISchedulingRule rule) {
				return rule == this || rule.contains(this);
			}

			public String toString() {
				return name;
			};
		};
	}

	/**
	 * Runs a job in the background through a progress service that provides UI
	 * progress. Because the progress service provides UI progress, the initial
	 * launching of the services is done from the UI thread BEFORE the
	 * background job is started . A progress service is required and cannot be
	 * null.
	 * 
	 */
	public static void runBackgroundJobWithUIProgress(final IRunnableWithProgress runnableWithProgress,
			final IRunnableContext progressService, final String jobLabel) throws Exception {
		// This outer runnable launches the background job
		final IRunnableWithProgress outerRunnable = new IRunnableWithProgress() {
			public void run(final IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
				monitor.beginTask(jobLabel, IProgressMonitor.UNKNOWN);
				// Fork outside UI thread
				Job job = new Job(jobLabel) {
					@Override
					public IStatus run(IProgressMonitor monitor) {
						SubMonitor subMonitor = SubMonitor.convert(monitor);
						subMonitor.setTaskName(jobLabel);
						try {
							runnableWithProgress.run(subMonitor);
						} catch (Throwable e) {
							FrameworkCoreActivator.log(e);
						} finally {
							subMonitor.done();
						}
						return Status.OK_STATUS;
					}

				};
				job.schedule();
			}
		};

		// Progress services needs to be launched in UI thread.
		Exception[] error = new Exception[1];
		Display.getDefault().syncExec(() -> {
			try {
				progressService.run(true, true, outerRunnable);
			} catch (InvocationTargetException e) {
				error[0] = e;
			} catch (InterruptedException e) {
				error[0] = e;
			}
		});

		if (error[0] != null) {
			throw error[0];
		}
	}

}

@@ -652,16 +652,63 @@
 ception;
+%0Aimport java.util.concurrent.CompletableFuture;
 %0A%0Aimport
@@ -1268,16 +1268,100 @@
 obUtil %7B
+%0A%09%0A%09public interface JobBody %7B%0A%09%09void run(IProgressMonitor mon) throws Exception;%0A%09%7D
 %0A%0A%09/**%0A%09
@@ -4598,10 +4598,483 @@
 %09%09%7D%0A%09%7D%0A%0A
+%09public static CompletableFuture%3CVoid%3E runInJob(String jobName, JobBody doit) %7B%0A%09%09CompletableFuture%3CVoid%3E f = new CompletableFuture%3C%3E();%0A%09%09Job job = new Job(jobName) %7B%0A%0A%09%09%09@Override%0A%09%09%09protected IStatus run(IProgressMonitor monitor) %7B%0A%09%09%09%09try %7B%0A%09%09%09%09%09doit.run(monitor);%0A%09%09%09%09%09f.complete(null);%0A%09%09%09%09%7D catch (Throwable e) %7B%0A%09%09%09%09%09f.completeExceptionally(e);%0A%09%09%09%09%7D finally %7B%0A%09%09%09%09%09monitor.done();%0A%09%09%09%09%7D%0A%09%09%09%09return Status.OK_STATUS;%0A%09%09%09%7D%0A%09%09%09%0A%09%09%7D;%0A%09%09job.schedule();%0A%09%09return f;%0A%09%7D%0A%0A
 %7D%0A

Add utility to run code in a job and return a future