KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > StartupThreading


1 /*******************************************************************************
2  * Copyright (c) 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  ******************************************************************************/

11
12 package org.eclipse.ui.internal;
13
14 import org.eclipse.ui.PartInitException;
15 import org.eclipse.ui.WorkbenchException;
16 import org.eclipse.ui.internal.misc.StatusUtil;
17
18 /**
19  * @since 3.3
20  *
21  */

22 public final class StartupThreading {
23
24     static Workbench workbench;
25
26     public static abstract class StartupRunnable implements Runnable JavaDoc {
27         private Throwable JavaDoc throwable;
28
29         public final void run() {
30             try {
31                 runWithException();
32             } catch (Throwable JavaDoc t) {
33                 this.throwable = t;
34             }
35         }
36
37         public abstract void runWithException() throws Throwable JavaDoc;
38
39         public Throwable JavaDoc getThrowable() {
40             return throwable;
41         }
42     }
43
44     static void setWorkbench(Workbench wb) {
45         workbench = wb;
46     }
47
48     public static void runWithWorkbenchExceptions(StartupRunnable r)
49             throws WorkbenchException {
50         workbench.getDisplay().syncExec(r);
51         Throwable JavaDoc throwable = r.getThrowable();
52         if (throwable != null) {
53             if (throwable instanceof Error JavaDoc) {
54                 throw (Error JavaDoc) throwable;
55             } else if (throwable instanceof RuntimeException JavaDoc) {
56                 throw (RuntimeException JavaDoc) throwable;
57             } else if (throwable instanceof WorkbenchException) {
58                 throw (WorkbenchException) throwable;
59             } else {
60                 throw new WorkbenchException(StatusUtil.newStatus(
61                         WorkbenchPlugin.PI_WORKBENCH, throwable));
62             }
63         }
64     }
65
66     public static void runWithPartInitExceptions(StartupRunnable r)
67             throws PartInitException {
68         workbench.getDisplay().syncExec(r);
69         Throwable JavaDoc throwable = r.getThrowable();
70         if (throwable != null) {
71             if (throwable instanceof Error JavaDoc) {
72                 throw (Error JavaDoc) throwable;
73             } else if (throwable instanceof RuntimeException JavaDoc) {
74                 throw (RuntimeException JavaDoc) throwable;
75             } else if (throwable instanceof WorkbenchException) {
76                 throw (PartInitException) throwable;
77             } else {
78                 throw new PartInitException(StatusUtil.newStatus(
79                         WorkbenchPlugin.PI_WORKBENCH, throwable));
80             }
81         }
82     }
83
84     public static void runWithThrowable(StartupRunnable r) throws Throwable JavaDoc {
85         workbench.getDisplay().syncExec(r);
86         Throwable JavaDoc throwable = r.getThrowable();
87         if (throwable != null) {
88             throw throwable;
89         }
90     }
91
92     public static void runWithoutExceptions(StartupRunnable r)
93             throws RuntimeException JavaDoc {
94         workbench.getDisplay().syncExec(r);
95         Throwable JavaDoc throwable = r.getThrowable();
96         if (throwable != null) {
97             if (throwable instanceof Error JavaDoc) {
98                 throw (Error JavaDoc) throwable;
99             } else if (throwable instanceof RuntimeException JavaDoc) {
100                 throw (RuntimeException JavaDoc) throwable;
101             } else {
102                 throw new RuntimeException JavaDoc(throwable);
103             }
104         }
105     }
106
107 }
108
Popular Tags