revalidate status bar if side components changed
[jSite2.git] / src / net / pterodactylus / util / swing / StatusBar.java
1 /*
2  * jSite2 - StatusBar.java -
3  * Copyright © 2008 David Roden
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 package net.pterodactylus.util.swing;
21
22 import java.awt.BorderLayout;
23 import java.awt.Component;
24 import java.awt.GridBagConstraints;
25 import java.awt.GridBagLayout;
26 import java.awt.Insets;
27 import java.util.ArrayList;
28 import java.util.List;
29
30 import javax.swing.JFrame;
31 import javax.swing.JLabel;
32 import javax.swing.JPanel;
33 import javax.swing.border.EtchedBorder;
34
35 /**
36  * Status bar component that can be added to the {@link BorderLayout#SOUTH} area
37  * of a {@link JFrame}’s {@link JFrame#getContentPane() content pane}.
38  * 
39  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
40  * @version $Id$
41  */
42 /* TODO - make it possible to add further components. */
43 public class StatusBar extends JPanel {
44
45         /** The layout. */
46         private GridBagLayout layout = new GridBagLayout();
47
48         /** The label. */
49         private JLabel statusLabel = new JLabel();
50
51         /** Addition components. */
52         private List<Component> sideComponents = new ArrayList<Component>();
53
54         /**
55          * Creates a new status bar.
56          */
57         public StatusBar() {
58                 setLayout(layout);
59                 statusLabel.setBorder(new EtchedBorder(EtchedBorder.LOWERED));
60                 add(statusLabel, new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0, GridBagConstraints.LINE_START, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
61         }
62
63         /**
64          * Sets the text of the label.
65          * 
66          * @param text
67          *            The text of the label
68          */
69         public void setText(String text) {
70                 statusLabel.setText(text);
71         }
72
73         /**
74          * Adds a side component to the right side of the status bar, pushing all
75          * previously added side components to the left.
76          * 
77          * @param component
78          *            The component to add
79          */
80         public void addSideComponent(Component component) {
81                 sideComponents.add(component);
82                 int newIndex = sideComponents.size();
83                 add(component, new GridBagConstraints(newIndex, 0, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.BOTH, new Insets(0, 2, 0, 0), 0, 0));
84                 validate();
85         }
86
87         /**
88          * Returns the number of side components.
89          * 
90          * @return The number of side components
91          */
92         public int getSideComponentCount() {
93                 return sideComponents.size();
94         }
95
96         /**
97          * Returns all side components in order.
98          * 
99          * @return All side components
100          */
101         public List<Component> getSideComponents() {
102                 return sideComponents;
103         }
104
105         /**
106          * Removes the side component with the given index.
107          * 
108          * @param sideComponentIndex
109          *            The index of the side component to remove
110          */
111         public void removeSideComponent(int sideComponentIndex) {
112                 Component sideComponent = sideComponents.remove(sideComponentIndex);
113                 remove(sideComponent);
114                 validate();
115         }
116
117         /**
118          * Removes the given side component.
119          * 
120          * @param sideComponent
121          *            The side component to remove
122          */
123         public void removeSideComponent(Component sideComponent) {
124                 sideComponents.remove(sideComponent);
125                 remove(sideComponent);
126                 validate();
127         }
128
129 }