KickJava   Java API By Example, From Geeks To Geeks.

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


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 /**
23  * A contributor to the aggragete progress indication.
24  * <b> This class is not threadsafe, you should access the contributor from
25  * one thread only. </b>
26  * @see AggregateProgressHandle#addContributor(ProgressContributor)
27  *
28  * @author mkleint
29  */

30 public final class ProgressContributor {
31     private String JavaDoc id;
32     private int workunits;
33     private int current;
34     private int parentUnits;
35     private int lastParentedUnit;
36     private AggregateProgressHandle parent;
37
38     /** Creates a new instance of ProgressContributor */
39     ProgressContributor(String JavaDoc id) {
40         this.id = id;
41         workunits = 0;
42         current = 0;
43         lastParentedUnit = 0;
44     }
45     
46     /**
47      * an id that allows identification of the progress contributor by the monitor.
48      */

49     public String JavaDoc getTrackingId() {
50         return id;
51     }
52     
53     void setParent(AggregateProgressHandle par) {
54         parent = par;
55     }
56     
57     int getWorkUnits() {
58         return workunits;
59     }
60     
61     int getRemainingParentWorkUnits() {
62         return parentUnits;
63     }
64     
65     void setAvailableParentWorkUnits(int newCount) {
66         parentUnits = newCount;
67     }
68     
69     double getCompletedRatio() {
70         return workunits == 0 ? 0 : (double)(current / workunits);
71     }
72     
73     /**
74      * start the progress indication for a task with known number of steps.
75      * @param workunits a total number of workunits that this contributor will process.
76      */

77     public void start(int workunits) {
78         if (parent == null) {
79             return;
80         }
81         this.workunits = workunits;
82         parent.processContributorStart(this, null);
83     }
84     
85     
86     /**
87      * finish the contributor, possibly also the whole task if this instance was
88      * the last one to finish.
89      */

90     public void finish() {
91         if (parent == null) {
92             return;
93         }
94         if (current < workunits) {
95             progress(null, workunits);
96         }
97         parent.processContributorFinish(this);
98     }
99     
100     
101     /**
102      * Notify the user about completed workunits.
103      * @param workunit a cumulative number of workunits completed so far
104      */

105     public void progress(int workunit) {
106         progress(null, workunit);
107     }
108     
109     /**
110      * Notify the user about progress by showing message with details.
111      * @param message detailed info about current progress
112      */

113     public void progress(String JavaDoc message) {
114         progress(message, current);
115     }
116     
117     /**
118      * Notify the user about completed workunits.
119      * @param message detailed info about current progress
120      * @param unit a cumulative number of workunits completed so far
121      */

122     public void progress(String JavaDoc message, int unit) {
123         if (parent == null) {
124             return;
125         }
126         assert unit >= current && unit <= workunits;
127         if (message != null && unit == current) {
128             // we need to process the message in any case..
129
parent.processContributorStep(this, message, 0);
130             return;
131         }
132         current = unit;
133         int delta = current - lastParentedUnit;
134         double step = (1 / ((double)parentUnits / (double)(workunits - lastParentedUnit)));
135 // System.out.println("progress.. current=" + current + " latparented=" + lastParentedUnit);
136
// System.out.println(" parent units=" + parentUnits);
137
// System.out.println(" delta=" + delta);
138
// System.out.println(" step=" + step);
139
if (delta >= step) {
140             int count = (int) (delta / step);
141             lastParentedUnit = lastParentedUnit + (int)(count * step);
142             parentUnits = parentUnits - count;
143 // System.out.println(" count=" + count);
144
// System.out.println(" newparented=" + lastParentedUnit);
145
// System.out.println(" parentUnits=" + parentUnits);
146
// call parent..
147
parent.processContributorStep(this, message, count);
148         }
149     }
150 }
151
Popular Tags