/*******************************************************************************
 * Copyright (c) 2000, 2011 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.jdt.internal.ui.util;

import java.lang.reflect.InvocationTargetException;

import org.eclipse.swt.custom.BusyIndicator;

import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.OperationCanceledException;

import org.eclipse.jface.operation.IRunnableContext;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.operation.ModalContext;

/**
 * A runnable context that shows the busy cursor instead of a progress
 * monitor. Note, that the UI thread is blocked even if the runnable
 * is executed in a separate thread by passing <code>fork= true</code>
 * to the context's run method. Furthermore this context doesn't provide
 * any UI to cancel the operation.
 */
public class BusyIndicatorRunnableContext implements IRunnableContext {

	private static class BusyRunnable implements Runnable {

		private static class ThreadContext extends Thread {
			IRunnableWithProgress fRunnable;
			Throwable fThrowable;

			public ThreadContext(IRunnableWithProgress runnable) {
				this(runnable, "BusyCursorRunnableContext-Thread"); //$NON-NLS-1$
			}
			protected ThreadContext(IRunnableWithProgress runnable, String name) {
				super(name);
				fRunnable= runnable;
			}
			@Override
			public void run() {
				try {
					fRunnable.run(new NullProgressMonitor());
				} catch (ThreadDeath e) {
					fThrowable= e;
					throw e;
				} catch (InvocationTargetException | InterruptedException | RuntimeException | Error e) {
					fThrowable= e;
				}
			}
			void sync() {
				try {
					join();
				} catch (InterruptedException e) {
					// ok to ignore exception
				}
			}
		}

		public Throwable fThrowable;
		private boolean fFork;
		private IRunnableWithProgress fRunnable;
		public BusyRunnable(boolean fork, IRunnableWithProgress runnable) {
			fFork= fork;
			fRunnable= runnable;
		}
		@Override
		public void run() {
			try {
				internalRun(fFork, fRunnable);
			} catch (InvocationTargetException | InterruptedException e) {
				fThrowable= e;
			}
		}
		private void internalRun(boolean fork, final IRunnableWithProgress runnable) throws InvocationTargetException, InterruptedException {
			Thread thread= Thread.currentThread();
			// Do not spawn another thread if we are already in a modal context
			// thread or inside a busy context thread.
			if (thread instanceof ThreadContext || ModalContext.isModalContextThread(thread))
				fork= false;

			if (fork) {
				final ThreadContext t= new ThreadContext(runnable);
				t.start();
				t.sync();
				// Check if the separate thread was terminated by an exception
				Throwable throwable= t.fThrowable;
				if (throwable != null) {
					if (throwable instanceof InvocationTargetException) {
						throw (InvocationTargetException) throwable;
					} else if (throwable instanceof InterruptedException) {
						throw (InterruptedException) throwable;
					} else if (throwable instanceof OperationCanceledException) {
						throw new InterruptedException();
					} else {
						throw new InvocationTargetException(throwable);
					}
				}
			} else {
				try {
					runnable.run(new NullProgressMonitor());
				} catch (OperationCanceledException e) {
					throw new InterruptedException();
				}
			}
		}
	}

	@Override
	public void run(boolean fork, boolean cancelable, IRunnableWithProgress runnable) throws InvocationTargetException, InterruptedException {
		BusyRunnable busyRunnable= new BusyRunnable(fork, runnable);
		BusyIndicator.showWhile(null, busyRunnable);
		Throwable throwable= busyRunnable.fThrowable;
		if (throwable instanceof InvocationTargetException) {
			throw (InvocationTargetException)throwable;
		} else if (throwable instanceof InterruptedException) {
			throw (InterruptedException)throwable;
		}
	}
}
