1 package eu
.etaxonomy
.taxeditor
.model
;
3 import java
.io
.PrintWriter
;
4 import java
.io
.StringWriter
;
5 import java
.util
.ArrayList
;
8 import org
.apache
.commons
.lang
.exception
.ExceptionUtils
;
9 import org
.apache
.log4j
.Logger
;
10 import org
.eclipse
.core
.runtime
.IStatus
;
11 import org
.eclipse
.core
.runtime
.MultiStatus
;
12 import org
.eclipse
.core
.runtime
.Platform
;
13 import org
.eclipse
.core
.runtime
.Status
;
14 import org
.eclipse
.jface
.dialogs
.MessageDialog
;
15 import org
.eclipse
.swt
.widgets
.Display
;
17 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.permission
.SecurityExceptionUtils
;
18 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
19 import eu
.etaxonomy
.taxeditor
.store
.internal
.TaxeditorStorePlugin
;
22 * Utility class which handles all the messaging information generated by the
25 * This includes logging as well as dialogs.
30 public class MessagingUtils
{
31 public final static String UNEXPECTED_ERROR_MESSAGE
= "This is an unexpected error.";
32 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).";
33 public final static String DEFAULT_MESSAGE
= "Error thrown but no associated message";
36 * Gets the Log4J logger for a given class
39 * a {@link java.lang.Class} object.
40 * @return a {@link org.apache.log4j.Logger} object.
42 public static Logger
getLog4JLogger(Class clazz
) {
43 return Logger
.getLogger(clazz
);
47 * Logs details from a given Status object
50 * a {@link org.eclipse.core.runtime.IStatus} object.
52 private static void log(IStatus status
) {
53 TaxeditorStorePlugin
.getDefault().getLog().log(status
);
57 * Logs a status object as information.
60 * a {@link org.eclipse.core.runtime.IStatus} object.
62 public static void info(IStatus status
) {
67 * Logs a string as information.
70 * a {@link java.lang.String} object.
72 public static void info(String message
) {
73 IStatus status
= new Status(IStatus
.INFO
, AbstractUtility
.getPluginId(), message
);
78 * Logs an exception from a given source as a warning.
83 public static void warn(Class source
, Throwable t
) {
84 IStatus status
= new Status(IStatus
.WARNING
, AbstractUtility
.getPluginId(), t
.getMessage(), t
);
85 MessagingUtils
.getLog4JLogger(source
).warn(t
);
90 * Logs a status object from a given source as a warning.
95 public static void warn(Class source
, IStatus status
) {
96 MessagingUtils
.getLog4JLogger(source
).warn(status
.getMessage(), status
.getException());
101 * Logs a string from a given source as a warning.
105 * a {@link java.lang.Class} object.
107 * a {@link java.lang.String} object.
109 public static void warn(Class source
, String message
) {
110 IStatus status
= new Status(IStatus
.WARNING
, AbstractUtility
.getPluginId(), message
);
111 MessagingUtils
.getLog4JLogger(source
).warn(message
);
116 * Logs a status object from a given source as an error.
120 * a {@link java.lang.Class} object.
122 * a {@link org.eclipse.core.runtime.IStatus} object.
124 public static void error(Class source
, IStatus status
) {
125 getLog4JLogger(source
)
126 .error(status
.getMessage(), status
.getException());
131 * Logs a string and exception from a given source as an error.
135 * a {@link java.lang.Class} object.
137 * a {@link java.lang.String} object.
139 * a {@link java.lang.Throwable} object.
141 public static void error(Class source
, String message
, Throwable t
) {
142 IStatus status
= new Status(IStatus
.ERROR
, AbstractUtility
.getPluginId(), message
, t
);
143 error(source
, status
);
149 * Logs an exception from a given source as an error.
153 * a {@link java.lang.Class} object.
155 * a {@link java.lang.Throwable} object.
157 public static void error(Class source
, Throwable t
) {
158 error(source
.getClass(), t
.getMessage(), t
);
164 * Returns a list of strings, providing info on,
167 * - server (address + source name)
168 * - db schema version
172 public static List
<String
> getContextInfo() {
173 List
<String
> contextInfo
= new ArrayList
<String
>();
175 String schemaVersion
= "";
180 version
= Platform
.getBundle("eu.etaxonomy.taxeditor.application").getHeaders().get(org
.osgi
.framework
.Constants
.BUNDLE_VERSION
);
182 if(CdmStore
.getActiveCdmSource() != null ) {
183 login
= CdmStore
.getLoginManager().getAuthenticatedUser().getUsername();
184 name
= CdmStore
.getActiveCdmSource().getName();
185 schemaVersion
= CdmStore
.getActiveCdmSource().getDbSchemaVersion();
186 server
= CdmStore
.getActiveCdmSource().getServer();
189 } catch (Exception e
) {
192 contextInfo
.add("login : " + login
);
193 contextInfo
.add("editor version : " + version
);
194 contextInfo
.add("server : " + server
+ " / " + name
);
195 contextInfo
.add("schema version : " + schemaVersion
);
196 contextInfo
.add("os : " + System
.getProperty("os.name")+" "+System
.getProperty("os.version")+" "+System
.getProperty("os.arch"));
197 contextInfo
.add("java : "+System
.getProperty("java.version"));
202 public static String
getStackTraceAndContextInfo(Throwable t
, List
<String
> contextInfo
) {
203 StringBuffer stackTraceAndContextInfo
= new StringBuffer();
204 Throwable throwable
= t
;
206 for(String infoItem
: contextInfo
) {
207 stackTraceAndContextInfo
.append(infoItem
+ System
.getProperty("line.separator"));
210 StringWriter sw
= new StringWriter();
212 if(throwable
== null) {
213 throwable
= getDefaultThrowable();
215 throwable
.printStackTrace(new PrintWriter(sw
));
217 stackTraceAndContextInfo
.append(sw
.toString());
219 return stackTraceAndContextInfo
.toString();
222 private static Throwable
getDefaultThrowable() {
223 return new Throwable("Error thrown but no associated exception");
229 * Displays a {@link eu.etaxonomy.taxeditor.model.CdmErrorDialog}.
238 private static void errorDialog(final String title
,
241 final List
<String
> contextInfo
,
242 final String message
,
243 final MultiStatus status
) {
245 Display
.getDefault().asyncExec(new Runnable() {
249 String stackTraceWithContext
= getStackTraceAndContextInfo(t
, contextInfo
);
250 CdmErrorDialog ced
= new CdmErrorDialog(AbstractUtility
.getShell(), title
, message
, status
, stackTraceWithContext
);
252 Class
<?
extends Object
> clazz
= source
!= null ? source
.getClass() : this.getClass();
255 IStatus singleStatus
= new Status(IStatus
.ERROR
,
258 new Exception(stackTraceWithContext
));
260 error(clazz
, singleStatus
);
266 * Displays a {@link eu.etaxonomy.taxeditor.model.CdmErrorDialog}.
274 public static void errorDialog(final String title
,
276 final String message
,
277 final String pluginId
,
279 boolean addContactMesg
) {
281 Throwable throwable
= t
;
282 StringBuffer sbStackTrace
= new StringBuffer();
284 // We need to build a MultiStatus object since the simple
285 // idea of writing out the stack trace as a single string
286 // leads to a single line on windows
287 List
<Status
> childStatuses
= new ArrayList
<Status
>();
290 List
<String
> contextInfo
= getContextInfo();
291 for(String infoItem
: contextInfo
) {
292 childStatuses
.add(new Status(IStatus
.ERROR
, pluginId
, infoItem
));
295 if(throwable
== null) {
296 throwable
= getDefaultThrowable();
302 for(Throwable th
: ExceptionUtils
.getThrowables(throwable
)) {
303 // add main exception
305 for (StackTraceElement ste
: th
.getStackTrace()) {
306 childStatuses
.add(new Status(IStatus
.ERROR
, pluginId
, " at " + ste
.toString()));
309 // add recursive causes
311 childStatuses
.add(new Status(IStatus
.ERROR
, pluginId
, ""));
312 String msg
= th
.toString();
313 childStatuses
.add(new Status(IStatus
.ERROR
, pluginId
, "Caused by : " + msg
));
315 for (StackTraceElement ste
: th
.getStackTrace()) {
316 // add only pre-defined number of trace elements
317 if(traceCount
> maxTraces
) {
318 childStatuses
.add(new Status(IStatus
.ERROR
, pluginId
, " ...."));
321 // build & add status
322 childStatuses
.add(new Status(IStatus
.ERROR
, pluginId
, " at " + ste
.toString()));
329 String finalMessage
= message
;
331 if(finalMessage
== null || finalMessage
.isEmpty()) {
332 finalMessage
= DEFAULT_MESSAGE
;
336 // add edit support contact info to message
337 finalMessage
+= MessagingUtils
.CONTACT_MESSAGE
;
340 MultiStatus ms
= new MultiStatus(pluginId
,
342 childStatuses
.toArray(new Status
[] {}),
343 throwable
.toString(),
346 errorDialog(title
, source
, throwable
, contextInfo
, finalMessage
, ms
);
350 * Displays a dialog for an exception occurring in an operation.
352 * This will be either a {@link eu.etaxonomy.taxeditor.model.CdmErrorDialog} in case of a
353 * security runtime exception or a warning {@link org.eclipse.jface.dialogs.MessageDialog} in
354 * case of any other exception.
357 * a {@link java.lang.String} object.
359 * a {@link java.lang.Object} object.
361 * a {@link org.eclipse.core.runtime.IStatus} object.
363 public static void operationDialog(final Object source
,
365 final String pluginId
,
366 final String operationlabel
,
369 Display
.getDefault().asyncExec(new Runnable() {
373 MultiStatus info
= null;
376 // FIXME cannot access TaxonomicEditorPlugin.PLUGIN_ID from here
377 // String PID = TaxonomicEditorPlugin.PLUGIN_ID;
378 String PID
= "eu.etaxonomy.taxeditor.application";
380 // checking security exceptions for every operation
381 RuntimeException securityRuntimeException
= SecurityExceptionUtils
.findSecurityRuntimeException(ex
);
383 // in case of a security exception it is a warning, else it is an error
384 if(securityRuntimeException
!= null){
385 title
= "Your changes could not be saved!";
386 warningDialog(title
, source
, String
.format("You are missing sufficient permissions for the operation \"%s\". %s", operationlabel
, hint
));
388 title
= "Error executing operation";
389 errorDialog(title
, source
, String
.format("An error occured while executing %s. %s", operationlabel
, hint
), pluginId
, ex
, true);
402 * Displays a question {@link org.eclipse.jface.dialogs.MessageDialog}.
405 * a {@link java.lang.String} object.
407 * a {@link java.lang.String} object.
410 public static boolean confirmDialog(String title
, String message
) {
411 return MessageDialog
.openQuestion(AbstractUtility
.getShell(), title
, message
);
415 * Displays a message {@link org.eclipse.jface.dialogs.MessageDialog}.
421 public static void messageDialog(final String title
, final Object source
, final String message
) {
422 MessagingUtils
.messageDialog(title
, source
, message
, null, true);
428 * Displays an error {@link org.eclipse.jface.dialogs.MessageDialog}.
433 * The object where the warning was generated (used by log4j)
435 * An informative String to be presented to the user
439 * a Throwable if one exists or null
441 public static void messageDialog(final String title
,
443 final String message
,
445 MessagingUtils
.messageDialog(title
, source
, message
, t
, true);
449 * Displays an error {@link org.eclipse.jface.dialogs.MessageDialog}.
454 * The object where the warning was generated (used by log4j)
456 * An informative String to be presented to the user
460 * a Throwable if one exists or null
462 public static void messageDialog(final String title
,
464 final String message
,
468 Display
.getDefault().asyncExec(new Runnable() {
472 MessageDialog
.openError(AbstractUtility
.getShell(), title
, message
+ getCauseRecursively(t
));
473 Class
<?
extends Object
> clazz
= source
!= null ? source
474 .getClass() : this.getClass();
475 error(clazz
, message
, t
);
481 MessageDialog
.openError(AbstractUtility
.getShell(), title
, message
+ getCauseRecursively(t
));
482 Class
<?
extends Object
> clazz
= source
!= null ? source
.getClass() : TaxeditorStorePlugin
.class;
483 error(clazz
, message
, t
);
487 public static String
getCauseRecursively(Throwable t
) {
492 if(t
.getCause() != null){
493 return getCauseRecursively(t
.getCause());
495 return String
.format("\n\nException: %s\nMessage: %s", t
.getClass().getSimpleName(), t
.getMessage());
500 * Displays a warning {@link org.eclipse.jface.dialogs.MessageDialog}.
506 public static void warningDialog(String title
, Object source
,
508 MessagingUtils
.warningDialog(title
, source
, status
.getMessage());
512 * Standard warning dialog for the case when the application is not yet connected to the datasource
516 public static void noDataSourceWarningDialog(Object source
) {
519 "Application is not connected to a datastore",
521 "The requested operation is only available when "
522 + "connected to a datasource. You may choose a datasource to connect to or create a new one in the datasource view.");
526 * Displays a warning {@link org.eclipse.jface.dialogs.MessageDialog}.
531 * The object where the warning was generated (used by log4j)
533 * An informative String to be presented to the user
535 public static void warningDialog(final String title
, final Object source
, final String message
) {
536 Display
.getDefault().asyncExec(new Runnable() {
540 MessageDialog
.openWarning(AbstractUtility
.getShell(), title
, message
);
541 Class
<?
extends Object
> clazz
= source
!= null ? source
542 .getClass() : AbstractUtility
.class;
543 warn(clazz
, message
);
549 * Displays an information {@link org.eclipse.jface.dialogs.MessageDialog}.
554 public static void informationDialog(final String title
, final IStatus status
) {
555 MessagingUtils
.informationDialog(title
, status
.getMessage());
559 * Displays an information {@link org.eclipse.jface.dialogs.MessageDialog}.
562 * a {@link java.lang.String} object.
564 * a {@link java.lang.String} object.
566 public static void informationDialog(final String title
,
567 final String message
) {
568 Display
.getDefault().asyncExec(new Runnable() {
572 MessageDialog
.openInformation(AbstractUtility
.getShell(), title
, message
);
578 * Open a message box that informs the user about unimplemented
579 * functionality. This method is for developer convenience.
582 * a {@link java.lang.Object} object.
584 public static void notImplementedMessage(Object source
) {
585 warningDialog("Not yet implemented", source
,
586 "This functionality is not yet implemented.");