whitespace fixups
[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  */
41 /* TODO - make it possible to add further components. */
42 public class StatusBar extends JPanel {
43
44         /** The layout. */
45         private GridBagLayout layout = new GridBagLayout();
46
47         /** The label. */
48         private JLabel statusLabel = new JLabel();
49
50         /** Addition components. */
51         private List<Component> sideComponents = new ArrayList<Component>();
52
53         /**
54          * Creates a new status bar.
55          */
56         public StatusBar() {
57                 setLayout(layout);
58                 statusLabel.setBorder(new EtchedBorder(EtchedBorder.LOWERED));
59                 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));
60         }
61
62         /**
63          * Sets the text of the label.
64          *
65          * @param text
66          *            The text of the label
67          */
68         public void setText(String text) {
69                 statusLabel.setText(text);
70         }
71
72         /**
73          * Adds a side component to the right side of the status bar, pushing all
74          * previously added side components to the left.
75          *
76          * @param component
77          *            The component to add
78          */
79         public void addSideComponent(Component component) {
80                 sideComponents.add(component);
81                 int newIndex = sideComponents.size();
82                 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));
83                 validate();
84         }
85
86         /**
87          * Returns the number of side components.
88          *
89          * @return The number of side components
90          */
91         public int getSideComponentCount() {
92                 return sideComponents.size();
93         }
94
95         /**
96          * Returns all side components in order.
97          *
98          * @return All side components
99          */
100         public List<Component> getSideComponents() {
101                 return sideComponents;
102         }
103
104         /**
105          * Removes the side component with the given index.
106          *
107          * @param sideComponentIndex
108          *            The index of the side component to remove
109          */
110         public void removeSideComponent(int sideComponentIndex) {
111                 Component sideComponent = sideComponents.remove(sideComponentIndex);
112                 remove(sideComponent);
113                 validate();
114         }
115
116         /**
117          * Removes the given side component.
118          *
119          * @param sideComponent
120          *            The side component to remove
121          */
122         public void removeSideComponent(Component sideComponent) {
123                 sideComponents.remove(sideComponent);
124                 remove(sideComponent);
125                 validate();
126         }
127
128 }