Mercurial > projects > dwt-addons
annotate dwtx/jface/util/Policy.d @ 196:66bceed20048
Fix: NullPointer access.
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Tue, 10 Feb 2009 17:12:01 +0100 |
parents | 7ffeace6c47f |
children |
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 ); |
196 | 88 if( e.info !is null ){ |
89 foreach( msg; e.info ){ | |
90 Stderr.formatln( " trc: {}", msg ); | |
91 } | |
4 | 92 } |
93 // status.getException().printStackTrace(); | |
94 } | |
95 } | |
96 }; | |
97 } | |
98 | |
99 /** | |
100 * Sets the logger used by JFace to log errors. | |
101 * | |
102 * @param logger | |
103 * the logger to use, or <code>null</code> to use the default | |
104 * logger | |
105 * @since 3.1 | |
106 */ | |
107 public static void setLog(ILogger logger) { | |
108 log = logger; | |
109 } | |
110 | |
111 /** | |
112 * Returns the logger used by JFace to log errors. | |
113 * <p> | |
114 * The default logger prints the status to <code>System.err</code>. | |
115 * </p> | |
116 * | |
117 * @return the logger | |
118 * @since 3.1 | |
119 */ | |
120 public static ILogger getLog() { | |
121 if (log is null) { | |
122 log = getDummyLog(); | |
123 } | |
124 return log; | |
125 } | |
126 | |
127 /** | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
40
diff
changeset
|
128 * Sets the status handler used by JFace to handle statuses. |
71 | 129 * |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
40
diff
changeset
|
130 * @param status |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
40
diff
changeset
|
131 * 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
|
132 * one |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
40
diff
changeset
|
133 * @since 3.4 |
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 public static void setStatusHandler(StatusHandler status) { |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
40
diff
changeset
|
136 statusHandler = status; |
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 |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
40
diff
changeset
|
139 /** |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
40
diff
changeset
|
140 * Returns the status handler used by JFace to handle statuses. |
71 | 141 * |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
40
diff
changeset
|
142 * @return the status handler |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
40
diff
changeset
|
143 * @since 3.4 |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
40
diff
changeset
|
144 */ |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
40
diff
changeset
|
145 public static StatusHandler getStatusHandler() { |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
40
diff
changeset
|
146 if (statusHandler is null) { |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
40
diff
changeset
|
147 statusHandler = getDummyStatusHandler(); |
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 return statusHandler; |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
40
diff
changeset
|
150 } |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
40
diff
changeset
|
151 |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
40
diff
changeset
|
152 private static StatusHandler getDummyStatusHandler() { |
71 | 153 return new class StatusHandler { |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
40
diff
changeset
|
154 private SafeRunnableDialog dialog; |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
40
diff
changeset
|
155 |
71 | 156 public void show(IStatus status, String title) { |
72
5df4896124c7
JFace and its examples do compile
Frank Benoit <benoit@tionex.de>
parents:
71
diff
changeset
|
157 Runnable runnable = dgRunnable( (IStatus status_) { |
5df4896124c7
JFace and its examples do compile
Frank Benoit <benoit@tionex.de>
parents:
71
diff
changeset
|
158 if (dialog is null || dialog.getShell().isDisposed()) { |
5df4896124c7
JFace and its examples do compile
Frank Benoit <benoit@tionex.de>
parents:
71
diff
changeset
|
159 dialog = new SafeRunnableDialog(status_); |
5df4896124c7
JFace and its examples do compile
Frank Benoit <benoit@tionex.de>
parents:
71
diff
changeset
|
160 dialog.create(); |
5df4896124c7
JFace and its examples do compile
Frank Benoit <benoit@tionex.de>
parents:
71
diff
changeset
|
161 dialog.getShell().addDisposeListener( |
5df4896124c7
JFace and its examples do compile
Frank Benoit <benoit@tionex.de>
parents:
71
diff
changeset
|
162 new class DisposeListener { |
71 | 163 public void widgetDisposed( |
164 DisposeEvent e) { | |
72
5df4896124c7
JFace and its examples do compile
Frank Benoit <benoit@tionex.de>
parents:
71
diff
changeset
|
165 dialog = null; |
71 | 166 } |
167 }); | |
72
5df4896124c7
JFace and its examples do compile
Frank Benoit <benoit@tionex.de>
parents:
71
diff
changeset
|
168 dialog.open(); |
71 | 169 } else { |
72
5df4896124c7
JFace and its examples do compile
Frank Benoit <benoit@tionex.de>
parents:
71
diff
changeset
|
170 dialog.addStatus(status_); |
5df4896124c7
JFace and its examples do compile
Frank Benoit <benoit@tionex.de>
parents:
71
diff
changeset
|
171 dialog.refresh(); |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
40
diff
changeset
|
172 } |
72
5df4896124c7
JFace and its examples do compile
Frank Benoit <benoit@tionex.de>
parents:
71
diff
changeset
|
173 }, status ); |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
40
diff
changeset
|
174 if (Display.getCurrent() !is null) { |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
40
diff
changeset
|
175 runnable.run(); |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
40
diff
changeset
|
176 } else { |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
40
diff
changeset
|
177 Display.getDefault().asyncExec(runnable); |
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 } |
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 /** |
4 | 184 * Return the default comparator used by JFace to sort strings. |
185 * | |
186 * @return a default comparator used by JFace to sort strings | |
187 */ | |
188 private static Comparator getDefaultComparator() { | |
189 return new class() Comparator { | |
190 /** | |
191 * Compares string s1 to string s2. | |
192 * | |
193 * @param s1 | |
194 * string 1 | |
195 * @param s2 | |
196 * string 2 | |
197 * @return Returns an integer value. Value is less than zero if | |
198 * source is less than target, value is zero if source and | |
199 * target are equal, value is greater than zero if source is | |
200 * greater than target. | |
201 * @exception ClassCastException | |
202 * the arguments cannot be cast to Strings. | |
203 */ | |
204 public int compare(Object s1, Object s2) { | |
205 auto a = (cast(ArrayWrapperString) s1).array; | |
206 auto b = (cast(ArrayWrapperString) s2).array; | |
207 return a < b; | |
208 } | |
209 }; | |
210 } | |
211 | |
212 /** | |
213 * Return the comparator used by JFace to sort strings. | |
214 * | |
215 * @return the comparator used by JFace to sort strings | |
216 * @since 3.2 | |
217 */ | |
218 public static Comparator getComparator() { | |
219 if (viewerComparator is null) { | |
220 viewerComparator = getDefaultComparator(); | |
221 } | |
222 return viewerComparator; | |
223 } | |
224 | |
225 /** | |
226 * Sets the comparator used by JFace to sort strings. | |
227 * | |
228 * @param comparator | |
229 * comparator used by JFace to sort strings | |
230 * @since 3.2 | |
231 */ | |
232 public static void setComparator(Comparator comparator) { | |
233 dwtx.core.runtime.Assert.Assert.isTrue(viewerComparator is null); | |
234 viewerComparator = comparator; | |
235 } | |
236 | |
237 /** | |
238 * Sets the animator factory used by JFace to create control animator | |
239 * instances. | |
240 * | |
241 * @param factory | |
242 * the AnimatorFactory to use. | |
243 * @since 3.2 | |
244 * @deprecated this is no longer in use as of 3.3 | |
245 */ | |
246 public static void setAnimatorFactory(AnimatorFactory factory) { | |
247 animatorFactory = factory; | |
248 } | |
249 | |
250 /** | |
251 * Returns the animator factory used by JFace to create control animator | |
252 * instances. | |
253 * | |
254 * @return the animator factory used to create control animator instances. | |
255 * @since 3.2 | |
256 * @deprecated this is no longer in use as of 3.3 | |
257 */ | |
258 public static AnimatorFactory getAnimatorFactory() { | |
259 if (animatorFactory is null) | |
260 animatorFactory = new AnimatorFactory(); | |
261 return animatorFactory; | |
262 } | |
263 | |
264 /** | |
265 * Set the error support provider for error dialogs. | |
266 * | |
267 * @param provider | |
268 * @since 3.3 | |
269 */ | |
270 public static void setErrorSupportProvider(ErrorSupportProvider provider) { | |
271 errorSupportProvider = provider; | |
272 } | |
273 | |
274 /** | |
275 * Return the ErrorSupportProvider for the receiver. | |
276 * | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
40
diff
changeset
|
277 * @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
|
278 * set |
4 | 279 * @since 3.3 |
280 */ | |
281 public static ErrorSupportProvider getErrorSupportProvider() { | |
282 return errorSupportProvider; | |
283 } | |
284 | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
40
diff
changeset
|
285 /** |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
40
diff
changeset
|
286 * Log the Exception to the logger. |
71 | 287 * |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
40
diff
changeset
|
288 * @param exception |
90 | 289 * @since 3.4 |
70
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( |
72
5df4896124c7
JFace and its examples do compile
Frank Benoit <benoit@tionex.de>
parents:
71
diff
changeset
|
293 new Status(IStatus.ERROR, JFACE, ExceptionGetLocalizedMessage( exception ) |
5df4896124c7
JFace and its examples do compile
Frank Benoit <benoit@tionex.de>
parents:
71
diff
changeset
|
294 , exception)); |
70
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 } |