Bundle: fix memory leak at init/reset
[fanfix.git] / 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 private Object lock = new Object();
32
33 public ProgressBar() {
34 bars = new HashMap<Progress, JProgressBar>();
35 actionListeners = new ArrayList<ActionListener>();
36 updateListeners = new ArrayList<ActionListener>();
37 }
38
39 public ProgressBar(Progress pg) {
40 this();
41 setProgress(pg);
42 }
43
44 public void setProgress(final Progress pg) {
45 this.pg = pg;
46
47 SwingUtilities.invokeLater(new Runnable() {
48 @Override
49 public void run() {
50 if (pg != null) {
51 final JProgressBar bar = new JProgressBar();
52 bar.setStringPainted(true);
53
54 bars.clear();
55 bars.put(pg, bar);
56
57 bar.setMinimum(pg.getMin());
58 bar.setMaximum(pg.getMax());
59 bar.setValue(pg.getProgress());
60 bar.setString(pg.getName());
61
62 pg.addProgressListener(new Progress.ProgressListener() {
63 @Override
64 public void progress(Progress progress, String name) {
65 final Progress.ProgressListener l = this;
66 SwingUtilities.invokeLater(new Runnable() {
67 @Override
68 public void run() {
69 Map<Progress, JProgressBar> newBars = new HashMap<Progress, JProgressBar>();
70 newBars.put(pg, bar);
71
72 bar.setMinimum(pg.getMin());
73 bar.setMaximum(pg.getMax());
74 bar.setValue(pg.getProgress());
75 bar.setString(pg.getName());
76
77 synchronized (lock) {
78 for (Progress pgChild : getChildrenAsOrderedList(pg)) {
79 JProgressBar barChild = bars
80 .get(pgChild);
81 if (barChild == null) {
82 barChild = new JProgressBar();
83 barChild.setStringPainted(true);
84 }
85
86 newBars.put(pgChild, barChild);
87
88 barChild.setMinimum(pgChild.getMin());
89 barChild.setMaximum(pgChild.getMax());
90 barChild.setValue(pgChild.getProgress());
91 barChild.setString(pgChild.getName());
92 }
93
94 if (ProgressBar.this.pg == null) {
95 bars.clear();
96 } else {
97 bars = newBars;
98 }
99 }
100
101 if (ProgressBar.this.pg != null) {
102 if (pg.isDone()) {
103 pg.removeProgressListener(l);
104 for (ActionListener listener : actionListeners) {
105 listener.actionPerformed(new ActionEvent(
106 ProgressBar.this, 0,
107 "done"));
108 }
109 }
110
111 update();
112 }
113 }
114 });
115 }
116 });
117 }
118
119 update();
120 }
121 });
122 }
123
124 public void addActionListener(ActionListener l) {
125 actionListeners.add(l);
126 }
127
128 public void clearActionListeners() {
129 actionListeners.clear();
130 }
131
132 public void addUpdateListener(ActionListener l) {
133 updateListeners.add(l);
134 }
135
136 public void clearUpdateListeners() {
137 updateListeners.clear();
138 }
139
140 public int getProgress() {
141 if (pg == null) {
142 return 0;
143 }
144
145 return pg.getProgress();
146 }
147
148 // only named ones
149 private List<Progress> getChildrenAsOrderedList(Progress pg) {
150 List<Progress> children = new ArrayList<Progress>();
151
152 synchronized (lock) {
153 for (Progress child : pg.getChildren()) {
154 if (child.getName() != null && !child.getName().isEmpty()) {
155 children.add(child);
156 }
157 children.addAll(getChildrenAsOrderedList(child));
158 }
159 }
160
161 return children;
162 }
163
164 private void update() {
165 synchronized (lock) {
166 invalidate();
167 removeAll();
168
169 if (pg != null) {
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);
174 if (jbar != null) {
175 add(jbar);
176 }
177 }
178 }
179
180 validate();
181 repaint();
182 }
183
184 for (ActionListener listener : updateListeners) {
185 listener.actionPerformed(new ActionEvent(this, 0, "update"));
186 }
187 }
188 }