1 package eu
.etaxonomy
.taxeditor
.model
;
3 import java
.io
.PrintWriter
;
4 import java
.io
.StringWriter
;
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
;
13 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.permission
.SecurityExceptionUtils
;
14 import eu
.etaxonomy
.taxeditor
.store
.internal
.TaxeditorStorePlugin
;
17 * Utility class which handles all the messaging information generated by the
20 * This includes logging as well as dialogs.
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).";
30 * Gets the Log4J logger for a given class
33 * a {@link java.lang.Class} object.
34 * @return a {@link org.apache.log4j.Logger} object.
36 public static Logger
getLog4JLogger(Class clazz
) {
37 return Logger
.getLogger(clazz
);
41 * Logs details from a given Status object
44 * a {@link org.eclipse.core.runtime.IStatus} object.
46 private static void log(IStatus status
) {
47 TaxeditorStorePlugin
.getDefault().getLog().log(status
);
51 * Logs a status object as information.
54 * a {@link org.eclipse.core.runtime.IStatus} object.
56 public static void info(IStatus status
) {
61 * Logs a string as information.
64 * a {@link java.lang.String} object.
66 public static void info(String message
) {
67 IStatus status
= new Status(IStatus
.INFO
, AbstractUtility
.getPluginId(), message
);
72 * Logs an exception from a given source as a warning.
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
);
84 * Logs a status object from a given source as a warning.
89 public static void warn(Class source
, IStatus status
) {
90 MessagingUtils
.getLog4JLogger(source
).warn(status
.getMessage(), status
.getException());
95 * Logs a string from a given source as a warning.
99 * a {@link java.lang.Class} object.
101 * a {@link java.lang.String} object.
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
);
110 * Logs a status object from a given source as an error.
114 * a {@link java.lang.Class} object.
116 * a {@link org.eclipse.core.runtime.IStatus} object.
118 public static void error(Class source
, IStatus status
) {
119 getLog4JLogger(source
)
120 .error(status
.getMessage(), status
.getException());
125 * Logs a string and exception from a given source as an error.
129 * a {@link java.lang.Class} object.
131 * a {@link java.lang.String} object.
133 * a {@link java.lang.Throwable} object.
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
);
143 * Logs an exception from a given source as an error.
147 * a {@link java.lang.Class} object.
149 * a {@link java.lang.Throwable} object.
151 public static void error(Class source
, Throwable t
) {
152 error(source
.getClass(), t
.getMessage(), t
);
156 * Displays a {@link eu.etaxonomy.taxeditor.model.CdmErrorDialog}.
159 * a {@link java.lang.String} object.
161 * a {@link java.lang.Object} object.
163 * a {@link org.eclipse.core.runtime.IStatus} object.
165 public static void errorDialog(final String title
,
167 final String message
,
168 final IStatus status
) {
170 Display
.getDefault().asyncExec(new Runnable() {
174 CdmErrorDialog ced
= new CdmErrorDialog(AbstractUtility
.getShell(), title
, message
, status
);
176 Class
<?
extends Object
> clazz
= source
!= null ? source
177 .getClass() : this.getClass();
178 error(clazz
, status
);
184 * Displays a {@link eu.etaxonomy.taxeditor.model.CdmErrorDialog}.
192 public static void errorDialog(final String title
,
194 final String message
,
195 final String pluginId
,
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();
205 finalMessage
+= MessagingUtils
.CONTACT_MESSAGE
;
207 IStatus status
= new Status(IStatus
.ERROR
,
210 new Exception(sw
.toString()));
211 errorDialog(title
, source
, message
, status
);
217 * Displays a {@link eu.etaxonomy.taxeditor.model.CdmErrorDialog}.
220 * a {@link java.lang.String} object.
222 * a {@link java.lang.Object} object.
224 * a {@link org.eclipse.core.runtime.IStatus} object.
226 public static void errorDialog(final String title
,
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);
234 errorDialog(title
, source
, message
, status
);
240 * Displays a {@link eu.etaxonomy.taxeditor.model.CdmErrorDialog}.
243 * a {@link java.lang.String} object.
245 * a {@link java.lang.Object} object.
247 * a {@link org.eclipse.core.runtime.IStatus} object.
249 public static void errorDialog(final String title
, final Object source
,
250 final IStatus status
) {
251 errorDialog(title
, source
, null, status
);
255 * Displays a dialog for an exception occurring in an operation.
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.
262 * a {@link java.lang.String} object.
264 * a {@link java.lang.Object} object.
266 * a {@link org.eclipse.core.runtime.IStatus} object.
268 public static void operationDialog(final Object source
,
270 final String pluginId
,
271 final String operationlabel
,
274 Display
.getDefault().asyncExec(new Runnable() {
278 MultiStatus info
= null;
281 // FIXME cannot access TaxonomicEditorPlugin.PLUGIN_ID from here
282 // String PID = TaxonomicEditorPlugin.PLUGIN_ID;
283 String PID
= "eu.etaxonomy.taxeditor.application";
285 // checking security exceptions for every operation
286 RuntimeException securityRuntimeException
= SecurityExceptionUtils
.findSecurityRuntimeException(ex
);
288 // in case of a security exception it is a warning, else it is an error
289 if(securityRuntimeException
!= null){
290 title
= "Your changes could not be saved!";
291 warningDialog(title
, source
, String
.format("You are missing sufficient permissions for the operation \"%s\". %s", operationlabel
, hint
));
293 title
= "Error executing operation";
294 errorDialog(title
, source
, String
.format("An error occured while executing %s. %s", operationlabel
, hint
), pluginId
, ex
, true);
307 * Displays a question {@link org.eclipse.jface.dialogs.MessageDialog}.
310 * a {@link java.lang.String} object.
312 * a {@link java.lang.String} object.
315 public static boolean confirmDialog(String title
, String message
) {
316 return MessageDialog
.openQuestion(AbstractUtility
.getShell(), title
, message
);
320 * Displays a message {@link org.eclipse.jface.dialogs.MessageDialog}.
326 public static void messageDialog(final String title
, final Object source
, final String message
) {
327 MessagingUtils
.messageDialog(title
, source
, message
, null, true);
333 * Displays an error {@link org.eclipse.jface.dialogs.MessageDialog}.
338 * The object where the warning was generated (used by log4j)
340 * An informative String to be presented to the user
344 * a Throwable if one exists or null
346 public static void messageDialog(final String title
,
348 final String message
,
350 MessagingUtils
.messageDialog(title
, source
, message
, t
, true);
354 * Displays an error {@link org.eclipse.jface.dialogs.MessageDialog}.
359 * The object where the warning was generated (used by log4j)
361 * An informative String to be presented to the user
365 * a Throwable if one exists or null
367 public static void messageDialog(final String title
,
369 final String message
,
373 Display
.getDefault().asyncExec(new Runnable() {
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
);
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
);
392 public static String
getCauseRecursively(Throwable t
) {
397 if(t
.getCause() != null){
398 return getCauseRecursively(t
.getCause());
400 return String
.format("\n\nException: %s\nMessage: %s", t
.getClass().getSimpleName(), t
.getMessage());
405 * Displays a warning {@link org.eclipse.jface.dialogs.MessageDialog}.
411 public static void warningDialog(String title
, Object source
,
413 MessagingUtils
.warningDialog(title
, source
, status
.getMessage());
417 * Displays a warning {@link org.eclipse.jface.dialogs.MessageDialog}.
422 * The object where the warning was generated (used by log4j)
424 * An informative String to be presented to the user
426 public static void warningDialog(final String title
, final Object source
, final String message
) {
427 Display
.getDefault().asyncExec(new Runnable() {
431 MessageDialog
.openWarning(AbstractUtility
.getShell(), title
, message
);
432 Class
<?
extends Object
> clazz
= source
!= null ? source
433 .getClass() : AbstractUtility
.class;
434 warn(clazz
, message
);
440 * Displays an information {@link org.eclipse.jface.dialogs.MessageDialog}.
445 public static void informationDialog(final String title
, final IStatus status
) {
446 MessagingUtils
.informationDialog(title
, status
.getMessage());
450 * Displays an information {@link org.eclipse.jface.dialogs.MessageDialog}.
453 * a {@link java.lang.String} object.
455 * a {@link java.lang.String} object.
457 public static void informationDialog(final String title
,
458 final String message
) {
459 Display
.getDefault().asyncExec(new Runnable() {
463 MessageDialog
.openInformation(AbstractUtility
.getShell(), title
, message
);
469 * Open a message box that informs the user about unimplemented
470 * functionality. This method is for developer convenience.
473 * a {@link java.lang.Object} object.
475 public static void notImplementedMessage(Object source
) {
476 warningDialog("Not yet implemented", source
,
477 "This functionality is not yet implemented.");