JAL-2587 Added progress bar to overview. Not fully working yet.
[jalview.git] / src / jalview / gui / ProgressPanel.java
diff --git a/src/jalview/gui/ProgressPanel.java b/src/jalview/gui/ProgressPanel.java
new file mode 100644 (file)
index 0000000..f69f0cf
--- /dev/null
@@ -0,0 +1,101 @@
+/*
+ * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
+ * Copyright (C) $$Year-Rel$$ The Jalview Authors
+ * 
+ * This file is part of Jalview.
+ * 
+ * Jalview is free software: you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License 
+ * as published by the Free Software Foundation, either version 3
+ * of the License, or (at your option) any later version.
+ *  
+ * Jalview is distributed in the hope that it will be useful, but 
+ * WITHOUT ANY WARRANTY; without even the implied warranty 
+ * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
+ * PURPOSE.  See the GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
+ * The Jalview Authors are detailed in the 'AUTHORS' file.
+ */
+package jalview.gui;
+
+import jalview.api.RendererListenerI;
+
+import java.awt.BorderLayout;
+import java.beans.PropertyChangeEvent;
+
+import javax.swing.JLabel;
+import javax.swing.JPanel;
+import javax.swing.JProgressBar;
+import javax.swing.border.EmptyBorder;
+
+/**
+ * A class to manage a panel containing a label and progress bar updated by an
+ * event firing
+ * 
+ * @author kmourao
+ *
+ */
+public class ProgressPanel extends JPanel implements RendererListenerI
+{
+  // max value of progress bar: values expected to be %s
+  private final int MAXVALUE = 100;
+
+  // name of event property which updates the progress bar
+  private String eventName;
+
+  private JProgressBar progressBar;
+
+  private JLabel progressLabel;
+
+  /**
+   * Construct a JPanel containing a progress bar and a label.
+   * 
+   * @param eventPropertyName
+   *          The name of the event property to update the progress bar
+   * @param label
+   *          The label to place next to the progress bar
+   */
+  public ProgressPanel(String eventPropertyName, String label)
+  {
+    super(new BorderLayout(10, 0));
+    setBorder(new EmptyBorder(0, 3, 0, 20));
+
+    eventName = eventPropertyName;
+
+    progressBar = new JProgressBar();
+    progressBar.setMinimum(0);
+    progressLabel = new JLabel(label);
+  
+    add(progressLabel, BorderLayout.WEST);
+    add(progressBar, BorderLayout.CENTER);
+  }
+
+  @Override
+  /**
+   * Update the progress bar in response to the event. Expects the value
+   * supplied by the event to be in the range 0-100 i.e. a percentage
+   */
+  public void propertyChange(PropertyChangeEvent evt)
+  {
+    if (evt.getPropertyName().equals(eventName))
+    {
+      int progress = (int) evt.getNewValue();
+
+      System.out.println(progress);
+
+      progressBar.setValue(progress);
+      if (progress < MAXVALUE && !progressBar.isVisible())
+      {
+        progressBar.setVisible(true);
+        progressLabel.setVisible(true);
+      }
+      else if (progress >= MAXVALUE)
+      {
+        progressBar.setVisible(false);
+        progressLabel.setVisible(false);
+      }
+    }
+  }
+}