KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > util > SafeRunnable


1 /*******************************************************************************
2  * Copyright (c) 2000, 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  * Chris Gross (schtoo@schtoo.com) - support for ISafeRunnableRunner added
11  * (bug 49497 [RCP] JFace dependency on org.eclipse.core.runtime enlarges standalone JFace applications)
12  *******************************************************************************/

13 package org.eclipse.jface.util;
14
15 import org.eclipse.core.runtime.ISafeRunnable;
16 import org.eclipse.core.runtime.IStatus;
17 import org.eclipse.core.runtime.OperationCanceledException;
18 import org.eclipse.core.runtime.Status;
19 import org.eclipse.jface.resource.JFaceResources;
20 import org.eclipse.swt.events.DisposeEvent;
21 import org.eclipse.swt.events.DisposeListener;
22 import org.eclipse.swt.widgets.Display;
23
24 /**
25  * Implements a default implementation of ISafeRunnable. The default
26  * implementation of <code>handleException</code> opens a message dialog.
27  * <p>
28  * <b>Note:<b> This class can open an error dialog and should not be used
29  * outside of the UI Thread.
30  * </p>
31  */

32 public abstract class SafeRunnable implements ISafeRunnable {
33
34     private static boolean ignoreErrors = false;
35
36     private static ISafeRunnableRunner runner;
37
38     private String JavaDoc message;
39
40     private static SafeRunnableDialog dialog;
41
42     /**
43      * Creates a new instance of SafeRunnable with a default error message.
44      */

45     public SafeRunnable() {
46         // do nothing
47
}
48
49     /**
50      * Creates a new instance of SafeRunnable with the given error message.
51      *
52      * @param message
53      * the error message to use
54      */

55     public SafeRunnable(String JavaDoc message) {
56         this.message = message;
57     }
58
59     
60     /* (non-Javadoc)
61      * @see org.eclipse.core.runtime.ISafeRunnable#handleException(java.lang.Throwable)
62      */

63     public void handleException(Throwable JavaDoc e) {
64         // Workaround to avoid interactive error dialogs during automated
65
// testing
66
if (!ignoreErrors) {
67             if (message == null)
68                 message = JFaceResources.getString("SafeRunnable.errorMessage"); //$NON-NLS-1$
69

70             final IStatus status = new Status(IStatus.ERROR, Policy.JFACE, message,e);
71
72             Runnable JavaDoc runnable = new Runnable JavaDoc() {
73                 public void run() {
74                     if (dialog == null || dialog.getShell().isDisposed()) {
75                         dialog = new SafeRunnableDialog(status);
76                         dialog.create();
77                         dialog.getShell().addDisposeListener(
78                                 new DisposeListener() {
79                                     public void widgetDisposed(DisposeEvent e) {
80                                         dialog = null;
81                                     }
82                                 });
83                         dialog.open();
84                     } else {
85                         dialog.addStatus(status);
86                         dialog.refresh();
87                     }
88                 }
89             };
90             if (Display.getCurrent() != null) {
91                 runnable.run();
92             } else {
93                 Display.getDefault().asyncExec(runnable);
94             }
95         }
96     }
97
98     /**
99      * Flag to avoid interactive error dialogs during automated testing.
100      *
101      * @param flag
102      * @return true if errors should be ignored
103      * @deprecated use getIgnoreErrors()
104      */

105     public static boolean getIgnoreErrors(boolean flag) {
106         return ignoreErrors;
107     }
108
109     /**
110      * Flag to avoid interactive error dialogs during automated testing.
111      *
112      * @return true if errors should be ignored
113      *
114      * @since 3.0
115      */

116     public static boolean getIgnoreErrors() {
117         return ignoreErrors;
118     }
119
120     /**
121      * Flag to avoid interactive error dialogs during automated testing.
122      *
123      * @param flag
124      * set to true if errors should be ignored
125      */

126     public static void setIgnoreErrors(boolean flag) {
127         ignoreErrors = flag;
128     }
129
130     /**
131      * Returns the safe runnable runner.
132      *
133      * @return the safe runnable runner
134      *
135      * @since 3.1
136      */

137     public static ISafeRunnableRunner getRunner() {
138         if (runner == null) {
139             runner = createDefaultRunner();
140         }
141         return runner;
142     }
143
144     /**
145      * Creates the default safe runnable runner.
146      *
147      * @return the default safe runnable runner
148      * @since 3.1
149      */

150     private static ISafeRunnableRunner createDefaultRunner() {
151         return new ISafeRunnableRunner() {
152             public void run(ISafeRunnable code) {
153                 try {
154                     code.run();
155                 } catch (Exception JavaDoc e) {
156                     handleException(code, e);
157                 } catch (LinkageError JavaDoc e) {
158                     handleException(code, e);
159                 }
160             }
161
162             private void handleException(ISafeRunnable code, Throwable JavaDoc e) {
163                 if (!(e instanceof OperationCanceledException)) {
164                     try {
165                         Policy.getLog().log(
166                                 new Status(IStatus.ERROR, Policy.JFACE,
167                                         IStatus.ERROR, "Exception occurred", e)); //$NON-NLS-1$
168
} catch (Exception JavaDoc ex) {
169                         e.printStackTrace();
170                     }
171                 }
172                 code.handleException(e);
173             }
174         };
175     }
176
177     /**
178      * Sets the safe runnable runner.
179      *
180      * @param runner
181      * the runner to set, or <code>null</code> to reset to the
182      * default runner
183      * @since 3.1
184      */

185     public static void setRunner(ISafeRunnableRunner runner) {
186         SafeRunnable.runner = runner;
187     }
188
189     /**
190      * Runs the given safe runnable using the safe runnable runner. This is a
191      * convenience method, equivalent to:
192      * <code>SafeRunnable.getRunner().run(runnable)</code>.
193      *
194      * @param runnable
195      * the runnable to run
196      * @since 3.1
197      */

198     public static void run(ISafeRunnable runnable) {
199         getRunner().run(runnable);
200     }
201
202 }
203
Popular Tags