KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > apt > ui > internal > util > ExceptionHandler


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation, BEA Systems, Inc., 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  * wharley@bea.com - based on org.eclipse.jdt.internal.ui.util.ExceptionHandler
10  *******************************************************************************/

11
12 package org.eclipse.jdt.apt.ui.internal.util;
13
14 import java.io.StringWriter JavaDoc;
15 import java.lang.reflect.InvocationTargetException JavaDoc;
16
17 import org.eclipse.core.runtime.CoreException;
18 import org.eclipse.core.runtime.IStatus;
19 import org.eclipse.core.runtime.Status;
20 import org.eclipse.jdt.apt.ui.internal.AptUIPlugin;
21 import org.eclipse.jface.dialogs.ErrorDialog;
22 import org.eclipse.jface.dialogs.MessageDialog;
23 import org.eclipse.swt.widgets.Shell;
24
25 /**
26  * This code is
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>AptUIPlugin.INTERNAL_ERROR</code>.
40      */

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

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

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

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

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

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

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