From: Niki Roo Date: Sun, 5 Mar 2017 17:07:17 +0000 (+0100) Subject: Version 1.3.5: better ProgressBar X-Git-Tag: nikiroo-utils-1.3.5~1 X-Git-Url: http://git.nikiroo.be/?p=nikiroo-utils.git;a=commitdiff_plain;h=88b36f83bb4bb9201339432c97e6d826aa9e1903 Version 1.3.5: better ProgressBar --- diff --git a/VERSION b/VERSION index d0149fe..80e78df 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -1.3.4 +1.3.5 diff --git a/changelog b/changelog index 588ef48..7f23859 100644 --- 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 ------------- diff --git a/src/be/nikiroo/utils/Progress.java b/src/be/nikiroo/utils/Progress.java index 473e806..b31cde4 100644 --- a/src/be/nikiroo/utils/Progress.java +++ b/src/be/nikiroo/utils/Progress.java @@ -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 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 index 0000000..b17cfb8 --- /dev/null +++ b/src/be/nikiroo/utils/test/ProgressBarManualTest.java @@ -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); + } +} diff --git a/src/be/nikiroo/utils/ui/ProgressBar.java b/src/be/nikiroo/utils/ui/ProgressBar.java index 2abd2b7..6e1e7ce 100644 --- a/src/be/nikiroo/utils/ui/ProgressBar.java +++ b/src/be/nikiroo/utils/ui/ProgressBar.java @@ -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 bars; private List listeners; + private Progress pg; public ProgressBar() { - bar = new JProgressBar(); - label = new JLabel(); + bars = new HashMap(); listeners = new ArrayList(); - - 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 newBars = new HashMap(); + 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 getChildrenAsOrderedList(Progress pg) { + List children = new ArrayList(); + 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(); } }