Cannot compile on 1.6 because var names not clear
[nikiroo-utils.git] / src / be / nikiroo / utils / ui / ProgressBar.java
CommitLineData
b3aad1f9
NR
1package be.nikiroo.utils.ui;
2
88b36f83 3import java.awt.GridLayout;
b3aad1f9
NR
4import java.awt.event.ActionEvent;
5import java.awt.event.ActionListener;
6import java.util.ArrayList;
88b36f83 7import java.util.HashMap;
b3aad1f9 8import java.util.List;
88b36f83 9import java.util.Map;
b3aad1f9 10
b3aad1f9
NR
11import javax.swing.JPanel;
12import javax.swing.JProgressBar;
13import javax.swing.SwingUtilities;
14
15import be.nikiroo.utils.Progress;
16
2998b78a
NR
17/**
18 * A graphical control to show the progress of a {@link Progress}.
19 * <p>
20 * This control is <b>NOT</b> thread-safe.
21 *
22 * @author niki
23 */
b3aad1f9
NR
24public class ProgressBar extends JPanel {
25 private static final long serialVersionUID = 1L;
26
88b36f83 27 private Map<Progress, JProgressBar> bars;
2998b78a
NR
28 private List<ActionListener> actionListeners;
29 private List<ActionListener> updateListeners;
88b36f83 30 private Progress pg;
b3aad1f9
NR
31
32 public ProgressBar() {
88b36f83 33 bars = new HashMap<Progress, JProgressBar>();
2998b78a
NR
34 actionListeners = new ArrayList<ActionListener>();
35 updateListeners = new ArrayList<ActionListener>();
b3aad1f9
NR
36 }
37
88b36f83
NR
38 public void setProgress(final Progress pg) {
39 this.pg = pg;
40
2998b78a
NR
41 SwingUtilities.invokeLater(new Runnable() {
42 public void run() {
43 if (pg != null) {
44 final JProgressBar bar = new JProgressBar();
45 bar.setStringPainted(true);
46
47 bars.clear();
48 bars.put(pg, bar);
49
50 bar.setMinimum(pg.getMin());
51 bar.setMaximum(pg.getMax());
52 bar.setValue(pg.getProgress());
53 bar.setString(pg.getName());
54
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() {
59 public void run() {
60 Map<Progress, JProgressBar> newBars = new HashMap<Progress, JProgressBar>();
61 newBars.put(pg, bar);
62
63 bar.setMinimum(pg.getMin());
64 bar.setMaximum(pg.getMax());
65 bar.setValue(pg.getProgress());
66 bar.setString(pg.getName());
67
9acc9360
NR
68 for (Progress pgChild : getChildrenAsOrderedList(pg)) {
69 JProgressBar barChild = bars
70 .get(pgChild);
71 if (barChild == null) {
72 barChild = new JProgressBar();
73 barChild.setStringPainted(true);
2998b78a
NR
74 }
75
9acc9360 76 newBars.put(pgChild, barChild);
2998b78a 77
9acc9360
NR
78 barChild.setMinimum(pgChild.getMin());
79 barChild.setMaximum(pgChild.getMax());
80 barChild.setValue(pgChild.getProgress());
81 barChild.setString(pgChild.getName());
2998b78a
NR
82 }
83
84 if (ProgressBar.this.pg == null) {
85 bars.clear();
86 } else {
87 bars = newBars;
88
89 if (pg.isDone()) {
90 pg.removeProgressListener(l);
91 for (ActionListener listener : actionListeners) {
92 listener.actionPerformed(new ActionEvent(
93 ProgressBar.this, 0,
94 "done"));
95 }
96 }
97
98 update();
99 }
b3aad1f9 100 }
2998b78a 101 });
b3aad1f9
NR
102 }
103 });
104 }
b3aad1f9 105
2998b78a
NR
106 update();
107 }
108 });
b3aad1f9
NR
109 }
110
2998b78a
NR
111 public void addActionListener(ActionListener l) {
112 actionListeners.add(l);
b3aad1f9
NR
113 }
114
115 public void clearActionListeners() {
2998b78a
NR
116 actionListeners.clear();
117 }
118
119 public void addUpdateListener(ActionListener l) {
120 updateListeners.add(l);
b3aad1f9
NR
121 }
122
2998b78a
NR
123 public void clearUpdateListeners() {
124 updateListeners.clear();
125 }
126
127 public int getProgress() {
128 if (pg == null) {
129 return 0;
130 }
131
132 return pg.getProgress();
133 }
134
135 // only named ones
88b36f83
NR
136 private List<Progress> getChildrenAsOrderedList(Progress pg) {
137 List<Progress> children = new ArrayList<Progress>();
138 for (Progress child : pg.getChildren()) {
2998b78a
NR
139 if (child.getName() != null && !child.getName().isEmpty()) {
140 children.add(child);
141 }
88b36f83
NR
142 children.addAll(getChildrenAsOrderedList(child));
143 }
144
145 return children;
146 }
147
2998b78a
NR
148 private void update() {
149 invalidate();
b3aad1f9
NR
150 removeAll();
151
2998b78a 152 if (pg != null) {
88b36f83
NR
153 setLayout(new GridLayout(bars.size(), 1));
154 add(bars.get(pg), 0);
155 for (Progress child : getChildrenAsOrderedList(pg)) {
2998b78a
NR
156 JProgressBar jbar = bars.get(child);
157 if (jbar != null) {
158 add(jbar);
159 }
88b36f83 160 }
b3aad1f9
NR
161 }
162
2998b78a 163 validate();
b3aad1f9 164 repaint();
2998b78a
NR
165
166 for (ActionListener listener : updateListeners) {
167 listener.actionPerformed(new ActionEvent(this, 0, "update"));
168 }
b3aad1f9
NR
169 }
170}