/*******************************************************************************
 * 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
 * http://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.ICoreRunnable;
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.springsource.ide.eclipse.commons.frameworks.core.FrameworkCoreActivator;

public class JobUtil {

	/**
	 * 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 ICoreRunnable coreRunner, final IRunnableContext progressService,
			final String jobLabel) throws Exception {
		final IRunnableWithProgress runner = 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 {
							coreRunner.run(subMonitor);
						} catch (Throwable e) {
							FrameworkCoreActivator.log(e);
						} finally {
							subMonitor.done();
						}
						return Status.OK_STATUS;
					}

				};
				job.schedule();
			}
		};

		// To get the UI progress, must initially launch in UI thread
		// Do a sync execution to avoid unnecessary further delays in launching
		// the background job.
		Exception[] error = new Exception[1];
		Display.getDefault().syncExec(()->{
			try {
				progressService.run(true, true, runner);
			} catch (InvocationTargetException e) {
				error[0] = e;
			} catch (InterruptedException e) {
				error[0] = e;
			}
		});
		
		if (error[0] != null) {
			throw error[0];
		}
	}

}

@@ -657,55 +657,8 @@
 n;%0A%0A
-import org.eclipse.core.runtime.ICoreRunnable;%0A
 impo
@@ -2287,20 +2287,16 @@
 al I
-Core
 Runnable
  cor
@@ -2295,21 +2295,46 @@
 able
- coreRunner, 
+WithProgress runnableWithProgress,%0A%09%09%09
 fina
@@ -2368,20 +2368,17 @@
 Service,
-%0A%09%09%09
+ 
 final St
@@ -2411,16 +2411,69 @@
 ption %7B%0A
+%09%09// This outer runnable launches the background job%0A
 %09%09final 
@@ -2494,22 +2494,29 @@
 rogress 
-runner
+outerRunnable
  = new I
@@ -2960,18 +2960,28 @@
 %09%09%09%09
-coreRunner
+runnableWithProgress
 .run
@@ -3200,164 +3200,59 @@
 %09// 
-To get the UI progress, must initially launch in UI thread%0A%09%09// Do a sync execution to avoid unnecessary further delays in launching%0A%09%09// the background job
+Progress services needs to be launched in UI thread
 .%0A%09%09
@@ -3327,10 +3327,12 @@
 c(()
+ 
 -%3E
+ 
 %7B%0A%09%09
@@ -3378,14 +3378,21 @@
 ue, 
-runner
+outerRunnable
 );%0A%09
@@ -3518,18 +3518,16 @@
 %7D%0A%09%09%7D);%0A
-%09%09
 %0A%09%09if (e

Change to IRunnableWithProgress for backward compatibility