Add status bar.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Tue, 27 Jan 2009 07:24:25 +0000 (08:24 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Tue, 27 Jan 2009 07:24:25 +0000 (08:24 +0100)
src/net/pterodactylus/util/swing/StatusBar.java [new file with mode: 0644]

diff --git a/src/net/pterodactylus/util/swing/StatusBar.java b/src/net/pterodactylus/util/swing/StatusBar.java
new file mode 100644 (file)
index 0000000..647601b
--- /dev/null
@@ -0,0 +1,110 @@
+package net.pterodactylus.util.swing;
+
+
+import java.awt.BorderLayout;
+import java.awt.Component;
+import java.awt.GridBagConstraints;
+import java.awt.GridBagLayout;
+import java.awt.Insets;
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.swing.JFrame;
+import javax.swing.JLabel;
+import javax.swing.JPanel;
+import javax.swing.border.EtchedBorder;
+
+/**
+ * Status bar component that can be added to the {@link BorderLayout#SOUTH} area
+ * of a {@link JFrame}’s {@link JFrame#getContentPane() content pane}.
+ *
+ * @author David ‘Bombe’ Roden &lt;bombe@freenetproject.org&gt;
+ */
+/* TODO - make it possible to add further components. */
+public class StatusBar extends JPanel {
+
+       /** The layout. */
+       private GridBagLayout layout = new GridBagLayout();
+
+       /** The label. */
+       private JLabel statusLabel = new JLabel();
+
+       /** Addition components. */
+       private List<Component> sideComponents = new ArrayList<Component>();
+
+       /**
+        * Creates a new status bar.
+        */
+       public StatusBar() {
+               setLayout(layout);
+               statusLabel.setBorder(new EtchedBorder(EtchedBorder.LOWERED));
+               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));
+       }
+
+       /**
+        * Sets the text of the label.
+        *
+        * @param text
+        *            The text of the label
+        */
+       public void setText(String text) {
+               statusLabel.setText(text);
+       }
+
+       /**
+        * Adds a side component to the right side of the status bar, pushing all
+        * previously added side components to the left.
+        *
+        * @param component
+        *            The component to add
+        */
+       public void addSideComponent(Component component) {
+               sideComponents.add(component);
+               int newIndex = sideComponents.size();
+               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));
+               validate();
+       }
+
+       /**
+        * Returns the number of side components.
+        *
+        * @return The number of side components
+        */
+       public int getSideComponentCount() {
+               return sideComponents.size();
+       }
+
+       /**
+        * Returns all side components in order.
+        *
+        * @return All side components
+        */
+       public List<Component> getSideComponents() {
+               return sideComponents;
+       }
+
+       /**
+        * Removes the side component with the given index.
+        *
+        * @param sideComponentIndex
+        *            The index of the side component to remove
+        */
+       public void removeSideComponent(int sideComponentIndex) {
+               Component sideComponent = sideComponents.remove(sideComponentIndex);
+               remove(sideComponent);
+               validate();
+       }
+
+       /**
+        * Removes the given side component.
+        *
+        * @param sideComponent
+        *            The side component to remove
+        */
+       public void removeSideComponent(Component sideComponent) {
+               sideComponents.remove(sideComponent);
+               remove(sideComponent);
+               validate();
+       }
+
+}