Improve error reporting in the branch dialog
[egit/imyousuf.git] / org.spearce.egit.core / src / org / spearce / egit / core / internal / util / ExceptionCollector.java
blob404ee00975ff9f221f73e1f0f6c3e2a2f19a514e
1 /*******************************************************************************
2 * Copyright (c) 2000, 2006 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
8 * Contributors:
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package org.spearce.egit.core.internal.util;
13 import java.util.ArrayList;
14 import java.util.Iterator;
15 import java.util.List;
17 import org.eclipse.core.runtime.CoreException;
18 import org.eclipse.core.runtime.ILog;
19 import org.eclipse.core.runtime.IStatus;
20 import org.eclipse.core.runtime.MultiStatus;
21 import org.eclipse.core.runtime.Status;
23 /**
24 * Collects exceptions and can be configured to ignore duplicates exceptions.
25 * Exceptions can be logged and a MultiStatus containing all collected
26 * exceptions can be returned.
28 * @see org.eclipse.core.runtime.MultiStatus
29 * @see org.eclipse.core.runtime.IStatus
31 * @since 3.0
33 public class ExceptionCollector {
35 private final List<IStatus> statuses = new ArrayList<IStatus>();
37 private final String message;
39 private final String pluginId;
41 private final int severity;
43 private final ILog log;
45 /**
46 * Creates a collector and initializes the parameters for the top-level
47 * exception that would be returned from <code>getStatus</code> is
48 * exceptions are collected.
50 * @param message
51 * a human-readable message, localized to the current locale
52 * @param pluginId
53 * the unique identifier of the relevant plug-in
54 * @param severity
55 * the severity; one of <code>OK</code>, <code>ERROR</code>,
56 * <code>INFO</code>, or <code>WARNING</code>
57 * @param log
58 * the log to output the exceptions to, or <code>null</code> if
59 * exceptions should not be logged.
61 public ExceptionCollector(String message, String pluginId, int severity,
62 ILog log) {
63 this.message = message;
64 this.pluginId = pluginId;
65 this.severity = severity;
66 this.log = log;
69 /**
70 * Clears the exceptions collected.
72 public void clear() {
73 statuses.clear();
76 /**
77 * Returns a status that represents the exceptions collected. If the
78 * collector is empty <code>IStatus.OK</code> is returned. Otherwise a
79 * MultiStatus containing all collected exceptions is returned.
81 * @return a multistatus containing the exceptions collected or IStatus.OK
82 * if the collector is empty.
84 public IStatus getStatus() {
85 if (statuses.isEmpty()) {
86 return Status.OK_STATUS;
87 } else {
88 final MultiStatus multiStatus = new MultiStatus(pluginId, severity,
89 message, null);
90 final Iterator it = statuses.iterator();
91 while (it.hasNext()) {
92 final IStatus status = (IStatus) it.next();
93 multiStatus.merge(status);
95 return multiStatus;
99 /**
100 * Add this exception to the collector. If a log was specified in the
101 * constructor then the exception will be output to the log. You can
102 * retreive exceptions using <code>getStatus</code>.
104 * @param exception
105 * the exception to collect
107 public void handleException(CoreException exception) {
108 if (log != null) {
109 log.log(new Status(severity, pluginId, 0, message, exception));
112 // Record each status individually to flatten the resulting multi-status
113 final IStatus exceptionStatus = exception.getStatus();
115 // Wrap the exception so the stack trace is not lost.
116 final IStatus status = new Status(exceptionStatus.getSeverity(),
117 exceptionStatus.getPlugin(), exceptionStatus.getCode(),
118 exceptionStatus.getMessage(), exception);
120 recordStatus(status);
121 for (IStatus childStatus : status.getChildren())
122 recordStatus(childStatus);
125 private void recordStatus(IStatus status) {
126 statuses.add(status);