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
.common
.CdmUtils
;
18 import eu
.etaxonomy
.cdm
.config
.ICdmSource
;
19 import eu
.etaxonomy
.cdm
.test
.integration
.SecurityExceptionUtils
;
20 import eu
.etaxonomy
.taxeditor
.remoting
.source
.CdmRemoteSource
;
21 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
22 import eu
.etaxonomy
.taxeditor
.store
.internal
.TaxeditorStorePlugin
;
25 * Utility class which handles all the messaging information generated by the
28 * This includes logging as well as dialogs.
33 public class MessagingUtils
{
34 public final static String UNEXPECTED_ERROR_MESSAGE
= "This is an unexpected error.";
35 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).";
36 public final static String DEFAULT_MESSAGE
= "Error thrown but no associated message";
37 public final static String CONNECTION_FAILURE_MESSAGE
= "The connection to the remote server has been broken";
38 public final static String REMOTE_ACCESS_FAILURE_MESSAGE
= "Maybe the server is currently not available. If the problem persists please contact the server admin with the error trace below.";
39 public static final String WIDGET_IS_DISPOSED_MESSAGE
= "A widget was called, which was already disposed";
40 public static final String ACCESS_DENIED
= "The connection to the server could not be esablished because the access was denied.";
42 * Gets the Log4J logger for a given class
45 * a {@link java.lang.Class} object.
46 * @return a {@link org.apache.log4j.Logger} object.
48 public static Logger
getLog4JLogger(Class clazz
) {
49 return Logger
.getLogger(clazz
);
53 * Logs details from a given Status object
56 * a {@link org.eclipse.core.runtime.IStatus} object.
58 private static void log(IStatus status
) {
59 TaxeditorStorePlugin
.getDefault().getLog().log(status
);
63 * Logs a status object as information.
66 * a {@link org.eclipse.core.runtime.IStatus} object.
68 public static void info(IStatus status
) {
73 * Logs a string as information.
76 * a {@link java.lang.String} object.
78 public static void info(String message
) {
79 IStatus status
= new Status(IStatus
.INFO
, AbstractUtility
.getPluginId(), message
);
84 * Logs an exception from a given source as a warning.
89 public static void warn(Class source
, Throwable t
) {
90 IStatus status
= new Status(IStatus
.WARNING
, AbstractUtility
.getPluginId(), t
.getMessage(), t
);
91 MessagingUtils
.getLog4JLogger(source
).warn(t
);
96 * Logs a status object from a given source as a warning.
101 public static void warn(Class source
, IStatus status
) {
102 MessagingUtils
.getLog4JLogger(source
).warn(status
.getMessage(), status
.getException());
107 * Logs a string from a given source as a warning.
111 * a {@link java.lang.Class} object.
113 * a {@link java.lang.String} object.
115 public static void warn(Class source
, String message
) {
116 IStatus status
= new Status(IStatus
.WARNING
, AbstractUtility
.getPluginId(), message
);
117 MessagingUtils
.getLog4JLogger(source
).warn(message
);
122 * Logs a status object from a given source as an error.
126 * a {@link java.lang.Class} object.
128 * a {@link org.eclipse.core.runtime.IStatus} object.
130 public static void error(Class source
, IStatus status
) {
131 getLog4JLogger(source
)
132 .error(status
.getMessage(), status
.getException());
137 * Logs a string and exception from a given source as an error.
141 * a {@link java.lang.Class} object.
143 * a {@link java.lang.String} object.
145 * a {@link java.lang.Throwable} object.
147 public static void error(Class source
, String message
, Throwable t
) {
148 IStatus status
= new Status(IStatus
.ERROR
, AbstractUtility
.getPluginId(), message
, t
);
149 error(source
, status
);
155 * Logs an exception from a given source as an error.
159 * a {@link java.lang.Class} object.
161 * a {@link java.lang.Throwable} object.
163 public static void error(Class source
, Throwable t
) {
164 error(source
.getClass(), t
.getMessage(), t
);
170 * Returns a list of strings, providing info on,
173 * - server (address + source name)
174 * - db schema version
178 public static List
<String
> getContextInfo() {
179 List
<String
> contextInfo
= new ArrayList
<String
>();
181 String contextPath
= "";
182 String schemaVersion
= "";
187 version
= Platform
.getBundle("eu.etaxonomy.taxeditor.application").getHeaders().get(org
.osgi
.framework
.Constants
.BUNDLE_VERSION
);
189 ICdmSource activeCdmSource
= CdmStore
.getActiveCdmSource();
190 if(activeCdmSource
!= null ) {
191 login
= CdmStore
.getLoginManager().getAuthenticatedUser().getUsername();
192 name
= activeCdmSource
.getName();
193 schemaVersion
= activeCdmSource
.getDbSchemaVersion();
194 server
= activeCdmSource
.getServer();
195 if(activeCdmSource
instanceof CdmRemoteSource
){
196 contextPath
= ((CdmRemoteSource
) activeCdmSource
).getContextPath();
197 if (contextPath
!= null && contextPath
.startsWith("cdmserver/")){
198 contextPath
= contextPath
.substring("cdmserver/".length());
203 } catch (Exception e
) {
206 contextInfo
.add("login : " + login
);
207 contextInfo
.add("editor version : " + version
);
208 contextInfo
.add("server : " + server
+ " (" + name
+ ")" + (CdmUtils
.isNotBlank(contextPath
)?
" / "+contextPath
:""));
209 contextInfo
.add("schema version : " + schemaVersion
);
210 contextInfo
.add("os : " + System
.getProperty("os.name")+" "+System
.getProperty("os.version")+" "+System
.getProperty("os.arch"));
211 contextInfo
.add("java : "+System
.getProperty("java.version"));
216 public static String
getStackTraceAndContextInfo(Throwable t
, List
<String
> contextInfo
) {
217 StringBuffer stackTraceAndContextInfo
= new StringBuffer();
218 Throwable throwable
= t
;
220 for(String infoItem
: contextInfo
) {
221 stackTraceAndContextInfo
.append(infoItem
+ System
.getProperty("line.separator"));
224 StringWriter sw
= new StringWriter();
226 if(throwable
== null) {
227 throwable
= getDefaultThrowable();
229 throwable
.printStackTrace(new PrintWriter(sw
));
231 stackTraceAndContextInfo
.append(sw
.toString());
233 return stackTraceAndContextInfo
.toString();
236 public static String
getContextInfo(List
<String
> contextInfo
) {
237 StringBuffer scontextInfoStringBuffer
= new StringBuffer();
240 for(String infoItem
: contextInfo
) {
241 scontextInfoStringBuffer
.append(infoItem
+ System
.getProperty("line.separator"));
246 return scontextInfoStringBuffer
.toString();
249 private static Throwable
getDefaultThrowable() {
250 return new Throwable("Error thrown but no associated exception");
256 * Displays a {@link eu.etaxonomy.taxeditor.model.CdmErrorDialog}.
265 private static void errorDialog(final String title
,
268 final List
<String
> contextInfo
,
269 final String message
,
270 final MultiStatus status
,
271 final boolean showReason
) {
273 Display
.getDefault().asyncExec(new Runnable() {
277 String stackTraceWithContext
= getStackTraceAndContextInfo(t
, contextInfo
);
278 CdmErrorDialog ced
= new CdmErrorDialog(AbstractUtility
.getShell(), title
, message
, status
, stackTraceWithContext
, showReason
);
280 Class
<?
extends Object
> clazz
= source
!= null ? source
.getClass() : this.getClass();
283 IStatus singleStatus
= new Status(IStatus
.ERROR
,
286 new Exception(stackTraceWithContext
));
288 error(clazz
, singleStatus
);
293 public static void errorDialog(final String title
,
295 final String message
,
296 final String pluginId
,
298 boolean addContactMesg
) {
299 errorDialog(title
, source
, message
, pluginId
, t
, addContactMesg
, true);
303 * Displays a {@link eu.etaxonomy.taxeditor.model.CdmErrorDialog}.
311 public static void errorDialog(final String title
,
313 final String message
,
314 final String pluginId
,
316 boolean addContactMesg
,
317 boolean showReason
) {
319 Throwable throwable
= t
;
320 StringBuffer sbStackTrace
= new StringBuffer();
322 // We need to build a MultiStatus object since the simple
323 // idea of writing out the stack trace as a single string
324 // leads to a single line on windows
325 List
<Status
> childStatuses
= new ArrayList
<Status
>();
328 List
<String
> contextInfo
= getContextInfo();
329 for(String infoItem
: contextInfo
) {
330 childStatuses
.add(new Status(IStatus
.ERROR
, pluginId
, infoItem
));
333 if(throwable
== null) {
334 throwable
= getDefaultThrowable();
340 for(Throwable th
: ExceptionUtils
.getThrowables(throwable
)) {
341 // add main exception
343 for (StackTraceElement ste
: th
.getStackTrace()) {
344 childStatuses
.add(new Status(IStatus
.ERROR
, pluginId
, " at " + ste
.toString()));
347 // add recursive causes
349 childStatuses
.add(new Status(IStatus
.ERROR
, pluginId
, ""));
350 String msg
= th
.toString();
351 childStatuses
.add(new Status(IStatus
.ERROR
, pluginId
, "Caused by : " + msg
));
353 for (StackTraceElement ste
: th
.getStackTrace()) {
354 // add only pre-defined number of trace elements
355 if(traceCount
> maxTraces
) {
356 childStatuses
.add(new Status(IStatus
.ERROR
, pluginId
, " ...."));
359 // build & add status
360 childStatuses
.add(new Status(IStatus
.ERROR
, pluginId
, " at " + ste
.toString()));
367 String finalMessage
= message
;
369 if(finalMessage
== null || finalMessage
.isEmpty()) {
370 finalMessage
= DEFAULT_MESSAGE
;
374 // add edit support contact info to message
375 finalMessage
+= MessagingUtils
.CONTACT_MESSAGE
;
378 MultiStatus ms
= new MultiStatus(pluginId
,
380 childStatuses
.toArray(new Status
[] {}),
381 throwable
.toString(),
384 errorDialog(title
, source
, throwable
, contextInfo
, finalMessage
, ms
, showReason
);
388 * Displays a dialog for an exception occurring in an operation.
390 * This will be either a {@link eu.etaxonomy.taxeditor.model.CdmErrorDialog} in case of a
391 * security runtime exception or a warning {@link org.eclipse.jface.dialogs.MessageDialog} in
392 * case of any other exception.
395 * a {@link java.lang.String} object.
397 * a {@link java.lang.Object} object.
399 * a {@link org.eclipse.core.runtime.IStatus} object.
401 public static void operationDialog(final Object source
,
403 final String pluginId
,
404 final String operationlabel
,
407 Display
.getDefault().asyncExec(new Runnable() {
411 MultiStatus info
= null;
414 // FIXME cannot access TaxonomicEditorPlugin.PLUGIN_ID from here
415 // String PID = TaxonomicEditorPlugin.PLUGIN_ID;
416 String PID
= "eu.etaxonomy.taxeditor.application";
418 // checking security exceptions for every operation
419 RuntimeException securityRuntimeException
= SecurityExceptionUtils
.findSecurityRuntimeException(ex
);
421 // in case of a security exception it is a warning, else it is an error
422 if(securityRuntimeException
!= null){
423 title
= "Your changes could not be saved!";
424 warningDialog(title
, source
, String
.format("You are missing sufficient permissions for the operation \"%s\". %s", operationlabel
, hint
));
426 title
= "Error executing operation";
427 errorDialog(title
, source
, String
.format("An error occurred while executing %s. %s", operationlabel
, hint
), pluginId
, ex
, true);
440 * Displays a question {@link org.eclipse.jface.dialogs.MessageDialog}.
443 * a {@link java.lang.String} object.
445 * a {@link java.lang.String} object.
448 public static boolean confirmDialog(String title
, String message
) {
449 return MessageDialog
.openQuestion(AbstractUtility
.getShell(), title
, message
);
452 public static int confirmDialog(String title
, String message
, String
...labels
){
453 MessageDialog dialog
=new MessageDialog(AbstractUtility
.getShell(), title
, null, message
, MessageDialog
.QUESTION
,labels
, 0);
454 int result
= dialog
.open();
459 * Displays a message {@link org.eclipse.jface.dialogs.MessageDialog}.
465 public static void messageDialog(final String title
, final Object source
, final String message
) {
466 MessagingUtils
.messageDialog(title
, source
, message
, null, true);
472 * Displays an error {@link org.eclipse.jface.dialogs.MessageDialog}.
477 * The object where the warning was generated (used by log4j)
479 * An informative String to be presented to the user
483 * a Throwable if one exists or null
485 public static void messageDialog(final String title
,
487 final String message
,
489 MessagingUtils
.messageDialog(title
, source
, message
, t
, true);
493 * Displays an error {@link org.eclipse.jface.dialogs.MessageDialog}.
498 * The object where the warning was generated (used by log4j)
500 * An informative String to be presented to the user
504 * a Throwable if one exists or null
506 public static void messageDialog(final String title
,
508 final String message
,
512 Display
.getDefault().asyncExec(new Runnable() {
516 MessageDialog
.openError(AbstractUtility
.getShell(), title
, message
+ getCauseRecursively(t
));
517 Class
<?
extends Object
> clazz
= source
!= null ? source
518 .getClass() : this.getClass();
519 error(clazz
, message
, t
);
525 MessageDialog
.openError(AbstractUtility
.getShell(), title
, message
+ getCauseRecursively(t
));
526 Class
<?
extends Object
> clazz
= source
!= null ? source
.getClass() : TaxeditorStorePlugin
.class;
527 error(clazz
, message
, t
);
531 public static String
getCauseRecursively(Throwable t
) {
536 if(t
.getCause() != null){
537 return getCauseRecursively(t
.getCause());
539 return String
.format("\n\nException: %s\nMessage: %s", t
.getClass().getSimpleName(), t
.getMessage());
544 * Displays a warning {@link org.eclipse.jface.dialogs.MessageDialog}.
550 public static void warningDialog(String title
, Object source
,
552 MessagingUtils
.warningDialog(title
, source
, status
.getMessage());
556 * Standard warning dialog for the case when the application is not yet connected to the datasource
560 public static void noDataSourceWarningDialog(Object source
) {
563 "Application is not connected to a datastore",
565 "The requested operation is only available when "
566 + "connected to a datasource. You may choose a datasource to connect to or create a new one in the datasource view.");
570 * Standard warning dialog for the case when the datasource is not available
574 public static void dataSourceNotAvailableWarningDialog(Object source
) {
577 "The datasource is not available",
579 "The editor is not connected to a datasource. Maybe the datasource is not available.");
584 * Displays a warning {@link org.eclipse.jface.dialogs.MessageDialog}.
589 * The object where the warning was generated (used by log4j)
591 * An informative String to be presented to the user
593 public static void warningDialog(final String title
, final Object source
, final String message
) {
594 Display
.getDefault().asyncExec(new Runnable() {
598 MessageDialog
.openWarning(AbstractUtility
.getShell(), title
, message
);
599 Class
<?
extends Object
> clazz
= source
!= null ? source
600 .getClass() : AbstractUtility
.class;
601 warn(clazz
, message
);
607 * Displays an information {@link org.eclipse.jface.dialogs.MessageDialog}.
612 public static void informationDialog(final String title
, final IStatus status
) {
613 MessagingUtils
.informationDialog(title
, status
.getMessage());
617 * Displays an information {@link org.eclipse.jface.dialogs.MessageDialog}.
620 * a {@link java.lang.String} object.
622 * a {@link java.lang.String} object.
624 public static void informationDialog(final String title
,
625 final String message
) {
626 Display
.getDefault().asyncExec(new Runnable() {
630 MessageDialog
.openInformation(AbstractUtility
.getShell(), title
, message
);
636 * Open a message box that informs the user about unimplemented
637 * functionality. This method is for developer convenience.
640 * a {@link java.lang.Object} object.
642 public static void notImplementedMessage(Object source
) {
643 warningDialog("Not yet implemented", source
,
644 "This functionality is not yet implemented.");