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