Version 1.3.5: better ProgressBar
authorNiki Roo <niki@nikiroo.be>
Sun, 5 Mar 2017 17:07:17 +0000 (18:07 +0100)
committerNiki Roo <niki@nikiroo.be>
Sun, 5 Mar 2017 17:07:17 +0000 (18:07 +0100)
VERSION
changelog
src/be/nikiroo/utils/Progress.java
src/be/nikiroo/utils/test/ProgressBarManualTest.java [new file with mode: 0644]
src/be/nikiroo/utils/ui/ProgressBar.java

diff --git a/VERSION b/VERSION
index d0149fef743a8035720ed161412709e87702dcab..80e78df6830f8bf4da6777e2ab28252afc8c7507 100644 (file)
--- a/VERSION
+++ b/VERSION
@@ -1 +1 @@
-1.3.4
+1.3.5
index 588ef48a977107d8fb7d9e073c949c4dd8257c3b..7f23859d30e86f7f6828c9945a49ffa583f38eb3 100644 (file)
--- a/changelog
+++ b/changelog
@@ -1,3 +1,10 @@
+Version 1.3.5
+-------------
+
+Improve ProgressBar UI
+       It now shows all the progression bars of the different steps of
+       progression at the same time
+
 Version 1.3.4
 -------------
 
index 473e80662a4a816a1fb8af21d82421c654f6ee3d..b31cde49456174ef5fd5487f2feab00324a451f6 100644 (file)
@@ -6,6 +6,7 @@ import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
+import java.util.Set;
 
 /**
  * Progress reporting system, possibly nested.
@@ -18,7 +19,7 @@ public class Progress {
                 * A progression event.
                 * 
                 * @param progress
-                *            the {@link Progress} object that generated it, or a parent
+                *            the {@link Progress} object that generated it
                 * @param name
                 *            the first non-null name of the {@link Progress} step that
                 *            generated this event
@@ -188,7 +189,7 @@ public class Progress {
        public void setProgress(int progress) {
                int diff = this.progress - this.localProgress;
                this.localProgress = progress;
-               setTotalProgress(name, progress + diff);
+               setTotalProgress(this, name, progress + diff);
        }
 
        /**
@@ -211,22 +212,34 @@ public class Progress {
                return (((double) progress) / (max - min));
        }
 
+       /**
+        * Return the list of direct children of this {@link Progress}.
+        * 
+        * @return the children (who will think of them??)
+        */
+       public Set<Progress> getChildren() {
+               return children.keySet();
+       }
+
        /**
         * Set the total progress value (including the optional children
         * {@link Progress}), on a {@link Progress#getMin()} to
         * {@link Progress#getMax()} scale.
         * 
+        * @param pg
+        *            the {@link Progress} to report as the progression emitter
         * @param name
         *            the current name (if it is NULL, the first non-null name in
-        *            the hierarchy will overwrite it)
+        *            the hierarchy will overwrite it) of the {@link Progress} who
+        *            emitted this change
         * @param progress
         *            the progress to set
         */
-       private void setTotalProgress(String name, int progress) {
+       private void setTotalProgress(Progress pg, String name, int progress) {
                this.progress = progress;
 
                for (ProgressListener l : listeners) {
-                       l.progress(this, name);
+                       l.progress(pg, name);
                }
        }
 
@@ -270,7 +283,8 @@ public class Progress {
                                        name = Progress.this.name;
                                }
 
-                               setTotalProgress(name, (int) Math.round(total * (max - min)));
+                               setTotalProgress(progress, name,
+                                               (int) Math.round(total * (max - min)));
                        }
                });
 
diff --git a/src/be/nikiroo/utils/test/ProgressBarManualTest.java b/src/be/nikiroo/utils/test/ProgressBarManualTest.java
new file mode 100644 (file)
index 0000000..b17cfb8
--- /dev/null
@@ -0,0 +1,62 @@
+package be.nikiroo.utils.test;
+
+import java.awt.BorderLayout;
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
+
+import javax.swing.JButton;
+import javax.swing.JFrame;
+
+import be.nikiroo.utils.Progress;
+import be.nikiroo.utils.ui.ProgressBar;
+
+public class ProgressBarManualTest extends JFrame {
+       private static final long serialVersionUID = 1L;
+       private int i = 0;
+
+       public ProgressBarManualTest() {
+               final ProgressBar bar = new ProgressBar();
+               final Progress pg = new Progress("name");
+               final Progress pg2 = new Progress("second level");
+
+               setLayout(new BorderLayout());
+               this.add(bar, BorderLayout.SOUTH);
+
+               final JButton b = new JButton("Set pg to 10%");
+               b.addActionListener(new ActionListener() {
+                       public void actionPerformed(ActionEvent e) {
+                               switch (i) {
+                               case 0:
+                                       pg.setProgress(10);
+                                       b.setText("Set pg to 20%");
+                                       break;
+                               case 1:
+                                       pg.setProgress(20);
+                                       b.setText("Add second pg");
+                                       break;
+                               case 2:
+                                       pg.addProgress(pg2, 80);
+                                       pg2.setProgress(0);
+                                       b.setText("set second pg to 100%");
+                                       break;
+                               case 3:
+                                       pg2.setProgress(100);
+                                       b.setText("");
+                                       break;
+                               }
+
+                               i++;
+                       }
+               });
+               this.add(b, BorderLayout.CENTER);
+
+               setSize(800, 600);
+               setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+
+               bar.setProgress(pg);
+       }
+
+       public static void main(String[] args) {
+               new ProgressBarManualTest().setVisible(true);
+       }
+}
index 2abd2b7dc25d1a868a781e5ce9fbcae55fb585e2..6e1e7cef151c5fd561c953a58bf3bf7565667e1e 100644 (file)
@@ -1,12 +1,13 @@
 package be.nikiroo.utils.ui;
 
-import java.awt.BorderLayout;
+import java.awt.GridLayout;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
 import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
 
-import javax.swing.JLabel;
 import javax.swing.JPanel;
 import javax.swing.JProgressBar;
 import javax.swing.SwingUtilities;
@@ -16,40 +17,70 @@ import be.nikiroo.utils.Progress;
 public class ProgressBar extends JPanel {
        private static final long serialVersionUID = 1L;
 
-       private JProgressBar bar;
-       private JLabel label;
+       private Map<Progress, JProgressBar> bars;
        private List<ActionListener> listeners;
+       private Progress pg;
 
        public ProgressBar() {
-               bar = new JProgressBar();
-               label = new JLabel();
+               bars = new HashMap<Progress, JProgressBar>();
                listeners = new ArrayList<ActionListener>();
-
-               setLayout(new BorderLayout());
        }
 
-       public void setProgress(Progress pg) {
+       public void setProgress(final Progress pg) {
+               this.pg = pg;
+
                if (pg == null) {
                        setPresent(false);
                } else {
-                       label.setText(pg.getName());
+                       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(final Progress progress, final String name) {
+                               public void progress(Progress progress, String name) {
+
                                        SwingUtilities.invokeLater(new Runnable() {
                                                public void run() {
-                                                       label.setText(name);
-                                                       bar.setValue(progress.getProgress());
+                                                       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);
+                                                               }
 
-                                                       if (progress.isDone()) {
+                                                               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"));
                                                                }
                                                        }
+
+                                                       setPresent(true);
                                                }
                                        });
                                }
@@ -67,15 +98,28 @@ public class ProgressBar extends JPanel {
                listeners.clear();
        }
 
+       private List<Progress> getChildrenAsOrderedList(Progress pg) {
+               List<Progress> children = new ArrayList<Progress>();
+               for (Progress child : pg.getChildren()) {
+                       children.add(child);
+                       children.addAll(getChildrenAsOrderedList(child));
+               }
+
+               return children;
+       }
+
        private void setPresent(boolean present) {
                removeAll();
 
                if (present) {
-                       add(label, BorderLayout.NORTH);
-                       add(bar, BorderLayout.CENTER);
+                       setLayout(new GridLayout(bars.size(), 1));
+                       add(bars.get(pg), 0);
+                       for (Progress child : getChildrenAsOrderedList(pg)) {
+                               add(bars.get(child));
+                       }
                }
 
-               validate();
+               revalidate();
                repaint();
        }
 }