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
.log4j
.Logger
;
9 import org
.eclipse
.core
.runtime
.IStatus
;
10 import org
.eclipse
.core
.runtime
.MultiStatus
;
11 import org
.eclipse
.core
.runtime
.Status
;
12 import org
.eclipse
.jface
.dialogs
.MessageDialog
;
13 import org
.eclipse
.swt
.widgets
.Display
;
15 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.permission
.SecurityExceptionUtils
;
16 import eu
.etaxonomy
.taxeditor
.store
.internal
.TaxeditorStorePlugin
;
19 * Utility class which handles all the messaging information generated by the
22 * This includes logging as well as dialogs.
27 public class MessagingUtils
{
28 public final static String UNEXPECTED_ERROR_MESSAGE
= "This is an unexpected error.";
29 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).";
32 * Gets the Log4J logger for a given class
35 * a {@link java.lang.Class} object.
36 * @return a {@link org.apache.log4j.Logger} object.
38 public static Logger
getLog4JLogger(Class clazz
) {
39 return Logger
.getLogger(clazz
);
43 * Logs details from a given Status object
46 * a {@link org.eclipse.core.runtime.IStatus} object.
48 private static void log(IStatus status
) {
49 TaxeditorStorePlugin
.getDefault().getLog().log(status
);
53 * Logs a status object as information.
56 * a {@link org.eclipse.core.runtime.IStatus} object.
58 public static void info(IStatus status
) {
63 * Logs a string as information.
66 * a {@link java.lang.String} object.
68 public static void info(String message
) {
69 IStatus status
= new Status(IStatus
.INFO
, AbstractUtility
.getPluginId(), message
);
74 * Logs an exception from a given source as a warning.
79 public static void warn(Class source
, Throwable t
) {
80 IStatus status
= new Status(IStatus
.WARNING
, AbstractUtility
.getPluginId(), t
.getMessage(), t
);
81 MessagingUtils
.getLog4JLogger(source
).warn(t
);
86 * Logs a status object from a given source as a warning.
91 public static void warn(Class source
, IStatus status
) {
92 MessagingUtils
.getLog4JLogger(source
).warn(status
.getMessage(), status
.getException());
97 * Logs a string from a given source as a warning.
101 * a {@link java.lang.Class} object.
103 * a {@link java.lang.String} object.
105 public static void warn(Class source
, String message
) {
106 IStatus status
= new Status(IStatus
.WARNING
, AbstractUtility
.getPluginId(), message
);
107 MessagingUtils
.getLog4JLogger(source
).warn(message
);
112 * Logs a status object from a given source as an error.
116 * a {@link java.lang.Class} object.
118 * a {@link org.eclipse.core.runtime.IStatus} object.
120 public static void error(Class source
, IStatus status
) {
121 getLog4JLogger(source
)
122 .error(status
.getMessage(), status
.getException());
127 * Logs a string and exception from a given source as an error.
131 * a {@link java.lang.Class} object.
133 * a {@link java.lang.String} object.
135 * a {@link java.lang.Throwable} object.
137 public static void error(Class source
, String message
, Throwable t
) {
138 IStatus status
= new Status(IStatus
.ERROR
, AbstractUtility
.getPluginId(), message
, t
);
139 error(source
, status
);
145 * Logs an exception from a given source as an error.
149 * a {@link java.lang.Class} object.
151 * a {@link java.lang.Throwable} object.
153 public static void error(Class source
, Throwable t
) {
154 error(source
.getClass(), t
.getMessage(), t
);
158 * Displays a {@link eu.etaxonomy.taxeditor.model.CdmErrorDialog}.
161 * a {@link java.lang.String} object.
163 * a {@link java.lang.Object} object.
165 * a {@link org.eclipse.core.runtime.IStatus} object.
167 private static void errorDialog(final String title
,
169 final String message
,
170 final IStatus status
) {
172 Display
.getDefault().asyncExec(new Runnable() {
176 CdmErrorDialog ced
= new CdmErrorDialog(AbstractUtility
.getShell(), title
, message
, status
);
178 Class
<?
extends Object
> clazz
= source
!= null ? source
.getClass() : this.getClass();
179 error(clazz
, status
);
184 private static void errorDialog(final String title
,
187 final MultiStatus status
) {
189 Display
.getDefault().asyncExec(new Runnable() {
193 CdmErrorDialog ced
= new CdmErrorDialog(AbstractUtility
.getShell(), title
, t
.getMessage(), status
);
195 Class
<?
extends Object
> clazz
= source
!= null ? source
.getClass() : this.getClass();
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 singleStatus
= new Status(IStatus
.ERROR
,
205 new Exception(sw
.toString()));
207 error(clazz
, singleStatus
);
213 * Displays a {@link eu.etaxonomy.taxeditor.model.CdmErrorDialog}.
221 public static void errorDialog(final String title
,
223 final String message
,
224 final String pluginId
,
226 boolean addContactMesg
) {
229 StringBuffer sbStackTrace
= new StringBuffer();
231 // We need to build a MultiStatus object since the simple
232 // idea of writing out the stack trace as a single string
233 // leads to a single line on windows
234 List
<Status
> childStatuses
= new ArrayList
<Status
>();
235 for (StackTraceElement ste
: t
.getStackTrace()) {
236 // build & add status
237 childStatuses
.add(new Status(IStatus
.ERROR
, pluginId
, "at " + ste
.toString()));
240 // build message with contact info
241 String finalMessage
= message
;
243 if(finalMessage
== null || finalMessage
.isEmpty()) {
248 finalMessage
+= MessagingUtils
.CONTACT_MESSAGE
;
251 MultiStatus ms
= new MultiStatus(pluginId
,
253 childStatuses
.toArray(new Status
[] {}),
257 errorDialog(title
, source
, t
, ms
);
262 * Displays a dialog for an exception occurring in an operation.
264 * This will be either a {@link eu.etaxonomy.taxeditor.model.CdmErrorDialog} in case of a
265 * security runtime exception or a warning {@link org.eclipse.jface.dialogs.MessageDialog} in
266 * case of any other exception.
269 * a {@link java.lang.String} object.
271 * a {@link java.lang.Object} object.
273 * a {@link org.eclipse.core.runtime.IStatus} object.
275 public static void operationDialog(final Object source
,
277 final String pluginId
,
278 final String operationlabel
,
281 Display
.getDefault().asyncExec(new Runnable() {
285 MultiStatus info
= null;
288 // FIXME cannot access TaxonomicEditorPlugin.PLUGIN_ID from here
289 // String PID = TaxonomicEditorPlugin.PLUGIN_ID;
290 String PID
= "eu.etaxonomy.taxeditor.application";
292 // checking security exceptions for every operation
293 RuntimeException securityRuntimeException
= SecurityExceptionUtils
.findSecurityRuntimeException(ex
);
295 // in case of a security exception it is a warning, else it is an error
296 if(securityRuntimeException
!= null){
297 title
= "Your changes could not be saved!";
298 warningDialog(title
, source
, String
.format("You are missing sufficient permissions for the operation \"%s\". %s", operationlabel
, hint
));
300 title
= "Error executing operation";
301 errorDialog(title
, source
, String
.format("An error occured while executing %s. %s", operationlabel
, hint
), pluginId
, ex
, true);
314 * Displays a question {@link org.eclipse.jface.dialogs.MessageDialog}.
317 * a {@link java.lang.String} object.
319 * a {@link java.lang.String} object.
322 public static boolean confirmDialog(String title
, String message
) {
323 return MessageDialog
.openQuestion(AbstractUtility
.getShell(), title
, message
);
327 * Displays a message {@link org.eclipse.jface.dialogs.MessageDialog}.
333 public static void messageDialog(final String title
, final Object source
, final String message
) {
334 MessagingUtils
.messageDialog(title
, source
, message
, null, true);
340 * Displays an error {@link org.eclipse.jface.dialogs.MessageDialog}.
345 * The object where the warning was generated (used by log4j)
347 * An informative String to be presented to the user
351 * a Throwable if one exists or null
353 public static void messageDialog(final String title
,
355 final String message
,
357 MessagingUtils
.messageDialog(title
, source
, message
, t
, true);
361 * Displays an error {@link org.eclipse.jface.dialogs.MessageDialog}.
366 * The object where the warning was generated (used by log4j)
368 * An informative String to be presented to the user
372 * a Throwable if one exists or null
374 public static void messageDialog(final String title
,
376 final String message
,
380 Display
.getDefault().asyncExec(new Runnable() {
384 MessageDialog
.openError(AbstractUtility
.getShell(), title
, message
+ getCauseRecursively(t
));
385 Class
<?
extends Object
> clazz
= source
!= null ? source
386 .getClass() : this.getClass();
387 error(clazz
, message
, t
);
393 MessageDialog
.openError(AbstractUtility
.getShell(), title
, message
+ getCauseRecursively(t
));
394 Class
<?
extends Object
> clazz
= source
!= null ? source
.getClass() : TaxeditorStorePlugin
.class;
395 error(clazz
, message
, t
);
399 public static String
getCauseRecursively(Throwable t
) {
404 if(t
.getCause() != null){
405 return getCauseRecursively(t
.getCause());
407 return String
.format("\n\nException: %s\nMessage: %s", t
.getClass().getSimpleName(), t
.getMessage());
412 * Displays a warning {@link org.eclipse.jface.dialogs.MessageDialog}.
418 public static void warningDialog(String title
, Object source
,
420 MessagingUtils
.warningDialog(title
, source
, status
.getMessage());
424 * Displays a warning {@link org.eclipse.jface.dialogs.MessageDialog}.
429 * The object where the warning was generated (used by log4j)
431 * An informative String to be presented to the user
433 public static void warningDialog(final String title
, final Object source
, final String message
) {
434 Display
.getDefault().asyncExec(new Runnable() {
438 MessageDialog
.openWarning(AbstractUtility
.getShell(), title
, message
);
439 Class
<?
extends Object
> clazz
= source
!= null ? source
440 .getClass() : AbstractUtility
.class;
441 warn(clazz
, message
);
447 * Displays an information {@link org.eclipse.jface.dialogs.MessageDialog}.
452 public static void informationDialog(final String title
, final IStatus status
) {
453 MessagingUtils
.informationDialog(title
, status
.getMessage());
457 * Displays an information {@link org.eclipse.jface.dialogs.MessageDialog}.
460 * a {@link java.lang.String} object.
462 * a {@link java.lang.String} object.
464 public static void informationDialog(final String title
,
465 final String message
) {
466 Display
.getDefault().asyncExec(new Runnable() {
470 MessageDialog
.openInformation(AbstractUtility
.getShell(), title
, message
);
476 * Open a message box that informs the user about unimplemented
477 * functionality. This method is for developer convenience.
480 * a {@link java.lang.Object} object.
482 public static void notImplementedMessage(Object source
) {
483 warningDialog("Not yet implemented", source
,
484 "This functionality is not yet implemented.");