1 11 package org.eclipse.jdt.internal.ui.util; 12 13 import java.io.StringWriter ; 14 import java.lang.reflect.InvocationTargetException ; 15 16 import org.eclipse.swt.widgets.Shell; 17 18 import org.eclipse.core.runtime.CoreException; 19 import org.eclipse.core.runtime.IStatus; 20 import org.eclipse.core.runtime.Status; 21 22 import org.eclipse.jdt.internal.ui.JavaPlugin; 23 import org.eclipse.jdt.internal.ui.IJavaStatusConstants; 24 import org.eclipse.jdt.internal.ui.JavaUIMessages; 25 import org.eclipse.jface.dialogs.ErrorDialog; 26 import org.eclipse.jface.dialogs.MessageDialog; 27 28 35 public class ExceptionHandler { 36 37 private static ExceptionHandler fgInstance= new ExceptionHandler(); 38 39 43 public static void log(Throwable t, String message) { 44 JavaPlugin.log(new Status(IStatus.ERROR, JavaPlugin.getPluginId(), 45 IJavaStatusConstants.INTERNAL_ERROR, message, t)); 46 } 47 48 56 public static void handle(CoreException e, String title, String message) { 57 handle(e, JavaPlugin.getActiveWorkbenchShell(), title, message); 58 } 59 60 68 public static void handle(CoreException e, Shell parent, String title, String message) { 69 fgInstance.perform(e, parent, title, message); 70 } 71 72 80 public static void handle(InvocationTargetException e, String title, String message) { 81 handle(e, JavaPlugin.getActiveWorkbenchShell(), title, message); 82 } 83 84 92 public static void handle(InvocationTargetException e, Shell parent, String title, String message) { 93 fgInstance.perform(e, parent, title, message); 94 } 95 96 98 protected void perform(CoreException e, Shell shell, String title, String message) { 99 JavaPlugin.log(e); 100 IStatus status= e.getStatus(); 101 if (status != null) { 102 ErrorDialog.openError(shell, title, message, status); 103 } else { 104 displayMessageDialog(e, e.getMessage(), shell, title, message); 105 } 106 } 107 108 protected void perform(InvocationTargetException e, Shell shell, String title, String message) { 109 Throwable target= e.getTargetException(); 110 if (target instanceof CoreException) { 111 perform((CoreException)target, shell, title, message); 112 } else { 113 JavaPlugin.log(e); 114 if (e.getMessage() != null && e.getMessage().length() > 0) { 115 displayMessageDialog(e, e.getMessage(), shell, title, message); 116 } else { 117 displayMessageDialog(e, target.getMessage(), shell, title, message); 118 } 119 } 120 } 121 122 124 private void displayMessageDialog(Throwable t, String exceptionMessage, Shell shell, String title, String message) { 125 StringWriter msg= new StringWriter (); 126 if (message != null) { 127 msg.write(message); 128 msg.write("\n\n"); } 130 if (exceptionMessage == null || exceptionMessage.length() == 0) 131 msg.write(JavaUIMessages.ExceptionDialog_seeErrorLogMessage); 132 else 133 msg.write(exceptionMessage); 134 MessageDialog.openError(shell, title, msg.toString()); 135 } 136 } 137 | Popular Tags |