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;
17 public class ExceptionHandler {
18 private static ExceptionHandler fgInstance= new ExceptionHandler();
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));
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, String message) {
30 fgInstance.perform(e, parent, title, message);
33 public static void handle(InvocationTargetException e, String title, String message) {
34 handle(e, PHPeclipsePlugin.getActiveWorkbenchShell(), title, message);
37 public static void handle(InvocationTargetException e, Shell parent, String title, String message) {
38 fgInstance.perform(e, parent, title, message);
41 protected void perform(CoreException e, Shell shell, String title, String message) {
42 PHPeclipsePlugin.log(e);
43 IStatus status= e.getStatus();
45 ErrorDialog.openError(shell, title, message, status);
47 displayMessageDialog(e, e.getMessage(), shell, title, message);
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);
56 PHPeclipsePlugin.log(e);
57 if (e.getMessage() != null && e.getMessage().length() > 0) {
58 displayMessageDialog(e, e.getMessage(), shell, title, message);
60 displayMessageDialog(e, target.getMessage(), shell, title, message);
65 private void displayMessageDialog(Throwable t, String exceptionMessage, Shell shell, String title, String message) {
66 StringWriter msg= new StringWriter();
67 if (message != null) {
71 if (exceptionMessage == null || exceptionMessage.length() == 0)
72 msg.write(PHPUIMessages.getString("ExceptionDialog.seeErrorLogMessage"));
74 msg.write(exceptionMessage);
75 MessageDialog.openError(shell, title, msg.toString());