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 // Usually the status contains only the first line of the stack trace.
198 // For the unexpected messages we need the entire stack trace so we
199 // create a new status with the entire stacktrace
200 StringWriter sw
= new StringWriter();
201 t
.printStackTrace(new PrintWriter(sw
));
202 IStatus status
= new Status(IStatus
.ERROR
,
205 new Exception(sw
.toString()));
206 errorDialog(title
, source
, message
, status
);
210 * Displays a {@link eu.etaxonomy.taxeditor.model.CdmErrorDialog}.
213 * a {@link java.lang.String} object.
215 * a {@link java.lang.Object} object.
217 * a {@link org.eclipse.core.runtime.IStatus} object.
219 public static void errorDialog(final String title
,
221 final String message
,
222 final IStatus status
,
223 final boolean showStackTrace
) {
224 if(showStackTrace
&& status
.getException() != null) {
225 errorDialog(title
, source
, status
.getPlugin(), message
, status
.getException());
227 errorDialog(title
, source
, message
, status
);
233 * Displays a {@link eu.etaxonomy.taxeditor.model.CdmErrorDialog}.
236 * a {@link java.lang.String} object.
238 * a {@link java.lang.Object} object.
240 * a {@link org.eclipse.core.runtime.IStatus} object.
242 public static void errorDialog(final String title
, final Object source
,
243 final IStatus status
) {
244 errorDialog(title
, source
, null, status
);
248 * Displays a dialog for an exception occurring in an operation.
250 * This will be either a {@link eu.etaxonomy.taxeditor.model.CdmErrorDialog} in case of a
251 * security runtime exception or a warning {@link org.eclipse.jface.dialogs.MessageDialog} in
252 * case of any other exception.
255 * a {@link java.lang.String} object.
257 * a {@link java.lang.Object} object.
259 * a {@link org.eclipse.core.runtime.IStatus} object.
261 public static void operationDialog(final Object source
,
263 final String operationlabel
,
266 Display
.getDefault().asyncExec(new Runnable() {
270 MultiStatus info
= null;
273 // FIXME cannot access TaxonomicEditorPlugin.PLUGIN_ID from here
274 // String PID = TaxonomicEditorPlugin.PLUGIN_ID;
275 String PID
= "eu.etaxonomy.taxeditor.application";
277 // checking security exceptions for every operation
278 RuntimeException securityRuntimeException
= SecurityExceptionUtils
.findSecurityRuntimeException(ex
);
280 // in case of a security exception it is a warning, else it is an error
281 if(securityRuntimeException
!= null){
282 title
= "Your changes could not be saved!";
283 warningDialog(title
, source
, String
.format("You are missing sufficient permissions for the operation \"%s\". %s", operationlabel
, hint
));
285 title
= "Error executing operation";
286 errorDialog(title
, source
, String
.format("An error occured while executing %s. %s", operationlabel
, hint
), PID
, ex
);
299 * Displays a question {@link org.eclipse.jface.dialogs.MessageDialog}.
302 * a {@link java.lang.String} object.
304 * a {@link java.lang.String} object.
307 public static boolean confirmDialog(String title
, String message
) {
308 return MessageDialog
.openQuestion(AbstractUtility
.getShell(), title
, message
);
312 * Displays a message {@link org.eclipse.jface.dialogs.MessageDialog}.
318 public static void messageDialog(final String title
, final Object source
, final String message
) {
319 MessagingUtils
.messageDialog(title
, source
, message
, null);
323 * Displays an error {@link org.eclipse.jface.dialogs.MessageDialog}.
328 * The object where the warning was generated (used by log4j)
330 * An informative String to be presented to the user
334 * a Throwable if one exists or null
336 public static void messageDialog(final String title
, final Object source
,
337 final String message
, final Throwable t
) {
338 Display
.getDefault().asyncExec(new Runnable() {
342 MessageDialog
.openError(AbstractUtility
.getShell(), title
, message
+ getCauseRecursively(t
));
343 Class
<?
extends Object
> clazz
= source
!= null ? source
344 .getClass() : this.getClass();
345 error(clazz
, message
, t
);
348 private String
getCauseRecursively(Throwable t
) {
353 if(t
.getCause() != null){
354 return getCauseRecursively(t
.getCause());
356 return String
.format("\n\nException: %s\nMessage: %s", t
.getClass().getSimpleName(), t
.getMessage());
364 * Displays a warning {@link org.eclipse.jface.dialogs.MessageDialog}.
370 public static void warningDialog(String title
, Object source
,
372 MessagingUtils
.warningDialog(title
, source
, status
.getMessage());
376 * Displays a warning {@link org.eclipse.jface.dialogs.MessageDialog}.
381 * The object where the warning was generated (used by log4j)
383 * An informative String to be presented to the user
385 public static void warningDialog(final String title
, final Object source
, final String message
) {
386 Display
.getDefault().asyncExec(new Runnable() {
390 MessageDialog
.openWarning(AbstractUtility
.getShell(), title
, message
);
391 Class
<?
extends Object
> clazz
= source
!= null ? source
392 .getClass() : AbstractUtility
.class;
393 warn(clazz
, message
);
399 * Displays an information {@link org.eclipse.jface.dialogs.MessageDialog}.
404 public static void informationDialog(final String title
, final IStatus status
) {
405 MessagingUtils
.informationDialog(title
, status
.getMessage());
409 * Displays an information {@link org.eclipse.jface.dialogs.MessageDialog}.
412 * a {@link java.lang.String} object.
414 * a {@link java.lang.String} object.
416 public static void informationDialog(final String title
,
417 final String message
) {
418 Display
.getDefault().asyncExec(new Runnable() {
422 MessageDialog
.openInformation(AbstractUtility
.getShell(), title
, message
);
428 * Open a message box that informs the user about unimplemented
429 * functionality. This method is for developer convenience.
432 * a {@link java.lang.Object} object.
434 public static void notImplementedMessage(Object source
) {
435 warningDialog("Not yet implemented", source
,
436 "This functionality is not yet implemented.");