throw new NullPointerException();
}
if (!GridLayout.class
- .isAssignableFrom(container.getLayout().getClass()))
+ .isAssignableFrom(container.getLayout().getClass()))
{
throw new IllegalArgumentException("Container must have GridLayout");
}
@Override
public void run()
{
- JPanel progressPanel = progressBars.get(id);
- if (progressPanel != null)
+ if (progressBars.containsKey(id))
{
/*
* Progress bar is displayed for this id - remove it now, and any handler
*/
- progressBars.remove(id);
+ removeProgressBar(id);
if (message != null && statusBar != null)
{
statusBar.setText(message);
}
- if (progressBarHandlers.containsKey(id))
- {
- progressBarHandlers.remove(id);
- }
- removeRow(progressPanel);
}
else
{
/*
* No progress bar for this id - add one now
*/
- progressPanel = new JPanel(new BorderLayout(10, 5));
-
- JProgressBar progressBar = new JProgressBar();
- progressBar.setIndeterminate(true);
-
- progressPanel.add(new JLabel(message), BorderLayout.WEST);
- progressPanel.add(progressBar, BorderLayout.CENTER);
-
- addRow(progressPanel);
-
- progressBars.put(id, progressPanel);
+ addProgressBar(id, message);
}
-
- refreshLayout();
}
});
}
-
+
+ /**
+ * Add a progress bar for the given id if it doesn't exist displaying the
+ * provided message. Subsequent calls do nothing.
+ *
+ * @param id
+ * progress bar identifier
+ * @param message
+ * displayed message
+ */
+ @Override
+ public void addProgressBar(final long id, final String message)
+ {
+ if (progressBars.containsKey(id))
+ return;
+ JPanel progressPanel = new JPanel(new BorderLayout(10, 5));
+ progressBars.put(id, progressPanel);
+ Runnable r = () -> {
+ JProgressBar progressBar = new JProgressBar();
+ progressBar.setIndeterminate(true);
+ progressPanel.add(new JLabel(message), BorderLayout.WEST);
+ progressPanel.add(progressBar, BorderLayout.CENTER);
+ addRow(progressPanel);
+ refreshLayout();
+ };
+ if (SwingUtilities.isEventDispatchThread())
+ r.run();
+ else
+ SwingUtilities.invokeLater(r);
+ }
+
+ /**
+ * Remove a progress bar for the given id if it exists. Subsequent calls do
+ * nothing.
+ *
+ * @param id
+ * id of the progress bar to be removed
+ */
@Override
public void removeProgressBar(final long id)
{
- SwingUtilities.invokeLater(() -> {
- JPanel progressPanel = progressBars.get(id);
- if (progressPanel != null)
- {
- progressBars.remove(id);
- if (progressBarHandlers.containsKey(id))
- {
- progressBarHandlers.remove(id);
- }
- removeRow(progressPanel);
- }
- });
+ JPanel progressPanel = progressBars.remove(id);
+ if (progressPanel == null)
+ return;
+ progressBarHandlers.remove(id);
+ Runnable r = () -> {
+ removeRow(progressPanel);
+ refreshLayout();
+ };
+ if (SwingUtilities.isEventDispatchThread())
+ r.run();
+ else
+ SwingUtilities.invokeLater(r);
}
/**
*/
@Override
public void registerHandler(final long id,
- final IProgressIndicatorHandler handler)
+ final IProgressIndicatorHandler handler)
{
final IProgressIndicator us = this;
if (progressPanel == null)
{
System.err.println(
- "call setProgressBar before registering the progress bar's handler.");
+ "call setProgressBar before registering the progress bar's handler.");
return;
}
progressBarHandlers.put(id, handler);
JButton cancel = new JButton(
- MessageManager.getString("action.cancel"));
+ MessageManager.getString("action.cancel"));
cancel.addActionListener(new ActionListener()
{
{
handler.cancelActivity(id);
us.setProgressBar(MessageManager
- .formatMessage("label.cancelled_params", new Object[]
- { ((JLabel) progressPanel.getComponent(0)).getText() }),
- id);
+ .formatMessage("label.cancelled_params", new Object[]
+ { ((JLabel) progressPanel.getComponent(0)).getText() }),
+ id);
}
});
progressPanel.add(cancel, BorderLayout.EAST);