refactory: added UI removed from core plugin.
[phpeclipse.git] / net.sourceforge.phpeclipse.ui / src / net / sourceforge / phpdt / internal / ui / util / ExceptionHandler.java
diff --git a/net.sourceforge.phpeclipse.ui/src/net/sourceforge/phpdt/internal/ui/util/ExceptionHandler.java b/net.sourceforge.phpeclipse.ui/src/net/sourceforge/phpdt/internal/ui/util/ExceptionHandler.java
new file mode 100644 (file)
index 0000000..a5807a9
--- /dev/null
@@ -0,0 +1,86 @@
+package net.sourceforge.phpdt.internal.ui.util;
+
+import java.io.StringWriter;
+import java.lang.reflect.InvocationTargetException;
+
+import net.sourceforge.phpdt.internal.ui.PHPUIMessages;
+//import net.sourceforge.phpeclipse.PHPeclipsePlugin;
+import net.sourceforge.phpeclipse.ui.WebUI;
+
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.Status;
+import org.eclipse.jface.dialogs.ErrorDialog;
+import org.eclipse.jface.dialogs.MessageDialog;
+import org.eclipse.swt.widgets.Shell;
+
+public class ExceptionHandler {
+       private static ExceptionHandler fgInstance = new ExceptionHandler();
+
+       public static void log(Throwable t, String message) {
+               WebUI.getDefault().getLog().log(
+                               new Status(IStatus.ERROR, WebUI.PLUGIN_ID,
+                                               IStatus.ERROR, message, t));
+       }
+
+       public static void handle(CoreException e, String title, String message) {
+               handle(e, WebUI.getActiveWorkbenchShell(), title, message);
+       }
+
+       public static void handle(CoreException e, Shell parent, String title,
+                       String message) {
+               fgInstance.perform(e, parent, title, message);
+       }
+
+       public static void handle(InvocationTargetException e, String title,
+                       String message) {
+               handle(e, WebUI.getActiveWorkbenchShell(), title, message);
+       }
+
+       public static void handle(InvocationTargetException e, Shell parent,
+                       String title, String message) {
+               fgInstance.perform(e, parent, title, message);
+       }
+
+       protected void perform(CoreException e, Shell shell, String title,
+                       String message) {
+               WebUI.log(e);
+               IStatus status = e.getStatus();
+               if (status != null) {
+                       ErrorDialog.openError(shell, title, message, status);
+               } else {
+                       displayMessageDialog(e, e.getMessage(), shell, title, message);
+               }
+       }
+
+       protected void perform(InvocationTargetException e, Shell shell,
+                       String title, String message) {
+               Throwable target = e.getTargetException();
+               if (target instanceof CoreException) {
+                       perform((CoreException) target, shell, title, message);
+               } else {
+                       WebUI.log(e);
+                       if (e.getMessage() != null && e.getMessage().length() > 0) {
+                               displayMessageDialog(e, e.getMessage(), shell, title, message);
+                       } else {
+                               displayMessageDialog(e, target.getMessage(), shell, title,
+                                               message);
+                       }
+               }
+       }
+
+       private void displayMessageDialog(Throwable t, String exceptionMessage,
+                       Shell shell, String title, String message) {
+               StringWriter msg = new StringWriter();
+               if (message != null) {
+                       msg.write(message);
+                       msg.write("\n\n");
+               }
+               if (exceptionMessage == null || exceptionMessage.length() == 0)
+                       msg.write(PHPUIMessages
+                                       .getString("ExceptionDialog.seeErrorLogMessage"));
+               else
+                       msg.write(exceptionMessage);
+               MessageDialog.openError(shell, title, msg.toString());
+       }
+}
\ No newline at end of file