KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > compare > internal > ExceptionHandler


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.compare.internal;
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.jface.dialogs.ErrorDialog;
23 import org.eclipse.jface.dialogs.MessageDialog;
24
25 /**
26  * The default exception handler shows an error dialog when one of its handle methods
27  * is called. If the passed exception is a <code>CoreException</code> an error dialog
28  * pops up showing the exception's status information. For a <code>InvocationTargetException</code>
29  * a normal message dialog pops up showing the exception's message. Additionally the exception
30  * is written to the platform log.
31  */

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

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

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

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

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

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

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

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