001: /*******************************************************************************
002: * Copyright (c) 2000, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.internal.ide;
011:
012: import java.util.ArrayList;
013: import java.util.Iterator;
014: import java.util.List;
015:
016: import org.eclipse.core.runtime.IStatus;
017: import org.eclipse.core.runtime.MultiStatus;
018: import org.eclipse.core.runtime.Status;
019:
020: /**
021: * Utility class to create status objects.
022: *
023: * PRIVATE
024: * This class is an internal implementation class and should
025: * not be referenced or subclassed outside of the workbench
026: *
027: * @since 3.0
028: */
029: public class StatusUtil {
030:
031: /**
032: * Answer a flat collection of the passed status and its recursive children
033: */
034: protected static List flatten(IStatus aStatus) {
035: List result = new ArrayList();
036:
037: if (aStatus.isMultiStatus()) {
038: IStatus[] children = aStatus.getChildren();
039: for (int i = 0; i < children.length; i++) {
040: IStatus currentChild = children[i];
041: if (currentChild.isMultiStatus()) {
042: Iterator childStatiiEnum = flatten(currentChild)
043: .iterator();
044: while (childStatiiEnum.hasNext()) {
045: result.add(childStatiiEnum.next());
046: }
047: } else {
048: result.add(currentChild);
049: }
050: }
051: } else {
052: result.add(aStatus);
053: }
054:
055: return result;
056: }
057:
058: /**
059: * This method must not be called outside the workbench.
060: *
061: * Utility method for creating status.
062: */
063: protected static IStatus newStatus(IStatus[] stati, String message,
064: Throwable exception) {
065:
066: if (message == null || message.trim().length() == 0) {
067: throw new IllegalArgumentException();
068: }
069: return new MultiStatus(IDEWorkbenchPlugin.IDE_WORKBENCH,
070: IStatus.ERROR, stati, message, exception);
071: }
072:
073: /**
074: * This method must not be called outside the workbench.
075: *
076: * Utility method for creating status.
077: */
078: public static IStatus newStatus(int severity, String message,
079: Throwable exception) {
080:
081: String statusMessage = message;
082: if (message == null || message.trim().length() == 0) {
083: if (exception == null) {
084: throw new IllegalArgumentException();
085: } else if (exception.getMessage() == null) {
086: statusMessage = exception.toString();
087: } else {
088: statusMessage = exception.getMessage();
089: }
090: }
091:
092: return new Status(severity, IDEWorkbenchPlugin.IDE_WORKBENCH,
093: severity, statusMessage, exception);
094: }
095:
096: /**
097: * This method must not be called outside the workbench.
098: *
099: * Utility method for creating status.
100: */
101: public static IStatus newStatus(List children, String message,
102: Throwable exception) {
103:
104: List flatStatusCollection = new ArrayList();
105: Iterator iter = children.iterator();
106: while (iter.hasNext()) {
107: IStatus currentStatus = (IStatus) iter.next();
108: Iterator 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: }
|