Project

General

Profile

Download (13.6 KB) Statistics
| Branch: | Tag: | Revision:
1
package eu.etaxonomy.taxeditor.model;
2

    
3
import java.io.PrintWriter;
4
import java.io.StringWriter;
5

    
6
import org.apache.log4j.Logger;
7
import org.eclipse.core.runtime.IStatus;
8
import org.eclipse.core.runtime.MultiStatus;
9
import org.eclipse.core.runtime.Status;
10
import org.eclipse.jface.dialogs.MessageDialog;
11
import org.eclipse.swt.widgets.Display;
12

    
13
import eu.etaxonomy.cdm.persistence.hibernate.permission.SecurityExceptionUtils;
14
import eu.etaxonomy.taxeditor.store.internal.TaxeditorStorePlugin;
15

    
16
/**
17
 * Utility class which handles all the messaging information generated by the
18
 * Editor.
19
 *
20
 * This includes logging as well as dialogs.
21
 *
22
 * @author cmathew
23
 *
24
 */
25
public class MessagingUtils {
26
	public final static String UNEXPECTED_ERROR_MESSAGE = "This is an unexpected error.";
27
	public final static String CONTACT_MESSAGE = System.getProperty("line.separator") +  "Please contact EDIT Support (EditSupport@bgbm.org) with the error trace below (click on the 'Details' button).";
28

    
29
	/**
30
	 * Gets the Log4J logger for a given class
31
	 *
32
	 * @param clazz
33
	 *            a {@link java.lang.Class} object.
34
	 * @return a {@link org.apache.log4j.Logger} object.
35
	 */
36
	public static Logger getLog4JLogger(Class clazz) {
37
		return Logger.getLogger(clazz);
38
	}
39

    
40
	/**
41
	 * Logs details from a given Status object
42
	 *
43
	 * @param status
44
	 * 				a {@link org.eclipse.core.runtime.IStatus} object.
45
	 */
46
	private static void log(IStatus status) {
47
		TaxeditorStorePlugin.getDefault().getLog().log(status);
48
	}
49

    
50
	/**
51
	 * Logs a status object as information.
52
	 *
53
	 * @param status
54
	 *            a {@link org.eclipse.core.runtime.IStatus} object.
55
	 */
56
	public static void info(IStatus status) {
57
		log(status);
58
	}
59

    
60
	/**
61
	 * Logs a string as information.
62
	 *
63
	 * @param message
64
	 *            a {@link java.lang.String} object.
65
	 */
66
	public static void info(String message) {
67
		IStatus status = new Status(IStatus.INFO, AbstractUtility.getPluginId(), message);
68
		info(status);
69
	}
70

    
71
	/**
72
	 * Logs an exception from a given source as a warning.
73
	 *
74
	 * @param source
75
	 * @param t
76
	 */
77
	public static void warn(Class source, Throwable t) {
78
		IStatus status = new Status(IStatus.WARNING, AbstractUtility.getPluginId(), t.getMessage(), t);
79
		MessagingUtils.getLog4JLogger(source).warn(t);
80
		log(status);
81
	}
82

    
83
	/**
84
	 * Logs a status object from a given source as a warning.
85
	 *
86
	 * @param source
87
	 * @param status
88
	 */
89
	public static void warn(Class source, IStatus status) {
90
		MessagingUtils.getLog4JLogger(source).warn(status.getMessage(), status.getException());
91
		log(status);
92
	}
93

    
94
	/**
95
	 * Logs a string from a given source as a warning.
96
	 *
97
	 *
98
	 * @param source
99
	 *            a {@link java.lang.Class} object.
100
	 * @param message
101
	 *            a {@link java.lang.String} object.
102
	 */
103
	public static void warn(Class source, String message) {
104
		IStatus status = new Status(IStatus.WARNING, AbstractUtility.getPluginId(), message);
105
		MessagingUtils.getLog4JLogger(source).warn(message);
106
		log(status);
107
	}
108

    
109
	/**
110
	 * Logs a status object from a given source as an error.
111
	 *
112
	 *
113
	 * @param source
114
	 *            a {@link java.lang.Class} object.
115
	 * @param status
116
	 *            a {@link org.eclipse.core.runtime.IStatus} object.
117
	 */
118
	public static void error(Class source, IStatus status) {
119
		getLog4JLogger(source)
120
				.error(status.getMessage(), status.getException());
121
		log(status);
122
	}
123

    
124
	/**
125
	 * Logs a string and exception from a given source as an error.
126
	 *
127
	 *
128
	 * @param source
129
	 *            a {@link java.lang.Class} object.
130
	 * @param message
131
	 *            a {@link java.lang.String} object.
132
	 * @param t
133
	 *            a {@link java.lang.Throwable} object.
134
	 */
135
	public static void error(Class source, String message, Throwable t) {
136
		IStatus status = new Status(IStatus.ERROR, AbstractUtility.getPluginId(), message, t);
137
		error(source, status);
138
	}
139

    
140

    
141

    
142
	/**
143
	 * Logs an exception from a given source as an error.
144
	 *
145
	 *
146
	 * @param source
147
	 *            a {@link java.lang.Class} object.
148
	 * @param t
149
	 *            a {@link java.lang.Throwable} object.
150
	 */
151
	public static void error(Class source, Throwable t) {
152
		error(source.getClass(), t.getMessage(), t);
153
	}
154

    
155
	/**
156
	 * Displays a {@link eu.etaxonomy.taxeditor.model.CdmErrorDialog}.
157
	 *
158
	 * @param title
159
	 *            a {@link java.lang.String} object.
160
	 * @param source
161
	 *            a {@link java.lang.Object} object.
162
	 * @param status
163
	 *            a {@link org.eclipse.core.runtime.IStatus} object.
164
	 */
165
	public static void errorDialog(final String title,
166
			final Object source,
167
			final String message,
168
			final IStatus status) {
169

    
170
		Display.getDefault().asyncExec(new Runnable() {
171

    
172
			@Override
173
			public void run() {
174
				CdmErrorDialog ced = new CdmErrorDialog(AbstractUtility.getShell(), title, message, status);
175
				ced.open();
176
				Class<? extends Object> clazz = source != null ? source
177
						.getClass() : this.getClass();
178
						error(clazz, status);
179
			}
180
		});
181
	}
182

    
183
	/**
184
	 * Displays a {@link eu.etaxonomy.taxeditor.model.CdmErrorDialog}.
185
	 *
186
	 * @param title
187
	 * @param source
188
	 * @param message
189
	 * @param pluginId
190
	 * @param t
191
	 */
192
	public static void errorDialog(final String title,
193
			final Object source,
194
			final String message,
195
			final String pluginId,
196
			final Throwable t,
197
			boolean addContactMesg) {
198
		// Usually the status contains only the first line of the stack trace.
199
		// For the unexpected messages we need the entire stack trace so we
200
		// create a new status with the entire stacktrace
201
		StringWriter sw = new StringWriter();
202
		t.printStackTrace(new PrintWriter(sw));
203
		String finalMessage = t.getMessage();
204
		if(addContactMesg) {
205
			finalMessage += MessagingUtils.CONTACT_MESSAGE;
206
		}
207
		IStatus status = new Status(IStatus.ERROR,
208
				pluginId,
209
				finalMessage,
210
				new Exception(sw.toString()));
211
		errorDialog(title, source, message, status);
212
	}
213

    
214

    
215

    
216
	/**
217
	 * Displays a {@link eu.etaxonomy.taxeditor.model.CdmErrorDialog}.
218
	 *
219
	 * @param title
220
	 *            a {@link java.lang.String} object.
221
	 * @param source
222
	 *            a {@link java.lang.Object} object.
223
	 * @param status
224
	 *            a {@link org.eclipse.core.runtime.IStatus} object.
225
	 */
226
	public static void errorDialog(final String title,
227
			final Object source,
228
			final String message,
229
			final IStatus status,
230
			final boolean showStackTrace) {
231
		if(showStackTrace && status.getException() != null) {
232
			errorDialog(title, source,  status.getPlugin(), message, status.getException(),true);
233
		} else {
234
			errorDialog(title, source, message, status);
235
		}
236
	}
237

    
238

    
239
	/**
240
	 * Displays a {@link eu.etaxonomy.taxeditor.model.CdmErrorDialog}.
241
	 *
242
	 * @param title
243
	 *            a {@link java.lang.String} object.
244
	 * @param source
245
	 *            a {@link java.lang.Object} object.
246
	 * @param status
247
	 *            a {@link org.eclipse.core.runtime.IStatus} object.
248
	 */
249
	public static void errorDialog(final String title, final Object source,
250
			final IStatus status) {
251
		errorDialog(title, source, null, status);
252
	}
253

    
254
	/**
255
	 * Displays a dialog for an exception occurring in an operation.
256
	 *
257
	 * This will be either a {@link eu.etaxonomy.taxeditor.model.CdmErrorDialog} in case of a
258
	 * security runtime exception or a warning {@link org.eclipse.jface.dialogs.MessageDialog} in
259
	 * case of any other exception.
260
	 *
261
	 * @param title
262
	 *            a {@link java.lang.String} object.
263
	 * @param source
264
	 *            a {@link java.lang.Object} object.
265
	 * @param status
266
	 *            a {@link org.eclipse.core.runtime.IStatus} object.
267
	 */
268
	public static void operationDialog(final Object source,
269
			final Exception ex,
270
			final String pluginId,
271
			final String operationlabel,
272
			final String hint) {
273

    
274
		Display.getDefault().asyncExec(new Runnable() {
275

    
276
			@Override
277
			public void run() {
278
				MultiStatus info = null;
279
		        String title = null;
280

    
281
		        // FIXME cannot access TaxonomicEditorPlugin.PLUGIN_ID from here
282
		        // String PID = TaxonomicEditorPlugin.PLUGIN_ID;
283
		        String PID = "eu.etaxonomy.taxeditor.application";
284

    
285
		        // checking security exceptions for every operation
286
		        RuntimeException securityRuntimeException = SecurityExceptionUtils.findSecurityRuntimeException(ex);
287

    
288
		        // in case of a security exception it is a warning, else it is an error
289
		        if(securityRuntimeException != null){
290
		        	title = "Your changes could not be saved!";
291
		        	warningDialog(title, source, String.format("You are missing sufficient permissions for the operation \"%s\". %s", operationlabel, hint));
292
		        } else {
293
		        	title = "Error executing operation";
294
		        	errorDialog(title, source, String.format("An error occured while executing %s. %s", operationlabel, hint), pluginId, ex, true);
295

    
296
		        }
297

    
298

    
299
			}
300
		});
301
	}
302

    
303

    
304

    
305

    
306
	/**
307
	 * Displays a question {@link org.eclipse.jface.dialogs.MessageDialog}.
308
	 *
309
	 * @param title
310
	 *            a {@link java.lang.String} object.
311
	 * @param message
312
	 *            a {@link java.lang.String} object.
313
	 * @return a boolean.
314
	 */
315
	public static boolean confirmDialog(String title, String message) {
316
		return MessageDialog.openQuestion(AbstractUtility.getShell(), title, message);
317
	}
318

    
319
	/**
320
	 * Displays a message {@link org.eclipse.jface.dialogs.MessageDialog}.
321
	 *
322
	 * @param title
323
	 * @param source
324
	 * @param message
325
	 */
326
	public static void messageDialog(final String title, final Object source, final String message) {
327
		MessagingUtils.messageDialog(title, source, message, null, true);
328
	}
329

    
330

    
331

    
332
	/**
333
	 * Displays an error {@link org.eclipse.jface.dialogs.MessageDialog}.
334
	 *
335
	 * @param title
336
	 *            The dialogs title
337
	 * @param source
338
	 *            The object where the warning was generated (used by log4j)
339
	 * @param message
340
	 *            An informative String to be presented to the user
341
	 * @param title
342
	 *            The dialogs title
343
	 * @param t
344
	 *            a Throwable if one exists or null
345
	 */
346
	public static void messageDialog(final String title,
347
	        final Object source,
348
			final String message,
349
			final Throwable t) {
350
	    MessagingUtils.messageDialog(title, source, message, t, true);
351
	}
352

    
353
	   /**
354
     * Displays an error {@link org.eclipse.jface.dialogs.MessageDialog}.
355
     *
356
     * @param title
357
     *            The dialogs title
358
     * @param source
359
     *            The object where the warning was generated (used by log4j)
360
     * @param message
361
     *            An informative String to be presented to the user
362
     * @param title
363
     *            The dialogs title
364
     * @param t
365
     *            a Throwable if one exists or null
366
     */
367
	public static void messageDialog(final String title,
368
	        final Object source,
369
	        final String message,
370
	        final Throwable t,
371
	        boolean async) {
372
	    if(async) {
373
	        Display.getDefault().asyncExec(new Runnable() {
374

    
375
	            @Override
376
	            public void run() {
377
	                MessageDialog.openError(AbstractUtility.getShell(), title, message + getCauseRecursively(t));
378
	                Class<? extends Object> clazz = source != null ? source
379
	                        .getClass() : this.getClass();
380
	                        error(clazz, message, t);
381
	            }
382

    
383

    
384
	        });
385
	    } else {
386
	        MessageDialog.openError(AbstractUtility.getShell(), title, message + getCauseRecursively(t));
387
	        Class<? extends Object> clazz = source != null ? source.getClass() : TaxeditorStorePlugin.class;
388
	        error(clazz, message, t);
389
	    }
390
	}
391

    
392
    public static String getCauseRecursively(Throwable t) {
393
        if(t == null){
394
            return "";
395
        }
396

    
397
        if(t.getCause() != null){
398
            return getCauseRecursively(t.getCause());
399
        }else{
400
            return String.format("\n\nException: %s\nMessage: %s", t.getClass().getSimpleName(), t.getMessage());
401
        }
402

    
403
    }
404
	/**
405
	 * Displays a warning {@link org.eclipse.jface.dialogs.MessageDialog}.
406
	 *
407
	 * @param title
408
	 * @param termBase
409
	 * @param status
410
	 */
411
	public static void warningDialog(String title, Object source,
412
			IStatus status) {
413
		MessagingUtils.warningDialog(title, source, status.getMessage());
414
	}
415

    
416
	/**
417
	 * Displays a warning {@link org.eclipse.jface.dialogs.MessageDialog}.
418
	 *
419
	 * @param title
420
	 *            The dialogs title
421
	 * @param source
422
	 *            The object where the warning was generated (used by log4j)
423
	 * @param message
424
	 *            An informative String to be presented to the user
425
	 */
426
	public static void warningDialog(final String title, final Object source, final String message) {
427
		Display.getDefault().asyncExec(new Runnable() {
428

    
429
			@Override
430
	        public void run() {
431
				MessageDialog.openWarning(AbstractUtility.getShell(), title, message);
432
				Class<? extends Object> clazz = source != null ? source
433
						.getClass() : AbstractUtility.class;
434
				warn(clazz, message);
435
			}
436
		});
437
	}
438

    
439
	/**
440
	 * Displays an information {@link org.eclipse.jface.dialogs.MessageDialog}.
441
	 *
442
	 * @param title
443
	 * @param status
444
	 */
445
	public static void informationDialog(final String title, final IStatus status) {
446
		MessagingUtils.informationDialog(title, status.getMessage());
447
	}
448

    
449
	/**
450
	 * Displays an information {@link org.eclipse.jface.dialogs.MessageDialog}.
451
	 *
452
	 * @param title
453
	 *            a {@link java.lang.String} object.
454
	 * @param message
455
	 *            a {@link java.lang.String} object.
456
	 */
457
	public static void informationDialog(final String title,
458
			final String message) {
459
		Display.getDefault().asyncExec(new Runnable() {
460

    
461
			@Override
462
	        public void run() {
463
				MessageDialog.openInformation(AbstractUtility.getShell(), title, message);
464
			}
465
		});
466
	}
467

    
468
	/**
469
	 * Open a message box that informs the user about unimplemented
470
	 * functionality. This method is for developer convenience.
471
	 *
472
	 * @param source
473
	 *            a {@link java.lang.Object} object.
474
	 */
475
	public static void notImplementedMessage(Object source) {
476
		warningDialog("Not yet implemented", source,
477
				"This functionality is not yet implemented.");
478
	}
479

    
480
}
(26-26/35)