001: /*******************************************************************************
002: * Copyright (c) 2000, 2005 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.internal.ui.util;
011:
012: import java.io.StringWriter;
013: import java.lang.reflect.InvocationTargetException;
014:
015: import org.eclipse.swt.widgets.Shell;
016:
017: import org.eclipse.core.runtime.CoreException;
018: import org.eclipse.core.runtime.IStatus;
019: import org.eclipse.core.runtime.Status;
020:
021: import org.eclipse.jdt.internal.ui.JavaPlugin;
022: import org.eclipse.jdt.internal.ui.IJavaStatusConstants;
023: import org.eclipse.jdt.internal.ui.JavaUIMessages;
024: import org.eclipse.jface.dialogs.ErrorDialog;
025: import org.eclipse.jface.dialogs.MessageDialog;
026:
027: /**
028: * The default exception handler shows an error dialog when one of its handle methods
029: * is called. If the passed exception is a <code>CoreException</code> an error dialog
030: * pops up showing the exception's status information. For a <code>InvocationTargetException</code>
031: * a normal message dialog pops up showing the exception's message. Additionally the exception
032: * is written to the platform log.
033: */
034: public class ExceptionHandler {
035:
036: private static ExceptionHandler fgInstance = new ExceptionHandler();
037:
038: /**
039: * Logs the given exception using the platform's logging mechanism. The exception is
040: * logged as an error with the error code <code>JavaStatusConstants.INTERNAL_ERROR</code>.
041: */
042: public static void log(Throwable t, String message) {
043: JavaPlugin.log(new Status(IStatus.ERROR, JavaPlugin
044: .getPluginId(), IJavaStatusConstants.INTERNAL_ERROR,
045: message, t));
046: }
047:
048: /**
049: * Handles the given <code>CoreException</code>. The workbench shell is used as a parent
050: * for the dialog window.
051: *
052: * @param e the <code>CoreException</code> to be handled
053: * @param title the dialog window's window title
054: * @param message message to be displayed by the dialog window
055: */
056: public static void handle(CoreException e, String title,
057: String message) {
058: handle(e, JavaPlugin.getActiveWorkbenchShell(), title, message);
059: }
060:
061: /**
062: * Handles the given <code>CoreException</code>.
063: *
064: * @param e the <code>CoreException</code> to be handled
065: * @param parent the dialog window's parent shell
066: * @param title the dialog window's window title
067: * @param message message to be displayed by the dialog window
068: */
069: public static void handle(CoreException e, Shell parent,
070: String title, String message) {
071: fgInstance.perform(e, parent, title, message);
072: }
073:
074: /**
075: * Handles the given <code>InvocationTargetException</code>. The workbench shell is used
076: * as a parent for the dialog window.
077: *
078: * @param e the <code>InvocationTargetException</code> to be handled
079: * @param title the dialog window's window title
080: * @param message message to be displayed by the dialog window
081: */
082: public static void handle(InvocationTargetException e,
083: String title, String message) {
084: handle(e, JavaPlugin.getActiveWorkbenchShell(), title, message);
085: }
086:
087: /**
088: * Handles the given <code>InvocationTargetException</code>.
089: *
090: * @param e the <code>InvocationTargetException</code> to be handled
091: * @param parent the dialog window's parent shell
092: * @param title the dialog window's window title
093: * @param message message to be displayed by the dialog window
094: */
095: public static void handle(InvocationTargetException e,
096: Shell parent, String title, String message) {
097: fgInstance.perform(e, parent, title, message);
098: }
099:
100: //---- Hooks for subclasses to control exception handling ------------------------------------
101:
102: protected void perform(CoreException e, Shell shell, String title,
103: String message) {
104: JavaPlugin.log(e);
105: IStatus status = e.getStatus();
106: if (status != null) {
107: ErrorDialog.openError(shell, title, message, status);
108: } else {
109: displayMessageDialog(e, e.getMessage(), shell, title,
110: message);
111: }
112: }
113:
114: protected void perform(InvocationTargetException e, Shell shell,
115: String title, String message) {
116: Throwable target = e.getTargetException();
117: if (target instanceof CoreException) {
118: perform((CoreException) target, shell, title, message);
119: } else {
120: JavaPlugin.log(e);
121: if (e.getMessage() != null && e.getMessage().length() > 0) {
122: displayMessageDialog(e, e.getMessage(), shell, title,
123: message);
124: } else {
125: displayMessageDialog(e, target.getMessage(), shell,
126: title, message);
127: }
128: }
129: }
130:
131: //---- Helper methods -----------------------------------------------------------------------
132:
133: private void displayMessageDialog(Throwable t,
134: String exceptionMessage, Shell shell, String title,
135: String message) {
136: StringWriter msg = new StringWriter();
137: if (message != null) {
138: msg.write(message);
139: msg.write("\n\n"); //$NON-NLS-1$
140: }
141: if (exceptionMessage == null || exceptionMessage.length() == 0)
142: msg
143: .write(JavaUIMessages.ExceptionDialog_seeErrorLogMessage);
144: else
145: msg.write(exceptionMessage);
146: MessageDialog.openError(shell, title, msg.toString());
147: }
148: }
|