KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > junit > util > ExceptionHandler


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.junit.util;
12
13 import java.io.StringWriter JavaDoc;
14 import java.lang.reflect.InvocationTargetException JavaDoc;
15
16 import org.eclipse.core.runtime.CoreException;
17 import org.eclipse.core.runtime.IStatus;
18 import org.eclipse.jdt.internal.junit.ui.JUnitPlugin;
19 import org.eclipse.jdt.internal.junit.wizards.WizardMessages;
20 import org.eclipse.jface.dialogs.ErrorDialog;
21 import org.eclipse.jface.dialogs.MessageDialog;
22 import org.eclipse.swt.widgets.Shell;
23
24 /**
25  * The default exception handler shows an error dialog when one of its handle methods
26  * is called. If the passed exception is a <code>CoreException</code> an error dialog
27  * pops up showing the exception's status information. For a <code>InvocationTargetException</code>
28  * a normal message dialog pops up showing the exception's message. Additionally the exception
29  * is written to the platform log.
30  *
31  * TO DO: this class is duplicated from org.eclipse.jdt.ui
32  */

33 public class ExceptionHandler {
34
35     private static ExceptionHandler fgInstance= new ExceptionHandler();
36     
37     /**
38      * Handles the given <code>CoreException</code>.
39      *
40      * @param e the <code>CoreException</code> to be handled
41      * @param parent the dialog window's parent shell
42      * @param title the dialog window's window title
43      * @param message message to be displayed by the dialog window
44      */

45     public static void handle(CoreException e, Shell parent, String JavaDoc title, String JavaDoc message) {
46         fgInstance.perform(e, parent, title, message);
47     }
48     
49     /**
50      * Handles the given <code>InvocationTargetException</code>.
51      *
52      * @param e the <code>InvocationTargetException</code> to be handled
53      * @param parent the dialog window's parent shell
54      * @param title the dialog window's window title
55      * @param message message to be displayed by the dialog window
56      */

57     public static void handle(InvocationTargetException JavaDoc e, Shell parent, String JavaDoc title, String JavaDoc message) {
58         fgInstance.perform(e, parent, title, message);
59     }
60
61     //---- Hooks for subclasses to control exception handling ------------------------------------
62

63     protected void perform(CoreException e, Shell shell, String JavaDoc title, String JavaDoc message) {
64         JUnitPlugin.log(e);
65         IStatus status= e.getStatus();
66         if (status != null) {
67             ErrorDialog.openError(shell, title, message, status);
68         } else {
69             displayMessageDialog(e, e.getMessage(), shell, title, message);
70         }
71     }
72
73     protected void perform(InvocationTargetException JavaDoc e, Shell shell, String JavaDoc title, String JavaDoc message) {
74         Throwable JavaDoc target= e.getTargetException();
75         if (target instanceof CoreException) {
76             perform((CoreException)target, shell, title, message);
77         } else {
78             JUnitPlugin.log(e);
79             if (e.getMessage() != null && e.getMessage().length() > 0) {
80                 displayMessageDialog(e, e.getMessage(), shell, title, message);
81             } else {
82                 displayMessageDialog(e, target.getMessage(), shell, title, message);
83             }
84         }
85     }
86     
87     private void displayMessageDialog(Throwable JavaDoc t, String JavaDoc exceptionMessage, Shell shell, String JavaDoc title, String JavaDoc message) {
88         StringWriter JavaDoc msg= new StringWriter JavaDoc();
89         if (message != null) {
90             msg.write(message);
91             msg.write("\n\n"); //$NON-NLS-1$
92
}
93         if (exceptionMessage == null || exceptionMessage.length() == 0)
94             msg.write(WizardMessages.ExceptionDialog_seeErrorLogMessage);
95         else
96             msg.write(exceptionMessage);
97         MessageDialog.openError(shell, title, msg.toString());
98     }
99 }
100
Popular Tags