JAL-2587 Made progress bar display before overview calc starts
[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.awt.CardLayout;
27 import java.beans.PropertyChangeEvent;
28
29 import javax.swing.BorderFactory;
30 import javax.swing.JLabel;
31 import javax.swing.JPanel;
32 import javax.swing.JProgressBar;
33 import javax.swing.border.EmptyBorder;
34 import javax.swing.border.EtchedBorder;
35
36 /**
37  * A class to manage a panel containing a label and progress bar updated by an
38  * event firing
39  * 
40  * @author kmourao
41  *
42  */
43 public class ProgressPanel extends JPanel implements RendererListenerI
44 {
45   // max value of progress bar: values expected to be %s
46   private final int MAXVALUE = 100;
47
48   private final String VISIBLE = "VISIBLE";
49
50   private final String INVISIBLE = "INVISIBLE";
51
52   // name of event property which updates the progress bar
53   private String eventName;
54
55   private JProgressBar progressBar;
56
57   private JLabel progressLabel;
58
59   private JPanel labelPanel = new JPanel();
60
61   private CardLayout labelLayout = new CardLayout();
62
63   private JPanel barPanel = new JPanel();
64
65   private CardLayout barLayout = new CardLayout();
66
67   /**
68    * Construct a JPanel containing a progress bar and a label.
69    * 
70    * @param eventPropertyName
71    *          The name of the event property to update the progress bar
72    * @param label
73    *          The label to place next to the progress bar
74    */
75   public ProgressPanel(String eventPropertyName, String label)
76   {
77     super(new BorderLayout(10, 0));
78     setBorder(new EmptyBorder(0, 3, 0, 20));
79
80     eventName = eventPropertyName;
81     String labelText = label;
82
83     progressBar = new JProgressBar();
84     progressBar.setMinimum(0);
85     progressLabel = new JLabel(labelText);
86
87     // Use a CardLayout to stop the progress bar panel moving around when
88     // changing visibility
89     labelPanel.setLayout(labelLayout);
90     barPanel.setLayout(barLayout);
91   
92     labelPanel.add(progressLabel, VISIBLE);
93     labelPanel.add(new JPanel(), INVISIBLE);
94     barPanel.add(progressBar, VISIBLE);
95     barPanel.add(new JPanel(), INVISIBLE);
96
97     labelLayout.show(labelPanel, VISIBLE);
98     barLayout.show(barPanel, VISIBLE);
99
100     add(labelPanel, BorderLayout.WEST);
101     add(barPanel, BorderLayout.CENTER);
102     add(new JLabel(" "), BorderLayout.EAST);
103
104     setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));
105   }
106
107   @Override
108   /**
109    * Update the progress bar in response to the event. Expects the value
110    * supplied by the event to be in the range 0-100 i.e. a percentage
111    */
112   public void propertyChange(PropertyChangeEvent evt)
113   {
114     if (evt.getPropertyName().equals(eventName))
115     {
116       int progress = (int) evt.getNewValue();
117       progressBar.setValue(progress);
118
119       // switch progress bar to visible if it is not visible and current
120       // progress is less than MAXVALUE
121       // switch progress bar to invisible if it is visible and we reached
122       // MAXVALUE
123       if (progress < MAXVALUE && !progressBar.isVisible())
124       {
125         labelLayout.show(labelPanel, VISIBLE);
126         barLayout.show(barPanel, VISIBLE);
127       }
128       if (progress >= MAXVALUE)
129       {
130         labelLayout.show(labelPanel, INVISIBLE);
131         barLayout.show(barPanel, INVISIBLE);
132       }
133     }
134   }
135 }