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() {
45 final JProgressBar bar
= new JProgressBar();
46 bar
.setStringPainted(true);
51 bar
.setMinimum(pg
.getMin());
52 bar
.setMaximum(pg
.getMax());
53 bar
.setValue(pg
.getProgress());
54 bar
.setString(pg
.getName());
56 pg
.addProgressListener(new Progress
.ProgressListener() {
58 public void progress(Progress progress
, String name
) {
59 final Progress
.ProgressListener l
= this;
60 SwingUtilities
.invokeLater(new Runnable() {
63 Map
<Progress
, JProgressBar
> newBars
= new HashMap
<Progress
, JProgressBar
>();
66 bar
.setMinimum(pg
.getMin());
67 bar
.setMaximum(pg
.getMax());
68 bar
.setValue(pg
.getProgress());
69 bar
.setString(pg
.getName());
71 for (Progress pgChild
: getChildrenAsOrderedList(pg
)) {
72 JProgressBar barChild
= bars
74 if (barChild
== null) {
75 barChild
= new JProgressBar();
76 barChild
.setStringPainted(true);
79 newBars
.put(pgChild
, barChild
);
81 barChild
.setMinimum(pgChild
.getMin());
82 barChild
.setMaximum(pgChild
.getMax());
83 barChild
.setValue(pgChild
.getProgress());
84 barChild
.setString(pgChild
.getName());
87 if (ProgressBar
.this.pg
== null) {
93 pg
.removeProgressListener(l
);
94 for (ActionListener listener
: actionListeners
) {
95 listener
.actionPerformed(new ActionEvent(
114 public void addActionListener(ActionListener l
) {
115 actionListeners
.add(l
);
118 public void clearActionListeners() {
119 actionListeners
.clear();
122 public void addUpdateListener(ActionListener l
) {
123 updateListeners
.add(l
);
126 public void clearUpdateListeners() {
127 updateListeners
.clear();
130 public int getProgress() {
135 return pg
.getProgress();
139 private List
<Progress
> getChildrenAsOrderedList(Progress pg
) {
140 List
<Progress
> children
= new ArrayList
<Progress
>();
141 for (Progress child
: pg
.getChildren()) {
142 if (child
.getName() != null && !child
.getName().isEmpty()) {
145 children
.addAll(getChildrenAsOrderedList(child
));
151 private void update() {
156 setLayout(new GridLayout(bars
.size(), 1));
157 add(bars
.get(pg
), 0);
158 for (Progress child
: getChildrenAsOrderedList(pg
)) {
159 JProgressBar jbar
= bars
.get(child
);
169 for (ActionListener listener
: updateListeners
) {
170 listener
.actionPerformed(new ActionEvent(this, 0, "update"));