1 package net.sourceforge.phpdt.internal.ui.util;
3 import java.io.StringWriter;
4 import java.lang.reflect.InvocationTargetException;
6 import net.sourceforge.phpdt.internal.ui.PHPUIMessages;
7 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
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;
16 public class ExceptionHandler {
17 private static ExceptionHandler fgInstance = new ExceptionHandler();
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));
25 public static void handle(CoreException e, String title, String message) {
26 handle(e, PHPeclipsePlugin.getActiveWorkbenchShell(), title, message);
29 public static void handle(CoreException e, Shell parent, String title,
31 fgInstance.perform(e, parent, title, message);
34 public static void handle(InvocationTargetException e, String title,
36 handle(e, PHPeclipsePlugin.getActiveWorkbenchShell(), title, message);
39 public static void handle(InvocationTargetException e, Shell parent,
40 String title, String message) {
41 fgInstance.perform(e, parent, title, message);
44 protected void perform(CoreException e, Shell shell, String title,
46 PHPeclipsePlugin.log(e);
47 IStatus status = e.getStatus();
49 ErrorDialog.openError(shell, title, message, status);
51 displayMessageDialog(e, e.getMessage(), shell, title, message);
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);
61 PHPeclipsePlugin.log(e);
62 if (e.getMessage() != null && e.getMessage().length() > 0) {
63 displayMessageDialog(e, e.getMessage(), shell, title, message);
65 displayMessageDialog(e, target.getMessage(), shell, title,
71 private void displayMessageDialog(Throwable t, String exceptionMessage,
72 Shell shell, String title, String message) {
73 StringWriter msg = new StringWriter();
74 if (message != null) {
78 if (exceptionMessage == null || exceptionMessage.length() == 0)
79 msg.write(PHPUIMessages
80 .getString("ExceptionDialog.seeErrorLogMessage"));
82 msg.write(exceptionMessage);
83 MessageDialog.openError(shell, title, msg.toString());