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;
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;
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(new Status(IStatus.ERROR, PHPeclipsePlugin.PLUGIN_ID,
21 IStatus.ERROR, message, t));
24 public static void handle(CoreException e, String title, String message) {
25 handle(e, PHPeclipsePlugin.getActiveWorkbenchShell(), title, message);
28 public static void handle(CoreException e, Shell parent, String title, String message) {
29 fgInstance.perform(e, parent, title, message);
32 public static void handle(InvocationTargetException e, String title, String message) {
33 handle(e, PHPeclipsePlugin.getActiveWorkbenchShell(), title, message);
36 public static void handle(InvocationTargetException e, Shell parent, String title, String message) {
37 fgInstance.perform(e, parent, title, message);
40 protected void perform(CoreException e, Shell shell, String title, String message) {
41 PHPeclipsePlugin.log(e);
42 IStatus status= e.getStatus();
44 ErrorDialog.openError(shell, title, message, status);
46 displayMessageDialog(e, e.getMessage(), shell, title, message);
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);
55 PHPeclipsePlugin.log(e);
56 if (e.getMessage() != null && e.getMessage().length() > 0) {
57 displayMessageDialog(e, e.getMessage(), shell, title, message);
59 displayMessageDialog(e, target.getMessage(), shell, title, message);
64 private void displayMessageDialog(Throwable t, String exceptionMessage, Shell shell, String title, String message) {
65 StringWriter msg= new StringWriter();
66 if (message != null) {
70 if (exceptionMessage == null || exceptionMessage.length() == 0)
71 msg.write(PHPUIMessages.getString("ExceptionDialog.seeErrorLogMessage"));
73 msg.write(exceptionMessage);
74 MessageDialog.openError(shell, title, msg.toString());