Project

General

Profile

Download (12.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 operationlabel, 
271
			final String hint) {
272

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

    
275
			@Override
276
			public void run() {		
277
				MultiStatus info = null;
278
		        String title = null;
279
		        
280
		        // FIXME cannot access TaxonomicEditorPlugin.PLUGIN_ID from here
281
		        // String PID = TaxonomicEditorPlugin.PLUGIN_ID;
282
		        String PID = "eu.etaxonomy.taxeditor.application";
283

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

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

    
304

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

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

    
329
	/**
330
	 * Displays an error {@link org.eclipse.jface.dialogs.MessageDialog}.
331
	 *
332
	 * @param title
333
	 *            The dialogs title
334
	 * @param source
335
	 *            The object where the warning was generated (used by log4j)
336
	 * @param message
337
	 *            An informative String to be presented to the user
338
	 * @param title
339
	 *            The dialogs title
340
	 * @param t
341
	 *            a Throwable if one exists or null
342
	 */
343
	public static void messageDialog(final String title, final Object source,
344
			final String message, final Throwable t) {
345
		Display.getDefault().asyncExec(new Runnable() {
346
	
347
			@Override
348
	        public void run() {
349
				MessageDialog.openError(AbstractUtility.getShell(), title, message + getCauseRecursively(t));
350
				Class<? extends Object> clazz = source != null ? source
351
						.getClass() : this.getClass();
352
				error(clazz, message, t);
353
			}
354
	
355
			private String getCauseRecursively(Throwable t) {
356
				if(t == null){
357
					return "";
358
				}
359
	
360
				if(t.getCause() != null){
361
					return getCauseRecursively(t.getCause());
362
				}else{
363
					return String.format("\n\nException: %s\nMessage: %s", t.getClass().getSimpleName(), t.getMessage());
364
				}
365
	
366
			}
367
		});
368
	}
369

    
370
	/**
371
	 * Displays a warning {@link org.eclipse.jface.dialogs.MessageDialog}.
372
	 * 
373
	 * @param title
374
	 * @param termBase
375
	 * @param status
376
	 */
377
	public static void warningDialog(String title, Object source,
378
			IStatus status) {
379
		MessagingUtils.warningDialog(title, source, status.getMessage());
380
	}
381

    
382
	/**
383
	 * Displays a warning {@link org.eclipse.jface.dialogs.MessageDialog}.
384
	 *
385
	 * @param title
386
	 *            The dialogs title
387
	 * @param source
388
	 *            The object where the warning was generated (used by log4j)
389
	 * @param message
390
	 *            An informative String to be presented to the user
391
	 */
392
	public static void warningDialog(final String title, final Object source, final String message) {
393
		Display.getDefault().asyncExec(new Runnable() {
394
	
395
			@Override
396
	        public void run() {
397
				MessageDialog.openWarning(AbstractUtility.getShell(), title, message);
398
				Class<? extends Object> clazz = source != null ? source
399
						.getClass() : AbstractUtility.class;
400
				warn(clazz, message);
401
			}
402
		});
403
	}
404

    
405
	/**
406
	 * Displays an information {@link org.eclipse.jface.dialogs.MessageDialog}.
407
	 * 
408
	 * @param title
409
	 * @param status
410
	 */
411
	public static void informationDialog(final String title, final IStatus status) {
412
		MessagingUtils.informationDialog(title, status.getMessage());
413
	}
414

    
415
	/**
416
	 * Displays an information {@link org.eclipse.jface.dialogs.MessageDialog}.
417
	 *
418
	 * @param title
419
	 *            a {@link java.lang.String} object.
420
	 * @param message
421
	 *            a {@link java.lang.String} object.
422
	 */
423
	public static void informationDialog(final String title,
424
			final String message) {
425
		Display.getDefault().asyncExec(new Runnable() {
426
	
427
			@Override
428
	        public void run() {
429
				MessageDialog.openInformation(AbstractUtility.getShell(), title, message);
430
			}
431
		});
432
	}
433

    
434
	/**
435
	 * Open a message box that informs the user about unimplemented
436
	 * functionality. This method is for developer convenience.
437
	 *
438
	 * @param source
439
	 *            a {@link java.lang.Object} object.
440
	 */
441
	public static void notImplementedMessage(Object source) {
442
		warningDialog("Not yet implemented", source,
443
				"This functionality is not yet implemented.");
444
	}
445

    
446
}
(26-26/35)