2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
23 import jalview.util.MessageManager;
25 import java.awt.BorderLayout;
26 import java.awt.Component;
27 import java.awt.GridLayout;
28 import java.awt.event.ActionEvent;
29 import java.awt.event.ActionListener;
30 import java.util.Hashtable;
33 import javax.swing.JButton;
34 import javax.swing.JLabel;
35 import javax.swing.JPanel;
36 import javax.swing.JProgressBar;
37 import javax.swing.SwingUtilities;
40 * A class to manage multiple progress bars embedded in a JPanel.
43 * Refactored from code duplicated in AlignFrame, PCAPanel, WebserviceInfo.
45 public class ProgressBar implements IProgressIndicator
48 * Progress bars in progress, keyed by any arbitrary long value
50 Map<Long, JPanel> progressBars;
53 * Optional handlers for the progress bars
55 Map<Long, IProgressIndicatorHandler> progressBarHandlers;
58 * The panel containing the progress bars - must have GridLayout
60 private JPanel statusPanel;
63 * Optional label where a status update message can be written on completion
66 private JLabel statusBar;
69 * Constructor. Note that the container of the progress bars, and the status
70 * bar to which an optional completion message is written, should be unchanged
71 * for the lifetime of this object for consistent behaviour.
74 * the panel holding the progress bars; must have GridLayout manager
76 * an optional place to write a message when a progress bar is
79 public ProgressBar(JPanel container, JLabel statusBar)
81 if (container == null)
83 throw new NullPointerException();
86 .isAssignableFrom(container.getLayout().getClass()))
88 throw new IllegalArgumentException("Container must have GridLayout");
90 this.statusPanel = container;
91 this.statusBar = statusBar;
92 this.progressBars = new Hashtable<>();
93 this.progressBarHandlers = new Hashtable<>();
98 * Returns true if any progress bars are still active
103 public boolean operationInProgress()
105 if (progressBars != null && progressBars.size() > 0)
113 * First call for a given id will show the message as a new progress bar. A
114 * second call with the same id will remove it. The 'removal' message is
115 * written to the status bar field (if neither is null).
117 * To avoid progress bars being left orphaned, ensure their removal is
118 * performed in a finally block if there is any risk of an error during
122 public void setProgressBar(final String message, final long id)
124 SwingUtilities.invokeLater(new Runnable()
129 JPanel progressPanel = progressBars.get(id);
130 if (progressPanel != null)
133 * Progress bar is displayed for this id - remove it now, and any handler
135 progressBars.remove(id);
136 if (message != null && statusBar != null)
138 statusBar.setText(message);
140 if (progressBarHandlers.containsKey(id))
142 progressBarHandlers.remove(id);
144 removeRow(progressPanel);
149 * No progress bar for this id - add one now
151 progressPanel = new JPanel(new BorderLayout(10, 5));
153 JProgressBar progressBar = new JProgressBar();
154 progressBar.setIndeterminate(true);
156 progressPanel.add(new JLabel(message), BorderLayout.WEST);
157 progressPanel.add(progressBar, BorderLayout.CENTER);
159 addRow(progressPanel);
161 progressBars.put(id, progressPanel);
171 public void removeProgressBar(final long id)
173 SwingUtilities.invokeLater(() -> {
174 JPanel progressPanel = progressBars.get(id);
175 if (progressPanel != null)
177 progressBars.remove(id);
178 if (progressBarHandlers.containsKey(id))
180 progressBarHandlers.remove(id);
182 removeRow(progressPanel);
188 * Lays out progress bar container hierarchy
190 protected void refreshLayout()
193 * lay out progress bar container hierarchy
195 Component root = SwingUtilities.getRoot(statusPanel);
203 * Remove one row with a progress bar, in a thread-safe manner
205 * @param progressPanel
207 protected void removeRow(JPanel progressPanel)
209 synchronized (statusPanel)
211 statusPanel.remove(progressPanel);
212 GridLayout layout = (GridLayout) statusPanel.getLayout();
213 layout.setRows(layout.getRows() - 1);
214 statusPanel.remove(progressPanel);
219 * Add one row with a progress bar, in a thread-safe manner
221 * @param progressPanel
223 protected void addRow(JPanel progressPanel)
225 synchronized (statusPanel)
227 GridLayout layout = (GridLayout) statusPanel.getLayout();
228 layout.setRows(layout.getRows() + 1);
229 statusPanel.add(progressPanel);
234 * Add a 'Cancel' handler for the given progress bar id. This should be called
235 * _after_ setProgressBar to have any effect.
238 public void registerHandler(final long id,
239 final IProgressIndicatorHandler handler)
241 final IProgressIndicator us = this;
243 SwingUtilities.invokeLater(new Runnable()
248 final JPanel progressPanel = progressBars.get(id);
249 if (progressPanel == null)
252 "call setProgressBar before registering the progress bar's handler.");
257 * Nothing useful to do if not a Cancel handler
259 if (!handler.canCancel())
264 progressBarHandlers.put(id, handler);
265 JButton cancel = new JButton(
266 MessageManager.getString("action.cancel"));
267 cancel.addActionListener(new ActionListener()
271 public void actionPerformed(ActionEvent e)
273 handler.cancelActivity(id);
274 us.setProgressBar(MessageManager
275 .formatMessage("label.cancelled_params", new Object[]
276 { ((JLabel) progressPanel.getComponent(0)).getText() }),
280 progressPanel.add(cancel, BorderLayout.EAST);