KickJava   Java API By Example, From Geeks To Geeks.

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

30 public class ExceptionHandler {
31
32     private static ExceptionHandler fgInstance= new ExceptionHandler();
33     
34     /**
35      * Handles the given <code>CoreException</code>. The workbench shell is used as a parent
36      * for the dialog window.
37      *
38      * @param e the <code>CoreException</code> to be handled
39      * @param title the dialog window's window title
40      * @param message message to be displayed by the dialog window
41      */

42     public static void handle(CoreException e, String JavaDoc title, String JavaDoc message) {
43         handle(e, JDIDebugUIPlugin.getActiveWorkbenchShell(), title, message);
44     }
45     
46     /**
47      * Handles the given <code>CoreException</code>.
48      *
49      * @param e the <code>CoreException</code> to be handled
50      * @param parent the dialog window's parent shell
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, Shell parent, String JavaDoc title, String JavaDoc message) {
55         fgInstance.perform(e, parent, title, message);
56     }
57     
58     /**
59      * Handles the given <code>InvocationTargetException</code>. The workbench shell is used
60      * as a parent for the dialog window.
61      *
62      * @param e the <code>InvocationTargetException</code> to be handled
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(InvocationTargetException JavaDoc e, String JavaDoc title, String JavaDoc message) {
67         handle(e, JDIDebugUIPlugin.getActiveWorkbenchShell(), title, message);
68     }
69     
70     /**
71      * Handles the given <code>InvocationTargetException</code>.
72      *
73      * @param e the <code>InvocationTargetException</code> to be handled
74      * @param parent the dialog window's parent shell
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, Shell parent, String JavaDoc title, String JavaDoc message) {
79         fgInstance.perform(e, parent, title, message);
80     }
81
82     //---- Hooks for subclasses to control exception handling ------------------------------------
83

84     protected void perform(CoreException e, Shell shell, String JavaDoc title, String JavaDoc message) {
85         IStatus status= e.getStatus();
86         JDIDebugUIPlugin.log(e);
87         if (status != null) {
88             ErrorDialog.openError(shell, title, message, status);
89         } else {
90             displayMessageDialog(e.getMessage(), shell, title, message);
91         }
92     }
93
94     protected void perform(InvocationTargetException JavaDoc e, Shell shell, String JavaDoc title, String JavaDoc message) {
95         Throwable JavaDoc target= e.getTargetException();
96         if (target instanceof CoreException) {
97             perform((CoreException)target, shell, title, message);
98         } else {
99             JDIDebugUIPlugin.log(e);
100             if (e.getMessage() != null && e.getMessage().length() > 0) {
101                 displayMessageDialog(e.getMessage(), shell, title, message);
102             } else {
103                 displayMessageDialog(target.getMessage(), shell, title, message);
104             }
105         }
106     }
107     
108     private void displayMessageDialog(String JavaDoc exceptionMessage, Shell shell, String JavaDoc title, String JavaDoc message) {
109         StringWriter JavaDoc msg= new StringWriter JavaDoc();
110         if (message != null) {
111             msg.write(message);
112             msg.write("\n\n"); //$NON-NLS-1$
113
}
114         if (exceptionMessage == null || exceptionMessage.length() == 0) {
115             msg.write(DebugUIMessages.ExceptionHandler_seeErrorLogMessage);
116         }
117         else {
118             msg.write(exceptionMessage);
119         }
120         MessageDialog.openError(shell, title, msg.toString());
121     }
122 }
123
Popular Tags