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
.test
.integration
.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";
34 public final static String CONNECTION_FAILURE_MESSAGE
= "The connection to the remote server has been broken";
35 public final static String REMOTE_ACCESS_FAILURE_MESSAGE
= "Problem accessing remote server";
38 * Gets the Log4J logger for a given class
41 * a {@link java.lang.Class} object.
42 * @return a {@link org.apache.log4j.Logger} object.
44 public static Logger
getLog4JLogger(Class clazz
) {
45 return Logger
.getLogger(clazz
);
49 * Logs details from a given Status object
52 * a {@link org.eclipse.core.runtime.IStatus} object.
54 private static void log(IStatus status
) {
55 TaxeditorStorePlugin
.getDefault().getLog().log(status
);
59 * Logs a status object as information.
62 * a {@link org.eclipse.core.runtime.IStatus} object.
64 public static void info(IStatus status
) {
69 * Logs a string as information.
72 * a {@link java.lang.String} object.
74 public static void info(String message
) {
75 IStatus status
= new Status(IStatus
.INFO
, AbstractUtility
.getPluginId(), message
);
80 * Logs an exception from a given source as a warning.
85 public static void warn(Class source
, Throwable t
) {
86 IStatus status
= new Status(IStatus
.WARNING
, AbstractUtility
.getPluginId(), t
.getMessage(), t
);
87 MessagingUtils
.getLog4JLogger(source
).warn(t
);
92 * Logs a status object from a given source as a warning.
97 public static void warn(Class source
, IStatus status
) {
98 MessagingUtils
.getLog4JLogger(source
).warn(status
.getMessage(), status
.getException());
103 * Logs a string from a given source as a warning.
107 * a {@link java.lang.Class} object.
109 * a {@link java.lang.String} object.
111 public static void warn(Class source
, String message
) {
112 IStatus status
= new Status(IStatus
.WARNING
, AbstractUtility
.getPluginId(), message
);
113 MessagingUtils
.getLog4JLogger(source
).warn(message
);
118 * Logs a status object from a given source as an error.
122 * a {@link java.lang.Class} object.
124 * a {@link org.eclipse.core.runtime.IStatus} object.
126 public static void error(Class source
, IStatus status
) {
127 getLog4JLogger(source
)
128 .error(status
.getMessage(), status
.getException());
133 * Logs a string and exception from a given source as an error.
137 * a {@link java.lang.Class} object.
139 * a {@link java.lang.String} object.
141 * a {@link java.lang.Throwable} object.
143 public static void error(Class source
, String message
, Throwable t
) {
144 IStatus status
= new Status(IStatus
.ERROR
, AbstractUtility
.getPluginId(), message
, t
);
145 error(source
, status
);
151 * Logs an exception from a given source as an error.
155 * a {@link java.lang.Class} object.
157 * a {@link java.lang.Throwable} object.
159 public static void error(Class source
, Throwable t
) {
160 error(source
.getClass(), t
.getMessage(), t
);
166 * Returns a list of strings, providing info on,
169 * - server (address + source name)
170 * - db schema version
174 public static List
<String
> getContextInfo() {
175 List
<String
> contextInfo
= new ArrayList
<String
>();
177 String schemaVersion
= "";
182 version
= Platform
.getBundle("eu.etaxonomy.taxeditor.application").getHeaders().get(org
.osgi
.framework
.Constants
.BUNDLE_VERSION
);
184 if(CdmStore
.getActiveCdmSource() != null ) {
185 login
= CdmStore
.getLoginManager().getAuthenticatedUser().getUsername();
186 name
= CdmStore
.getActiveCdmSource().getName();
187 schemaVersion
= CdmStore
.getActiveCdmSource().getDbSchemaVersion();
188 server
= CdmStore
.getActiveCdmSource().getServer();
191 } catch (Exception e
) {
194 contextInfo
.add("login : " + login
);
195 contextInfo
.add("editor version : " + version
);
196 contextInfo
.add("server : " + server
+ " / " + name
);
197 contextInfo
.add("schema version : " + schemaVersion
);
198 contextInfo
.add("os : " + System
.getProperty("os.name")+" "+System
.getProperty("os.version")+" "+System
.getProperty("os.arch"));
199 contextInfo
.add("java : "+System
.getProperty("java.version"));
204 public static String
getStackTraceAndContextInfo(Throwable t
, List
<String
> contextInfo
) {
205 StringBuffer stackTraceAndContextInfo
= new StringBuffer();
206 Throwable throwable
= t
;
208 for(String infoItem
: contextInfo
) {
209 stackTraceAndContextInfo
.append(infoItem
+ System
.getProperty("line.separator"));
212 StringWriter sw
= new StringWriter();
214 if(throwable
== null) {
215 throwable
= getDefaultThrowable();
217 throwable
.printStackTrace(new PrintWriter(sw
));
219 stackTraceAndContextInfo
.append(sw
.toString());
221 return stackTraceAndContextInfo
.toString();
224 public static String
getContextInfo(List
<String
> contextInfo
) {
225 StringBuffer scontextInfoStringBuffer
= new StringBuffer();
228 for(String infoItem
: contextInfo
) {
229 scontextInfoStringBuffer
.append(infoItem
+ System
.getProperty("line.separator"));
234 return scontextInfoStringBuffer
.toString();
237 private static Throwable
getDefaultThrowable() {
238 return new Throwable("Error thrown but no associated exception");
244 * Displays a {@link eu.etaxonomy.taxeditor.model.CdmErrorDialog}.
253 private static void errorDialog(final String title
,
256 final List
<String
> contextInfo
,
257 final String message
,
258 final MultiStatus status
,
259 final boolean showReason
) {
261 Display
.getDefault().asyncExec(new Runnable() {
265 String stackTraceWithContext
= getStackTraceAndContextInfo(t
, contextInfo
);
266 CdmErrorDialog ced
= new CdmErrorDialog(AbstractUtility
.getShell(), title
, message
, status
, stackTraceWithContext
, showReason
);
268 Class
<?
extends Object
> clazz
= source
!= null ? source
.getClass() : this.getClass();
271 IStatus singleStatus
= new Status(IStatus
.ERROR
,
274 new Exception(stackTraceWithContext
));
276 error(clazz
, singleStatus
);
281 public static void errorDialog(final String title
,
283 final String message
,
284 final String pluginId
,
286 boolean addContactMesg
) {
287 errorDialog(title
, source
, message
, pluginId
, t
, addContactMesg
, true);
291 * Displays a {@link eu.etaxonomy.taxeditor.model.CdmErrorDialog}.
299 public static void errorDialog(final String title
,
301 final String message
,
302 final String pluginId
,
304 boolean addContactMesg
,
305 boolean showReason
) {
307 Throwable throwable
= t
;
308 StringBuffer sbStackTrace
= new StringBuffer();
310 // We need to build a MultiStatus object since the simple
311 // idea of writing out the stack trace as a single string
312 // leads to a single line on windows
313 List
<Status
> childStatuses
= new ArrayList
<Status
>();
316 List
<String
> contextInfo
= getContextInfo();
317 for(String infoItem
: contextInfo
) {
318 childStatuses
.add(new Status(IStatus
.ERROR
, pluginId
, infoItem
));
321 if(throwable
== null) {
322 throwable
= getDefaultThrowable();
328 for(Throwable th
: ExceptionUtils
.getThrowables(throwable
)) {
329 // add main exception
331 for (StackTraceElement ste
: th
.getStackTrace()) {
332 childStatuses
.add(new Status(IStatus
.ERROR
, pluginId
, " at " + ste
.toString()));
335 // add recursive causes
337 childStatuses
.add(new Status(IStatus
.ERROR
, pluginId
, ""));
338 String msg
= th
.toString();
339 childStatuses
.add(new Status(IStatus
.ERROR
, pluginId
, "Caused by : " + msg
));
341 for (StackTraceElement ste
: th
.getStackTrace()) {
342 // add only pre-defined number of trace elements
343 if(traceCount
> maxTraces
) {
344 childStatuses
.add(new Status(IStatus
.ERROR
, pluginId
, " ...."));
347 // build & add status
348 childStatuses
.add(new Status(IStatus
.ERROR
, pluginId
, " at " + ste
.toString()));
355 String finalMessage
= message
;
357 if(finalMessage
== null || finalMessage
.isEmpty()) {
358 finalMessage
= DEFAULT_MESSAGE
;
362 // add edit support contact info to message
363 finalMessage
+= MessagingUtils
.CONTACT_MESSAGE
;
366 MultiStatus ms
= new MultiStatus(pluginId
,
368 childStatuses
.toArray(new Status
[] {}),
369 throwable
.toString(),
372 errorDialog(title
, source
, throwable
, contextInfo
, finalMessage
, ms
, showReason
);
376 * Displays a dialog for an exception occurring in an operation.
378 * This will be either a {@link eu.etaxonomy.taxeditor.model.CdmErrorDialog} in case of a
379 * security runtime exception or a warning {@link org.eclipse.jface.dialogs.MessageDialog} in
380 * case of any other exception.
383 * a {@link java.lang.String} object.
385 * a {@link java.lang.Object} object.
387 * a {@link org.eclipse.core.runtime.IStatus} object.
389 public static void operationDialog(final Object source
,
391 final String pluginId
,
392 final String operationlabel
,
395 Display
.getDefault().asyncExec(new Runnable() {
399 MultiStatus info
= null;
402 // FIXME cannot access TaxonomicEditorPlugin.PLUGIN_ID from here
403 // String PID = TaxonomicEditorPlugin.PLUGIN_ID;
404 String PID
= "eu.etaxonomy.taxeditor.application";
406 // checking security exceptions for every operation
407 RuntimeException securityRuntimeException
= SecurityExceptionUtils
.findSecurityRuntimeException(ex
);
409 // in case of a security exception it is a warning, else it is an error
410 if(securityRuntimeException
!= null){
411 title
= "Your changes could not be saved!";
412 warningDialog(title
, source
, String
.format("You are missing sufficient permissions for the operation \"%s\". %s", operationlabel
, hint
));
414 title
= "Error executing operation";
415 errorDialog(title
, source
, String
.format("An error occured while executing %s. %s", operationlabel
, hint
), pluginId
, ex
, true);
428 * Displays a question {@link org.eclipse.jface.dialogs.MessageDialog}.
431 * a {@link java.lang.String} object.
433 * a {@link java.lang.String} object.
436 public static boolean confirmDialog(String title
, String message
) {
437 return MessageDialog
.openQuestion(AbstractUtility
.getShell(), title
, message
);
441 * Displays a message {@link org.eclipse.jface.dialogs.MessageDialog}.
447 public static void messageDialog(final String title
, final Object source
, final String message
) {
448 MessagingUtils
.messageDialog(title
, source
, message
, null, true);
454 * Displays an error {@link org.eclipse.jface.dialogs.MessageDialog}.
459 * The object where the warning was generated (used by log4j)
461 * An informative String to be presented to the user
465 * a Throwable if one exists or null
467 public static void messageDialog(final String title
,
469 final String message
,
471 MessagingUtils
.messageDialog(title
, source
, message
, t
, true);
475 * Displays an error {@link org.eclipse.jface.dialogs.MessageDialog}.
480 * The object where the warning was generated (used by log4j)
482 * An informative String to be presented to the user
486 * a Throwable if one exists or null
488 public static void messageDialog(final String title
,
490 final String message
,
494 Display
.getDefault().asyncExec(new Runnable() {
498 MessageDialog
.openError(AbstractUtility
.getShell(), title
, message
+ getCauseRecursively(t
));
499 Class
<?
extends Object
> clazz
= source
!= null ? source
500 .getClass() : this.getClass();
501 error(clazz
, message
, t
);
507 MessageDialog
.openError(AbstractUtility
.getShell(), title
, message
+ getCauseRecursively(t
));
508 Class
<?
extends Object
> clazz
= source
!= null ? source
.getClass() : TaxeditorStorePlugin
.class;
509 error(clazz
, message
, t
);
513 public static String
getCauseRecursively(Throwable t
) {
518 if(t
.getCause() != null){
519 return getCauseRecursively(t
.getCause());
521 return String
.format("\n\nException: %s\nMessage: %s", t
.getClass().getSimpleName(), t
.getMessage());
526 * Displays a warning {@link org.eclipse.jface.dialogs.MessageDialog}.
532 public static void warningDialog(String title
, Object source
,
534 MessagingUtils
.warningDialog(title
, source
, status
.getMessage());
538 * Standard warning dialog for the case when the application is not yet connected to the datasource
542 public static void noDataSourceWarningDialog(Object source
) {
545 "Application is not connected to a datastore",
547 "The requested operation is only available when "
548 + "connected to a datasource. You may choose a datasource to connect to or create a new one in the datasource view.");
552 * Displays a warning {@link org.eclipse.jface.dialogs.MessageDialog}.
557 * The object where the warning was generated (used by log4j)
559 * An informative String to be presented to the user
561 public static void warningDialog(final String title
, final Object source
, final String message
) {
562 Display
.getDefault().asyncExec(new Runnable() {
566 MessageDialog
.openWarning(AbstractUtility
.getShell(), title
, message
);
567 Class
<?
extends Object
> clazz
= source
!= null ? source
568 .getClass() : AbstractUtility
.class;
569 warn(clazz
, message
);
575 * Displays an information {@link org.eclipse.jface.dialogs.MessageDialog}.
580 public static void informationDialog(final String title
, final IStatus status
) {
581 MessagingUtils
.informationDialog(title
, status
.getMessage());
585 * Displays an information {@link org.eclipse.jface.dialogs.MessageDialog}.
588 * a {@link java.lang.String} object.
590 * a {@link java.lang.String} object.
592 public static void informationDialog(final String title
,
593 final String message
) {
594 Display
.getDefault().asyncExec(new Runnable() {
598 MessageDialog
.openInformation(AbstractUtility
.getShell(), title
, message
);
604 * Open a message box that informs the user about unimplemented
605 * functionality. This method is for developer convenience.
608 * a {@link java.lang.Object} object.
610 public static void notImplementedMessage(Object source
) {
611 warningDialog("Not yet implemented", source
,
612 "This functionality is not yet implemented.");