KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > api > progress > aggregate > AggregateProgressHandle


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.api.progress.aggregate;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.Collection JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import javax.swing.Action JavaDoc;
26 import javax.swing.JComponent JavaDoc;
27 import javax.swing.JLabel JavaDoc;
28 import org.netbeans.api.progress.ProgressHandle;
29 import org.netbeans.api.progress.ProgressHandleFactory;
30 import org.openide.util.Cancellable;
31
32 /**
33  * a progress handle that allows aggregation of progress indication from multiple
34  * independant sources. All of the progress contributors are considered equal and are given
35  * equal share of the global progress.
36  * The task progress contributors can be added dynamically and
37  * the progress bar adjusts accordingly, never stepping back though.
38  *
39  * For a more simple version of progress indication, see {@link org.netbeans.api.progress.ProgressHandle}
40  * @author Milos Kleint (mkleint@netbeans.org)
41  */

42 public final class AggregateProgressHandle {
43     private ProgressMonitor monitor;
44     private ProgressHandle handle;
45     static final int WORKUNITS = 10000;
46     private boolean finished;
47     private Collection JavaDoc<ProgressContributor> contributors;
48     private int current;
49     
50     /** Creates a new instance of AggregateProgressHandle */
51     AggregateProgressHandle(String JavaDoc displayName, ProgressContributor[] contribs, Cancellable cancellable, Action JavaDoc listAction, boolean systemtask) {
52         handle = ProgressHandleFactory.createHandle(displayName, cancellable, listAction);
53         finished = false;
54         contributors = new ArrayList JavaDoc<ProgressContributor>();
55         if (contribs != null) {
56             for (int i = 0; i < contribs.length; i++) {
57                 addContributor(contribs[i]);
58             }
59         }
60     }
61     
62
63     /**
64      * start the progress indication for the task, shows the progress in the UI, events from the contributors are
65      * expected after this call.
66      */

67     public void start() {
68         start(-1);
69     }
70
71     /**
72      * start the progress indication for the task with an initial time estimate, shows the progress in the UI, events from the contributors are
73      * expected after this call.
74      * @param estimate estimated time to process the task in seconds
75      */

76     public synchronized void start(long estimate) {
77         handle.start(WORKUNITS, estimate);
78         current = 0;
79     }
80     
81     /**
82      * finish the task, remove the task's component from the progress bar UI, any additional incoming events from the
83      * contributors will be ignored.
84      */

85     public synchronized void finish() {
86         if (finished) {
87             return;
88         }
89         finished = true;
90         handle.finish();
91     }
92     
93     /**
94      * allows to set a custom initial delay for the progress task to appear in the
95      * status bar. This delay marks the time between starting of the progress handle
96      * and it's appearance in the status bar. If it finishes earlier, it's not shown at all.
97      * There is a default < 1s value for this. If you want to to appear earlier or later,
98      * call this method with the value you prefer before starting the handle.
99      * <p> Progress bars that are placed in custom dialogs do always appear right away without a delay.
100      * @param millis amount of miliseconds that shall pass before the progress appears in status bar.
101      */

102     public void setInitialDelay(int millis) {
103        handle.setInitialDelay(millis);
104     }
105     
106     /**
107      * add a contributor to the global, aggregated progress.
108      * Adding makes sense only if the task is still in progress.
109      */

110     public synchronized void addContributor(ProgressContributor contributor) {
111         if (finished) {
112             return;
113         }
114 // System.out.println("adding contributor=" + contributor.getTrackingId());
115
int length = contributors.size();
116         int remainingUnits = 0;
117         double completedRatio = 0;
118         Iterator JavaDoc<ProgressContributor> it;
119         if (length > 0) {
120             it = contributors.iterator();
121             while (it.hasNext()) {
122                 ProgressContributor cont = it.next();
123                 remainingUnits = remainingUnits + cont.getRemainingParentWorkUnits();
124                 completedRatio = completedRatio + (1 - cont.getCompletedRatio());
125             }
126         } else {
127             remainingUnits = WORKUNITS;
128             completedRatio = 0;
129         }
130
131 // int idealShare = WORKUNITS / (length + 1);
132
int currentShare = (int)(remainingUnits / (completedRatio + 1));
133 // System.out.println("ideal share=" + idealShare);
134
// System.out.println("current share=" + currentShare);
135
it = contributors.iterator();
136         while (it.hasNext()) {
137             ProgressContributor cont = it.next();
138             int newshare = (int)((1 - cont.getCompletedRatio()) * currentShare);
139 // System.out.println(" new share for " + cont.getTrackingId() + " is " + newshare);
140
remainingUnits = remainingUnits - newshare;
141             cont.setAvailableParentWorkUnits(newshare);
142         }
143 // System.out.println("new contributor share is=" + remainingUnits);
144
contributor.setAvailableParentWorkUnits(remainingUnits);
145         contributors.add(contributor);
146         contributor.setParent(this);
147         
148     }
149     
150     /**
151      * @deprecated do, not use, for tests only
152      */

153     int getCurrentProgress() {
154         return current;
155     }
156     
157     
158     void processContributorStep(ProgressContributor contributor, String JavaDoc message, int delta) {
159         synchronized (this) {
160             if (finished) {
161                 return;
162             }
163             current = current + delta;
164             handle.progress(message, current);
165         }
166         //shall we sychronize the monitor calls? since it calls out to client code,
167
// cannot guarantee how long it will last..
168
if (monitor != null) {
169             monitor.progressed(contributor);
170         }
171         
172     }
173     
174     void processContributorStart(ProgressContributor contributor, String JavaDoc message) {
175         synchronized (this) {
176             if (finished) {
177                 return;
178             }
179             if (message != null) {
180                 handle.progress(message);
181             }
182         }
183         //shall we sychronize the monitor calls? since it calls out to client code,
184
// cannot guarantee how long it will last..
185
if (monitor != null) {
186             monitor.started(contributor);
187         }
188     }
189     
190     void processContributorFinish(ProgressContributor contributor) {
191         synchronized (this) {
192             if (finished) {
193                 return;
194             }
195             contributors.remove(contributor);
196             if (contributors.size() == 0) {
197                 finish();
198             }
199         }
200         //shall we sychronize the monitor calls? since it calls out to client code,
201
// cannot guarantee how long it will last..
202
if (monitor != null) {
203             monitor.finished(contributor);
204         }
205     }
206     
207     
208     /**
209      * allow to watch the incoming events from the individual progress contributors.
210      */

211     public void setMonitor(ProgressMonitor monitor) {
212         this.monitor = monitor;
213     }
214     
215     /**
216      * change the display name of the progress task. Use with care, please make sure the changed name is not completely different,
217      * or otherwise it might appear to the user as a different task.
218      * @since 1.5
219      */

220     public void setDisplayName(String JavaDoc newDisplayName) {
221         handle.setDisplayName(newDisplayName);
222     }
223     
224    /**
225      * have the component in custom location, don't include in the status bar.
226      */

227     JComponent JavaDoc extractComponent() {
228         return ProgressHandleFactory.createProgressComponent(handle);
229     }
230
231     JLabel JavaDoc extractDetailLabel() {
232         return ProgressHandleFactory.createDetailLabelComponent(handle);
233     }
234
235     JLabel JavaDoc extractMainLabel() {
236         return ProgressHandleFactory.createMainLabelComponent(handle);
237     }
238 }
239
Popular Tags