1 /******************************************************************************* 2 * Copyright (c) 2000, 2008 IBM Corporation and others. 3 * 4 * This program and the accompanying materials 5 * are made available under the terms of the Eclipse Public License 2.0 6 * which accompanies this distribution, and is available at 7 * https://www.eclipse.org/legal/epl-2.0/ 8 * 9 * SPDX-License-Identifier: EPL-2.0 10 * 11 * Contributors: 12 * IBM Corporation - initial API and implementation 13 *******************************************************************************/ 14 package org.eclipse.jdt.internal.junit.util; 15 16 import java.io.StringWriter; 17 import java.lang.reflect.InvocationTargetException; 18 19 import org.eclipse.swt.widgets.Shell; 20 21 import org.eclipse.core.runtime.CoreException; 22 import org.eclipse.core.runtime.IStatus; 23 24 import org.eclipse.jface.dialogs.ErrorDialog; 25 import org.eclipse.jface.dialogs.MessageDialog; 26 27 import org.eclipse.jdt.internal.junit.ui.JUnitPlugin; 28 import org.eclipse.jdt.internal.junit.wizards.WizardMessages; 29 30 /** 31 * The default exception handler shows an error dialog when one of its handle methods 32 * is called. If the passed exception is a <code>CoreException</code> an error dialog 33 * pops up showing the exception's status information. For a <code>InvocationTargetException</code> 34 * a normal message dialog pops up showing the exception's message. Additionally the exception 35 * is written to the platform log. 36 * 37 * TO DO: this class is duplicated from org.eclipse.jdt.ui 38 */ 39 public class ExceptionHandler { 40 41 private static ExceptionHandler fgInstance= new ExceptionHandler(); 42 43 /** 44 * Handles the given <code>CoreException</code>. The workbench shell is used as a parent 45 * for the dialog window. 46 * 47 * @param e the <code>CoreException</code> to be handled 48 * @param title the dialog window's window title 49 * @param message message to be displayed by the dialog window 50 */ handle(CoreException e, String title, String message)51 public static void handle(CoreException e, String title, String message) { 52 handle(e, JUnitPlugin.getActiveWorkbenchShell(), title, message); 53 } 54 55 /** 56 * Handles the given <code>CoreException</code>. 57 * 58 * @param e the <code>CoreException</code> to be handled 59 * @param parent the dialog window's parent shell or <code>null</code> 60 * @param title the dialog window's window title 61 * @param message message to be displayed by the dialog window 62 */ handle(CoreException e, Shell parent, String title, String message)63 public static void handle(CoreException e, Shell parent, String title, String message) { 64 fgInstance.perform(e, parent, title, message); 65 } 66 67 /** 68 * Handles the given <code>InvocationTargetException</code>. 69 * 70 * @param e the <code>InvocationTargetException</code> to be handled 71 * @param parent the dialog window's parent shell or <code>null</code> 72 * @param title the dialog window's window title 73 * @param message message to be displayed by the dialog window 74 */ handle(InvocationTargetException e, Shell parent, String title, String message)75 public static void handle(InvocationTargetException e, Shell parent, String title, String message) { 76 fgInstance.perform(e, parent, title, message); 77 } 78 79 //---- Hooks for subclasses to control exception handling ------------------------------------ 80 perform(CoreException e, Shell shell, String title, String message)81 protected void perform(CoreException e, Shell shell, String title, String message) { 82 JUnitPlugin.log(e); 83 IStatus status= e.getStatus(); 84 if (status != null) { 85 ErrorDialog.openError(shell, title, message, status); 86 } else { 87 displayMessageDialog(e.getMessage(), shell, title, message); 88 } 89 } 90 perform(InvocationTargetException e, Shell shell, String title, String message)91 protected void perform(InvocationTargetException e, Shell shell, String title, String message) { 92 Throwable target= e.getTargetException(); 93 if (target instanceof CoreException) { 94 perform((CoreException)target, shell, title, message); 95 } else { 96 JUnitPlugin.log(e); 97 if (e.getMessage() != null && e.getMessage().length() > 0) { 98 displayMessageDialog(e.getMessage(), shell, title, message); 99 } else { 100 displayMessageDialog(target.getMessage(), shell, title, message); 101 } 102 } 103 } 104 displayMessageDialog(String exceptionMessage, Shell shell, String title, String message)105 private void displayMessageDialog(String exceptionMessage, Shell shell, String title, String message) { 106 StringWriter msg= new StringWriter(); 107 if (message != null) { 108 msg.write(message); 109 msg.write("\n\n"); //$NON-NLS-1$ 110 } 111 if (exceptionMessage == null || exceptionMessage.length() == 0) 112 msg.write(WizardMessages.ExceptionDialog_seeErrorLogMessage); 113 else 114 msg.write(exceptionMessage); 115 MessageDialog.openError(shell, title, msg.toString()); 116 } 117 } 118