bea57845bcefaf4a8fd336d8e284750ae8488f8c
1 package be
.nikiroo
.utils
;
3 import java
.util
.ArrayList
;
4 import java
.util
.EventListener
;
5 import java
.util
.HashMap
;
8 import java
.util
.Map
.Entry
;
12 * Progress reporting system, possibly nested.
16 public class Progress
{
17 public interface ProgressListener
extends EventListener
{
19 * A progression event.
22 * the {@link Progress} object that generated it
24 * the first non-null name of the {@link Progress} step that
25 * generated this event
27 public void progress(Progress progress
, String name
);
31 private Map
<Progress
, Double
> children
;
32 private List
<ProgressListener
> listeners
;
35 private int localProgress
;
36 private int progress
; // children included
39 * Create a new default unnamed {@link Progress}, from 0 to 100.
46 * Create a new default {@link Progress}, from 0 to 100.
49 * the name of this {@link Progress} step
51 public Progress(String name
) {
56 * Create a new unnamed {@link Progress}, from min to max.
59 * the minimum progress value (and starting value) -- must be
62 * the maximum progress value
64 public Progress(int min
, int max
) {
69 * Create a new {@link Progress}, from min to max.
72 * the name of this {@link Progress} step
74 * the minimum progress value (and starting value) -- must be
77 * the maximum progress value
79 public Progress(String name
, int min
, int max
) {
81 this.children
= new HashMap
<Progress
, Double
>();
82 this.listeners
= new ArrayList
<Progress
.ProgressListener
>();
88 * The name of this {@link Progress} step.
92 public String
getName() {
97 * The name of this {@link Progress} step.
102 public void setName(String name
) {
104 // will fire an action event:
105 setProgress(this.localProgress
);
109 * The minimum progress value.
113 public int getMin() {
118 * The minimum progress value.
123 public void setMin(int min
) {
125 throw new Error("negative values not supported");
130 "The minimum progress value must be <= the maximum progress value");
137 * The maximum progress value.
141 public int getMax() {
146 * The maximum progress value (must be >= the minimum progress value).
151 public void setMax(int max
) {
154 "The maximum progress value must be >= the minimum progress value");
161 * Set both the minimum and maximum progress values.
168 public void setMinMax(int min
, int max
) {
170 throw new Error("negative values not supported");
175 "The minimum progress value must be <= the maximum progress value");
183 * Get the total progress value (including the optional children
184 * {@link Progress}) on a {@link Progress#getMin()} to
185 * {@link Progress#getMax()} scale.
187 * @return the progress the value
189 public int getProgress() {
194 * Set the local progress value (not including the optional children
195 * {@link Progress}), on a {@link Progress#getMin()} to
196 * {@link Progress#getMax()} scale.
199 * the progress to set
201 public void setProgress(int progress
) {
202 int diff
= this.progress
- this.localProgress
;
203 this.localProgress
= progress
;
204 setTotalProgress(this, name
, progress
+ diff
);
208 * Check if the action corresponding to this {@link Progress} is done (i.e.,
209 * if its progress value is >= its max value).
211 * @return TRUE if it is
213 public boolean isDone() {
214 return progress
>= max
;
218 * Get the total progress value (including the optional children
219 * {@link Progress}) on a 0.0 to 1.0 scale.
221 * @return the progress
223 public double getRelativeProgress() {
224 return (((double) progress
) / (max
- min
));
228 * Return the list of direct children of this {@link Progress}.
230 * @return the children (who will think of them??)
232 public Set
<Progress
> getChildren() {
233 return children
.keySet();
237 * Set the total progress value (including the optional children
238 * {@link Progress}), on a {@link Progress#getMin()} to
239 * {@link Progress#getMax()} scale.
242 * the {@link Progress} to report as the progression emitter
244 * the current name (if it is NULL, the first non-null name in
245 * the hierarchy will overwrite it) of the {@link Progress} who
246 * emitted this change
248 * the progress to set
250 private void setTotalProgress(Progress pg
, String name
, int progress
) {
251 this.progress
= progress
;
253 for (ProgressListener l
: listeners
) {
254 l
.progress(pg
, name
);
259 * Add a {@link ProgressListener} that will trigger on progress changes.
264 public void addProgressListener(ProgressListener l
) {
265 this.listeners
.add(l
);
269 * Remove a {@link ProgressListener} that would trigger on progress changes.
274 * @return TRUE if it was found (and removed)
276 public boolean removeProgressListener(ProgressListener l
) {
277 return this.listeners
.remove(l
);
281 * Add a child {@link Progress} of the given weight.
284 * the child {@link Progress} to add
286 * the weight (on a {@link Progress#getMin()} to
287 * {@link Progress#getMax()} scale) of this child
288 * {@link Progress} in relation to its parent
290 public void addProgress(Progress progress
, double weight
) {
291 if (weight
< min
|| weight
> max
) {
293 "A Progress object cannot have a weight outside its parent range");
296 // Note: this is quite inefficient, especially with many children
298 progress
.addProgressListener(new ProgressListener() {
299 public void progress(Progress progress
, String name
) {
300 double total
= ((double) localProgress
) / (max
- min
);
301 for (Entry
<Progress
, Double
> entry
: children
.entrySet()) {
302 total
+= (entry
.getValue() / (max
- min
))
303 * entry
.getKey().getRelativeProgress();
307 name
= Progress
.this.name
;
310 setTotalProgress(progress
, name
,
311 (int) Math
.round(total
* (max
- min
)));
315 this.children
.put(progress
, weight
);