+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
-------------
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
+import java.util.Set;
/**
* Progress reporting system, possibly nested.
* 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
public void setProgress(int progress) {
int diff = this.progress - this.localProgress;
this.localProgress = progress;
- setTotalProgress(name, progress + diff);
+ setTotalProgress(this, name, progress + diff);
}
/**
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);
}
}
name = Progress.this.name;
}
- setTotalProgress(name, (int) Math.round(total * (max - min)));
+ setTotalProgress(progress, name,
+ (int) Math.round(total * (max - min)));
}
});
--- /dev/null
+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);
+ }
+}
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;
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);
}
});
}
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();
}
}