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 void setProgress(final Progress pg
) {
42 SwingUtilities
.invokeLater(new Runnable() {
46 final JProgressBar bar
= new JProgressBar();
47 bar
.setStringPainted(true);
52 bar
.setMinimum(pg
.getMin());
53 bar
.setMaximum(pg
.getMax());
54 bar
.setValue(pg
.getProgress());
55 bar
.setString(pg
.getName());
57 pg
.addProgressListener(new Progress
.ProgressListener() {
59 public void progress(Progress progress
, String name
) {
60 final Progress
.ProgressListener l
= this;
61 SwingUtilities
.invokeLater(new Runnable() {
64 Map
<Progress
, JProgressBar
> newBars
= new HashMap
<Progress
, JProgressBar
>();
67 bar
.setMinimum(pg
.getMin());
68 bar
.setMaximum(pg
.getMax());
69 bar
.setValue(pg
.getProgress());
70 bar
.setString(pg
.getName());
73 for (Progress pgChild
: getChildrenAsOrderedList(pg
)) {
74 JProgressBar barChild
= bars
76 if (barChild
== null) {
77 barChild
= new JProgressBar();
78 barChild
.setStringPainted(true);
81 newBars
.put(pgChild
, barChild
);
83 barChild
.setMinimum(pgChild
.getMin());
84 barChild
.setMaximum(pgChild
.getMax());
85 barChild
.setValue(pgChild
.getProgress());
86 barChild
.setString(pgChild
.getName());
89 if (ProgressBar
.this.pg
== null) {
96 if (ProgressBar
.this.pg
!= null) {
98 pg
.removeProgressListener(l
);
99 for (ActionListener listener
: actionListeners
) {
100 listener
.actionPerformed(new ActionEvent(
119 public void addActionListener(ActionListener l
) {
120 actionListeners
.add(l
);
123 public void clearActionListeners() {
124 actionListeners
.clear();
127 public void addUpdateListener(ActionListener l
) {
128 updateListeners
.add(l
);
131 public void clearUpdateListeners() {
132 updateListeners
.clear();
135 public int getProgress() {
140 return pg
.getProgress();
144 private List
<Progress
> getChildrenAsOrderedList(Progress pg
) {
145 List
<Progress
> children
= new ArrayList
<Progress
>();
147 synchronized (lock
) {
148 for (Progress child
: pg
.getChildren()) {
149 if (child
.getName() != null && !child
.getName().isEmpty()) {
152 children
.addAll(getChildrenAsOrderedList(child
));
159 private void update() {
160 synchronized (lock
) {
165 setLayout(new GridLayout(bars
.size(), 1));
166 add(bars
.get(pg
), 0);
167 for (Progress child
: getChildrenAsOrderedList(pg
)) {
168 JProgressBar jbar
= bars
.get(child
);
179 for (ActionListener listener
: updateListeners
) {
180 listener
.actionPerformed(new ActionEvent(this, 0, "update"));