1 11 12 package org.eclipse.jdt.apt.ui.internal.util; 13 14 import java.io.StringWriter ; 15 import java.lang.reflect.InvocationTargetException ; 16 17 import org.eclipse.core.runtime.CoreException; 18 import org.eclipse.core.runtime.IStatus; 19 import org.eclipse.core.runtime.Status; 20 import org.eclipse.jdt.apt.ui.internal.AptUIPlugin; 21 import org.eclipse.jface.dialogs.ErrorDialog; 22 import org.eclipse.jface.dialogs.MessageDialog; 23 import org.eclipse.swt.widgets.Shell; 24 25 33 public class ExceptionHandler { 34 35 private static ExceptionHandler fgInstance= new ExceptionHandler(); 36 37 41 public static void log(Throwable t, String message) { 42 AptUIPlugin.log(new Status(IStatus.ERROR, AptUIPlugin.PLUGIN_ID, 43 AptUIPlugin.INTERNAL_ERROR, message, t)); 44 } 45 46 54 public static void handle(CoreException e, String title, String message) { 55 handle(e, AptUIPlugin.getActiveWorkbenchShell(), title, message); 56 } 57 58 66 public static void handle(CoreException e, Shell parent, String title, String message) { 67 fgInstance.perform(e, parent, title, message); 68 } 69 70 78 public static void handle(InvocationTargetException e, String title, String message) { 79 handle(e, AptUIPlugin.getActiveWorkbenchShell(), title, message); 80 } 81 82 90 public static void handle(InvocationTargetException e, Shell parent, String title, String message) { 91 fgInstance.perform(e, parent, title, message); 92 } 93 94 96 protected void perform(CoreException e, Shell shell, String title, String message) { 97 AptUIPlugin.log(e); 98 IStatus status= e.getStatus(); 99 if (status != null) { 100 ErrorDialog.openError(shell, title, message, status); 101 } else { 102 displayMessageDialog(e, e.getMessage(), shell, title, message); 103 } 104 } 105 106 protected void perform(InvocationTargetException e, Shell shell, String title, String message) { 107 Throwable target= e.getTargetException(); 108 if (target instanceof CoreException) { 109 perform((CoreException)target, shell, title, message); 110 } else { 111 AptUIPlugin.log(e); 112 if (e.getMessage() != null && e.getMessage().length() > 0) { 113 displayMessageDialog(e, e.getMessage(), shell, title, message); 114 } else { 115 displayMessageDialog(e, target.getMessage(), shell, title, message); 116 } 117 } 118 } 119 120 122 private void displayMessageDialog(Throwable t, String exceptionMessage, Shell shell, String title, String message) { 123 StringWriter msg= new StringWriter (); 124 if (message != null) { 125 msg.write(message); 126 msg.write("\n\n"); } 128 if (exceptionMessage == null || exceptionMessage.length() == 0) 129 msg.write(Messages.ExceptionHandler_seeErrorLog); 130 else 131 msg.write(exceptionMessage); 132 MessageDialog.openError(shell, title, msg.toString()); 133 } 134 } 135 | Popular Tags |