KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > search > 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.search.internal.ui.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.core.runtime.Status;
19 import org.eclipse.jface.dialogs.ErrorDialog;
20 import org.eclipse.jface.dialogs.MessageDialog;
21 import org.eclipse.search.internal.ui.SearchMessages;
22 import org.eclipse.search.internal.ui.SearchPlugin;
23 import org.eclipse.search.ui.NewSearchUI;
24 import org.eclipse.swt.widgets.Shell;
25
26 /**
27  * The default exception handler shows an error dialog when one of its handle methods
28  * is called. If the passed exception is a <code>CoreException</code> an error dialog
29  * pops up showing the exception's status information. For a <code>InvocationTargetException</code>
30  * a normal message dialog pops up showing the exception's message. Additionally the exception
31  * is written to the platform log.
32  */

33 public class ExceptionHandler {
34
35     private static ExceptionHandler fgInstance= new ExceptionHandler();
36     
37     /**
38      * Logs the given exception using the platform's logging mechanism. The exception is
39      * logged as an error with the error code <code>JavaStatusConstants.INTERNAL_ERROR</code>.
40      * @param t The exception to log
41      * @param message The message to be used for teh status
42      */

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

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

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

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

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

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

123     public static void displayMessageDialog(Throwable JavaDoc t, Shell shell, String JavaDoc title, String JavaDoc message) {
124         fgInstance.displayMessageDialog(t, t.getMessage(), shell, title, message);
125     }
126
127     public static void displayMessageDialog(Throwable JavaDoc t, String JavaDoc title, String JavaDoc message) {
128         displayMessageDialog(t, SearchPlugin.getActiveWorkbenchShell(), title, message);
129     }
130     
131     private void displayMessageDialog(Throwable JavaDoc t, String JavaDoc exceptionMessage, Shell shell, String JavaDoc title, String JavaDoc message) {
132         StringWriter JavaDoc msg= new StringWriter JavaDoc();
133         if (message != null) {
134             msg.write(message);
135             msg.write("\n\n"); //$NON-NLS-1$
136
}
137         if (exceptionMessage == null || exceptionMessage.length() == 0)
138             msg.write(SearchMessages.ExceptionDialog_seeErrorLogMessage);
139         else
140             msg.write(exceptionMessage);
141         MessageDialog.openError(shell, title, msg.toString());
142     }
143 }
144
Popular Tags