comparison org.eclipse.jface/src/org/eclipse/jface/util/SafeRunnable.d @ 12:bc29606a740c

Added dwt-addons in original directory structure of eclipse.org
author Frank Benoit <benoit@tionex.de>
date Sat, 14 Mar 2009 18:23:29 +0100
parents
children
comparison
equal deleted inserted replaced
11:43904fec5dca 12:bc29606a740c
1 /*******************************************************************************
2 * Copyright (c) 2000, 2008 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 * Port to the D programming language:
13 * Frank Benoit <benoit@tionex.de>
14 *******************************************************************************/
15 module org.eclipse.jface.util.SafeRunnable;
16
17 import org.eclipse.jface.util.ISafeRunnableRunner;
18 import org.eclipse.jface.util.SafeRunnableDialog;
19 import org.eclipse.jface.util.Policy;
20 import org.eclipse.core.runtime.ISafeRunnable;
21 import org.eclipse.core.runtime.IStatus;
22 import org.eclipse.core.runtime.OperationCanceledException;
23 import org.eclipse.core.runtime.Status;
24 import org.eclipse.jface.resource.JFaceResources;
25
26 import java.lang.all;
27 import java.util.Set;
28
29 /**
30 * Implements a default implementation of ISafeRunnable. The default
31 * implementation of <code>handleException</code> opens a dialog to show any
32 * errors as they accumulate.
33 * <p>
34 * This may be executed on any thread.
35 */
36 public abstract class SafeRunnable : ISafeRunnable {
37
38 private static bool ignoreErrors = false;
39
40 private static ISafeRunnableRunner runner;
41
42 private String message;
43
44 /**
45 * Creates a new instance of SafeRunnable with a default error message.
46 */
47 public this() {
48 // do nothing
49 }
50
51 /**
52 * Creates a new instance of SafeRunnable with the given error message.
53 *
54 * @param message
55 * the error message to use
56 */
57 public this(String message) {
58 this.message = message;
59 }
60
61 /*
62 * (non-Javadoc)
63 *
64 * @see org.eclipse.core.runtime.ISafeRunnable#handleException(java.lang.Throwable)
65 */
66 public void handleException(Exception e) {
67 // Workaround to avoid interactive error dialogs during
68 // automated testing
69 if (ignoreErrors)
70 return;
71
72 if (message is null)
73 message = JFaceResources.getString("SafeRunnable.errorMessage"); //$NON-NLS-1$
74
75 Policy.getStatusHandler().show(
76 new Status(IStatus.ERROR, Policy.JFACE, message, e),
77 JFaceResources.getString("SafeRunnable.errorMessage")); //$NON-NLS-1$
78 }
79
80 /**
81 * Flag to avoid interactive error dialogs during automated testing.
82 *
83 * @param flag
84 * @return true if errors should be ignored
85 * @deprecated use getIgnoreErrors()
86 */
87 public static bool getIgnoreErrors(bool flag) {
88 return ignoreErrors;
89 }
90
91 /**
92 * Flag to avoid interactive error dialogs during automated testing.
93 *
94 * @return true if errors should be ignored
95 *
96 * @since 3.0
97 */
98 public static bool getIgnoreErrors() {
99 return ignoreErrors;
100 }
101
102 /**
103 * Flag to avoid interactive error dialogs during automated testing.
104 *
105 * @param flag
106 * set to true if errors should be ignored
107 */
108 public static void setIgnoreErrors(bool flag) {
109 ignoreErrors = flag;
110 }
111
112 /**
113 * Returns the safe runnable runner.
114 *
115 * @return the safe runnable runner
116 *
117 * @since 3.1
118 */
119 public static ISafeRunnableRunner getRunner() {
120 if (runner is null) {
121 runner = createDefaultRunner();
122 }
123 return runner;
124 }
125
126 /**
127 * Creates the default safe runnable runner.
128 *
129 * @return the default safe runnable runner
130 * @since 3.1
131 */
132 private static ISafeRunnableRunner createDefaultRunner() {
133 return new class ISafeRunnableRunner {
134 public void run(ISafeRunnable code) {
135 try {
136 code.run();
137 } catch (Exception e) {
138 handleException(code, e);
139 // } catch (LinkageError e) {
140 // handleException(code, e);
141 }
142 }
143
144 private void handleException(ISafeRunnable code, Exception e) {
145 if (!(cast(OperationCanceledException)e )) {
146 try {
147 Policy.getLog()
148 .log(
149 new Status(IStatus.ERROR, Policy.JFACE,
150 IStatus.ERROR,
151 "Exception occurred", e)); //$NON-NLS-1$
152 } catch (Exception ex) {
153 ExceptionPrintStackTrace(e);
154 }
155 }
156 code.handleException(e);
157 }
158 };
159 }
160
161 /**
162 * Sets the safe runnable runner.
163 *
164 * @param runner
165 * the runner to set, or <code>null</code> to reset to the
166 * default runner
167 * @since 3.1
168 */
169 public static void setRunner(ISafeRunnableRunner runner) {
170 SafeRunnable.runner = runner;
171 }
172
173 /**
174 * Runs the given safe runnable using the safe runnable runner. This is a
175 * convenience method, equivalent to:
176 * <code>SafeRunnable.getRunner().run(runnable)</code>.
177 *
178 * @param runnable
179 * the runnable to run
180 * @since 3.1
181 */
182 public static void run(ISafeRunnable runnable) {
183 getRunner().run(runnable);
184 }
185
186 }
187
188 import tango.core.Tuple;
189 import tango.core.Traits;
190
191 class _DgSafeRunnableT(Dg,T...) : SafeRunnable {
192
193 alias ParameterTupleOf!(Dg) DgArgs;
194 static assert( is(DgArgs == Tuple!(T)),
195 "Delegate args not correct" );
196
197 Dg dg;
198 T t;
199
200 private this( Dg dg, T t ){
201 this.dg = dg;
202 static if( T.length > 0 ){
203 this.t = t;
204 }
205 }
206
207 public void run( ){
208 dg(t);
209 }
210 }
211
212 public _DgSafeRunnableT!(Dg,T) dgSafeRunnable(Dg,T...)( Dg dg, T args ){
213 return new _DgSafeRunnableT!(Dg,T)(dg,args);
214 }