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