Refactory
[phpeclipse.git] / net.sourceforge.phpeclipse.ui / src / net / sourceforge / phpdt / internal / ui / util / ExceptionHandler.java
1 package net.sourceforge.phpdt.internal.ui.util;
2
3 import java.io.StringWriter;
4 import java.lang.reflect.InvocationTargetException;
5
6 import net.sourceforge.phpdt.internal.ui.PHPUIMessages;
7 //import net.sourceforge.phpeclipse.PHPeclipsePlugin;
8 import net.sourceforge.phpeclipse.ui.WebUI;
9
10 import org.eclipse.core.runtime.CoreException;
11 import org.eclipse.core.runtime.IStatus;
12 import org.eclipse.core.runtime.Status;
13 import org.eclipse.jface.dialogs.ErrorDialog;
14 import org.eclipse.jface.dialogs.MessageDialog;
15 import org.eclipse.swt.widgets.Shell;
16
17 public class ExceptionHandler {
18         private static ExceptionHandler fgInstance = new ExceptionHandler();
19
20         public static void log(Throwable t, String message) {
21                 WebUI.getDefault().getLog().log(
22                                 new Status(IStatus.ERROR, WebUI.PLUGIN_ID,
23                                                 IStatus.ERROR, message, t));
24         }
25
26         public static void handle(CoreException e, String title, String message) {
27                 handle(e, WebUI.getActiveWorkbenchShell(), title, message);
28         }
29
30         public static void handle(CoreException e, Shell parent, String title,
31                         String message) {
32                 fgInstance.perform(e, parent, title, message);
33         }
34
35         public static void handle(InvocationTargetException e, String title,
36                         String message) {
37                 handle(e, WebUI.getActiveWorkbenchShell(), title, message);
38         }
39
40         public static void handle(InvocationTargetException e, Shell parent,
41                         String title, String message) {
42                 fgInstance.perform(e, parent, title, message);
43         }
44
45         protected void perform(CoreException e, Shell shell, String title,
46                         String message) {
47                 WebUI.log(e);
48                 IStatus status = e.getStatus();
49                 if (status != null) {
50                         ErrorDialog.openError(shell, title, message, status);
51                 } else {
52                         displayMessageDialog(e, e.getMessage(), shell, title, message);
53                 }
54         }
55
56         protected void perform(InvocationTargetException e, Shell shell,
57                         String title, String message) {
58                 Throwable target = e.getTargetException();
59                 if (target instanceof CoreException) {
60                         perform((CoreException) target, shell, title, message);
61                 } else {
62                         WebUI.log(e);
63                         if (e.getMessage() != null && e.getMessage().length() > 0) {
64                                 displayMessageDialog(e, e.getMessage(), shell, title, message);
65                         } else {
66                                 displayMessageDialog(e, target.getMessage(), shell, title,
67                                                 message);
68                         }
69                 }
70         }
71
72         private void displayMessageDialog(Throwable t, String exceptionMessage,
73                         Shell shell, String title, String message) {
74                 StringWriter msg = new StringWriter();
75                 if (message != null) {
76                         msg.write(message);
77                         msg.write("\n\n");
78                 }
79                 if (exceptionMessage == null || exceptionMessage.length() == 0)
80                         msg.write(PHPUIMessages
81                                         .getString("ExceptionDialog.seeErrorLogMessage"));
82                 else
83                         msg.write(exceptionMessage);
84                 MessageDialog.openError(shell, title, msg.toString());
85         }
86 }