001: /*******************************************************************************
002: * Copyright (c) 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: ******************************************************************************/package org.eclipse.ui.internal;
011:
012: import org.eclipse.ui.PartInitException;
013: import org.eclipse.ui.WorkbenchException;
014: import org.eclipse.ui.internal.misc.StatusUtil;
015:
016: /**
017: * @since 3.3
018: *
019: */
020: public final class StartupThreading {
021:
022: static Workbench workbench;
023:
024: public static abstract class StartupRunnable implements Runnable {
025: private Throwable throwable;
026:
027: public final void run() {
028: try {
029: runWithException();
030: } catch (Throwable t) {
031: this .throwable = t;
032: }
033: }
034:
035: public abstract void runWithException() throws Throwable;
036:
037: public Throwable getThrowable() {
038: return throwable;
039: }
040: }
041:
042: static void setWorkbench(Workbench wb) {
043: workbench = wb;
044: }
045:
046: public static void runWithWorkbenchExceptions(StartupRunnable r)
047: throws WorkbenchException {
048: workbench.getDisplay().syncExec(r);
049: Throwable throwable = r.getThrowable();
050: if (throwable != null) {
051: if (throwable instanceof Error) {
052: throw (Error) throwable;
053: } else if (throwable instanceof RuntimeException) {
054: throw (RuntimeException) throwable;
055: } else if (throwable instanceof WorkbenchException) {
056: throw (WorkbenchException) throwable;
057: } else {
058: throw new WorkbenchException(StatusUtil.newStatus(
059: WorkbenchPlugin.PI_WORKBENCH, throwable));
060: }
061: }
062: }
063:
064: public static void runWithPartInitExceptions(StartupRunnable r)
065: throws PartInitException {
066: workbench.getDisplay().syncExec(r);
067: Throwable throwable = r.getThrowable();
068: if (throwable != null) {
069: if (throwable instanceof Error) {
070: throw (Error) throwable;
071: } else if (throwable instanceof RuntimeException) {
072: throw (RuntimeException) throwable;
073: } else if (throwable instanceof WorkbenchException) {
074: throw (PartInitException) throwable;
075: } else {
076: throw new PartInitException(StatusUtil.newStatus(
077: WorkbenchPlugin.PI_WORKBENCH, throwable));
078: }
079: }
080: }
081:
082: public static void runWithThrowable(StartupRunnable r)
083: throws Throwable {
084: workbench.getDisplay().syncExec(r);
085: Throwable throwable = r.getThrowable();
086: if (throwable != null) {
087: throw throwable;
088: }
089: }
090:
091: public static void runWithoutExceptions(StartupRunnable r)
092: throws RuntimeException {
093: workbench.getDisplay().syncExec(r);
094: Throwable throwable = r.getThrowable();
095: if (throwable != null) {
096: if (throwable instanceof Error) {
097: throw (Error) throwable;
098: } else if (throwable instanceof RuntimeException) {
099: throw (RuntimeException) throwable;
100: } else {
101: throw new RuntimeException(throwable);
102: }
103: }
104: }
105:
106: }
|