From 2998b78ae098aff12c0f8cfad2a6bc5303bb33e0 Mon Sep 17 00:00:00 2001 From: Niki Roo Date: Sun, 5 Mar 2017 19:48:00 +0100 Subject: [PATCH] Version 1.3.5: Progress/ProgressBar fixes (woopsie) --- src/be/nikiroo/utils/Progress.java | 24 +++ .../utils/test/ProgressBarManualTest.java | 4 +- src/be/nikiroo/utils/ui/ProgressBar.java | 172 +++++++++++------- 3 files changed, 135 insertions(+), 65 deletions(-) diff --git a/src/be/nikiroo/utils/Progress.java b/src/be/nikiroo/utils/Progress.java index b31cde4..bea5784 100644 --- a/src/be/nikiroo/utils/Progress.java +++ b/src/be/nikiroo/utils/Progress.java @@ -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. * diff --git a/src/be/nikiroo/utils/test/ProgressBarManualTest.java b/src/be/nikiroo/utils/test/ProgressBarManualTest.java index b17cfb8..c94ea61 100644 --- a/src/be/nikiroo/utils/test/ProgressBarManualTest.java +++ b/src/be/nikiroo/utils/test/ProgressBarManualTest.java @@ -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; } diff --git a/src/be/nikiroo/utils/ui/ProgressBar.java b/src/be/nikiroo/utils/ui/ProgressBar.java index 6e1e7ce..6e1e44a 100644 --- a/src/be/nikiroo/utils/ui/ProgressBar.java +++ b/src/be/nikiroo/utils/ui/ProgressBar.java @@ -14,112 +14,156 @@ import javax.swing.SwingUtilities; import be.nikiroo.utils.Progress; +/** + * A graphical control to show the progress of a {@link Progress}. + *

+ * This control is NOT thread-safe. + * + * @author niki + */ public class ProgressBar extends JPanel { private static final long serialVersionUID = 1L; private Map bars; - private List listeners; + private List actionListeners; + private List updateListeners; private Progress pg; public ProgressBar() { bars = new HashMap(); - listeners = new ArrayList(); + actionListeners = new ArrayList(); + updateListeners = new ArrayList(); } 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 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); - } - - 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 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); + } + + 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 getChildrenAsOrderedList(Progress pg) { List children = new ArrayList(); 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")); + } } } -- 2.27.0