KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > 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.ui.util;
12
13 import java.io.StringWriter JavaDoc;
14 import java.lang.reflect.InvocationTargetException JavaDoc;
15
16 import org.eclipse.swt.widgets.Shell;
17
18 import org.eclipse.core.runtime.CoreException;
19 import org.eclipse.core.runtime.IStatus;
20 import org.eclipse.core.runtime.Status;
21
22 import org.eclipse.jdt.internal.ui.JavaPlugin;
23 import org.eclipse.jdt.internal.ui.IJavaStatusConstants;
24 import org.eclipse.jdt.internal.ui.JavaUIMessages;
25 import org.eclipse.jface.dialogs.ErrorDialog;
26 import org.eclipse.jface.dialogs.MessageDialog;
27
28 /**
29  * The default exception handler shows an error dialog when one of its handle methods
30  * is called. If the passed exception is a <code>CoreException</code> an error dialog
31  * pops up showing the exception's status information. For a <code>InvocationTargetException</code>
32  * a normal message dialog pops up showing the exception's message. Additionally the exception
33  * is written to the platform log.
34  */

35 public class ExceptionHandler {
36
37     private static ExceptionHandler fgInstance= new ExceptionHandler();
38     
39     /**
40      * Logs the given exception using the platform's logging mechanism. The exception is
41      * logged as an error with the error code <code>JavaStatusConstants.INTERNAL_ERROR</code>.
42      */

43     public static void log(Throwable JavaDoc t, String JavaDoc message) {
44         JavaPlugin.log(new Status(IStatus.ERROR, JavaPlugin.getPluginId(),
45             IJavaStatusConstants.INTERNAL_ERROR, message, t));
46     }
47     
48     /**
49      * Handles the given <code>CoreException</code>. The workbench shell is used as a parent
50      * for the dialog window.
51      *
52      * @param e the <code>CoreException</code> to be handled
53      * @param title the dialog window's window title
54      * @param message message to be displayed by the dialog window
55      */

56     public static void handle(CoreException e, String JavaDoc title, String JavaDoc message) {
57         handle(e, JavaPlugin.getActiveWorkbenchShell(), title, message);
58     }
59     
60     /**
61      * Handles the given <code>CoreException</code>.
62      *
63      * @param e the <code>CoreException</code> to be handled
64      * @param parent the dialog window's parent shell
65      * @param title the dialog window's window title
66      * @param message message to be displayed by the dialog window
67      */

68     public static void handle(CoreException e, Shell parent, String JavaDoc title, String JavaDoc message) {
69         fgInstance.perform(e, parent, title, message);
70     }
71     
72     /**
73      * Handles the given <code>InvocationTargetException</code>. The workbench shell is used
74      * as a parent for the dialog window.
75      *
76      * @param e the <code>InvocationTargetException</code> to be handled
77      * @param title the dialog window's window title
78      * @param message message to be displayed by the dialog window
79      */

80     public static void handle(InvocationTargetException JavaDoc e, String JavaDoc title, String JavaDoc message) {
81         handle(e, JavaPlugin.getActiveWorkbenchShell(), title, message);
82     }
83     
84     /**
85      * Handles the given <code>InvocationTargetException</code>.
86      *
87      * @param e the <code>InvocationTargetException</code> to be handled
88      * @param parent the dialog window's parent shell
89      * @param title the dialog window's window title
90      * @param message message to be displayed by the dialog window
91      */

92     public static void handle(InvocationTargetException JavaDoc e, Shell parent, String JavaDoc title, String JavaDoc message) {
93         fgInstance.perform(e, parent, title, message);
94     }
95
96     //---- Hooks for subclasses to control exception handling ------------------------------------
97

98     protected void perform(CoreException e, Shell shell, String JavaDoc title, String JavaDoc message) {
99         JavaPlugin.log(e);
100         IStatus status= e.getStatus();
101         if (status != null) {
102             ErrorDialog.openError(shell, title, message, status);
103         } else {
104             displayMessageDialog(e, e.getMessage(), shell, title, message);
105         }
106     }
107
108     protected void perform(InvocationTargetException JavaDoc e, Shell shell, String JavaDoc title, String JavaDoc message) {
109         Throwable JavaDoc target= e.getTargetException();
110         if (target instanceof CoreException) {
111             perform((CoreException)target, shell, title, message);
112         } else {
113             JavaPlugin.log(e);
114             if (e.getMessage() != null && e.getMessage().length() > 0) {
115                 displayMessageDialog(e, e.getMessage(), shell, title, message);
116             } else {
117                 displayMessageDialog(e, target.getMessage(), shell, title, message);
118             }
119         }
120     }
121
122     //---- Helper methods -----------------------------------------------------------------------
123

124     private void displayMessageDialog(Throwable JavaDoc t, String JavaDoc exceptionMessage, Shell shell, String JavaDoc title, String JavaDoc message) {
125         StringWriter JavaDoc msg= new StringWriter JavaDoc();
126         if (message != null) {
127             msg.write(message);
128             msg.write("\n\n"); //$NON-NLS-1$
129
}
130         if (exceptionMessage == null || exceptionMessage.length() == 0)
131             msg.write(JavaUIMessages.ExceptionDialog_seeErrorLogMessage);
132         else
133             msg.write(exceptionMessage);
134         MessageDialog.openError(shell, title, msg.toString());
135     }
136 }
137
Popular Tags