1 /*******************************************************************************
2 * Copyright (c) 2001, 2008 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
9 * Rational Software - initial implementation
10 *******************************************************************************/
11 package org.eclipse.cdt.internal.ui.util;
14 import java.io.StringWriter;
15 import java.lang.reflect.InvocationTargetException;
17 import org.eclipse.swt.widgets.Shell;
19 import org.eclipse.core.runtime.CoreException;
20 import org.eclipse.core.runtime.IStatus;
21 import org.eclipse.core.runtime.Status;
23 import org.eclipse.cdt.ui.CUIPlugin;
24 import org.eclipse.cdt.internal.ui.CStatusConstants;
25 import org.eclipse.cdt.internal.ui.CUIMessages;
26 import org.eclipse.jface.dialogs.ErrorDialog;
27 import org.eclipse.jface.dialogs.MessageDialog;
30 * The default exception handler shows an error dialog when one of its handle methods
31 * is called. If the passed exception is a <code>CoreException</code> an error dialog
32 * pops up showing the exception's status information. For a <code>InvocationTargetException</code>
33 * a normal message dialog pops up showing the exception's message. Additionally the exception
34 * is written to the platform log.
36 public class ExceptionHandler {
38 private static ExceptionHandler fgInstance= new ExceptionHandler();
41 * Logs the given exception using the platform's logging mechanism. The exception is
42 * logged as an error with the error code <code>JavaStatusConstants.INTERNAL_ERROR</code>.
44 public static void log(Throwable t, String message) {
45 CUIPlugin.log(new Status(IStatus.ERROR, CUIPlugin.getPluginId(),
46 CStatusConstants.INTERNAL_ERROR, message, t));
50 * Handles the given <code>CoreException</code>. The workbench shell is used as a parent
51 * for the dialog window.
53 * @param e the <code>CoreException</code> to be handled
54 * @param title the dialog window's window title
55 * @param message message to be displayed by the dialog window
57 public static void handle(CoreException e, String title, String message) {
58 handle(e, CUIPlugin.getActiveWorkbenchShell(), title, message);
62 * Handles the given <code>CoreException</code>.
64 * @param e the <code>CoreException</code> to be handled
65 * @param parent the dialog window's parent shell
66 * @param title the dialog window's window title
67 * @param message message to be displayed by the dialog window
69 public static void handle(CoreException e, Shell parent, String title, String message) {
70 fgInstance.perform(e, parent, title, message);
74 * Handles the given <code>InvocationTargetException</code>. The workbench shell is used
75 * as a parent for the dialog window.
77 * @param e the <code>InvocationTargetException</code> to be handled
78 * @param title the dialog window's window title
79 * @param message message to be displayed by the dialog window
81 public static void handle(InvocationTargetException e, String title, String message) {
82 handle(e, CUIPlugin.getActiveWorkbenchShell(), title, message);
86 * Handles the given <code>InvocationTargetException</code>.
88 * @param e the <code>InvocationTargetException</code> to be handled
89 * @param parent the dialog window's parent shell
90 * @param title the dialog window's window title
91 * @param message message to be displayed by the dialog window
93 public static void handle(InvocationTargetException e, Shell parent, String title, String message) {
94 fgInstance.perform(e, parent, title, message);
97 //---- Hooks for subclasses to control exception handling ------------------------------------
99 protected void perform(CoreException e, Shell shell, String title, String message) {
101 IStatus status= e.getStatus();
102 if (status != null) {
103 ErrorDialog.openError(shell, title, message, status);
105 displayMessageDialog(e, e.getMessage(), shell, title, message);
109 protected void perform(InvocationTargetException e, Shell shell, String title, String message) {
110 Throwable target= e.getTargetException();
111 if (target instanceof CoreException) {
112 perform((CoreException)target, shell, title, message);
115 if (e.getMessage() != null && e.getMessage().length() > 0) {
116 displayMessageDialog(e, e.getMessage(), shell, title, message);
118 displayMessageDialog(e, target.getMessage(), shell, title, message);
123 //---- Helper methods -----------------------------------------------------------------------
125 private void displayMessageDialog(Throwable t, String exceptionMessage, Shell shell, String title, String message) {
126 StringWriter msg= new StringWriter();
127 if (message != null) {
129 msg.write("\n\n"); //$NON-NLS-1$
131 if (exceptionMessage == null || exceptionMessage.length() == 0)
132 msg.write(CUIMessages.ExceptionDialog_seeErrorLogMessage);
134 msg.write(exceptionMessage);
135 MessageDialog.openError(shell, title, msg.toString());