Version 1.3.5: Progress/ProgressBar fixes (woopsie)
authorNiki Roo <niki@nikiroo.be>
Sun, 5 Mar 2017 18:48:00 +0000 (19:48 +0100)
committerNiki Roo <niki@nikiroo.be>
Sun, 5 Mar 2017 18:48:00 +0000 (19:48 +0100)
src/be/nikiroo/utils/Progress.java
src/be/nikiroo/utils/test/ProgressBarManualTest.java
src/be/nikiroo/utils/ui/ProgressBar.java

index b31cde49456174ef5fd5487f2feab00324a451f6..bea57845bcefaf4a8fd336d8e284750ae8488f8c 100644 (file)
@@ -93,6 +93,18 @@ public class Progress {
                return name;
        }
 
+       /**
+        * The name of this {@link Progress} step.
+        * 
+        * @param name
+        *            the new name
+        */
+       public void setName(String name) {
+               this.name = name;
+               // will fire an action event:
+               setProgress(this.localProgress);
+       }
+
        /**
         * The minimum progress value.
         * 
@@ -253,6 +265,18 @@ public class Progress {
                this.listeners.add(l);
        }
 
+       /**
+        * Remove a {@link ProgressListener} that would trigger on progress changes.
+        * 
+        * @param l
+        *            the listener
+        * 
+        * @return TRUE if it was found (and removed)
+        */
+       public boolean removeProgressListener(ProgressListener l) {
+               return this.listeners.remove(l);
+       }
+
        /**
         * Add a child {@link Progress} of the given weight.
         * 
index b17cfb880e90e34763e72917f474da7d9b4e298f..c94ea61592c24c4bcbde579750ea5b461e0d01f5 100644 (file)
@@ -28,6 +28,7 @@ public class ProgressBarManualTest extends JFrame {
                                switch (i) {
                                case 0:
                                        pg.setProgress(10);
+                                       pg2.setProgress(0);
                                        b.setText("Set pg to 20%");
                                        break;
                                case 1:
@@ -41,7 +42,8 @@ public class ProgressBarManualTest extends JFrame {
                                        break;
                                case 3:
                                        pg2.setProgress(100);
-                                       b.setText("");
+                                       b.setText("Set pg to 10%");
+                                       i = -1;
                                        break;
                                }
 
index 6e1e7cef151c5fd561c953a58bf3bf7565667e1e..6e1e44a9f7057748389047c5d7def8c0aabf7982 100644 (file)
@@ -14,112 +14,156 @@ import javax.swing.SwingUtilities;
 
 import be.nikiroo.utils.Progress;
 
+/**
+ * A graphical control to show the progress of a {@link Progress}.
+ * <p>
+ * This control is <b>NOT</b> thread-safe.
+ * 
+ * @author niki
+ */
 public class ProgressBar extends JPanel {
        private static final long serialVersionUID = 1L;
 
        private Map<Progress, JProgressBar> bars;
-       private List<ActionListener> listeners;
+       private List<ActionListener> actionListeners;
+       private List<ActionListener> updateListeners;
        private Progress pg;
 
        public ProgressBar() {
                bars = new HashMap<Progress, JProgressBar>();
-               listeners = new ArrayList<ActionListener>();
+               actionListeners = new ArrayList<ActionListener>();
+               updateListeners = new ArrayList<ActionListener>();
        }
 
        public void setProgress(final Progress pg) {
                this.pg = pg;
 
-               if (pg == null) {
-                       setPresent(false);
-               } else {
-                       final JProgressBar bar = new JProgressBar();
-                       bar.setStringPainted(true);
-
-                       bars.clear();
-                       bars.put(pg, bar);
-
-                       bar.setMinimum(pg.getMin());
-                       bar.setMaximum(pg.getMax());
-                       bar.setValue(pg.getProgress());
-                       bar.setString(pg.getName());
-
-                       pg.addProgressListener(new Progress.ProgressListener() {
-                               public void progress(Progress progress, String name) {
-
-                                       SwingUtilities.invokeLater(new Runnable() {
-                                               public void run() {
-                                                       Map<Progress, JProgressBar> newBars = new HashMap<Progress, JProgressBar>();
-                                                       newBars.put(pg, bar);
-
-                                                       bar.setMinimum(pg.getMin());
-                                                       bar.setMaximum(pg.getMax());
-                                                       bar.setValue(pg.getProgress());
-                                                       bar.setString(pg.getName());
-
-                                                       for (Progress pg : getChildrenAsOrderedList(pg)) {
-                                                               JProgressBar bar = bars.get(pg);
-                                                               if (bar == null) {
-                                                                       bar = new JProgressBar();
-                                                                       bar.setStringPainted(true);
-                                                               }
-
-                                                               newBars.put(pg, bar);
-
-                                                               bar.setMinimum(pg.getMin());
-                                                               bar.setMaximum(pg.getMax());
-                                                               bar.setValue(pg.getProgress());
-                                                               bar.setString(pg.getName());
-                                                       }
-
-                                                       bars = newBars;
-
-                                                       if (pg.isDone()) {
-                                                               for (ActionListener listener : listeners) {
-                                                                       listener.actionPerformed(new ActionEvent(
-                                                                                       ProgressBar.this, 0, "done"));
+               SwingUtilities.invokeLater(new Runnable() {
+                       public void run() {
+                               if (pg != null) {
+                                       final JProgressBar bar = new JProgressBar();
+                                       bar.setStringPainted(true);
+
+                                       bars.clear();
+                                       bars.put(pg, bar);
+
+                                       bar.setMinimum(pg.getMin());
+                                       bar.setMaximum(pg.getMax());
+                                       bar.setValue(pg.getProgress());
+                                       bar.setString(pg.getName());
+
+                                       pg.addProgressListener(new Progress.ProgressListener() {
+                                               public void progress(Progress progress, String name) {
+                                                       final Progress.ProgressListener l = this;
+                                                       SwingUtilities.invokeLater(new Runnable() {
+                                                               public void run() {
+                                                                       Map<Progress, JProgressBar> newBars = new HashMap<Progress, JProgressBar>();
+                                                                       newBars.put(pg, bar);
+
+                                                                       bar.setMinimum(pg.getMin());
+                                                                       bar.setMaximum(pg.getMax());
+                                                                       bar.setValue(pg.getProgress());
+                                                                       bar.setString(pg.getName());
+
+                                                                       for (Progress pg : getChildrenAsOrderedList(pg)) {
+                                                                               JProgressBar bar = bars.get(pg);
+                                                                               if (bar == null) {
+                                                                                       bar = new JProgressBar();
+                                                                                       bar.setStringPainted(true);
+                                                                               }
+
+                                                                               newBars.put(pg, bar);
+
+                                                                               bar.setMinimum(pg.getMin());
+                                                                               bar.setMaximum(pg.getMax());
+                                                                               bar.setValue(pg.getProgress());
+                                                                               bar.setString(pg.getName());
+                                                                       }
+
+                                                                       if (ProgressBar.this.pg == null) {
+                                                                               bars.clear();
+                                                                       } else {
+                                                                               bars = newBars;
+
+                                                                               if (pg.isDone()) {
+                                                                                       pg.removeProgressListener(l);
+                                                                                       for (ActionListener listener : actionListeners) {
+                                                                                               listener.actionPerformed(new ActionEvent(
+                                                                                                               ProgressBar.this, 0,
+                                                                                                               "done"));
+                                                                                       }
+                                                                               }
+
+                                                                               update();
+                                                                       }
                                                                }
-                                                       }
-
-                                                       setPresent(true);
+                                                       });
                                                }
                                        });
                                }
-                       });
 
-                       setPresent(true);
-               }
+                               update();
+                       }
+               });
        }
 
-       public void addActioListener(ActionListener l) {
-               listeners.add(l);
+       public void addActionListener(ActionListener l) {
+               actionListeners.add(l);
        }
 
        public void clearActionListeners() {
-               listeners.clear();
+               actionListeners.clear();
+       }
+
+       public void addUpdateListener(ActionListener l) {
+               updateListeners.add(l);
        }
 
+       public void clearUpdateListeners() {
+               updateListeners.clear();
+       }
+
+       public int getProgress() {
+               if (pg == null) {
+                       return 0;
+               }
+
+               return pg.getProgress();
+       }
+
+       // only named ones
        private List<Progress> getChildrenAsOrderedList(Progress pg) {
                List<Progress> children = new ArrayList<Progress>();
                for (Progress child : pg.getChildren()) {
-                       children.add(child);
+                       if (child.getName() != null && !child.getName().isEmpty()) {
+                               children.add(child);
+                       }
                        children.addAll(getChildrenAsOrderedList(child));
                }
 
                return children;
        }
 
-       private void setPresent(boolean present) {
+       private void update() {
+               invalidate();
                removeAll();
 
-               if (present) {
+               if (pg != null) {
                        setLayout(new GridLayout(bars.size(), 1));
                        add(bars.get(pg), 0);
                        for (Progress child : getChildrenAsOrderedList(pg)) {
-                               add(bars.get(child));
+                               JProgressBar jbar = bars.get(child);
+                               if (jbar != null) {
+                                       add(jbar);
+                               }
                        }
                }
 
-               revalidate();
+               validate();
                repaint();
+
+               for (ActionListener listener : updateListeners) {
+                       listener.actionPerformed(new ActionEvent(this, 0, "update"));
+               }
        }
 }