001: /*
002: * AbstractStatusBarPanel.java
003: *
004: * Copyright (C) 2002, 2003, 2004, 2005, 2006 Takis Diakoumis
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License
008: * as published by the Free Software Foundation; either version 2
009: * of the License, or any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
019: *
020: */
021:
022: package org.underworldlabs.swing;
023:
024: import java.awt.Color;
025: import java.awt.Dimension;
026: import java.awt.Graphics;
027: import java.awt.Insets;
028: import java.util.Vector;
029: import javax.swing.JComponent;
030: import javax.swing.JLabel;
031: import javax.swing.JPanel;
032: import javax.swing.SwingUtilities;
033: import javax.swing.border.Border;
034:
035: /* ----------------------------------------------------------
036: * CVS NOTE: Changes to the CVS repository prior to the
037: * release of version 3.0.0beta1 has meant a
038: * resetting of CVS revision numbers.
039: * ----------------------------------------------------------
040: */
041:
042: /**
043: *
044: * @author Takis Diakoumis
045: * @version $Revision: 1.4 $
046: * @date $Date: 2006/05/14 06:56:07 $
047: */
048: public abstract class AbstractStatusBarPanel extends JPanel {
049:
050: /** default border colour */
051: private static Color BORDER_COLOUR;
052:
053: /** default for for non-JLabel components */
054: private static Border COMPONENT_BORDER;
055:
056: /** components within this status bar */
057: private Vector components;
058:
059: protected AbstractStatusBarPanel(int height) {
060: super (new StatusBarLayout(height));
061: }
062:
063: protected void addLabel(int index, int width, boolean resizable) {
064: if (components == null) {
065: components = new Vector();
066: }
067: StatusBarLabel label = new StatusBarLabel(false, true, false,
068: false);
069: add(label, new StatusBarLayoutConstraints(index, width,
070: resizable));
071: components.add(index, label);
072: }
073:
074: protected void addComponent(JComponent c, int index, int width,
075: boolean resizable) {
076: if (components == null) {
077: components = new Vector();
078: }
079:
080: if (COMPONENT_BORDER == null) {
081: COMPONENT_BORDER = new StatusBarComponentBorder();
082: }
083:
084: c.setBorder(COMPONENT_BORDER);
085: add(c, new StatusBarLayoutConstraints(index, width, resizable));
086: components.add(index, c);
087: }
088:
089: protected JLabel getLabel(int index) {
090: Object object = components.get(index);
091: if (object != null && object instanceof JLabel) {
092: return (JLabel) object;
093: }
094: return null;
095: }
096:
097: protected void setLabelText(int index, final String text) {
098: Object object = components.get(index);
099: if (object != null && object instanceof JLabel) {
100: final JLabel label = (JLabel) object;
101: Runnable update = new Runnable() {
102: public void run() {
103: label.setText(formatText(text));
104: Dimension dim = label.getSize();
105: label.paintImmediately(0, 0, dim.width, dim.height);
106: }
107: };
108: SwingUtilities.invokeLater(update);
109: }
110: }
111:
112: private String formatText(String text) {
113: if (text != null && text.length() > 0) {
114: char firstChar = text.charAt(0);
115: if (!Character.isWhitespace(firstChar)) {
116: return " " + text;
117: }
118: }
119: return text;
120: }
121:
122: public void paintComponent(Graphics g) {
123: super .paintComponent(g);
124:
125: int height = getHeight();
126: int width = getWidth();
127:
128: if (BORDER_COLOUR == null) {
129: BORDER_COLOUR = GUIUtils.getDefaultBorderColour();
130: }
131:
132: Insets insets = getInsets();
133:
134: g.setColor(BORDER_COLOUR);
135: g
136: .drawRect(insets.left, insets.top, width - insets.left
137: - insets.right, height - insets.top
138: - insets.bottom - 1);
139:
140: }
141:
142: }
|