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
;
31 private Object lock
= new Object();
33 public ProgressBar() {
34 bars
= new HashMap
<Progress
, JProgressBar
>();
35 actionListeners
= new ArrayList
<ActionListener
>();
36 updateListeners
= new ArrayList
<ActionListener
>();
39 public ProgressBar(Progress pg
) {
44 public void setProgress(final Progress pg
) {
47 SwingUtilities
.invokeLater(new Runnable() {
51 final JProgressBar bar
= new JProgressBar();
52 bar
.setStringPainted(true);
57 bar
.setMinimum(pg
.getMin());
58 bar
.setMaximum(pg
.getMax());
59 bar
.setValue(pg
.getProgress());
60 bar
.setString(pg
.getName());
62 pg
.addProgressListener(new Progress
.ProgressListener() {
64 public void progress(Progress progress
, String name
) {
65 final Progress
.ProgressListener l
= this;
66 SwingUtilities
.invokeLater(new Runnable() {
69 Map
<Progress
, JProgressBar
> newBars
= new HashMap
<Progress
, JProgressBar
>();
72 bar
.setMinimum(pg
.getMin());
73 bar
.setMaximum(pg
.getMax());
74 bar
.setValue(pg
.getProgress());
75 bar
.setString(pg
.getName());
78 for (Progress pgChild
: getChildrenAsOrderedList(pg
)) {
79 JProgressBar barChild
= bars
81 if (barChild
== null) {
82 barChild
= new JProgressBar();
83 barChild
.setStringPainted(true);
86 newBars
.put(pgChild
, barChild
);
88 barChild
.setMinimum(pgChild
.getMin());
89 barChild
.setMaximum(pgChild
.getMax());
90 barChild
.setValue(pgChild
.getProgress());
91 barChild
.setString(pgChild
.getName());
94 if (ProgressBar
.this.pg
== null) {
101 if (ProgressBar
.this.pg
!= null) {
103 pg
.removeProgressListener(l
);
104 for (ActionListener listener
: actionListeners
) {
105 listener
.actionPerformed(new ActionEvent(
124 public void addActionListener(ActionListener l
) {
125 actionListeners
.add(l
);
128 public void clearActionListeners() {
129 actionListeners
.clear();
132 public void addUpdateListener(ActionListener l
) {
133 updateListeners
.add(l
);
136 public void clearUpdateListeners() {
137 updateListeners
.clear();
140 public int getProgress() {
145 return pg
.getProgress();
149 private List
<Progress
> getChildrenAsOrderedList(Progress pg
) {
150 List
<Progress
> children
= new ArrayList
<Progress
>();
152 synchronized (lock
) {
153 for (Progress child
: pg
.getChildren()) {
154 if (child
.getName() != null && !child
.getName().isEmpty()) {
157 children
.addAll(getChildrenAsOrderedList(child
));
164 private void update() {
165 synchronized (lock
) {
170 setLayout(new GridLayout(bars
.size(), 1));
171 add(bars
.get(pg
), 0);
172 for (Progress child
: getChildrenAsOrderedList(pg
)) {
173 JProgressBar jbar
= bars
.get(child
);
184 for (ActionListener listener
: updateListeners
) {
185 listener
.actionPerformed(new ActionEvent(this, 0, "update"));