Mercurial > projects > dwt-addons
annotate dwtx/jface/util/Policy.d @ 71:4878bef4a38e
Some fixing
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Thu, 22 May 2008 04:03:58 +0200 |
parents | 46a6e0e6ccd4 |
children | 5df4896124c7 |
rev | line source |
---|---|
4 | 1 /******************************************************************************* |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
40
diff
changeset
|
2 * Copyright (c) 2004, 2008 IBM Corporation and others. |
4 | 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 | |
71 | 7 * |
4 | 8 * Contributors: |
9 * IBM Corporation - initial API and implementation | |
10 * Chris Gross (schtoo@schtoo.com) - support for ILogger 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.Policy; | |
16 | |
17 static import dwtx.core.runtime.Assert; | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
40
diff
changeset
|
18 import dwt.events.DisposeEvent; |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
40
diff
changeset
|
19 import dwt.events.DisposeListener; |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
40
diff
changeset
|
20 import dwt.widgets.Display; |
4 | 21 import dwtx.core.runtime.IStatus; |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
40
diff
changeset
|
22 import dwtx.core.runtime.Status; |
4 | 23 import dwtx.jface.dialogs.AnimatorFactory; |
24 import dwtx.jface.dialogs.ErrorSupportProvider; | |
25 | |
71 | 26 import dwtx.jface.util.StatusHandler; |
4 | 27 import dwtx.jface.util.ILogger; |
28 | |
29 import dwt.dwthelper.utils; | |
71 | 30 import dwt.dwthelper.Runnable; |
4 | 31 import tango.io.Stdout; |
32 | |
33 /** | |
34 * The Policy class handles settings for behaviour, debug flags and logging | |
35 * within JFace. | |
36 * | |
37 * @since 3.0 | |
38 */ | |
39 public class Policy { | |
40 | |
41 /** | |
42 * Constant for the the default setting for debug options. | |
43 */ | |
44 public static const bool DEFAULT = false; | |
45 | |
46 /** | |
47 * The unique identifier of the JFace plug-in. | |
48 */ | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
40
diff
changeset
|
49 public static const String JFACE = "dwtx.jface"; //$NON-NLS-1$ |
4 | 50 |
51 private static ILogger log; | |
52 | |
53 private static Comparator viewerComparator; | |
54 | |
55 private static AnimatorFactory animatorFactory; | |
56 | |
57 /** | |
58 * A flag to indicate whether unparented dialogs should be checked. | |
59 */ | |
60 public static bool DEBUG_DIALOG_NO_PARENT = DEFAULT; | |
61 | |
62 /** | |
63 * A flag to indicate whether actions are being traced. | |
64 */ | |
65 public static bool TRACE_ACTIONS = DEFAULT; | |
66 | |
67 /** | |
68 * A flag to indicate whether toolbars are being traced. | |
69 */ | |
70 | |
71 public static bool TRACE_TOOLBAR = DEFAULT; | |
72 | |
73 private static ErrorSupportProvider errorSupportProvider; | |
74 | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
40
diff
changeset
|
75 private static StatusHandler statusHandler; |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
40
diff
changeset
|
76 |
4 | 77 /** |
78 * Returns the dummy log to use if none has been set | |
79 */ | |
80 private static ILogger getDummyLog() { | |
81 return new class ILogger { | |
82 public void log(IStatus status) { | |
83 Stderr.formatln(status.getMessage()); | |
84 if (status.getException() !is null) { | |
85 auto e = status.getException(); | |
40
da5ad8eedf5d
debug prints, dwt.dwthelper restructure, ...
Frank Benoit <benoit@tionex.de>
parents:
4
diff
changeset
|
86 Stderr.formatln( "Exception of type {} in {}({}): {}", e.classinfo.name, e.file, e.line, e.msg ); |
4 | 87 foreach( msg; e.info ){ |
40
da5ad8eedf5d
debug prints, dwt.dwthelper restructure, ...
Frank Benoit <benoit@tionex.de>
parents:
4
diff
changeset
|
88 Stderr.formatln( " trc: {}", msg ); |
4 | 89 } |
90 // status.getException().printStackTrace(); | |
91 } | |
92 } | |
93 }; | |
94 } | |
95 | |
96 /** | |
97 * Sets the logger used by JFace to log errors. | |
98 * | |
99 * @param logger | |
100 * the logger to use, or <code>null</code> to use the default | |
101 * logger | |
102 * @since 3.1 | |
103 */ | |
104 public static void setLog(ILogger logger) { | |
105 log = logger; | |
106 } | |
107 | |
108 /** | |
109 * Returns the logger used by JFace to log errors. | |
110 * <p> | |
111 * The default logger prints the status to <code>System.err</code>. | |
112 * </p> | |
113 * | |
114 * @return the logger | |
115 * @since 3.1 | |
116 */ | |
117 public static ILogger getLog() { | |
118 if (log is null) { | |
119 log = getDummyLog(); | |
120 } | |
121 return log; | |
122 } | |
123 | |
124 /** | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
40
diff
changeset
|
125 * Sets the status handler used by JFace to handle statuses. |
71 | 126 * |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
40
diff
changeset
|
127 * @param status |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
40
diff
changeset
|
128 * the handler to use, or <code>null</code> to use the default |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
40
diff
changeset
|
129 * one |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
40
diff
changeset
|
130 * @since 3.4 |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
40
diff
changeset
|
131 */ |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
40
diff
changeset
|
132 public static void setStatusHandler(StatusHandler status) { |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
40
diff
changeset
|
133 statusHandler = status; |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
40
diff
changeset
|
134 } |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
40
diff
changeset
|
135 |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
40
diff
changeset
|
136 /** |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
40
diff
changeset
|
137 * Returns the status handler used by JFace to handle statuses. |
71 | 138 * |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
40
diff
changeset
|
139 * @return the status handler |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
40
diff
changeset
|
140 * @since 3.4 |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
40
diff
changeset
|
141 */ |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
40
diff
changeset
|
142 public static StatusHandler getStatusHandler() { |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
40
diff
changeset
|
143 if (statusHandler is null) { |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
40
diff
changeset
|
144 statusHandler = getDummyStatusHandler(); |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
40
diff
changeset
|
145 } |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
40
diff
changeset
|
146 return statusHandler; |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
40
diff
changeset
|
147 } |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
40
diff
changeset
|
148 |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
40
diff
changeset
|
149 private static StatusHandler getDummyStatusHandler() { |
71 | 150 return new class StatusHandler { |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
40
diff
changeset
|
151 private SafeRunnableDialog dialog; |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
40
diff
changeset
|
152 |
71 | 153 public void show(IStatus status, String title) { |
154 Runnable runnable = dgRunnable( (IStatus status_, SafeRunnableDialog* dialog_) { | |
155 if (*dialog_ is null || dialog_.getShell().isDisposed()) { | |
156 *dialog_ = new SafeRunnableDialog(status_); | |
157 *dialog_.create(); | |
158 *dialog_.getShell().addDisposeListener( | |
159 new class(dialog_) DisposeListener { | |
160 private SafeRunnableDialog* dialog__; | |
161 this( SafeRunnableDialog* a ){ | |
162 dialog__ = a; | |
163 } | |
164 public void widgetDisposed( | |
165 DisposeEvent e) { | |
166 *dialog__ = null; | |
167 } | |
168 }); | |
169 *dialog_.open(); | |
170 } else { | |
171 *dialog_.addStatus(status_); | |
172 *dialog_.refresh(); | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
40
diff
changeset
|
173 } |
71 | 174 }, status, &dialog); |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
40
diff
changeset
|
175 if (Display.getCurrent() !is null) { |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
40
diff
changeset
|
176 runnable.run(); |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
40
diff
changeset
|
177 } else { |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
40
diff
changeset
|
178 Display.getDefault().asyncExec(runnable); |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
40
diff
changeset
|
179 } |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
40
diff
changeset
|
180 } |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
40
diff
changeset
|
181 }; |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
40
diff
changeset
|
182 } |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
40
diff
changeset
|
183 |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
40
diff
changeset
|
184 /** |
4 | 185 * Return the default comparator used by JFace to sort strings. |
186 * | |
187 * @return a default comparator used by JFace to sort strings | |
188 */ | |
189 private static Comparator getDefaultComparator() { | |
190 return new class() Comparator { | |
191 /** | |
192 * Compares string s1 to string s2. | |
193 * | |
194 * @param s1 | |
195 * string 1 | |
196 * @param s2 | |
197 * string 2 | |
198 * @return Returns an integer value. Value is less than zero if | |
199 * source is less than target, value is zero if source and | |
200 * target are equal, value is greater than zero if source is | |
201 * greater than target. | |
202 * @exception ClassCastException | |
203 * the arguments cannot be cast to Strings. | |
204 */ | |
205 public int compare(Object s1, Object s2) { | |
206 auto a = (cast(ArrayWrapperString) s1).array; | |
207 auto b = (cast(ArrayWrapperString) s2).array; | |
208 return a < b; | |
209 } | |
210 }; | |
211 } | |
212 | |
213 /** | |
214 * Return the comparator used by JFace to sort strings. | |
215 * | |
216 * @return the comparator used by JFace to sort strings | |
217 * @since 3.2 | |
218 */ | |
219 public static Comparator getComparator() { | |
220 if (viewerComparator is null) { | |
221 viewerComparator = getDefaultComparator(); | |
222 } | |
223 return viewerComparator; | |
224 } | |
225 | |
226 /** | |
227 * Sets the comparator used by JFace to sort strings. | |
228 * | |
229 * @param comparator | |
230 * comparator used by JFace to sort strings | |
231 * @since 3.2 | |
232 */ | |
233 public static void setComparator(Comparator comparator) { | |
234 dwtx.core.runtime.Assert.Assert.isTrue(viewerComparator is null); | |
235 viewerComparator = comparator; | |
236 } | |
237 | |
238 /** | |
239 * Sets the animator factory used by JFace to create control animator | |
240 * instances. | |
241 * | |
242 * @param factory | |
243 * the AnimatorFactory to use. | |
244 * @since 3.2 | |
245 * @deprecated this is no longer in use as of 3.3 | |
246 */ | |
247 public static void setAnimatorFactory(AnimatorFactory factory) { | |
248 animatorFactory = factory; | |
249 } | |
250 | |
251 /** | |
252 * Returns the animator factory used by JFace to create control animator | |
253 * instances. | |
254 * | |
255 * @return the animator factory used to create control animator instances. | |
256 * @since 3.2 | |
257 * @deprecated this is no longer in use as of 3.3 | |
258 */ | |
259 public static AnimatorFactory getAnimatorFactory() { | |
260 if (animatorFactory is null) | |
261 animatorFactory = new AnimatorFactory(); | |
262 return animatorFactory; | |
263 } | |
264 | |
265 /** | |
266 * Set the error support provider for error dialogs. | |
267 * | |
268 * @param provider | |
269 * @since 3.3 | |
270 */ | |
271 public static void setErrorSupportProvider(ErrorSupportProvider provider) { | |
272 errorSupportProvider = provider; | |
273 } | |
274 | |
275 /** | |
276 * Return the ErrorSupportProvider for the receiver. | |
277 * | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
40
diff
changeset
|
278 * @return ErrorSupportProvider or <code>null</code> if this has not been |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
40
diff
changeset
|
279 * set |
4 | 280 * @since 3.3 |
281 */ | |
282 public static ErrorSupportProvider getErrorSupportProvider() { | |
283 return errorSupportProvider; | |
284 } | |
285 | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
40
diff
changeset
|
286 /** |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
40
diff
changeset
|
287 * Log the Exception to the logger. |
71 | 288 * |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
40
diff
changeset
|
289 * @param exception |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
40
diff
changeset
|
290 */ |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
40
diff
changeset
|
291 public static void logException(Exception exception) { |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
40
diff
changeset
|
292 getLog().log( |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
40
diff
changeset
|
293 new Status(IStatus.ERROR, JFACE, exception |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
40
diff
changeset
|
294 .getLocalizedMessage(), exception)); |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
40
diff
changeset
|
295 |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
40
diff
changeset
|
296 } |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
40
diff
changeset
|
297 |
4 | 298 } |