KickJava   Java API By Example, From Geeks To Geeks.

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

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

45     public static void log(Throwable JavaDoc t, String JavaDoc message) {
46         RefactoringUIPlugin.log(new Status(IStatus.ERROR, RefactoringUIPlugin.getPluginId(),
47             IRefactoringUIStatusCodes.INTERNAL_ERROR, message, t));
48     }
49     
50     /**
51      * Handles the given <code>CoreException</code>.
52      *
53      * @param e the <code>CoreException</code> to be handled
54      * @param parent the dialog window's parent shell
55      * @param title the dialog window's window title
56      * @param message message to be displayed by the dialog window
57      */

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

70     public static void handle(InvocationTargetException JavaDoc e, Shell parent, String JavaDoc title, String JavaDoc message) {
71         fgInstance.perform(e, parent, title, message);
72     }
73
74     //---- Hooks for subclasses to control exception handling ------------------------------------
75

76     protected void perform(CoreException e, Shell shell, String JavaDoc title, String JavaDoc message) {
77         RefactoringUIPlugin.log(e);
78         IStatus status= e.getStatus();
79         if (status != null) {
80             ErrorDialog.openError(shell, title, message, status);
81         } else {
82             displayMessageDialog(e, e.getMessage(), shell, title, message);
83         }
84     }
85
86     protected void perform(InvocationTargetException JavaDoc e, Shell shell, String JavaDoc title, String JavaDoc message) {
87         Throwable JavaDoc target= e.getTargetException();
88         if (target instanceof CoreException) {
89             perform((CoreException)target, shell, title, message);
90         } else {
91             RefactoringUIPlugin.log(e);
92             if (e.getMessage() != null && e.getMessage().length() > 0) {
93                 displayMessageDialog(e, e.getMessage(), shell, title, message);
94             } else {
95                 displayMessageDialog(e, target.getMessage(), shell, title, message);
96             }
97         }
98     }
99
100     //---- Helper methods -----------------------------------------------------------------------
101

102     private void displayMessageDialog(Throwable JavaDoc t, String JavaDoc exceptionMessage, Shell shell, String JavaDoc title, String JavaDoc message) {
103         StringWriter JavaDoc msg= new StringWriter JavaDoc();
104         if (message != null) {
105             msg.write(message);
106             msg.write("\n\n"); //$NON-NLS-1$
107
}
108         if (exceptionMessage == null || exceptionMessage.length() == 0)
109             msg.write(RefactoringUIMessages.ExceptionHandler_seeErrorLogMessage);
110         else
111             msg.write(exceptionMessage);
112         MessageDialog.openError(shell, title, msg.toString());
113     }
114 }
115
Popular Tags