upload tizen1.0 source
[sdk/ide/product.git] / org.eclipse.cdt.ui / src / org / eclipse / cdt / internal / ui / util / ExceptionHandler.java
1 /*******************************************************************************
2  * Copyright (c) 2001, 2008 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  *     Rational Software - initial implementation
10  *******************************************************************************/
11 package org.eclipse.cdt.internal.ui.util;
12
13
14 import java.io.StringWriter;
15 import java.lang.reflect.InvocationTargetException;
16
17 import org.eclipse.swt.widgets.Shell;
18
19 import org.eclipse.core.runtime.CoreException;
20 import org.eclipse.core.runtime.IStatus;
21 import org.eclipse.core.runtime.Status;
22
23 import org.eclipse.cdt.ui.CUIPlugin;
24 import org.eclipse.cdt.internal.ui.CStatusConstants;
25 import org.eclipse.cdt.internal.ui.CUIMessages;
26 import org.eclipse.jface.dialogs.ErrorDialog;
27 import org.eclipse.jface.dialogs.MessageDialog;
28
29 /**
30  * The default exception handler shows an error dialog when one of its handle methods
31  * is called. If the passed exception is a <code>CoreException</code> an error dialog
32  * pops up showing the exception's status information. For a <code>InvocationTargetException</code>
33  * a normal message dialog pops up showing the exception's message. Additionally the exception
34  * is written to the platform log.
35  */
36 public class ExceptionHandler {
37
38         private static ExceptionHandler fgInstance= new ExceptionHandler();
39         
40         /**
41          * Logs the given exception using the platform's logging mechanism. The exception is
42          * logged as an error with the error code <code>JavaStatusConstants.INTERNAL_ERROR</code>.
43          */
44         public static void log(Throwable t, String message) {
45                 CUIPlugin.log(new Status(IStatus.ERROR, CUIPlugin.getPluginId(), 
46                         CStatusConstants.INTERNAL_ERROR, message, t));
47         }
48         
49         /**
50          * Handles the given <code>CoreException</code>. The workbench shell is used as a parent
51          * for the dialog window.
52          * 
53          * @param e the <code>CoreException</code> to be handled
54          * @param title the dialog window's window title
55          * @param message message to be displayed by the dialog window
56          */
57         public static void handle(CoreException e, String title, String message) {
58                 handle(e, CUIPlugin.getActiveWorkbenchShell(), title, message);
59         }
60         
61         /**
62          * Handles the given <code>CoreException</code>. 
63          * 
64          * @param e the <code>CoreException</code> to be handled
65          * @param parent the dialog window's parent shell
66          * @param title the dialog window's window title
67          * @param message message to be displayed by the dialog window
68          */
69         public static void handle(CoreException e, Shell parent, String title, String message) {
70                 fgInstance.perform(e, parent, title, message);
71         }
72         
73         /**
74          * Handles the given <code>InvocationTargetException</code>. The workbench shell is used 
75          * as a parent for the dialog window.
76          * 
77          * @param e the <code>InvocationTargetException</code> to be handled
78          * @param title the dialog window's window title
79          * @param message message to be displayed by the dialog window
80          */
81         public static void handle(InvocationTargetException e, String title, String message) {
82                 handle(e, CUIPlugin.getActiveWorkbenchShell(), title, message);
83         }
84         
85         /**
86          * Handles the given <code>InvocationTargetException</code>. 
87          * 
88          * @param e the <code>InvocationTargetException</code> to be handled
89          * @param parent the dialog window's parent shell
90          * @param title the dialog window's window title
91          * @param message message to be displayed by the dialog window
92          */
93         public static void handle(InvocationTargetException e, Shell parent, String title, String message) {
94                 fgInstance.perform(e, parent, title, message);
95         }
96
97         //---- Hooks for subclasses to control exception handling ------------------------------------
98         
99         protected void perform(CoreException e, Shell shell, String title, String message) {
100                 CUIPlugin.log(e);
101                 IStatus status= e.getStatus();
102                 if (status != null) {
103                         ErrorDialog.openError(shell, title, message, status);
104                 } else {
105                         displayMessageDialog(e, e.getMessage(), shell, title, message);
106                 }
107         }
108
109         protected void perform(InvocationTargetException e, Shell shell, String title, String message) {
110                 Throwable target= e.getTargetException();
111                 if (target instanceof CoreException) {
112                         perform((CoreException)target, shell, title, message);
113                 } else {
114                         CUIPlugin.log(e);
115                         if (e.getMessage() != null && e.getMessage().length() > 0) {
116                                 displayMessageDialog(e, e.getMessage(), shell, title, message);
117                         } else {
118                                 displayMessageDialog(e, target.getMessage(), shell, title, message);
119                         }
120                 }
121         }
122
123         //---- Helper methods -----------------------------------------------------------------------
124         
125         private void displayMessageDialog(Throwable t, String exceptionMessage, Shell shell, String title, String message) {
126                 StringWriter msg= new StringWriter();
127                 if (message != null) {
128                         msg.write(message);
129                         msg.write("\n\n"); //$NON-NLS-1$
130                 }
131                 if (exceptionMessage == null || exceptionMessage.length() == 0)
132                         msg.write(CUIMessages.ExceptionDialog_seeErrorLogMessage); 
133                 else
134                         msg.write(exceptionMessage);
135                 MessageDialog.openError(shell, title, msg.toString());                  
136         }       
137 }