Add status bar.
[jkeytool.git] / src / net / pterodactylus / util / swing / StatusBar.java
1 package net.pterodactylus.util.swing;
2
3
4 import java.awt.BorderLayout;
5 import java.awt.Component;
6 import java.awt.GridBagConstraints;
7 import java.awt.GridBagLayout;
8 import java.awt.Insets;
9 import java.util.ArrayList;
10 import java.util.List;
11
12 import javax.swing.JFrame;
13 import javax.swing.JLabel;
14 import javax.swing.JPanel;
15 import javax.swing.border.EtchedBorder;
16
17 /**
18  * Status bar component that can be added to the {@link BorderLayout#SOUTH} area
19  * of a {@link JFrame}’s {@link JFrame#getContentPane() content pane}.
20  *
21  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
22  */
23 /* TODO - make it possible to add further components. */
24 public class StatusBar extends JPanel {
25
26         /** The layout. */
27         private GridBagLayout layout = new GridBagLayout();
28
29         /** The label. */
30         private JLabel statusLabel = new JLabel();
31
32         /** Addition components. */
33         private List<Component> sideComponents = new ArrayList<Component>();
34
35         /**
36          * Creates a new status bar.
37          */
38         public StatusBar() {
39                 setLayout(layout);
40                 statusLabel.setBorder(new EtchedBorder(EtchedBorder.LOWERED));
41                 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));
42         }
43
44         /**
45          * Sets the text of the label.
46          *
47          * @param text
48          *            The text of the label
49          */
50         public void setText(String text) {
51                 statusLabel.setText(text);
52         }
53
54         /**
55          * Adds a side component to the right side of the status bar, pushing all
56          * previously added side components to the left.
57          *
58          * @param component
59          *            The component to add
60          */
61         public void addSideComponent(Component component) {
62                 sideComponents.add(component);
63                 int newIndex = sideComponents.size();
64                 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));
65                 validate();
66         }
67
68         /**
69          * Returns the number of side components.
70          *
71          * @return The number of side components
72          */
73         public int getSideComponentCount() {
74                 return sideComponents.size();
75         }
76
77         /**
78          * Returns all side components in order.
79          *
80          * @return All side components
81          */
82         public List<Component> getSideComponents() {
83                 return sideComponents;
84         }
85
86         /**
87          * Removes the side component with the given index.
88          *
89          * @param sideComponentIndex
90          *            The index of the side component to remove
91          */
92         public void removeSideComponent(int sideComponentIndex) {
93                 Component sideComponent = sideComponents.remove(sideComponentIndex);
94                 remove(sideComponent);
95                 validate();
96         }
97
98         /**
99          * Removes the given side component.
100          *
101          * @param sideComponent
102          *            The side component to remove
103          */
104         public void removeSideComponent(Component sideComponent) {
105                 sideComponents.remove(sideComponent);
106                 remove(sideComponent);
107                 validate();
108         }
109
110 }