]> git.uio.no Git - ifi-stolz-refaktor.git/blame - case-study/jdt-after/ui/org/eclipse/jdt/internal/ui/util/ExceptionHandler.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / jdt-after / ui / org / eclipse / jdt / internal / ui / util / ExceptionHandler.java
CommitLineData
1b2798f6
EK
1/*******************************************************************************
2 * Copyright (c) 2000, 2009 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 *******************************************************************************/
11package org.eclipse.jdt.internal.ui.util;
12
13import java.io.StringWriter;
14import java.lang.reflect.InvocationTargetException;
15
16import org.eclipse.swt.widgets.Shell;
17
18import org.eclipse.core.runtime.CoreException;
19import org.eclipse.core.runtime.IStatus;
20import org.eclipse.core.runtime.Status;
21
22import org.eclipse.jface.dialogs.ErrorDialog;
23import org.eclipse.jface.dialogs.MessageDialog;
24
25import org.eclipse.jdt.internal.ui.IJavaStatusConstants;
26import org.eclipse.jdt.internal.ui.JavaPlugin;
27import org.eclipse.jdt.internal.ui.JavaUIMessages;
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 */
36public 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 * @param t the throwable to log
44 * @param message the message
45 */
46 public static void log(Throwable t, String message) {
47 JavaPlugin.log(new Status(IStatus.ERROR, JavaPlugin.getPluginId(),
48 IJavaStatusConstants.INTERNAL_ERROR, message, t));
49 }
50
51 /**
52 * Handles the given <code>CoreException</code>. The workbench shell is used as a parent
53 * for the dialog window.
54 *
55 * @param e the <code>CoreException</code> to be handled
56 * @param title the dialog window's window title
57 * @param message message to be displayed by the dialog window
58 */
59 public static void handle(CoreException e, String title, String message) {
60 handle(e, JavaPlugin.getActiveWorkbenchShell(), title, message);
61 }
62
63 /**
64 * Handles the given <code>IStatus</code>. The workbench shell is used as a parent for the
65 * dialog window.
66 *
67 * @param status the <code>IStatus</code> to be handled
68 * @param title the dialog window's window title
69 * @param message message to be displayed by the dialog window
70 * @since 3.5
71 */
72 public static void handle(IStatus status, String title, String message) {
73 fgInstance.perform(status, JavaPlugin.getActiveWorkbenchShell(), title, message);
74 }
75
76 /**
77 * Handles the given <code>CoreException</code>.
78 *
79 * @param e the <code>CoreException</code> to be handled
80 * @param parent the dialog window's parent shell
81 * @param title the dialog window's window title
82 * @param message message to be displayed by the dialog window
83 */
84 public static void handle(CoreException e, Shell parent, String title, String message) {
85 fgInstance.perform(e, parent, title, message);
86 }
87
88 /**
89 * Handles the given <code>InvocationTargetException</code>. The workbench shell is used
90 * as a parent for the dialog window.
91 *
92 * @param e the <code>InvocationTargetException</code> to be handled
93 * @param title the dialog window's window title
94 * @param message message to be displayed by the dialog window
95 */
96 public static void handle(InvocationTargetException e, String title, String message) {
97 handle(e, JavaPlugin.getActiveWorkbenchShell(), title, message);
98 }
99
100 /**
101 * Handles the given <code>InvocationTargetException</code>.
102 *
103 * @param e the <code>InvocationTargetException</code> to be handled
104 * @param parent the dialog window's parent shell
105 * @param title the dialog window's window title
106 * @param message message to be displayed by the dialog window
107 */
108 public static void handle(InvocationTargetException e, Shell parent, String title, String message) {
109 fgInstance.perform(e, parent, title, message);
110 }
111
112 //---- Hooks for subclasses to control exception handling ------------------------------------
113
114 protected void perform(IStatus status, Shell shell, String title, String message) {
115 JavaPlugin.log(status);
116 ErrorDialog.openError(shell, title, message, status);
117 }
118
119 protected void perform(CoreException e, Shell shell, String title, String message) {
120 JavaPlugin.log(e);
121 IStatus status= e.getStatus();
122 if (status != null) {
123 ErrorDialog.openError(shell, title, message, status);
124 } else {
125 displayMessageDialog(e.getMessage(), shell, title, message);
126 }
127 }
128
129 protected void perform(InvocationTargetException e, Shell shell, String title, String message) {
130 Throwable target= e.getTargetException();
131 if (target instanceof CoreException) {
132 perform((CoreException)target, shell, title, message);
133 } else {
134 JavaPlugin.log(e);
135 if (e.getMessage() != null && e.getMessage().length() > 0) {
136 displayMessageDialog(e.getMessage(), shell, title, message);
137 } else {
138 displayMessageDialog(target.getMessage(), shell, title, message);
139 }
140 }
141 }
142
143 //---- Helper methods -----------------------------------------------------------------------
144
145 private void displayMessageDialog(String exceptionMessage, Shell shell, String title, String message) {
146 StringWriter msg= new StringWriter();
147 if (message != null) {
148 msg.write(message);
149 msg.write("\n\n"); //$NON-NLS-1$
150 }
151 if (exceptionMessage == null || exceptionMessage.length() == 0)
152 msg.write(JavaUIMessages.ExceptionDialog_seeErrorLogMessage);
153 else
154 msg.write(exceptionMessage);
155 MessageDialog.openError(shell, title, msg.toString());
156 }
157}