From: Niki Roo Date: Sun, 5 Mar 2017 18:48:00 +0000 (+0100) Subject: Version 1.3.5: Progress/ProgressBar fixes (woopsie) X-Git-Tag: fanfix-swing-0.0.1~21^2~284 X-Git-Url: https://git.nikiroo.be/?a=commitdiff_plain;h=2998b78ae098aff12c0f8cfad2a6bc5303bb33e0;p=fanfix-swing.git Version 1.3.5: Progress/ProgressBar fixes (woopsie) --- diff --git a/src/be/nikiroo/utils/Progress.java b/src/be/nikiroo/utils/Progress.java index b31cde49..bea57845 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 b17cfb88..c94ea615 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 6e1e7cef..6e1e44a9 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")); + } } }