1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
|
21 |
|
package jalview.gui; |
22 |
|
|
23 |
|
import jalview.util.MessageManager; |
24 |
|
|
25 |
|
import java.awt.BorderLayout; |
26 |
|
import java.awt.Component; |
27 |
|
import java.awt.Container; |
28 |
|
import java.awt.GridLayout; |
29 |
|
import java.awt.event.ActionEvent; |
30 |
|
import java.awt.event.ActionListener; |
31 |
|
import java.util.Hashtable; |
32 |
|
import java.util.Map; |
33 |
|
|
34 |
|
import javax.swing.JButton; |
35 |
|
import javax.swing.JLabel; |
36 |
|
import javax.swing.JPanel; |
37 |
|
import javax.swing.JProgressBar; |
38 |
|
import javax.swing.SwingUtilities; |
39 |
|
|
40 |
|
|
41 |
|
|
42 |
|
|
43 |
|
|
44 |
|
|
45 |
|
|
|
|
| 46.3% |
Uncovered Elements: 66 (123) |
Complexity: 34 |
Complexity Density: 0.44 |
|
46 |
|
public class ProgressBar implements IProgressIndicator |
47 |
|
{ |
48 |
|
|
49 |
|
|
50 |
|
|
51 |
|
Map<Long, JPanel> progressBars; |
52 |
|
|
53 |
|
Map<Long, String> progressBarMessages; |
54 |
|
|
55 |
|
|
56 |
|
|
57 |
|
|
58 |
|
Map<Long, IProgressIndicatorHandler> progressBarHandlers; |
59 |
|
|
60 |
|
|
61 |
|
|
62 |
|
|
63 |
|
private JPanel statusPanel; |
64 |
|
|
65 |
|
|
66 |
|
|
67 |
|
|
68 |
|
|
69 |
|
private JLabel statusBar; |
70 |
|
|
71 |
|
|
72 |
|
|
73 |
|
|
74 |
|
|
75 |
|
|
76 |
|
@param |
77 |
|
|
78 |
|
@param |
79 |
|
|
80 |
|
|
81 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (13) |
Complexity: 3 |
Complexity Density: 0.33 |
|
82 |
223 |
public ProgressBar(JPanel container, JLabel statusBar)... |
83 |
|
{ |
84 |
223 |
if (container == null) |
85 |
|
{ |
86 |
1 |
throw new NullPointerException(); |
87 |
|
} |
88 |
222 |
if (!GridLayout.class |
89 |
|
.isAssignableFrom(container.getLayout().getClass())) |
90 |
|
{ |
91 |
1 |
throw new IllegalArgumentException("Container must have GridLayout"); |
92 |
|
} |
93 |
221 |
this.statusPanel = container; |
94 |
221 |
this.statusBar = statusBar; |
95 |
221 |
this.progressBars = new Hashtable<>(); |
96 |
221 |
this.progressBarHandlers = new Hashtable<>(); |
97 |
221 |
this.progressBarMessages = new Hashtable<>(); |
98 |
|
|
99 |
|
} |
100 |
|
|
101 |
|
|
102 |
|
|
103 |
|
|
104 |
|
@return |
105 |
|
|
|
|
| 0% |
Uncovered Elements: 5 (5) |
Complexity: 3 |
Complexity Density: 1 |
|
106 |
0 |
@Override... |
107 |
|
public boolean operationInProgress() |
108 |
|
{ |
109 |
0 |
if (progressBars != null && progressBars.size() > 0) |
110 |
|
{ |
111 |
0 |
return true; |
112 |
|
} |
113 |
0 |
return false; |
114 |
|
} |
115 |
|
|
116 |
|
|
117 |
|
|
118 |
|
|
119 |
|
|
120 |
|
|
121 |
|
|
122 |
|
|
123 |
|
|
124 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
125 |
63 |
@Override... |
126 |
|
public void setProgressBar(final String message, final long id) |
127 |
|
{ |
128 |
63 |
SwingUtilities.invokeLater(new Runnable() |
129 |
|
{ |
|
|
| 85.2% |
Uncovered Elements: 4 (27) |
Complexity: 7 |
Complexity Density: 0.41 |
|
130 |
54 |
@Override... |
131 |
|
public void run() |
132 |
|
{ |
133 |
54 |
JPanel progressPanel = progressBars.get(id); |
134 |
54 |
if (progressPanel != null) |
135 |
|
{ |
136 |
|
|
137 |
|
|
138 |
|
|
139 |
27 |
progressBars.remove(id); |
140 |
27 |
if (message != null && statusBar != null) |
141 |
|
{ |
142 |
13 |
statusBar.setText(message); |
143 |
|
} |
144 |
27 |
if (progressBarHandlers.containsKey(id)) |
145 |
|
{ |
146 |
0 |
progressBarHandlers.remove(id); |
147 |
|
} |
148 |
27 |
removeRow(progressPanel); |
149 |
|
} |
150 |
|
else |
151 |
|
{ |
152 |
|
|
153 |
|
|
154 |
|
|
155 |
27 |
progressPanel = new JPanel(new BorderLayout(10, 5)); |
156 |
|
|
157 |
27 |
JProgressBar progressBar = new JProgressBar(); |
158 |
27 |
progressBar.setIndeterminate(true); |
159 |
|
|
160 |
27 |
progressPanel.add(new JLabel(message==null ? "" : message), BorderLayout.WEST); |
161 |
27 |
progressPanel.add(progressBar, BorderLayout.CENTER); |
162 |
|
|
163 |
27 |
addRow(progressPanel); |
164 |
|
|
165 |
27 |
progressBars.put(id, progressPanel); |
166 |
27 |
progressBarMessages.put(id, message == null ? "" : message); |
167 |
|
} |
168 |
|
|
169 |
54 |
refreshLayout(); |
170 |
|
} |
171 |
|
}); |
172 |
|
|
173 |
|
} |
174 |
|
|
175 |
|
|
176 |
|
|
177 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
178 |
54 |
protected void refreshLayout()... |
179 |
|
{ |
180 |
|
|
181 |
|
|
182 |
|
|
183 |
54 |
Component root = SwingUtilities.getRoot(statusPanel); |
184 |
54 |
if (root != null) |
185 |
|
{ |
186 |
44 |
root.validate(); |
187 |
|
} |
188 |
|
} |
189 |
|
|
190 |
|
|
191 |
|
|
192 |
|
|
193 |
|
@param |
194 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
|
195 |
27 |
protected void removeRow(JPanel progressPanel)... |
196 |
|
{ |
197 |
27 |
synchronized (statusPanel) |
198 |
|
{ |
199 |
27 |
statusPanel.remove(progressPanel); |
200 |
27 |
GridLayout layout = (GridLayout) statusPanel.getLayout(); |
201 |
27 |
layout.setRows(layout.getRows() - 1); |
202 |
27 |
statusPanel.remove(progressPanel); |
203 |
|
} |
204 |
|
} |
205 |
|
|
206 |
|
|
207 |
|
|
208 |
|
|
209 |
|
@param |
210 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
211 |
27 |
protected void addRow(JPanel progressPanel)... |
212 |
|
{ |
213 |
27 |
synchronized (statusPanel) |
214 |
|
{ |
215 |
27 |
GridLayout layout = (GridLayout) statusPanel.getLayout(); |
216 |
27 |
layout.setRows(layout.getRows() + 1); |
217 |
27 |
statusPanel.add(progressPanel); |
218 |
|
} |
219 |
|
} |
220 |
|
|
221 |
|
|
222 |
|
|
223 |
|
|
224 |
|
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
225 |
0 |
@Override... |
226 |
|
public void registerHandler(final long id, |
227 |
|
final IProgressIndicatorHandler handler) |
228 |
|
{ |
229 |
0 |
final IProgressIndicator us = this; |
230 |
|
|
231 |
0 |
SwingUtilities.invokeLater(new Runnable() |
232 |
|
{ |
|
|
| 0% |
Uncovered Elements: 11 (11) |
Complexity: 2 |
Complexity Density: 0.22 |
|
233 |
0 |
@Override... |
234 |
|
public void run() |
235 |
|
{ |
236 |
0 |
final JPanel progressPanel = progressBars.get(id); |
237 |
0 |
if (progressPanel == null) |
238 |
|
{ |
239 |
0 |
jalview.bin.Console.errPrintln( |
240 |
|
"call setProgressBar before registering the progress bar's handler."); |
241 |
0 |
return; |
242 |
|
} |
243 |
0 |
progressBarHandlers.put(id, handler); |
244 |
0 |
JButton cancel = new JButton( |
245 |
|
MessageManager.getString("action.cancel")); |
246 |
0 |
cancel.addActionListener(new ActionListener() |
247 |
|
{ |
248 |
|
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
249 |
0 |
@Override... |
250 |
|
public void actionPerformed(ActionEvent e) |
251 |
|
{ |
252 |
0 |
handler.cancelActivity(id); |
253 |
0 |
us.setProgressBar(MessageManager |
254 |
|
.formatMessage("label.cancelled_params", new Object[] |
255 |
|
{ ((JLabel) progressPanel.getComponent(0)).getText() }), |
256 |
|
id); |
257 |
|
} |
258 |
|
}); |
259 |
0 |
progressPanel.add(cancel, BorderLayout.EAST); |
260 |
|
|
261 |
0 |
updateCancelHandler(progressPanel, handler); |
262 |
|
} |
263 |
|
}); |
264 |
|
} |
|
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
265 |
0 |
protected void updateCancelHandler(Container progressPanel, IProgressIndicatorHandler handler)... |
266 |
|
{ |
267 |
0 |
for (Component j:progressPanel.getComponents()) |
268 |
|
{ |
269 |
0 |
if (j instanceof JButton) |
270 |
|
{ |
271 |
0 |
j.setEnabled(handler.canCancel()); |
272 |
|
} |
273 |
|
} |
274 |
0 |
refreshLayout(); |
275 |
|
} |
276 |
|
|
277 |
|
|
278 |
|
|
279 |
|
|
|
|
| 0% |
Uncovered Elements: 11 (11) |
Complexity: 4 |
Complexity Density: 0.57 |
|
280 |
0 |
@Override... |
281 |
|
public JProgressBar getProgressBar(long id) |
282 |
|
{ |
283 |
0 |
Container progBar = progressBars.get(id); |
284 |
0 |
if (progBar == null || progBar.getComponentCount() == 0) |
285 |
|
{ |
286 |
0 |
return null; |
287 |
|
} |
288 |
0 |
for (Component component : progBar.getComponents()) |
289 |
|
{ |
290 |
0 |
if (component.getClass().equals(JProgressBar.class)) |
291 |
|
{ |
292 |
0 |
return (JProgressBar) component; |
293 |
|
} |
294 |
|
} |
295 |
0 |
return null; |
296 |
|
} |
297 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
298 |
0 |
@Override... |
299 |
|
public String getMessage(long id) |
300 |
|
{ |
301 |
0 |
return progressBarMessages.get(id); |
302 |
|
} |
303 |
|
|
304 |
|
|
305 |
|
|
306 |
|
@param |
307 |
|
@param |
308 |
|
|
|
|
| 0% |
Uncovered Elements: 16 (16) |
Complexity: 5 |
Complexity Density: 0.5 |
|
309 |
0 |
@Override... |
310 |
|
public void setProgressBarMessage(long id,String message) |
311 |
|
{ |
312 |
0 |
Container progBar = progressBars.get(id); |
313 |
0 |
if (progBar == null || progBar.getComponentCount() == 0) |
314 |
|
{ |
315 |
0 |
return; |
316 |
|
} |
317 |
0 |
for (Component component : progBar.getComponents()) |
318 |
|
{ |
319 |
0 |
if (component.getClass().equals(JLabel.class)) |
320 |
|
{ |
321 |
0 |
((JLabel) component).setText(message); |
322 |
0 |
IProgressIndicatorHandler handler = progressBarHandlers.get(id); |
323 |
0 |
if (handler!=null) { |
324 |
|
|
325 |
0 |
updateCancelHandler(progBar, handler); |
326 |
|
} else { |
327 |
0 |
refreshLayout(); |
328 |
|
} |
329 |
|
} |
330 |
|
} |
331 |
|
} |
332 |
|
|
333 |
|
} |