php formatter based on the JDT java formatter (very early version)
[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
17 public class ExceptionHandler {
18         private static ExceptionHandler fgInstance= new ExceptionHandler();
19         
20         public static void log(Throwable t, String message) {
21                 PHPeclipsePlugin.getDefault().getLog().log(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, String message) {
30                 fgInstance.perform(e, parent, title, message);
31         }
32         
33         public static void handle(InvocationTargetException e, String title, String message) {
34                 handle(e, PHPeclipsePlugin.getActiveWorkbenchShell(), title, message);
35         }
36         
37         public static void handle(InvocationTargetException e, Shell parent, String title, String message) {
38                 fgInstance.perform(e, parent, title, message);
39         }
40
41         protected void perform(CoreException e, Shell shell, String title, String message) {
42                 PHPeclipsePlugin.log(e);
43                 IStatus status= e.getStatus();
44                 if (status != null) {
45                         ErrorDialog.openError(shell, title, message, status);
46                 } else {
47                         displayMessageDialog(e, e.getMessage(), shell, title, message);
48                 }
49         }
50
51         protected void perform(InvocationTargetException e, Shell shell, String title, String message) {
52                 Throwable target= e.getTargetException();
53                 if (target instanceof CoreException) {
54                         perform((CoreException)target, shell, title, message);
55                 } else {
56                         PHPeclipsePlugin.log(e);
57                         if (e.getMessage() != null && e.getMessage().length() > 0) {
58                                 displayMessageDialog(e, e.getMessage(), shell, title, message);
59                         } else {
60                                 displayMessageDialog(e, target.getMessage(), shell, title, message);
61                         }
62                 }
63         }
64
65         private void displayMessageDialog(Throwable t, String exceptionMessage, Shell shell, String title, String message) {
66                 StringWriter msg= new StringWriter();
67                 if (message != null) {
68                         msg.write(message);
69                         msg.write("\n\n");
70                 }
71                 if (exceptionMessage == null || exceptionMessage.length() == 0)
72                         msg.write(PHPUIMessages.getString("ExceptionDialog.seeErrorLogMessage"));
73                 else
74                         msg.write(exceptionMessage);
75                 MessageDialog.openError(shell, title, msg.toString());                  
76         }       
77 }