6a70f6ad928e431b5b6c49cf8ac6961d4f502162
[jalview.git] / src / jalview / gui / ProgressPanel.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
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.
11  *  
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.
16  * 
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.
20  */
21 package jalview.gui;
22
23 import jalview.api.RendererListenerI;
24
25 import java.awt.BorderLayout;
26 import java.beans.PropertyChangeEvent;
27
28 import javax.swing.JLabel;
29 import javax.swing.JPanel;
30 import javax.swing.JProgressBar;
31 import javax.swing.border.EmptyBorder;
32
33 /**
34  * A class to manage a panel containing a label and progress bar updated by an
35  * event firing
36  * 
37  * @author kmourao
38  *
39  */
40 public class ProgressPanel extends JPanel implements RendererListenerI
41 {
42   // max value of progress bar: values expected to be %s
43   private final int MAXVALUE = 100;
44
45   // name of event property which updates the progress bar
46   private String eventName;
47
48   private JProgressBar progressBar;
49
50   private JLabel progressLabel;
51
52   private String labelText;
53
54   /**
55    * Construct a JPanel containing a progress bar and a label.
56    * 
57    * @param eventPropertyName
58    *          The name of the event property to update the progress bar
59    * @param label
60    *          The label to place next to the progress bar
61    */
62   public ProgressPanel(String eventPropertyName, String label)
63   {
64     super(new BorderLayout(10, 0));
65     setBorder(new EmptyBorder(0, 3, 0, 20));
66
67     eventName = eventPropertyName;
68     labelText = label;
69
70     progressBar = new JProgressBar();
71     progressBar.setMinimum(0);
72     progressLabel = new JLabel(labelText);
73   
74     add(progressLabel, BorderLayout.WEST);
75     add(progressBar, BorderLayout.CENTER);
76   }
77
78   @Override
79   /**
80    * Update the progress bar in response to the event. Expects the value
81    * supplied by the event to be in the range 0-100 i.e. a percentage
82    */
83   public void propertyChange(PropertyChangeEvent evt)
84   {
85     if (evt.getPropertyName().equals(eventName))
86     {
87       int progress = (int) evt.getNewValue();
88
89       System.out.println(progress);
90
91       progressBar.setValue(progress);
92       if (progress < MAXVALUE && !progressBar.isVisible())
93       {
94         progressBar.setVisible(true);
95         progressLabel.setText(labelText);
96       }
97       else if (progress >= MAXVALUE)
98       {
99         progressBar.setVisible(false);
100         progressLabel.setText(" "); // keep visible so panel stays visible
101       }
102     }
103   }
104 }