Project

General

Profile

« Previous | Next » 

Revision 5e581f5d

Added by Cherian Mathew over 7 years ago

added new error dialog for operations

View differences:

eu.etaxonomy.taxeditor.editor/src/main/java/eu/etaxonomy/taxeditor/editor/MultiPageTaxonEditor.java
30 30
import eu.etaxonomy.cdm.model.taxon.Taxon;
31 31
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
32 32
import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
33
import eu.etaxonomy.taxeditor.editor.internal.TaxeditorEditorPlugin;
33 34
import eu.etaxonomy.taxeditor.editor.name.TaxonNameEditor;
34 35
import eu.etaxonomy.taxeditor.editor.name.container.AbstractGroupedContainer;
35 36
import eu.etaxonomy.taxeditor.model.AbstractUtility;
......
146 147
            monitor.worked(1);            
147 148
        } catch (Exception e) {
148 149
            setFocus();
149
            MessagingUtils.operationDialog(this, e, "saving a taxon", " Please close and reopen the taxon again.");
150
            MessagingUtils.operationDialog(this, e, TaxeditorEditorPlugin.PLUGIN_ID,"saving a taxon", " Please close and reopen the taxon again.");
150 151
            disableEditor(true);
151 152
        } finally {
152 153
            monitor.done();
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/model/AbstractUtility.java
10 10

  
11 11
package eu.etaxonomy.taxeditor.model;
12 12

  
13
import java.io.PrintWriter;
14
import java.io.StringWriter;
15
import java.io.Writer;
16 13
import java.lang.reflect.InvocationTargetException;
17 14
import java.util.ArrayList;
18 15
import java.util.Collection;
......
25 22
import org.eclipse.core.runtime.IAdaptable;
26 23
import org.eclipse.core.runtime.IProgressMonitor;
27 24
import org.eclipse.core.runtime.IStatus;
28
import org.eclipse.core.runtime.MultiStatus;
29 25
import org.eclipse.core.runtime.NullProgressMonitor;
30 26
import org.eclipse.core.runtime.OperationCanceledException;
31 27
import org.eclipse.core.runtime.Status;
......
56 52
import org.eclipse.ui.themes.IThemeManager;
57 53

  
58 54
import eu.etaxonomy.cdm.model.common.IEnumTerm;
59
import eu.etaxonomy.cdm.persistence.hibernate.permission.SecurityExceptionUtils;
60 55
import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
61 56
import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
62 57
import eu.etaxonomy.taxeditor.store.internal.TaxeditorStorePlugin;
......
75 70
 */
76 71
public abstract class AbstractUtility {
77 72

  
78
	/** Constant <code>statusLineManager</code> */
79
	protected static IStatusLineManager statusLineManager;
73
    /** Constant <code>statusLineManager</code> */
74
    protected static IStatusLineManager statusLineManager;
75

  
80 76

  
81 77
	/**
82 78
	 * <p>
......
318 314
							uiInfoAdapter);
319 315
				} catch (ExecutionException e) {
320 316

  
321
					MessagingUtils.operationDialog(this, e, operationlabel, null);
317
					MessagingUtils.operationDialog(this, e, TaxeditorStorePlugin.PLUGIN_ID, operationlabel, null);
322 318

  
323 319
				} finally {
324 320
					monitor.done();
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/model/MessagingUtils.java
16 16
/**
17 17
 * Utility class which handles all the messaging information generated by the
18 18
 * Editor.
19
 * 
19
 *
20 20
 * This includes logging as well as dialogs.
21
 * 
21
 *
22 22
 * @author cmathew
23 23
 *
24 24
 */
......
36 36
	public static Logger getLog4JLogger(Class clazz) {
37 37
		return Logger.getLogger(clazz);
38 38
	}
39
	
39

  
40 40
	/**
41 41
	 * Logs details from a given Status object
42 42
	 *
......
48 48
	}
49 49

  
50 50
	/**
51
	 * Logs a status object as information.	 
51
	 * Logs a status object as information.
52 52
	 *
53 53
	 * @param status
54 54
	 *            a {@link org.eclipse.core.runtime.IStatus} object.
......
58 58
	}
59 59

  
60 60
	/**
61
	 * Logs a string as information.	 
61
	 * Logs a string as information.
62 62
	 *
63 63
	 * @param message
64 64
	 *            a {@link java.lang.String} object.
......
69 69
	}
70 70

  
71 71
	/**
72
	 * Logs an exception from a given source as a warning.	 
73
	 * 
72
	 * Logs an exception from a given source as a warning.
73
	 *
74 74
	 * @param source
75 75
	 * @param t
76 76
	 */
......
81 81
	}
82 82

  
83 83
	/**
84
	 * Logs a status object from a given source as a warning.	 
85
	 * 
84
	 * Logs a status object from a given source as a warning.
85
	 *
86 86
	 * @param source
87 87
	 * @param status
88 88
	 */
......
92 92
	}
93 93

  
94 94
	/**
95
	 * Logs a string from a given source as a warning.	 
96
	 * 
95
	 * Logs a string from a given source as a warning.
96
	 *
97 97
	 *
98 98
	 * @param source
99 99
	 *            a {@link java.lang.Class} object.
......
107 107
	}
108 108

  
109 109
	/**
110
	 * Logs a status object from a given source as an error.	 
111
	 * 
110
	 * Logs a status object from a given source as an error.
111
	 *
112 112
	 *
113 113
	 * @param source
114 114
	 *            a {@link java.lang.Class} object.
......
122 122
	}
123 123

  
124 124
	/**
125
	 * Logs a string and exception from a given source as an error.	 
126
	 * 
125
	 * Logs a string and exception from a given source as an error.
126
	 *
127 127
	 *
128 128
	 * @param source
129 129
	 *            a {@link java.lang.Class} object.
......
140 140

  
141 141

  
142 142
	/**
143
	 * Logs an exception from a given source as an error.	 
144
	 * 
143
	 * Logs an exception from a given source as an error.
144
	 *
145 145
	 *
146 146
	 * @param source
147 147
	 *            a {@link java.lang.Class} object.
......
162 162
	 * @param status
163 163
	 *            a {@link org.eclipse.core.runtime.IStatus} object.
164 164
	 */
165
	public static void errorDialog(final String title, 
166
			final Object source, 
167
			final String message, 
165
	public static void errorDialog(final String title,
166
			final Object source,
167
			final String message,
168 168
			final IStatus status) {
169 169

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

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

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

  
215
	
214

  
215

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

  
238 238

  
239 239
	/**
240 240
	 * Displays a {@link eu.etaxonomy.taxeditor.model.CdmErrorDialog}.
......
250 250
			final IStatus status) {
251 251
		errorDialog(title, source, null, status);
252 252
	}
253
	
253

  
254 254
	/**
255 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 
256
	 *
257
	 * This will be either a {@link eu.etaxonomy.taxeditor.model.CdmErrorDialog} in case of a
258 258
	 * security runtime exception or a warning {@link org.eclipse.jface.dialogs.MessageDialog} in
259 259
	 * case of any other exception.
260 260
	 *
......
265 265
	 * @param status
266 266
	 *            a {@link org.eclipse.core.runtime.IStatus} object.
267 267
	 */
268
	public static void operationDialog(final Object source, 			
268
	public static void operationDialog(final Object source,
269 269
			final Exception ex,
270
			final String operationlabel, 
270
			final String pluginId,
271
			final String operationlabel,
271 272
			final String hint) {
272 273

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

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

  
280 281
		        // FIXME cannot access TaxonomicEditorPlugin.PLUGIN_ID from here
281 282
		        // String PID = TaxonomicEditorPlugin.PLUGIN_ID;
282 283
		        String PID = "eu.etaxonomy.taxeditor.application";
......
289 290
		        	title = "Your changes could not be saved!";
290 291
		        	warningDialog(title, source, String.format("You are missing sufficient permissions for the operation \"%s\". %s", operationlabel, hint));
291 292
		        } 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
		        	
293
		        	title = "Error executing operation";
294
		        	errorDialog(title, source, String.format("An error occured while executing %s. %s", operationlabel, hint), pluginId, ex, true);
295

  
295 296
		        }
296
		        
297
		        
297

  
298

  
298 299
			}
299 300
		});
300 301
	}
301
	
302
	
302

  
303

  
303 304

  
304 305

  
305 306
	/**
......
317 318

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

  
330

  
331

  
329 332
	/**
330 333
	 * Displays an error {@link org.eclipse.jface.dialogs.MessageDialog}.
331 334
	 *
......
340 343
	 * @param t
341 344
	 *            a Throwable if one exists or null
342 345
	 */
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
		});
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);
368 351
	}
369 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
    }
370 404
	/**
371 405
	 * Displays a warning {@link org.eclipse.jface.dialogs.MessageDialog}.
372
	 * 
406
	 *
373 407
	 * @param title
374 408
	 * @param termBase
375 409
	 * @param status
......
391 425
	 */
392 426
	public static void warningDialog(final String title, final Object source, final String message) {
393 427
		Display.getDefault().asyncExec(new Runnable() {
394
	
428

  
395 429
			@Override
396 430
	        public void run() {
397 431
				MessageDialog.openWarning(AbstractUtility.getShell(), title, message);
......
404 438

  
405 439
	/**
406 440
	 * Displays an information {@link org.eclipse.jface.dialogs.MessageDialog}.
407
	 * 
441
	 *
408 442
	 * @param title
409 443
	 * @param status
410 444
	 */
......
423 457
	public static void informationDialog(final String title,
424 458
			final String message) {
425 459
		Display.getDefault().asyncExec(new Runnable() {
426
	
460

  
427 461
			@Override
428 462
	        public void run() {
429 463
				MessageDialog.openInformation(AbstractUtility.getShell(), title, message);

Also available in: Unified diff