X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Futils%2FProgress.java;h=5ee32217d466c1ad7bbb13e21ecf24e6fe457f57;hb=refs%2Ftags%2Fnikiroo-utils-1.4.1;hp=5af7b30d83b357654d2b34e3bff3faa3eb8de9d6;hpb=0924c45dd84be115c6c622b075445855b13951e5;p=nikiroo-utils.git diff --git a/src/be/nikiroo/utils/Progress.java b/src/be/nikiroo/utils/Progress.java index 5af7b30..5ee3221 100644 --- a/src/be/nikiroo/utils/Progress.java +++ b/src/be/nikiroo/utils/Progress.java @@ -106,8 +106,7 @@ public class Progress { */ public void setName(String name) { this.name = name; - // will fire an action event: - setProgress(this.localProgress); + changed(this); } /** @@ -228,9 +227,21 @@ public class Progress { } } + /** + * Add some value to the current progression of this {@link Progress}. + * + * @param step + * the amount to add + */ + public void add(int step) { + synchronized (getLock()) { + setProgress(localProgress + step); + } + } + /** * Check if the action corresponding to this {@link Progress} is done (i.e., - * if its progress value is >= its max value). + * if its progress value == its max value). * * @return TRUE if it is */ @@ -238,6 +249,13 @@ public class Progress { return progress >= max; } + /** + * Mark the {@link Progress} as done by setting its value to max. + */ + public void done() { + setProgress(getMax()); + } + /** * Get the total progress value (including the optional children * {@link Progress}) on a 0.0 to 1.0 scale. @@ -245,13 +263,17 @@ public class Progress { * @return the progress */ public double getRelativeProgress() { + if (max == min) { + return 1; + } + return (((double) progress) / (max - min)); } /** * Return the list of direct children of this {@link Progress}. * - * @return the children (who will think of them??) + * @return the children (Who will think of the children??) */ public Set getChildren() { return children.keySet(); @@ -273,8 +295,28 @@ public class Progress { */ private void setTotalProgress(Progress pg, String name, int progress) { synchronized (getLock()) { - this.progress = progress; + progress = Math.max(min, progress); + progress = Math.min(max, progress); + + if (progress != this.progress) { + this.progress = progress; + changed(pg); + } + } + } + + /** + * Notify the listeners that this {@link Progress} changed value. + * + * @param pg + * the emmiter + */ + private void changed(Progress pg) { + if (pg == null) { + pg = this; + } + synchronized (getLock()) { for (ProgressListener l : listeners) { l.progress(pg, name); }