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.search.internal.ui.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 import org.eclipse.core.runtime.Status; 24 25 import org.eclipse.jface.dialogs.ErrorDialog; 26 import org.eclipse.jface.dialogs.MessageDialog; 27 28 import org.eclipse.search.internal.ui.SearchMessages; 29 import org.eclipse.search.internal.ui.SearchPlugin; 30 import org.eclipse.search.ui.NewSearchUI; 31 32 /** 33 * The default exception handler shows an error dialog when one of its handle methods 34 * is called. If the passed exception is a <code>CoreException</code> an error dialog 35 * pops up showing the exception's status information. For a <code>InvocationTargetException</code> 36 * a normal message dialog pops up showing the exception's message. Additionally the exception 37 * is written to the platform log. 38 */ 39 public class ExceptionHandler { 40 41 private static ExceptionHandler fgInstance= new ExceptionHandler(); 42 43 /** 44 * Logs the given exception using the platform's logging mechanism. The exception is 45 * logged as an error with the error code <code>JavaStatusConstants.INTERNAL_ERROR</code>. 46 * @param t The exception to log 47 * @param message The message to be used for teh status 48 */ log(Throwable t, String message)49 public static void log(Throwable t, String message) { 50 SearchPlugin.log(new Status(IStatus.ERROR, NewSearchUI.PLUGIN_ID, IStatus.ERROR, message, t)); 51 } 52 53 /** 54 * Handles the given <code>CoreException</code>. The workbench shell is used as a parent 55 * for the dialog window. 56 * 57 * @param e the <code>CoreException</code> to be handled 58 * @param title the dialog window's window title 59 * @param message message to be displayed by the dialog window 60 */ handle(CoreException e, String title, String message)61 public static void handle(CoreException e, String title, String message) { 62 handle(e, SearchPlugin.getActiveWorkbenchShell(), title, message); 63 } 64 65 /** 66 * Handles the given <code>CoreException</code>. 67 * 68 * @param e the <code>CoreException</code> to be handled 69 * @param parent the dialog window's parent shell 70 * @param title the dialog window's window title 71 * @param message message to be displayed by the dialog window 72 */ handle(CoreException e, Shell parent, String title, String message)73 public static void handle(CoreException e, Shell parent, String title, String message) { 74 fgInstance.perform(e, parent, title, message); 75 } 76 77 /** 78 * Handles the given <code>InvocationTargetException</code>. The workbench shell is used 79 * as a parent for the dialog window. 80 * 81 * @param e the <code>InvocationTargetException</code> to be handled 82 * @param title the dialog window's window title 83 * @param message message to be displayed by the dialog window 84 */ handle(InvocationTargetException e, String title, String message)85 public static void handle(InvocationTargetException e, String title, String message) { 86 handle(e, SearchPlugin.getActiveWorkbenchShell(), title, message); 87 } 88 89 /** 90 * Handles the given <code>InvocationTargetException</code>. 91 * 92 * @param e the <code>InvocationTargetException</code> to be handled 93 * @param parent the dialog window's parent shell 94 * @param title the dialog window's window title 95 * @param message message to be displayed by the dialog window 96 */ handle(InvocationTargetException e, Shell parent, String title, String message)97 public static void handle(InvocationTargetException e, Shell parent, String title, String message) { 98 fgInstance.perform(e, parent, title, message); 99 } 100 101 //---- Hooks for subclasses to control exception handling ------------------------------------ 102 perform(CoreException e, Shell shell, String title, String message)103 protected void perform(CoreException e, Shell shell, String title, String message) { 104 SearchPlugin.log(e); 105 IStatus status= e.getStatus(); 106 if (status != null) { 107 ErrorDialog.openError(shell, title, message, status); 108 } else { 109 displayMessageDialog(e.getMessage(), shell, title, message); 110 } 111 } 112 perform(InvocationTargetException e, Shell shell, String title, String message)113 protected void perform(InvocationTargetException e, Shell shell, String title, String message) { 114 Throwable target= e.getTargetException(); 115 if (target instanceof CoreException) { 116 perform((CoreException)target, shell, title, message); 117 } else { 118 SearchPlugin.log(e); 119 if (e.getMessage() != null && !e.getMessage().isEmpty()) { 120 displayMessageDialog(e.getMessage(), shell, title, message); 121 } else { 122 displayMessageDialog(target.getMessage(), shell, title, message); 123 } 124 } 125 } 126 127 //---- Helper methods ----------------------------------------------------------------------- 128 displayMessageDialog(Throwable t, Shell shell, String title, String message)129 public static void displayMessageDialog(Throwable t, Shell shell, String title, String message) { 130 fgInstance.displayMessageDialog(t.getMessage(), shell, title, message); 131 } 132 displayMessageDialog(Throwable t, String title, String message)133 public static void displayMessageDialog(Throwable t, String title, String message) { 134 displayMessageDialog(t, SearchPlugin.getActiveWorkbenchShell(), title, message); 135 } 136 displayMessageDialog(String exceptionMessage, Shell shell, String title, String message)137 private void displayMessageDialog(String exceptionMessage, Shell shell, String title, String message) { 138 StringWriter msg= new StringWriter(); 139 if (message != null) { 140 msg.write(message); 141 msg.write("\n\n"); //$NON-NLS-1$ 142 } 143 if (exceptionMessage == null || exceptionMessage.isEmpty()) 144 msg.write(SearchMessages.ExceptionDialog_seeErrorLogMessage); 145 else 146 msg.write(exceptionMessage); 147 MessageDialog.openError(shell, title, msg.toString()); 148 } 149 } 150