Cannot compile on 1.6 because var names not clear
[nikiroo-utils.git] / src / be / nikiroo / utils / ui / ProgressBar.java
1 package be.nikiroo.utils.ui;
2
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;
8 import java.util.List;
9 import java.util.Map;
10
11 import javax.swing.JPanel;
12 import javax.swing.JProgressBar;
13 import javax.swing.SwingUtilities;
14
15 import be.nikiroo.utils.Progress;
16
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 */
24 public class ProgressBar extends JPanel {
25 private static final long serialVersionUID = 1L;
26
27 private Map<Progress, JProgressBar> bars;
28 private List<ActionListener> actionListeners;
29 private List<ActionListener> updateListeners;
30 private Progress pg;
31
32 public ProgressBar() {
33 bars = new HashMap<Progress, JProgressBar>();
34 actionListeners = new ArrayList<ActionListener>();
35 updateListeners = new ArrayList<ActionListener>();
36 }
37
38 public void setProgress(final Progress pg) {
39 this.pg = pg;
40
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
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);
74 }
75
76 newBars.put(pgChild, barChild);
77
78 barChild.setMinimum(pgChild.getMin());
79 barChild.setMaximum(pgChild.getMax());
80 barChild.setValue(pgChild.getProgress());
81 barChild.setString(pgChild.getName());
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 }
100 }
101 });
102 }
103 });
104 }
105
106 update();
107 }
108 });
109 }
110
111 public void addActionListener(ActionListener l) {
112 actionListeners.add(l);
113 }
114
115 public void clearActionListeners() {
116 actionListeners.clear();
117 }
118
119 public void addUpdateListener(ActionListener l) {
120 updateListeners.add(l);
121 }
122
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
136 private List<Progress> getChildrenAsOrderedList(Progress pg) {
137 List<Progress> children = new ArrayList<Progress>();
138 for (Progress child : pg.getChildren()) {
139 if (child.getName() != null && !child.getName().isEmpty()) {
140 children.add(child);
141 }
142 children.addAll(getChildrenAsOrderedList(child));
143 }
144
145 return children;
146 }
147
148 private void update() {
149 invalidate();
150 removeAll();
151
152 if (pg != null) {
153 setLayout(new GridLayout(bars.size(), 1));
154 add(bars.get(pg), 0);
155 for (Progress child : getChildrenAsOrderedList(pg)) {
156 JProgressBar jbar = bars.get(child);
157 if (jbar != null) {
158 add(jbar);
159 }
160 }
161 }
162
163 validate();
164 repaint();
165
166 for (ActionListener listener : updateListeners) {
167 listener.actionPerformed(new ActionEvent(this, 0, "update"));
168 }
169 }
170 }