| 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 |
|
|
| |
|
| 0% |
Uncovered Elements: 145 (145) |
Complexity: 39 |
Complexity Density: 0.43 |
|
| 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 |
|
|
| |
|
| 0% |
Uncovered Elements: 13 (13) |
Complexity: 3 |
Complexity Density: 0.33 |
|
| 82 |
0 |
public ProgressBar(JPanel container, JLabel statusBar)... |
| 83 |
|
{ |
| 84 |
0 |
if (container == null) |
| 85 |
|
{ |
| 86 |
0 |
throw new NullPointerException(); |
| 87 |
|
} |
| 88 |
0 |
if (!GridLayout.class |
| 89 |
|
.isAssignableFrom(container.getLayout().getClass())) |
| 90 |
|
{ |
| 91 |
0 |
throw new IllegalArgumentException("Container must have GridLayout"); |
| 92 |
|
} |
| 93 |
0 |
this.statusPanel = container; |
| 94 |
0 |
this.statusBar = statusBar; |
| 95 |
0 |
this.progressBars = new Hashtable<>(); |
| 96 |
0 |
this.progressBarHandlers = new Hashtable<>(); |
| 97 |
0 |
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 |
|
|
| |
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 125 |
0 |
@Override... |
| 126 |
|
public void setProgressBar(final String message, final long id) |
| 127 |
|
{ |
| 128 |
0 |
SwingUtilities.invokeLater(new Runnable() |
| 129 |
|
{ |
| |
|
| 0% |
Uncovered Elements: 9 (9) |
Complexity: 4 |
Complexity Density: 0.8 |
|
| 130 |
0 |
@Override... |
| 131 |
|
public void run() |
| 132 |
|
{ |
| 133 |
0 |
if (progressBars.containsKey(id)) |
| 134 |
|
{ |
| 135 |
|
|
| 136 |
|
|
| 137 |
|
|
| 138 |
0 |
removeProgressBar(id); |
| 139 |
0 |
if (message != null && statusBar != null) |
| 140 |
|
{ |
| 141 |
0 |
statusBar.setText(message); |
| 142 |
|
} |
| 143 |
|
} |
| 144 |
|
else |
| 145 |
|
{ |
| 146 |
|
|
| 147 |
|
|
| 148 |
|
|
| 149 |
0 |
addProgressBar(id, message); |
| 150 |
|
} |
| 151 |
|
} |
| 152 |
|
}); |
| 153 |
|
|
| 154 |
|
} |
| 155 |
|
|
| 156 |
|
|
| 157 |
|
|
| 158 |
|
|
| 159 |
|
|
| 160 |
|
@param |
| 161 |
|
|
| 162 |
|
@param |
| 163 |
|
|
| 164 |
|
|
| |
|
| 0% |
Uncovered Elements: 23 (23) |
Complexity: 5 |
Complexity Density: 0.33 |
|
| 165 |
0 |
@Override... |
| 166 |
|
public void addProgressBar(final long id, final String message) |
| 167 |
|
{ |
| 168 |
0 |
if (progressBars.containsKey(id)) |
| 169 |
0 |
return; |
| 170 |
0 |
JPanel progressPanel = new JPanel(new BorderLayout(10, 5)); |
| 171 |
0 |
progressBars.put(id, progressPanel); |
| 172 |
0 |
progressBarMessages.put(id, message == null ? "" : message); |
| 173 |
0 |
Runnable r = () -> { |
| 174 |
0 |
JProgressBar progressBar = new JProgressBar(); |
| 175 |
0 |
progressBar.setIndeterminate(true); |
| 176 |
0 |
progressPanel.add(new JLabel(message==null ? "" : message), BorderLayout.WEST); |
| 177 |
0 |
progressPanel.add(progressBar, BorderLayout.CENTER); |
| 178 |
0 |
addRow(progressPanel); |
| 179 |
0 |
refreshLayout(); |
| 180 |
|
}; |
| 181 |
0 |
if (SwingUtilities.isEventDispatchThread()) |
| 182 |
0 |
r.run(); |
| 183 |
|
else |
| 184 |
0 |
SwingUtilities.invokeLater(r); |
| 185 |
|
} |
| 186 |
|
|
| 187 |
|
|
| 188 |
|
|
| 189 |
|
|
| 190 |
|
|
| 191 |
|
@param |
| 192 |
|
|
| 193 |
|
|
| |
|
| 0% |
Uncovered Elements: 15 (15) |
Complexity: 3 |
Complexity Density: 0.27 |
|
| 194 |
0 |
@Override... |
| 195 |
|
public void removeProgressBar(final long id) |
| 196 |
|
{ |
| 197 |
0 |
JPanel progressPanel = progressBars.remove(id); |
| 198 |
0 |
progressBarMessages.remove(id); |
| 199 |
0 |
if (progressPanel == null) |
| 200 |
0 |
return; |
| 201 |
0 |
progressBarHandlers.remove(id); |
| 202 |
0 |
Runnable r = () -> { |
| 203 |
0 |
removeRow(progressPanel); |
| 204 |
0 |
refreshLayout(); |
| 205 |
|
}; |
| 206 |
0 |
if (SwingUtilities.isEventDispatchThread()) |
| 207 |
0 |
r.run(); |
| 208 |
|
else |
| 209 |
0 |
SwingUtilities.invokeLater(r); |
| 210 |
|
} |
| 211 |
|
|
| 212 |
|
|
| 213 |
|
|
| 214 |
|
|
| |
|
| 0% |
Uncovered Elements: 5 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
| 215 |
0 |
protected void refreshLayout()... |
| 216 |
|
{ |
| 217 |
|
|
| 218 |
|
|
| 219 |
|
|
| 220 |
0 |
Component root = SwingUtilities.getRoot(statusPanel); |
| 221 |
0 |
if (root != null) |
| 222 |
|
{ |
| 223 |
0 |
root.validate(); |
| 224 |
|
} |
| 225 |
|
} |
| 226 |
|
|
| 227 |
|
|
| 228 |
|
|
| 229 |
|
|
| 230 |
|
@param |
| 231 |
|
|
| |
|
| 0% |
Uncovered Elements: 5 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
|
| 232 |
0 |
protected void removeRow(JPanel progressPanel)... |
| 233 |
|
{ |
| 234 |
0 |
synchronized (statusPanel) |
| 235 |
|
{ |
| 236 |
0 |
statusPanel.remove(progressPanel); |
| 237 |
0 |
GridLayout layout = (GridLayout) statusPanel.getLayout(); |
| 238 |
0 |
layout.setRows(layout.getRows() - 1); |
| 239 |
0 |
statusPanel.remove(progressPanel); |
| 240 |
|
} |
| 241 |
|
} |
| 242 |
|
|
| 243 |
|
|
| 244 |
|
|
| 245 |
|
|
| 246 |
|
@param |
| 247 |
|
|
| |
|
| 0% |
Uncovered Elements: 4 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
| 248 |
0 |
protected void addRow(JPanel progressPanel)... |
| 249 |
|
{ |
| 250 |
0 |
synchronized (statusPanel) |
| 251 |
|
{ |
| 252 |
0 |
GridLayout layout = (GridLayout) statusPanel.getLayout(); |
| 253 |
0 |
layout.setRows(layout.getRows() + 1); |
| 254 |
0 |
statusPanel.add(progressPanel); |
| 255 |
|
} |
| 256 |
|
} |
| 257 |
|
|
| 258 |
|
|
| 259 |
|
|
| 260 |
|
|
| 261 |
|
|
| |
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 262 |
0 |
@Override... |
| 263 |
|
public void registerHandler(final long id, |
| 264 |
|
final IProgressIndicatorHandler handler) |
| 265 |
|
{ |
| 266 |
0 |
final IProgressIndicator us = this; |
| 267 |
|
|
| 268 |
0 |
SwingUtilities.invokeLater(new Runnable() |
| 269 |
|
{ |
| |
|
| 0% |
Uncovered Elements: 11 (11) |
Complexity: 2 |
Complexity Density: 0.22 |
|
| 270 |
0 |
@Override... |
| 271 |
|
public void run() |
| 272 |
|
{ |
| 273 |
0 |
final JPanel progressPanel = progressBars.get(id); |
| 274 |
0 |
if (progressPanel == null) |
| 275 |
|
{ |
| 276 |
0 |
jalview.bin.Console.errPrintln( |
| 277 |
|
"call setProgressBar before registering the progress bar's handler."); |
| 278 |
0 |
return; |
| 279 |
|
} |
| 280 |
0 |
progressBarHandlers.put(id, handler); |
| 281 |
0 |
JButton cancel = new JButton( |
| 282 |
|
MessageManager.getString("action.cancel")); |
| 283 |
0 |
cancel.addActionListener(new ActionListener() |
| 284 |
|
{ |
| 285 |
|
|
| |
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 286 |
0 |
@Override... |
| 287 |
|
public void actionPerformed(ActionEvent e) |
| 288 |
|
{ |
| 289 |
0 |
handler.cancelActivity(id); |
| 290 |
0 |
us.setProgressBar(MessageManager |
| 291 |
|
.formatMessage("label.cancelled_params", new Object[] |
| 292 |
|
{ ((JLabel) progressPanel.getComponent(0)).getText() }), |
| 293 |
|
id); |
| 294 |
|
} |
| 295 |
|
}); |
| 296 |
0 |
progressPanel.add(cancel, BorderLayout.EAST); |
| 297 |
|
|
| 298 |
0 |
updateCancelHandler(progressPanel, handler); |
| 299 |
|
} |
| 300 |
|
}); |
| 301 |
|
} |
| |
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
| 302 |
0 |
protected void updateCancelHandler(Container progressPanel, IProgressIndicatorHandler handler)... |
| 303 |
|
{ |
| 304 |
0 |
for (Component j:progressPanel.getComponents()) |
| 305 |
|
{ |
| 306 |
0 |
if (j instanceof JButton) |
| 307 |
|
{ |
| 308 |
0 |
j.setEnabled(handler.canCancel()); |
| 309 |
|
} |
| 310 |
|
} |
| 311 |
0 |
refreshLayout(); |
| 312 |
|
} |
| 313 |
|
|
| 314 |
|
|
| 315 |
|
|
| 316 |
|
|
| |
|
| 0% |
Uncovered Elements: 11 (11) |
Complexity: 4 |
Complexity Density: 0.57 |
|
| 317 |
0 |
@Override... |
| 318 |
|
public JProgressBar getProgressBar(long id) |
| 319 |
|
{ |
| 320 |
0 |
Container progBar = progressBars.get(id); |
| 321 |
0 |
if (progBar == null || progBar.getComponentCount() == 0) |
| 322 |
|
{ |
| 323 |
0 |
return null; |
| 324 |
|
} |
| 325 |
0 |
for (Component component : progBar.getComponents()) |
| 326 |
|
{ |
| 327 |
0 |
if (component.getClass().equals(JProgressBar.class)) |
| 328 |
|
{ |
| 329 |
0 |
return (JProgressBar) component; |
| 330 |
|
} |
| 331 |
|
} |
| 332 |
0 |
return null; |
| 333 |
|
} |
| 334 |
|
|
| |
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 335 |
0 |
@Override... |
| 336 |
|
public String getMessage(long id) |
| 337 |
|
{ |
| 338 |
0 |
return progressBarMessages.get(id); |
| 339 |
|
} |
| 340 |
|
|
| 341 |
|
|
| 342 |
|
|
| 343 |
|
@param |
| 344 |
|
@param |
| 345 |
|
|
| |
|
| 0% |
Uncovered Elements: 16 (16) |
Complexity: 5 |
Complexity Density: 0.5 |
|
| 346 |
0 |
@Override... |
| 347 |
|
public void setProgressBarMessage(long id,String message) |
| 348 |
|
{ |
| 349 |
0 |
Container progBar = progressBars.get(id); |
| 350 |
0 |
if (progBar == null || progBar.getComponentCount() == 0) |
| 351 |
|
{ |
| 352 |
0 |
return; |
| 353 |
|
} |
| 354 |
0 |
for (Component component : progBar.getComponents()) |
| 355 |
|
{ |
| 356 |
0 |
if (component.getClass().equals(JLabel.class)) |
| 357 |
|
{ |
| 358 |
0 |
((JLabel) component).setText(message); |
| 359 |
0 |
IProgressIndicatorHandler handler = progressBarHandlers.get(id); |
| 360 |
0 |
if (handler!=null) { |
| 361 |
|
|
| 362 |
0 |
updateCancelHandler(progBar, handler); |
| 363 |
|
} else { |
| 364 |
0 |
refreshLayout(); |
| 365 |
|
} |
| 366 |
|
} |
| 367 |
|
} |
| 368 |
|
} |
| 369 |
|
|
| 370 |
|
} |