1 package be
.nikiroo
.utils
.ui
;
3 import java
.awt
.GridLayout
;
4 import java
.awt
.event
.ActionEvent
;
5 import java
.awt
.event
.ActionListener
;
6 import java
.util
.ArrayList
;
7 import java
.util
.HashMap
;
11 import javax
.swing
.JPanel
;
12 import javax
.swing
.JProgressBar
;
13 import javax
.swing
.SwingUtilities
;
15 import be
.nikiroo
.utils
.Progress
;
18 * A graphical control to show the progress of a {@link Progress}.
20 * This control is <b>NOT</b> thread-safe.
24 public class ProgressBar
extends JPanel
{
25 private static final long serialVersionUID
= 1L;
27 private Map
<Progress
, JProgressBar
> bars
;
28 private List
<ActionListener
> actionListeners
;
29 private List
<ActionListener
> updateListeners
;
32 public ProgressBar() {
33 bars
= new HashMap
<Progress
, JProgressBar
>();
34 actionListeners
= new ArrayList
<ActionListener
>();
35 updateListeners
= new ArrayList
<ActionListener
>();
38 public void setProgress(final Progress pg
) {
41 SwingUtilities
.invokeLater(new Runnable() {
44 final JProgressBar bar
= new JProgressBar();
45 bar
.setStringPainted(true);
50 bar
.setMinimum(pg
.getMin());
51 bar
.setMaximum(pg
.getMax());
52 bar
.setValue(pg
.getProgress());
53 bar
.setString(pg
.getName());
55 pg
.addProgressListener(new Progress
.ProgressListener() {
56 public void progress(Progress progress
, String name
) {
57 final Progress
.ProgressListener l
= this;
58 SwingUtilities
.invokeLater(new Runnable() {
60 Map
<Progress
, JProgressBar
> newBars
= new HashMap
<Progress
, JProgressBar
>();
63 bar
.setMinimum(pg
.getMin());
64 bar
.setMaximum(pg
.getMax());
65 bar
.setValue(pg
.getProgress());
66 bar
.setString(pg
.getName());
68 for (Progress pg
: getChildrenAsOrderedList(pg
)) {
69 JProgressBar bar
= bars
.get(pg
);
71 bar
= new JProgressBar();
72 bar
.setStringPainted(true);
77 bar
.setMinimum(pg
.getMin());
78 bar
.setMaximum(pg
.getMax());
79 bar
.setValue(pg
.getProgress());
80 bar
.setString(pg
.getName());
83 if (ProgressBar
.this.pg
== null) {
89 pg
.removeProgressListener(l
);
90 for (ActionListener listener
: actionListeners
) {
91 listener
.actionPerformed(new ActionEvent(
110 public void addActionListener(ActionListener l
) {
111 actionListeners
.add(l
);
114 public void clearActionListeners() {
115 actionListeners
.clear();
118 public void addUpdateListener(ActionListener l
) {
119 updateListeners
.add(l
);
122 public void clearUpdateListeners() {
123 updateListeners
.clear();
126 public int getProgress() {
131 return pg
.getProgress();
135 private List
<Progress
> getChildrenAsOrderedList(Progress pg
) {
136 List
<Progress
> children
= new ArrayList
<Progress
>();
137 for (Progress child
: pg
.getChildren()) {
138 if (child
.getName() != null && !child
.getName().isEmpty()) {
141 children
.addAll(getChildrenAsOrderedList(child
));
147 private void update() {
152 setLayout(new GridLayout(bars
.size(), 1));
153 add(bars
.get(pg
), 0);
154 for (Progress child
: getChildrenAsOrderedList(pg
)) {
155 JProgressBar jbar
= bars
.get(child
);
165 for (ActionListener listener
: updateListeners
) {
166 listener
.actionPerformed(new ActionEvent(this, 0, "update"));