001: package net.sourceforge.squirrel_sql.fw.gui;
002:
003: /*
004: * Copyright (C) 2001-2003 Colin Bell
005: * colbell@users.sourceforge.net
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this library; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: */
021: import java.awt.*;
022:
023: import javax.swing.*;
024: import javax.swing.border.BevelBorder;
025: import javax.swing.border.Border;
026:
027: /**
028: * This is a statusbar component with a text control for messages.
029: *
030: * @author <A HREF="mailto:colbell@users.sourceforge.net">Colin Bell</A>
031: */
032: public class StatusBar extends JPanel {
033: /**
034: * Message to display if there is no msg to display. Defaults to a
035: * blank string.
036: */
037: private String _msgWhenEmpty = " ";
038:
039: /** Label showing the message in the statusbar. */
040: private final JLabel _textLbl = new JLabel();
041:
042: private final JProgressBar _progressBar = new JProgressBar();
043:
044: private final JPanel _pnlLabelOrProgress = new JPanel();
045:
046: /** Constraints used to add new controls to this statusbar. */
047: private final GridBagConstraints _gbc = new GridBagConstraints();
048:
049: private Font _font;
050:
051: /**
052: * Default ctor.
053: */
054: public StatusBar() {
055: super (new GridBagLayout());
056: createGUI();
057: }
058:
059: /**
060: * Set the font for controls in this statusbar.
061: *
062: * @param font The font to use.
063: *
064: * @throws IllegalArgumentException
065: * Thrown if <TT>null</TT> <TT>Font</TT> passed.
066: */
067: public synchronized void setFont(Font font) {
068: if (font == null) {
069: throw new IllegalArgumentException("Font == null");
070: }
071: super .setFont(font);
072: _font = font;
073: updateSubcomponentsFont(this );
074: }
075:
076: /**
077: * Set the text to display in the message label.
078: *
079: * @param text Text to display in the message label.
080: */
081: public synchronized void setText(String text) {
082: String myText = null;
083: if (text != null) {
084: myText = text.trim();
085: }
086: if (myText != null && myText.length() > 0) {
087: _textLbl.setText(myText);
088: } else {
089: clearText();
090: }
091: }
092:
093: /**
094: * Returns the text label's current value
095: * @return
096: */
097: public synchronized String getText() {
098: return _textLbl.getText();
099: }
100:
101: public synchronized void clearText() {
102: _textLbl.setText(_msgWhenEmpty);
103: }
104:
105: public synchronized void setTextWhenEmpty(String value) {
106: final boolean wasEmpty = _textLbl.getText().equals(
107: _msgWhenEmpty);
108: if (value != null && value.length() > 0) {
109: _msgWhenEmpty = value;
110: } else {
111: _msgWhenEmpty = " ";
112: }
113: if (wasEmpty) {
114: clearText();
115: }
116: }
117:
118: public synchronized void addJComponent(JComponent comp) {
119: if (comp == null) {
120: throw new IllegalArgumentException("JComponent == null");
121: }
122: comp.setBorder(createComponentBorder());
123: if (_font != null) {
124: comp.setFont(_font);
125: updateSubcomponentsFont(comp);
126: }
127: super .add(comp, _gbc);
128: }
129:
130: public static Border createComponentBorder() {
131: return BorderFactory.createCompoundBorder(BorderFactory
132: .createBevelBorder(BevelBorder.LOWERED), BorderFactory
133: .createEmptyBorder(0, 4, 0, 4));
134: }
135:
136: private void createGUI() {
137: clearText();
138:
139: Dimension progSize = _progressBar.getPreferredSize();
140: progSize.height = _textLbl.getPreferredSize().height;
141: _progressBar.setPreferredSize(progSize);
142:
143: _progressBar.setStringPainted(true);
144:
145: _pnlLabelOrProgress.setLayout(new GridLayout(1, 1));
146: _pnlLabelOrProgress.add(_textLbl);
147:
148: // The message area is on the right of the statusbar and takes
149: // up all available space.
150: _gbc.anchor = GridBagConstraints.WEST;
151: _gbc.weightx = 1.0;
152: _gbc.fill = GridBagConstraints.HORIZONTAL;
153: _gbc.gridy = 0;
154: _gbc.gridx = 0;
155: addJComponent(_pnlLabelOrProgress);
156:
157: // Any other components are on the right.
158: _gbc.weightx = 0.0;
159: _gbc.anchor = GridBagConstraints.CENTER;
160: _gbc.gridx = GridBagConstraints.RELATIVE;
161: }
162:
163: private void updateSubcomponentsFont(Container cont) {
164: Component[] comps = cont.getComponents();
165: for (int i = 0; i < comps.length; ++i) {
166: comps[i].setFont(_font);
167: if (comps[i] instanceof Container) {
168: updateSubcomponentsFont((Container) comps[i]);
169: }
170: }
171: }
172:
173: public void setStatusBarProgress(String msg, int minimum,
174: int maximum, int value) {
175: if (false == _pnlLabelOrProgress.getComponent(0) instanceof JProgressBar) {
176: _pnlLabelOrProgress.remove(0);
177: _pnlLabelOrProgress.add(_progressBar);
178: validate();
179: }
180:
181: _progressBar.setMinimum(minimum);
182: _progressBar.setMaximum(maximum);
183: _progressBar.setValue(value);
184:
185: if (null != msg) {
186: _progressBar.setString(msg);
187: } else {
188: _progressBar.setString("");
189: }
190: }
191:
192: public void setStatusBarProgressFinished() {
193: if (_pnlLabelOrProgress.getComponent(0) instanceof JProgressBar) {
194: _pnlLabelOrProgress.remove(0);
195: _pnlLabelOrProgress.add(_textLbl);
196: validate();
197: repaint();
198: }
199: }
200: }
|