comparison dwtx/jface/util/SafeRunnable.d @ 10:b6c35faf97c8

Viewers
author Frank Benoit <benoit@tionex.de>
date Mon, 31 Mar 2008 00:47:19 +0200
parents
children 644f1334b451
comparison
equal deleted inserted replaced
9:6c14e54dfc11 10:b6c35faf97c8
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 dwtx.core.runtime enlarges standalone JFace applications)
12 * Port to the D programming language:
13 * Frank Benoit <benoit@tionex.de>
14 *******************************************************************************/
15 module dwtx.jface.util.SafeRunnable;
16
17 import dwtx.jface.util.ISafeRunnableRunner;
18 import dwtx.jface.util.SafeRunnableDialog;
19 import dwtx.jface.util.Policy;
20
21 import dwt.events.DisposeEvent;
22 import dwt.events.DisposeListener;
23 import dwt.widgets.Display;
24 import dwtx.core.runtime.ISafeRunnable;
25 import dwtx.core.runtime.IStatus;
26 import dwtx.core.runtime.OperationCanceledException;
27 import dwtx.core.runtime.Status;
28 import dwtx.jface.resource.JFaceResources;
29
30 import dwt.dwthelper.utils;
31 import dwt.dwthelper.Runnable;
32
33 /**
34 * Implements a default implementation of ISafeRunnable. The default
35 * implementation of <code>handleException</code> opens a message dialog.
36 * <p>
37 * <b>Note:<b> This class can open an error dialog and should not be used
38 * outside of the UI Thread.
39 * </p>
40 */
41 public abstract class SafeRunnable : ISafeRunnable {
42
43 private static bool ignoreErrors = false;
44
45 private static ISafeRunnableRunner runner;
46
47 private String message;
48
49 private static SafeRunnableDialog dialog;
50
51 /**
52 * Creates a new instance of SafeRunnable with a default error message.
53 */
54 public this() {
55 // do nothing
56 }
57
58 /**
59 * Creates a new instance of SafeRunnable with the given error message.
60 *
61 * @param message
62 * the error message to use
63 */
64 public this(String message) {
65 this.message = message;
66 }
67
68
69 /* (non-Javadoc)
70 * @see dwtx.core.runtime.ISafeRunnable#handleException(java.lang.Throwable)
71 */
72 public void handleException(Exception e) {
73 // Workaround to avoid interactive error dialogs during automated
74 // testing
75 if (!ignoreErrors) {
76 if (message is null)
77 message = JFaceResources.getString("SafeRunnable.errorMessage"); //$NON-NLS-1$
78
79 Runnable runnable = new class Runnable {
80 IStatus status;
81 this(){
82 status = new Status(IStatus.ERROR, Policy.JFACE, message,e);
83 }
84 public void run() {
85 if (dialog is null || dialog.getShell().isDisposed()) {
86 dialog = new SafeRunnableDialog(status);
87 dialog.create();
88 dialog.getShell().addDisposeListener(
89 new class DisposeListener {
90 public void widgetDisposed(DisposeEvent e) {
91 dialog = null;
92 }
93 });
94 dialog.open();
95 } else {
96 dialog.addStatus(status);
97 dialog.refresh();
98 }
99 }
100 };
101 if (Display.getCurrent() !is null) {
102 runnable.run();
103 } else {
104 Display.getDefault().asyncExec(runnable);
105 }
106 }
107 }
108
109 /**
110 * Flag to avoid interactive error dialogs during automated testing.
111 *
112 * @param flag
113 * @return true if errors should be ignored
114 * @deprecated use getIgnoreErrors()
115 */
116 public static bool getIgnoreErrors(bool flag) {
117 return ignoreErrors;
118 }
119
120 /**
121 * Flag to avoid interactive error dialogs during automated testing.
122 *
123 * @return true if errors should be ignored
124 *
125 * @since 3.0
126 */
127 public static bool getIgnoreErrors() {
128 return ignoreErrors;
129 }
130
131 /**
132 * Flag to avoid interactive error dialogs during automated testing.
133 *
134 * @param flag
135 * set to true if errors should be ignored
136 */
137 public static void setIgnoreErrors(bool flag) {
138 ignoreErrors = flag;
139 }
140
141 /**
142 * Returns the safe runnable runner.
143 *
144 * @return the safe runnable runner
145 *
146 * @since 3.1
147 */
148 public static ISafeRunnableRunner getRunner() {
149 if (runner is null) {
150 runner = createDefaultRunner();
151 }
152 return runner;
153 }
154
155 /**
156 * Creates the default safe runnable runner.
157 *
158 * @return the default safe runnable runner
159 * @since 3.1
160 */
161 private static ISafeRunnableRunner createDefaultRunner() {
162 return new class ISafeRunnableRunner {
163 public void run(ISafeRunnable code) {
164 try {
165 code.run();
166 } catch (Exception e) {
167 handleException(code, e);
168 // } catch (LinkageError e) {
169 // handleException(code, e);
170 }
171 }
172
173 private void handleException(ISafeRunnable code, Exception e) {
174 if (!(cast(OperationCanceledException)e )) {
175 try {
176 Policy.getLog().log(
177 new Status(IStatus.ERROR, Policy.JFACE,
178 IStatus.ERROR, "Exception occurred", e)); //$NON-NLS-1$
179 } catch (Exception ex) {
180 ExceptionPrintStackTrace(e);
181 }
182 }
183 code.handleException(e);
184 }
185 };
186 }
187
188 /**
189 * Sets the safe runnable runner.
190 *
191 * @param runner
192 * the runner to set, or <code>null</code> to reset to the
193 * default runner
194 * @since 3.1
195 */
196 public static void setRunner(ISafeRunnableRunner runner) {
197 SafeRunnable.runner = runner;
198 }
199
200 /**
201 * Runs the given safe runnable using the safe runnable runner. This is a
202 * convenience method, equivalent to:
203 * <code>SafeRunnable.getRunner().run(runnable)</code>.
204 *
205 * @param runnable
206 * the runnable to run
207 * @since 3.1
208 */
209 public static void run(ISafeRunnable runnable) {
210 getRunner().run(runnable);
211 }
212
213 }