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.ltk.internal.ui.refactoring;
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.ltk.ui.refactoring.IRefactoringUIStatusCodes;
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 public class ExceptionHandler {
38 
39 	private static ExceptionHandler fgInstance= new ExceptionHandler();
40 
41 	/**
42 	 * Logs the given exception using the platform's logging mechanism. The exception is
43 	 * logged as an error with the error code <code>JavaStatusConstants.INTERNAL_ERROR</code>.
44 	 *
45 	 * @param t the throwable to log
46 	 * @param message the detail message
47 	 */
log(Throwable t, String message)48 	public static void log(Throwable t, String message) {
49 		RefactoringUIPlugin.log(new Status(IStatus.ERROR, RefactoringUIPlugin.getPluginId(),
50 			IRefactoringUIStatusCodes.INTERNAL_ERROR, message, t));
51 	}
52 
53 	/**
54 	 * Handles the given <code>CoreException</code>.
55 	 *
56 	 * @param e the <code>CoreException</code> to be handled
57 	 * @param parent the dialog window's parent shell
58 	 * @param title the dialog window's window title
59 	 * @param message message to be displayed by the dialog window
60 	 */
handle(CoreException e, Shell parent, String title, String message)61 	public static void handle(CoreException e, Shell parent, String title, String message) {
62 		fgInstance.perform(e, parent, title, message);
63 	}
64 
65 	/**
66 	 * Handles the given <code>InvocationTargetException</code>.
67 	 *
68 	 * @param e the <code>InvocationTargetException</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(InvocationTargetException e, Shell parent, String title, String message)73 	public static void handle(InvocationTargetException e, Shell parent, String title, String message) {
74 		fgInstance.perform(e, parent, title, message);
75 	}
76 
77 	//---- Hooks for subclasses to control exception handling ------------------------------------
78 
perform(CoreException e, Shell shell, String title, String message)79 	protected void perform(CoreException e, Shell shell, String title, String message) {
80 		RefactoringUIPlugin.log(e);
81 		IStatus status= e.getStatus();
82 		if (status != null) {
83 			ErrorDialog.openError(shell, title, message, status);
84 		} else {
85 			displayMessageDialog(e.getMessage(), shell, title, message);
86 		}
87 	}
88 
perform(InvocationTargetException e, Shell shell, String title, String message)89 	protected void perform(InvocationTargetException e, Shell shell, String title, String message) {
90 		Throwable target= e.getTargetException();
91 		if (target instanceof CoreException) {
92 			perform((CoreException)target, shell, title, message);
93 		} else {
94 			RefactoringUIPlugin.log(e);
95 			if (e.getMessage() != null && e.getMessage().length() > 0) {
96 				displayMessageDialog(e.getMessage(), shell, title, message);
97 			} else {
98 				displayMessageDialog(target.getMessage(), shell, title, message);
99 			}
100 		}
101 	}
102 
103 	//---- Helper methods -----------------------------------------------------------------------
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(RefactoringUIMessages.ExceptionHandler_seeErrorLogMessage);
113 		else
114 			msg.write(exceptionMessage);
115 		MessageDialog.openError(shell, title, msg.toString());
116 	}
117 }
118