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