KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > help > internal > search > ProgressDistributor


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
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.help.internal.search;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Collection JavaDoc;
15 import java.util.Iterator JavaDoc;
16
17 import org.eclipse.core.runtime.*;
18
19 /**
20  * Distributes progress information from this monitor to multiple monitors.
21  */

22 public class ProgressDistributor implements IProgressMonitor {
23     private int totalWork = -1;
24     private double worked = 0;
25     private boolean done = false;
26     String JavaDoc taskName;
27     String JavaDoc subTaskName;
28     /**
29      * Map work indexed by montitor
30      */

31     private Collection JavaDoc monitors = new ArrayList JavaDoc();
32
33     /**
34      * @see IProgressMonitor#beginTask(String, int)
35      */

36     public synchronized void beginTask(String JavaDoc name, int totalWork) {
37         this.totalWork = totalWork;
38         this.worked = 0;
39         this.done = false;
40         for (Iterator JavaDoc it = monitors.iterator(); it.hasNext();) {
41             IProgressMonitor m = (IProgressMonitor) it.next();
42             m.beginTask(name, totalWork);
43         }
44     }
45
46     /**
47      * @see IProgressMonitor#done()
48      */

49     public synchronized void done() {
50         done = true;
51         for (Iterator JavaDoc it = monitors.iterator(); it.hasNext();) {
52             IProgressMonitor m = (IProgressMonitor) it.next();
53             m.done();
54         }
55     }
56
57     /**
58      * @see IProgressMonitor#internalWorked(double)
59      */

60     public void internalWorked(double work) {
61         worked += work;
62         for (Iterator JavaDoc it = monitors.iterator(); it.hasNext();) {
63             IProgressMonitor m = (IProgressMonitor) it.next();
64             m.internalWorked(work);
65         }
66     }
67
68     /**
69      * @see IProgressMonitor#isCanceled()
70      */

71     public synchronized boolean isCanceled() {
72         for (Iterator JavaDoc it = monitors.iterator(); it.hasNext();) {
73             IProgressMonitor m = (IProgressMonitor) it.next();
74             if (m.isCanceled()) {
75                 return true;
76             }
77         }
78         return false;
79     }
80
81     /**
82      * @see IProgressMonitor#setCanceled(boolean)
83      */

84     public void setCanceled(boolean value) {
85     }
86
87     /**
88      * @see IProgressMonitor#setTaskName(String)
89      */

90     public synchronized void setTaskName(String JavaDoc name) {
91         taskName = name;
92         for (Iterator JavaDoc it = monitors.iterator(); it.hasNext();) {
93             IProgressMonitor m = (IProgressMonitor) it.next();
94             m.setTaskName(name);
95         }
96     }
97
98     /**
99      * @see IProgressMonitor#subTask(String)
100      */

101     public synchronized void subTask(String JavaDoc name) {
102         subTaskName = name;
103         for (Iterator JavaDoc it = monitors.iterator(); it.hasNext();) {
104             IProgressMonitor m = (IProgressMonitor) it.next();
105             m.subTask(name);
106         }
107     }
108
109     /**
110      * @see IProgressMonitor#worked(int)
111      */

112     public synchronized void worked(int work) {
113         internalWorked(work);
114     }
115     
116     public synchronized void addMonitor(IProgressMonitor m) {
117         if (totalWork > -1)
118             m.beginTask(taskName, totalWork);
119         if (subTaskName != null)
120             m.subTask(subTaskName);
121         if (worked > 0)
122             m.internalWorked(worked);
123         if (done)
124             m.done();
125         monitors.add(m);
126     }
127     public synchronized void removeMonitor(IProgressMonitor m) {
128         monitors.remove(m);
129     }
130     public synchronized void operationCanceled() {
131         totalWork = -1;
132         worked = 0;
133         done = false;
134     }
135 }
136
Popular Tags