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.Container;
28 import java.awt.GridLayout;
29 import java.awt.event.ActionEvent;
30 import java.awt.event.ActionListener;
31 import java.util.Hashtable;
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;
41 * A class to manage multiple progress bars embedded in a JPanel.
44 * Refactored from code duplicated in AlignFrame, PCAPanel, WebserviceInfo.
46 public class ProgressBar implements IProgressIndicator
49 * Progress bars in progress, keyed by any arbitrary long value
51 Map<Long, JPanel> progressBars;
54 * Optional handlers for the progress bars
56 Map<Long, IProgressIndicatorHandler> progressBarHandlers;
59 * The panel containing the progress bars - must have GridLayout
61 private JPanel statusPanel;
64 * Optional label where a status update message can be written on completion
67 private JLabel statusBar;
70 * Constructor. Note that the container of the progress bars, and the status
71 * bar to which an optional completion message is written, should be unchanged
72 * for the lifetime of this object for consistent behaviour.
75 * the panel holding the progress bars; must have GridLayout manager
77 * an optional place to write a message when a progress bar is
80 public ProgressBar(JPanel container, JLabel statusBar)
82 if (container == null)
84 throw new NullPointerException();
87 .isAssignableFrom(container.getLayout().getClass()))
89 throw new IllegalArgumentException("Container must have GridLayout");
91 this.statusPanel = container;
92 this.statusBar = statusBar;
93 this.progressBars = new Hashtable<>();
94 this.progressBarHandlers = new Hashtable<>();
99 * Returns true if any progress bars are still active
104 public boolean operationInProgress()
106 if (progressBars != null && progressBars.size() > 0)
114 * First call for a given id will show the message as a new progress bar. A
115 * second call with the same id will remove it. The 'removal' message is
116 * written to the status bar field (if neither is null).
118 * To avoid progress bars being left orphaned, ensure their removal is
119 * performed in a finally block if there is any risk of an error during
123 public void setProgressBar(final String message, final long id)
125 SwingUtilities.invokeLater(new Runnable()
130 JPanel progressPanel = progressBars.get(id);
131 if (progressPanel != null)
134 * Progress bar is displayed for this id - remove it now, and any handler
136 progressBars.remove(id);
137 if (message != null && statusBar != null)
139 statusBar.setText(message);
141 if (progressBarHandlers.containsKey(id))
143 progressBarHandlers.remove(id);
145 removeRow(progressPanel);
150 * No progress bar for this id - add one now
152 progressPanel = new JPanel(new BorderLayout(10, 5));
154 JProgressBar progressBar = new JProgressBar();
155 progressBar.setIndeterminate(true);
157 progressPanel.add(new JLabel(message), BorderLayout.WEST);
158 progressPanel.add(progressBar, BorderLayout.CENTER);
160 addRow(progressPanel);
162 progressBars.put(id, progressPanel);
172 * Lays out progress bar container hierarchy
174 protected void refreshLayout()
177 * lay out progress bar container hierarchy
179 Component root = SwingUtilities.getRoot(statusPanel);
187 * Remove one row with a progress bar, in a thread-safe manner
189 * @param progressPanel
191 protected void removeRow(JPanel progressPanel)
193 synchronized (statusPanel)
195 statusPanel.remove(progressPanel);
196 GridLayout layout = (GridLayout) statusPanel.getLayout();
197 layout.setRows(layout.getRows() - 1);
198 statusPanel.remove(progressPanel);
203 * Add one row with a progress bar, in a thread-safe manner
205 * @param progressPanel
207 protected void addRow(JPanel progressPanel)
209 synchronized (statusPanel)
211 GridLayout layout = (GridLayout) statusPanel.getLayout();
212 layout.setRows(layout.getRows() + 1);
213 statusPanel.add(progressPanel);
218 * Add a 'Cancel' handler for the given progress bar id. This should be called
219 * _after_ setProgressBar to have any effect.
222 public void registerHandler(final long id,
223 final IProgressIndicatorHandler handler)
225 final IProgressIndicator us = this;
227 SwingUtilities.invokeLater(new Runnable()
232 final JPanel progressPanel = progressBars.get(id);
233 if (progressPanel == null)
235 jalview.bin.Console.errPrintln(
236 "call setProgressBar before registering the progress bar's handler.");
241 * Nothing useful to do if not a Cancel handler
243 if (!handler.canCancel())
248 progressBarHandlers.put(id, handler);
249 JButton cancel = new JButton(
250 MessageManager.getString("action.cancel"));
251 cancel.addActionListener(new ActionListener()
255 public void actionPerformed(ActionEvent e)
257 handler.cancelActivity(id);
258 us.setProgressBar(MessageManager
259 .formatMessage("label.cancelled_params", new Object[]
260 { ((JLabel) progressPanel.getComponent(0)).getText() }),
264 progressPanel.add(cancel, BorderLayout.EAST);
274 public JProgressBar getProgressBar(long id)
276 Container progBar = progressBars.get(id);
277 if (progBar==null || progBar.getComponentCount()==0)
281 for (Component component : progBar.getComponents())
283 if (component.getClass().equals(JProgressBar.class))
285 return (JProgressBar) component;