KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > misc > StatusUtil


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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.ui.internal.misc;
12
13 import java.lang.reflect.InvocationTargetException JavaDoc;
14 import java.lang.reflect.Method JavaDoc;
15 import java.util.ArrayList JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.List JavaDoc;
18
19 import org.eclipse.core.runtime.Assert;
20 import org.eclipse.core.runtime.CoreException;
21 import org.eclipse.core.runtime.IStatus;
22 import org.eclipse.core.runtime.MultiStatus;
23 import org.eclipse.core.runtime.Status;
24 import org.eclipse.swt.widgets.Shell;
25 import org.eclipse.ui.internal.WorkbenchMessages;
26 import org.eclipse.ui.internal.WorkbenchPlugin;
27 import org.eclipse.ui.statushandlers.StatusManager;
28
29 /**
30  * Utility class to create status objects.
31  *
32  * @private - This class is an internal implementation class and should
33  * not be referenced or subclassed outside of the workbench
34  */

35 public class StatusUtil {
36     /**
37      * Answer a flat collection of the passed status and its recursive children
38      */

39     protected static List JavaDoc flatten(IStatus aStatus) {
40         List JavaDoc result = new ArrayList JavaDoc();
41
42         if (aStatus.isMultiStatus()) {
43             IStatus[] children = aStatus.getChildren();
44             for (int i = 0; i < children.length; i++) {
45                 IStatus currentChild = children[i];
46                 if (currentChild.isMultiStatus()) {
47                     Iterator JavaDoc childStatiiEnum = flatten(currentChild).iterator();
48                     while (childStatiiEnum.hasNext()) {
49                         result.add(childStatiiEnum.next());
50                     }
51                 } else {
52                     result.add(currentChild);
53                 }
54             }
55         } else {
56             result.add(aStatus);
57         }
58
59         return result;
60     }
61
62     /**
63      * This method must not be called outside the workbench.
64      *
65      * Utility method for creating status.
66      */

67     protected static IStatus newStatus(IStatus[] stati, String JavaDoc message,
68             Throwable JavaDoc exception) {
69
70         Assert.isTrue(message != null);
71         Assert.isTrue(message.trim().length() != 0);
72
73         return new MultiStatus(WorkbenchPlugin.PI_WORKBENCH, IStatus.ERROR,
74                 stati, message, exception);
75     }
76     
77     public static IStatus newStatus(String JavaDoc pluginId, Throwable JavaDoc exception) {
78         return newStatus(pluginId, getLocalizedMessage(exception), exception);
79     }
80
81     /**
82      * Returns a localized message describing the given exception. If the given exception does not
83      * have a localized message, this returns the string "An error occurred".
84      *
85      * @param exception
86      * @return
87      */

88     public static String JavaDoc getLocalizedMessage(Throwable JavaDoc exception) {
89         String JavaDoc message = exception.getLocalizedMessage();
90         
91         if (message != null) {
92             return message;
93         }
94         
95         // Workaround for the fact that CoreException does not implement a getLocalizedMessage() method.
96
// Remove this branch when and if CoreException implements getLocalizedMessage()
97
if (exception instanceof CoreException) {
98             CoreException ce = (CoreException)exception;
99             return ce.getStatus().getMessage();
100         }
101         
102         return WorkbenchMessages.StatusUtil_errorOccurred;
103     }
104     
105     /**
106      * Creates a new Status based on the original status, but with a different message
107      *
108      * @param originalStatus
109      * @param newMessage
110      * @return
111      */

112     public static IStatus newStatus(IStatus originalStatus, String JavaDoc newMessage) {
113         return new Status(originalStatus.getSeverity(),
114                 originalStatus.getPlugin(), originalStatus.getCode(), newMessage, originalStatus.getException());
115     }
116     
117     public static IStatus newStatus(String JavaDoc pluginId, String JavaDoc message, Throwable JavaDoc exception) {
118         return new Status(IStatus.ERROR, pluginId, IStatus.OK,
119                 message, getCause(exception));
120     }
121     
122     public static Throwable JavaDoc getCause(Throwable JavaDoc exception) {
123         // Figure out which exception should actually be logged -- if the given exception is
124
// a wrapper, unwrap it
125
Throwable JavaDoc cause = null;
126         if (exception != null) {
127             if (exception instanceof CoreException) {
128                 // Workaround: CoreException contains a cause, but does not actually implement getCause().
129
// If we get a CoreException, we need to manually unpack the cause. Otherwise, use
130
// the general-purpose mechanism. Remove this branch if CoreException ever implements
131
// a correct getCause() method.
132
CoreException ce = (CoreException)exception;
133                 cause = ce.getStatus().getException();
134             } else {
135                 // use reflect instead of a direct call to getCause(), to allow compilation against JCL Foundation (bug 80053)
136
try {
137                     Method JavaDoc causeMethod = exception.getClass().getMethod("getCause", new Class JavaDoc[0]); //$NON-NLS-1$
138
Object JavaDoc o = causeMethod.invoke(exception, new Object JavaDoc[0]);
139                     if (o instanceof Throwable JavaDoc) {
140                         cause = (Throwable JavaDoc) o;
141                     }
142                 }
143                 catch (NoSuchMethodException JavaDoc e) {
144                     // ignore
145
} catch (IllegalArgumentException JavaDoc e) {
146                     // ignore
147
} catch (IllegalAccessException JavaDoc e) {
148                     // ignore
149
} catch (InvocationTargetException JavaDoc e) {
150                     // ignore
151
}
152             }
153             
154             if (cause == null) {
155                 cause = exception;
156             }
157         }
158
159         return cause;
160     }
161         
162     /**
163      * This method must not be called outside the workbench.
164      *
165      * Utility method for creating status.
166      */

167     public static IStatus newStatus(int severity, String JavaDoc message,
168             Throwable JavaDoc exception) {
169
170         String JavaDoc statusMessage = message;
171         if (message == null || message.trim().length() == 0) {
172             if (exception.getMessage() == null) {
173                 statusMessage = exception.toString();
174             } else {
175                 statusMessage = exception.getMessage();
176             }
177         }
178
179         return new Status(severity, WorkbenchPlugin.PI_WORKBENCH, severity,
180                 statusMessage, getCause(exception));
181     }
182
183     /**
184      * This method must not be called outside the workbench.
185      *
186      * Utility method for creating status.
187      */

188     public static IStatus newStatus(List JavaDoc children, String JavaDoc message,
189             Throwable JavaDoc exception) {
190
191         List JavaDoc flatStatusCollection = new ArrayList JavaDoc();
192         Iterator JavaDoc iter = children.iterator();
193         while (iter.hasNext()) {
194             IStatus currentStatus = (IStatus) iter.next();
195             Iterator JavaDoc childrenIter = flatten(currentStatus).iterator();
196             while (childrenIter.hasNext()) {
197                 flatStatusCollection.add(childrenIter.next());
198             }
199         }
200
201         IStatus[] stati = new IStatus[flatStatusCollection.size()];
202         flatStatusCollection.toArray(stati);
203         return newStatus(stati, message, exception);
204     }
205     
206     /**
207      * This method must not be called outside the workbench.
208      *
209      * Utility method for handling status.
210      */

211     public static void handleStatus(IStatus status, int hint, Shell shell) {
212         StatusManager.getManager().handle(status, hint);
213     }
214     
215     /**
216      * This method must not be called outside the workbench.
217      *
218      * Utility method for handling status.
219      */

220     public static void handleStatus(Throwable JavaDoc e, int hint) {
221         StatusManager.getManager().handle(
222                 newStatus(WorkbenchPlugin.PI_WORKBENCH, e), hint);
223     }
224     
225     /**
226      * This method must not be called outside the workbench.
227      *
228      * Utility method for handling status.
229      */

230     public static void handleStatus(String JavaDoc message, Throwable JavaDoc e, int hint) {
231         StatusManager.getManager().handle(
232                 newStatus(WorkbenchPlugin.PI_WORKBENCH, message, e), hint);
233     }
234     
235     /**
236      * This method must not be called outside the workbench.
237      *
238      * Utility method for handling status.
239      */

240     public static void handleStatus(String JavaDoc message, Throwable JavaDoc e, int hint,
241             Shell shell) {
242         StatusManager.getManager().handle(
243                 newStatus(WorkbenchPlugin.PI_WORKBENCH, message, e), hint);
244     }
245     
246     /**
247      * This method must not be called outside the workbench.
248      *
249      * Utility method for handling status.
250      */

251     public static void handleStatus(IStatus status, String JavaDoc message, int hint) {
252         StatusManager.getManager().handle(newStatus(status, message), hint);
253     }
254     
255     /**
256      * This method must not be called outside the workbench.
257      *
258      * Utility method for handling status.
259      */

260     public static void handleStatus(IStatus status, String JavaDoc message, int hint,
261             Shell shell) {
262         StatusManager.getManager().handle(newStatus(status, message), hint);
263     }
264
265     /**
266      * This method must not be called outside the workbench.
267      *
268      * Utility method for handling status.
269      */

270     public static void handleStatus(String JavaDoc message, int hint) {
271         handleStatus(message, null, hint);
272     }
273     
274     /**
275      * This method must not be called outside the workbench.
276      *
277      * Utility method for handling status.
278      */

279     public static void handleStatus(String JavaDoc message, int hint, Shell shell) {
280         handleStatus(message, null, hint);
281     }
282 }
283
Popular Tags