3 import java.awt.BorderLayout;
4 import java.awt.GridLayout;
5 import java.awt.event.ActionEvent;
6 import java.awt.event.ActionListener;
7 import java.util.Hashtable;
10 import javax.swing.JButton;
11 import javax.swing.JLabel;
12 import javax.swing.JPanel;
13 import javax.swing.JProgressBar;
14 import javax.swing.SwingUtilities;
16 import jalview.util.MessageManager;
19 * A class to manage multiple progress bars embedded in a JPanel.
22 * Refactored from code duplicated in AlignFrame, PCAPanel, WebserviceInfo.
24 public class ProgressBar implements IProgressIndicator
27 * Progress bars in progress, keyed by any arbitrary long value
29 Map<Long, JPanel> progressBars;
32 * Optional handlers for the progress bars
34 Map<Long, IProgressIndicatorHandler> progressBarHandlers;
37 * The panel containing the progress bars - must have GridLayout
39 private JPanel statusPanel;
42 * Optional label where a status update message can be written on completion
45 private JLabel statusBar;
48 * Constructor. Note that the container of the progress bars, and the status
49 * bar to which an optional completion message is written, should be unchanged
50 * for the lifetime of this object for consistent behaviour.
53 * the panel holding the progress bars; must have GridLayout manager
55 * an optional place to write a message when a progress bar is
58 public ProgressBar(JPanel container, JLabel statusBar)
60 if (container == null)
62 throw new NullPointerException();
65 .isAssignableFrom(container.getLayout().getClass()))
67 throw new IllegalArgumentException("Container must have GridLayout");
69 this.statusPanel = container;
70 this.statusBar = statusBar;
71 this.progressBars = new Hashtable<Long, JPanel>();
72 this.progressBarHandlers = new Hashtable<Long, IProgressIndicatorHandler>();
77 * Returns true if any progress bars are still active
82 public boolean operationInProgress()
84 if (progressBars != null && progressBars.size() > 0)
92 * First call for a given id will show the message as a new progress bar. A
93 * second call with the same id will remove it. The 'removal' message is
94 * written to the status bar field (if neither is null).
96 * To avoid progress bars being left orphaned, ensure their removal is
97 * performed in a finally block if there is any risk of an error during
101 public void setProgressBar(String message, long id)
103 Long longId = Long.valueOf(id);
105 JPanel progressPanel = progressBars.get(longId);
106 if (progressPanel != null)
109 * Progress bar is displayed for this id - remove it now, and any handler
111 progressBars.remove(id);
112 if (message != null && statusBar != null)
114 statusBar.setText(message);
116 if (progressBarHandlers.containsKey(longId))
118 progressBarHandlers.remove(longId);
120 removeRow(progressPanel);
125 * No progress bar for this id - add one now
127 progressPanel = new JPanel(new BorderLayout(10, 5));
129 JProgressBar progressBar = new JProgressBar();
130 progressBar.setIndeterminate(true);
132 progressPanel.add(new JLabel(message), BorderLayout.WEST);
133 progressPanel.add(progressBar, BorderLayout.CENTER);
135 addRow(progressPanel);
137 progressBars.put(longId, progressPanel);
144 * Lays out progress bar container hierarchy
146 protected void refreshLayout()
149 * lay out progress bar container hierarchy
151 SwingUtilities.getRoot(statusPanel).validate();
155 * Remove one row with a progress bar, in a thread-safe manner
157 * @param progressPanel
159 protected void removeRow(JPanel progressPanel)
161 synchronized (statusPanel)
163 statusPanel.remove(progressPanel);
164 GridLayout layout = (GridLayout) statusPanel.getLayout();
165 layout.setRows(layout.getRows() - 1);
166 statusPanel.remove(progressPanel);
171 * Add one row with a progress bar, in a thread-safe manner
173 * @param progressPanel
175 protected void addRow(JPanel progressPanel)
177 synchronized (statusPanel)
179 GridLayout layout = (GridLayout) statusPanel.getLayout();
180 layout.setRows(layout.getRows() + 1);
181 statusPanel.add(progressPanel);
186 * Add a 'Cancel' handler for the given progress bar id. This should be called
187 * _after_ setProgressBar to have any effect.
190 public void registerHandler(final long id,
191 final IProgressIndicatorHandler handler)
193 Long longId = Long.valueOf(id);
194 final JPanel progressPanel = progressBars.get(longId);
195 if (progressPanel == null)
198 .println("call setProgressBar before registering the progress bar's handler.");
203 * Nothing useful to do if not a Cancel handler
205 if (!handler.canCancel())
210 progressBarHandlers.put(longId, handler);
211 JButton cancel = new JButton(MessageManager.getString("action.cancel"));
212 final IProgressIndicator us = this;
213 cancel.addActionListener(new ActionListener()
217 public void actionPerformed(ActionEvent e)
219 handler.cancelActivity(id);
220 us.setProgressBar(MessageManager.formatMessage(
221 "label.cancelled_params", new Object[]
222 { ((JLabel) progressPanel.getComponent(0)).getText() }), id);
225 progressPanel.add(cancel, BorderLayout.EAST);