blob: c15e53e3ca9e10e47f042dc88c7f51ff67ad79e8 [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2000, 2003 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Common Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/cpl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.help.internal.search;
import java.util.*;
import org.eclipse.core.runtime.IProgressMonitor;
/**
* Distributes progress information from this monitor
* to multiple monitors.
*/
public class ProgressDistributor implements IProgressMonitor {
private int totalWork = -1;
private int worked = 0;
private boolean done = false;
String taskName;
String subTaskName;
/**
* Map work indexed by montitor
*/
private Collection monitors = new ArrayList();
/**
* @see IProgressMonitor#beginTask(String, int)
*/
public synchronized void beginTask(String name, int totalWork) {
this.totalWork = totalWork;
this.worked=0;
this.done=false;
for (Iterator it = monitors.iterator(); it.hasNext();) {
IProgressMonitor m = (IProgressMonitor) it.next();
m.beginTask(name, totalWork);
}
}
/**
* @see IProgressMonitor#done()
*/
public synchronized void done() {
done = true;
for (Iterator it = monitors.iterator(); it.hasNext();) {
IProgressMonitor m = (IProgressMonitor) it.next();
m.done();
}
}
/**
* @see IProgressMonitor#internalWorked(double)
*/
public void internalWorked(double work) {
}
/**
* @see IProgressMonitor#isCanceled()
*/
public synchronized boolean isCanceled() {
for (Iterator it = monitors.iterator(); it.hasNext();) {
IProgressMonitor m = (IProgressMonitor) it.next();
if(m.isCanceled()){
return true;
}
}
return false;
}
/**
* @see IProgressMonitor#setCanceled(boolean)
*/
public void setCanceled(boolean value) {
}
/**
* @see IProgressMonitor#setTaskName(String)
*/
public synchronized void setTaskName(String name) {
taskName = name;
for (Iterator it = monitors.iterator(); it.hasNext();) {
IProgressMonitor m = (IProgressMonitor) it.next();
m.setTaskName(name);
}
}
/**
* @see IProgressMonitor#subTask(String)
*/
public synchronized void subTask(String name) {
subTaskName = name;
for (Iterator it = monitors.iterator(); it.hasNext();) {
IProgressMonitor m = (IProgressMonitor) it.next();
m.subTask(name);
}
}
/**
* @see IProgressMonitor#worked(int)
*/
public synchronized void worked(int work) {
worked += work;
for (Iterator it = monitors.iterator(); it.hasNext();) {
IProgressMonitor m = (IProgressMonitor) it.next();
m.worked(work);
}
}
public synchronized void addMonitor(IProgressMonitor m) {
if (totalWork > -1)
m.beginTask(taskName, totalWork);
if (subTaskName != null)
m.subTask(subTaskName);
if (worked > 0)
m.worked(worked);
if (done)
m.done();
monitors.add(m);
}
public synchronized void removeMonitor(IProgressMonitor m) {
monitors.remove(m);
}
public synchronized void operationCanceled(){
totalWork = -1;
worked = 0;
done = false;
}
}