KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > ide > 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.ide;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.eclipse.core.runtime.IStatus;
18 import org.eclipse.core.runtime.MultiStatus;
19 import org.eclipse.core.runtime.Status;
20
21 /**
22  * Utility class to create status objects.
23  *
24  * PRIVATE
25  * This class is an internal implementation class and should
26  * not be referenced or subclassed outside of the workbench
27  *
28  * @since 3.0
29  */

30 public class StatusUtil {
31
32     /**
33      * Answer a flat collection of the passed status and its recursive children
34      */

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

63     protected static IStatus newStatus(IStatus[] stati, String JavaDoc message,
64             Throwable JavaDoc exception) {
65
66         if (message == null || message.trim().length() == 0) {
67             throw new IllegalArgumentException JavaDoc();
68         }
69         return new MultiStatus(IDEWorkbenchPlugin.IDE_WORKBENCH, IStatus.ERROR,
70                 stati, message, exception);
71     }
72
73     /**
74      * This method must not be called outside the workbench.
75      *
76      * Utility method for creating status.
77      */

78     public static IStatus newStatus(int severity, String JavaDoc message,
79             Throwable JavaDoc exception) {
80
81         String JavaDoc statusMessage = message;
82         if (message == null || message.trim().length() == 0) {
83             if (exception == null) {
84                 throw new IllegalArgumentException JavaDoc();
85             } else if (exception.getMessage() == null) {
86                 statusMessage = exception.toString();
87             } else {
88                 statusMessage = exception.getMessage();
89             }
90         }
91
92         return new Status(severity, IDEWorkbenchPlugin.IDE_WORKBENCH, severity,
93                 statusMessage, exception);
94     }
95
96     /**
97      * This method must not be called outside the workbench.
98      *
99      * Utility method for creating status.
100      */

101     public static IStatus newStatus(List JavaDoc children, String JavaDoc message,
102             Throwable JavaDoc exception) {
103
104         List JavaDoc flatStatusCollection = new ArrayList JavaDoc();
105         Iterator JavaDoc iter = children.iterator();
106         while (iter.hasNext()) {
107             IStatus currentStatus = (IStatus) iter.next();
108             Iterator JavaDoc childrenIter = flatten(currentStatus).iterator();
109             while (childrenIter.hasNext()) {
110                 flatStatusCollection.add(childrenIter.next());
111             }
112         }
113
114         IStatus[] stati = new IStatus[flatStatusCollection.size()];
115         flatStatusCollection.toArray(stati);
116         return newStatus(stati, message, exception);
117     }
118 }
119
Popular Tags