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