Commit | Line | Data |
---|---|---|
b3aad1f9 NR |
1 | package be.nikiroo.utils.ui; |
2 | ||
88b36f83 | 3 | import java.awt.GridLayout; |
b3aad1f9 NR |
4 | import java.awt.event.ActionEvent; |
5 | import java.awt.event.ActionListener; | |
6 | import java.util.ArrayList; | |
88b36f83 | 7 | import java.util.HashMap; |
b3aad1f9 | 8 | import java.util.List; |
88b36f83 | 9 | import java.util.Map; |
b3aad1f9 | 10 | |
b3aad1f9 NR |
11 | import javax.swing.JPanel; |
12 | import javax.swing.JProgressBar; | |
13 | import javax.swing.SwingUtilities; | |
14 | ||
15 | import 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 |
24 | public 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 | 41 | SwingUtilities.invokeLater(new Runnable() { |
cd0c27d2 | 42 | @Override |
2998b78a NR |
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() { | |
cd0c27d2 | 57 | @Override |
2998b78a NR |
58 | public void progress(Progress progress, String name) { |
59 | final Progress.ProgressListener l = this; | |
60 | SwingUtilities.invokeLater(new Runnable() { | |
cd0c27d2 | 61 | @Override |
2998b78a NR |
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 | ||
9acc9360 NR |
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); | |
2998b78a NR |
77 | } |
78 | ||
9acc9360 | 79 | newBars.put(pgChild, barChild); |
2998b78a | 80 | |
9acc9360 NR |
81 | barChild.setMinimum(pgChild.getMin()); |
82 | barChild.setMaximum(pgChild.getMax()); | |
83 | barChild.setValue(pgChild.getProgress()); | |
84 | barChild.setString(pgChild.getName()); | |
2998b78a NR |
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 | } | |
b3aad1f9 | 103 | } |
2998b78a | 104 | }); |
b3aad1f9 NR |
105 | } |
106 | }); | |
107 | } | |
b3aad1f9 | 108 | |
2998b78a NR |
109 | update(); |
110 | } | |
111 | }); | |
b3aad1f9 NR |
112 | } |
113 | ||
2998b78a NR |
114 | public void addActionListener(ActionListener l) { |
115 | actionListeners.add(l); | |
b3aad1f9 NR |
116 | } |
117 | ||
118 | public void clearActionListeners() { | |
2998b78a NR |
119 | actionListeners.clear(); |
120 | } | |
121 | ||
122 | public void addUpdateListener(ActionListener l) { | |
123 | updateListeners.add(l); | |
b3aad1f9 NR |
124 | } |
125 | ||
2998b78a NR |
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 | |
88b36f83 NR |
139 | private List<Progress> getChildrenAsOrderedList(Progress pg) { |
140 | List<Progress> children = new ArrayList<Progress>(); | |
141 | for (Progress child : pg.getChildren()) { | |
2998b78a NR |
142 | if (child.getName() != null && !child.getName().isEmpty()) { |
143 | children.add(child); | |
144 | } | |
88b36f83 NR |
145 | children.addAll(getChildrenAsOrderedList(child)); |
146 | } | |
147 | ||
148 | return children; | |
149 | } | |
150 | ||
2998b78a NR |
151 | private void update() { |
152 | invalidate(); | |
b3aad1f9 NR |
153 | removeAll(); |
154 | ||
2998b78a | 155 | if (pg != null) { |
88b36f83 NR |
156 | setLayout(new GridLayout(bars.size(), 1)); |
157 | add(bars.get(pg), 0); | |
158 | for (Progress child : getChildrenAsOrderedList(pg)) { | |
2998b78a NR |
159 | JProgressBar jbar = bars.get(child); |
160 | if (jbar != null) { | |
161 | add(jbar); | |
162 | } | |
88b36f83 | 163 | } |
b3aad1f9 NR |
164 | } |
165 | ||
2998b78a | 166 | validate(); |
b3aad1f9 | 167 | repaint(); |
2998b78a NR |
168 | |
169 | for (ActionListener listener : updateListeners) { | |
170 | listener.actionPerformed(new ActionEvent(this, 0, "update")); | |
171 | } | |
b3aad1f9 NR |
172 | } |
173 | } |