1 11 package org.eclipse.jdt.internal.junit.util; 12 13 import java.io.StringWriter ; 14 import java.lang.reflect.InvocationTargetException ; 15 16 import org.eclipse.core.runtime.CoreException; 17 import org.eclipse.core.runtime.IStatus; 18 import org.eclipse.jdt.internal.junit.ui.JUnitPlugin; 19 import org.eclipse.jdt.internal.junit.wizards.WizardMessages; 20 import org.eclipse.jface.dialogs.ErrorDialog; 21 import org.eclipse.jface.dialogs.MessageDialog; 22 import org.eclipse.swt.widgets.Shell; 23 24 33 public class ExceptionHandler { 34 35 private static ExceptionHandler fgInstance= new ExceptionHandler(); 36 37 45 public static void handle(CoreException e, Shell parent, String title, String message) { 46 fgInstance.perform(e, parent, title, message); 47 } 48 49 57 public static void handle(InvocationTargetException e, Shell parent, String title, String message) { 58 fgInstance.perform(e, parent, title, message); 59 } 60 61 63 protected void perform(CoreException e, Shell shell, String title, String message) { 64 JUnitPlugin.log(e); 65 IStatus status= e.getStatus(); 66 if (status != null) { 67 ErrorDialog.openError(shell, title, message, status); 68 } else { 69 displayMessageDialog(e, e.getMessage(), shell, title, message); 70 } 71 } 72 73 protected void perform(InvocationTargetException e, Shell shell, String title, String message) { 74 Throwable target= e.getTargetException(); 75 if (target instanceof CoreException) { 76 perform((CoreException)target, shell, title, message); 77 } else { 78 JUnitPlugin.log(e); 79 if (e.getMessage() != null && e.getMessage().length() > 0) { 80 displayMessageDialog(e, e.getMessage(), shell, title, message); 81 } else { 82 displayMessageDialog(e, target.getMessage(), shell, title, message); 83 } 84 } 85 } 86 87 private void displayMessageDialog(Throwable t, String exceptionMessage, Shell shell, String title, String message) { 88 StringWriter msg= new StringWriter (); 89 if (message != null) { 90 msg.write(message); 91 msg.write("\n\n"); } 93 if (exceptionMessage == null || exceptionMessage.length() == 0) 94 msg.write(WizardMessages.ExceptionDialog_seeErrorLogMessage); 95 else 96 msg.write(exceptionMessage); 97 MessageDialog.openError(shell, title, msg.toString()); 98 } 99 } 100 | Popular Tags |