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