Mercurial > projects > dwt-addons
annotate dwtx/jface/util/SafeRunnable.d @ 104:04b47443bb01
Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections.
These new wrappers now use the tango.util.containers instead of the tango.util.collections.
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Thu, 07 Aug 2008 15:01:33 +0200 |
parents | 46a6e0e6ccd4 |
children |
rev | line source |
---|---|
10 | 1 /******************************************************************************* |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
39
diff
changeset
|
2 * Copyright (c) 2000, 2008 IBM Corporation and others. |
10 | 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 import dwtx.core.runtime.ISafeRunnable; | |
21 import dwtx.core.runtime.IStatus; | |
22 import dwtx.core.runtime.OperationCanceledException; | |
23 import dwtx.core.runtime.Status; | |
24 import dwtx.jface.resource.JFaceResources; | |
25 | |
26 import dwt.dwthelper.utils; | |
27 import dwt.dwthelper.Runnable; | |
28 | |
29 /** | |
30 * Implements a default implementation of ISafeRunnable. The default | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
39
diff
changeset
|
31 * implementation of <code>handleException</code> opens a dialog to show any |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
39
diff
changeset
|
32 * errors as they accumulate. |
10 | 33 * <p> |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
39
diff
changeset
|
34 * This may be executed on any thread. |
10 | 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 | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
39
diff
changeset
|
61 /* |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
39
diff
changeset
|
62 * (non-Javadoc) |
104
04b47443bb01
Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections.
Frank Benoit <benoit@tionex.de>
parents:
70
diff
changeset
|
63 * |
10 | 64 * @see dwtx.core.runtime.ISafeRunnable#handleException(java.lang.Throwable) |
65 */ | |
66 public void handleException(Exception e) { | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
39
diff
changeset
|
67 // Workaround to avoid interactive error dialogs during |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
39
diff
changeset
|
68 // automated testing |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
39
diff
changeset
|
69 if (ignoreErrors) |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
39
diff
changeset
|
70 return; |
10 | 71 |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
39
diff
changeset
|
72 if (message is null) |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
39
diff
changeset
|
73 message = JFaceResources.getString("SafeRunnable.errorMessage"); //$NON-NLS-1$ |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
39
diff
changeset
|
74 |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
39
diff
changeset
|
75 Policy.getStatusHandler().show( |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
39
diff
changeset
|
76 new Status(IStatus.ERROR, Policy.JFACE, message, e), |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
39
diff
changeset
|
77 JFaceResources.getString("SafeRunnable.errorMessage")); //$NON-NLS-1$ |
10 | 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 { | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
39
diff
changeset
|
147 Policy.getLog() |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
39
diff
changeset
|
148 .log( |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
39
diff
changeset
|
149 new Status(IStatus.ERROR, Policy.JFACE, |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
39
diff
changeset
|
150 IStatus.ERROR, |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
39
diff
changeset
|
151 "Exception occurred", e)); //$NON-NLS-1$ |
10 | 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 } | |
104
04b47443bb01
Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections.
Frank Benoit <benoit@tionex.de>
parents:
70
diff
changeset
|
187 |
04b47443bb01
Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections.
Frank Benoit <benoit@tionex.de>
parents:
70
diff
changeset
|
188 import tango.core.Tuple; |
04b47443bb01
Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections.
Frank Benoit <benoit@tionex.de>
parents:
70
diff
changeset
|
189 import tango.core.Traits; |
04b47443bb01
Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections.
Frank Benoit <benoit@tionex.de>
parents:
70
diff
changeset
|
190 |
04b47443bb01
Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections.
Frank Benoit <benoit@tionex.de>
parents:
70
diff
changeset
|
191 class _DgSafeRunnableT(Dg,T...) : SafeRunnable { |
04b47443bb01
Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections.
Frank Benoit <benoit@tionex.de>
parents:
70
diff
changeset
|
192 |
04b47443bb01
Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections.
Frank Benoit <benoit@tionex.de>
parents:
70
diff
changeset
|
193 alias ParameterTupleOf!(Dg) DgArgs; |
04b47443bb01
Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections.
Frank Benoit <benoit@tionex.de>
parents:
70
diff
changeset
|
194 static assert( is(DgArgs == Tuple!(T)), |
04b47443bb01
Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections.
Frank Benoit <benoit@tionex.de>
parents:
70
diff
changeset
|
195 "Delegate args not correct" ); |
04b47443bb01
Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections.
Frank Benoit <benoit@tionex.de>
parents:
70
diff
changeset
|
196 |
04b47443bb01
Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections.
Frank Benoit <benoit@tionex.de>
parents:
70
diff
changeset
|
197 Dg dg; |
04b47443bb01
Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections.
Frank Benoit <benoit@tionex.de>
parents:
70
diff
changeset
|
198 T t; |
04b47443bb01
Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections.
Frank Benoit <benoit@tionex.de>
parents:
70
diff
changeset
|
199 |
04b47443bb01
Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections.
Frank Benoit <benoit@tionex.de>
parents:
70
diff
changeset
|
200 private this( Dg dg, T t ){ |
04b47443bb01
Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections.
Frank Benoit <benoit@tionex.de>
parents:
70
diff
changeset
|
201 this.dg = dg; |
04b47443bb01
Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections.
Frank Benoit <benoit@tionex.de>
parents:
70
diff
changeset
|
202 static if( T.length > 0 ){ |
04b47443bb01
Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections.
Frank Benoit <benoit@tionex.de>
parents:
70
diff
changeset
|
203 this.t = t; |
04b47443bb01
Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections.
Frank Benoit <benoit@tionex.de>
parents:
70
diff
changeset
|
204 } |
04b47443bb01
Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections.
Frank Benoit <benoit@tionex.de>
parents:
70
diff
changeset
|
205 } |
04b47443bb01
Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections.
Frank Benoit <benoit@tionex.de>
parents:
70
diff
changeset
|
206 |
04b47443bb01
Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections.
Frank Benoit <benoit@tionex.de>
parents:
70
diff
changeset
|
207 public void run( ){ |
04b47443bb01
Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections.
Frank Benoit <benoit@tionex.de>
parents:
70
diff
changeset
|
208 dg(t); |
04b47443bb01
Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections.
Frank Benoit <benoit@tionex.de>
parents:
70
diff
changeset
|
209 } |
04b47443bb01
Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections.
Frank Benoit <benoit@tionex.de>
parents:
70
diff
changeset
|
210 } |
04b47443bb01
Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections.
Frank Benoit <benoit@tionex.de>
parents:
70
diff
changeset
|
211 |
04b47443bb01
Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections.
Frank Benoit <benoit@tionex.de>
parents:
70
diff
changeset
|
212 public _DgSafeRunnableT!(Dg,T) dgSafeRunnable(Dg,T...)( Dg dg, T args ){ |
04b47443bb01
Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections.
Frank Benoit <benoit@tionex.de>
parents:
70
diff
changeset
|
213 return new _DgSafeRunnableT!(Dg,T)(dg,args); |
04b47443bb01
Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections.
Frank Benoit <benoit@tionex.de>
parents:
70
diff
changeset
|
214 } |