Add more warnings source to 1.6) and fix warnings
[nikiroo-utils.git] / src / be / nikiroo / utils / Progress.java
index 5af7b30d83b357654d2b34e3bff3faa3eb8de9d6..2872530b68542585783467e3241835072e75570a 100644 (file)
@@ -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<Progress> getChildren() {
                return children.keySet();
@@ -272,9 +294,30 @@ public class Progress {
         *            the progress to set
         */
        private void setTotalProgress(Progress pg, String name, int progress) {
+               // TODO: name is not used... and this is probably a bug in this case
                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);
                        }
@@ -337,6 +380,7 @@ public class Progress {
                }
 
                progress.addProgressListener(new ProgressListener() {
+                       @Override
                        public void progress(Progress pg, String name) {
                                synchronized (getLock()) {
                                        double total = ((double) localProgress) / (max - min);