001/* ----------------------------------------------------------------------------
002   The Kiwi Toolkit - A Java Class Library
003   Copyright (C) 1998-2004 Mark A. Lindner
004
005   This library is free software; you can redistribute it and/or
006   modify it under the terms of the GNU General Public License as
007   published by the Free Software Foundation; either version 2 of the
008   License, or (at your option) any later version.
009
010   This library is distributed in the hope that it will be useful,
011   but WITHOUT ANY WARRANTY; without even the implied warranty of
012   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013   General Public License for more details.
014
015   You should have received a copy of the GNU General Public License
016   along with this library; if not, write to the Free Software
017   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
018   02111-1307, USA.
019 
020   The author may be contacted at: mark_a_lindner@yahoo.com
021   ----------------------------------------------------------------------------
022   $Log: StatusBar.java,v $
023   Revision 1.11  2004/05/14 00:14:23  markl
024   use JProgressBar for meter
025
026   Revision 1.10  2003/01/19 09:50:54  markl
027   Javadoc & comment header updates.
028
029   Revision 1.9  2001/03/12 09:28:00  markl
030   Source code and Javadoc cleanup.
031
032   Revision 1.8  1999/06/28 08:17:08  markl
033   Fixed font.
034
035   Revision 1.7  1999/04/25 04:13:08  markl
036   Fixed opacity bug.
037
038   Revision 1.6  1999/04/23 07:40:24  markl
039   Added synchronization, fixed doc comment.
040
041   Revision 1.5  1999/04/23 07:25:45  markl
042   Fixed layout problem, added support for adding additional components to the
043   status bar.
044
045   Revision 1.4  1999/04/19 05:30:45  markl
046   Added support for expiring messages.
047
048   Revision 1.3  1999/02/09 05:31:32  markl
049   Used softer border; only created one instance of it.
050
051   Revision 1.2  1999/01/10 03:00:07  markl
052   added GPL header & RCS tag
053   ----------------------------------------------------------------------------
054*/
055
056package kiwi.ui;
057
058import java.awt.*;
059import java.awt.event.*;
060import javax.swing.*;
061import javax.swing.border.*;
062
063import kiwi.util.*;
064
065/** This class represents a status bar that includes a message area
066  * and an optional progress meter. Status bars are typically placed
067  * at the bottom of an application window. Status messages disappear
068  * after a fixed number of seconds, unless they are specified as
069  * non-expiring; the delay is adjustable.
070  *
071  * <p><center>
072  * <img src="snapshot/StatusBar.gif"><br>
073  * <i>An example StatusBar.</i>
074  * </center>
075  *
076  * @author Mark Lindner
077  */
078
079public class StatusBar extends KPanel implements ProgressObserver,
080  ActionListener
081  {
082  private JTextField label = null;
083  private JProgressBar meter;
084  private boolean labelOnly;
085  private static final Border border
086    = new SoftBevelBorder(SoftBevelBorder.LOWERED);
087  private Timer timer;
088  private GridBagConstraints gbc;
089  private static final Insets insets = new Insets(0, 2, 0, 0);
090  
091  /** Construct a new <code>StatusBar</code>. Constructs a new status bar
092   * without a progress meter.
093   */
094
095  public StatusBar()
096    {
097    this(false);
098    }
099
100  /** Construct a new <code>StatusBar</code>.
101   *
102   * @param showMeter A flag specifying whether the status bar should include
103   * a progress meter.
104   */
105
106  public StatusBar(boolean showMeter)
107    {
108    timer = new Timer(10000, this);
109    
110    GridBagLayout gb = new GridBagLayout();
111    setLayout(gb);
112
113    gbc = new GridBagConstraints();
114    gbc.weighty = gbc.weightx = 1;
115    gbc.fill = gbc.BOTH;
116    gbc.ipady = 0;
117    gbc.ipadx = 2;
118
119    label = new JTextField();
120    label.setFont(KiwiUtils.boldFont);
121    label.setHighlighter(null);
122    label.setEditable(false);
123    label.setForeground(Color.black);
124    label.setOpaque(false);
125    label.setBorder(border);
126    add(label, gbc);
127
128    gbc.ipadx = 0;
129    gbc.fill = gbc.VERTICAL;
130    gbc.weightx = 0;
131    gbc.insets = insets;
132    
133    if(showMeter)
134      addStatusComponent(meter = new JProgressBar(0, 100));
135
136    // super.setOpaque(true);
137    }
138
139  /** Remove a component from the status bar. The specified component will be
140   * removed from the status bar. The text status field and the progress meter
141   * (if present) cannot be removed.
142   *
143   * @param c The component to remove.
144   * @exception java.lang.IllegalArgumentException If an attempt was made to
145   * remove the text status field or tje progress meter.
146   */
147  
148  public void removeStatusComponent(JComponent c)
149    {
150    if((c == label) || ((meter != null) && (c == meter)))
151      throw(new IllegalArgumentException("Cannot remove label or meter."));
152    
153    remove(c);
154    }
155
156  /** Add a component to the status bar. The new component will be fitted with
157   * a beveled border and made transparent to match the other components in the
158   * status bar, and will be added at the right end of the status bar.
159   *
160   * @param c The component to add.
161   */
162  
163  public void addStatusComponent(JComponent c)
164    {
165    c.setBorder(border);
166    c.setOpaque(false);
167    add(c, gbc);
168    }
169  
170  /** Start or stop the slider. Starts or stops the progress meter's slider.
171   * If this status bar was created without a progress meter, this method will
172   * have no effect.
173   *
174   * @param flag A flag specifying whether the slider should be started or
175   * stopped.
176   */
177
178  public synchronized void setBusy(boolean flag)
179    {
180    if(meter != null)
181      meter.setIndeterminate(flag);
182    }
183
184  /** Determine if the meter slider is currently active.
185   *
186   * @return <code>true</code> if the slider is active, and <code>false</code>
187   * otherwise.
188   */
189  
190  public synchronized boolean isBusy()
191    {
192    return((meter == null) ? false : meter.isIndeterminate());
193    }
194  
195  /** Set the message font. Sets the font for the text in the status bar.
196   *
197   * @param font The new font.
198   */
199
200  public void setFont(Font font)
201    {
202    if(label != null)
203      label.setFont(font);
204    }
205
206  /** Set the text color. Sets the color of the text displayed in the status
207   * bar.
208   *
209   * @param color The text new color.
210   */
211
212  public void setTextColor(Color color)
213    {
214    label.setForeground(color);
215    }
216
217  /** Set the meter color.
218   *
219   * @param color The new forground color for the progress meter.
220   */
221
222  public void setMeterColor(Color color)
223    {
224    meter.setForeground(color);
225    }
226
227  /** Set a percentage on the meter. The <code>percent</code> value, which must
228   * be between 0 and 100 inclusive, specifies how much of the meter should be
229   * filled in with the foreground color.
230   *
231   * @param percent The percentage, a value between 0 and 100 inclusive. If
232   * the value is out of range, it is clipped.
233   */
234
235  public void setProgress(int percent)
236    {
237    meter.setValue(percent);
238    }
239
240  /** Set the text to be displayed in the status bar. The status bar will be
241   * cleared when the delay expires.
242   *
243   * @param text The text to display in the status bar.
244   */
245
246  public void setText(String text)
247    {
248    setText(text, true);
249    }
250  
251  /** Set the text to be displayed in the status bar.
252   *
253   * @param text The text to display in the status bar.
254   * @param expires A flag specifying whether the message "expires." If
255   * <code>true</code>, the status bar will be cleared after the message has
256   * been in the status bar for a specified number of seconds. The default
257   * delay is 10 seconds, but can be adjusted via the <code>setDelay()</code>
258   * method.
259   *
260   * @see #setDelay
261   */
262  
263  public synchronized void setText(String text, boolean expires)
264    {
265    if(text == null) text = "";
266    label.setText(text);
267
268    if(expires)
269      {
270      if(timer.isRunning())
271        timer.restart();
272      else
273        timer.start();
274      }
275    else
276      {
277      if(timer.isRunning())
278        timer.stop();
279      }
280    }
281
282  /** Set the delay on status bar messages.
283   *
284   * @param seconds The number of seconds before a message disappears from the
285   * status bar.
286   */
287  
288  public synchronized void setDelay(int seconds)
289    {
290    timer.setDelay(seconds * 1000);
291    }
292  
293  /** This method is public as an implementation side-effect. */
294
295  public void actionPerformed(ActionEvent evt)
296    {
297    setText(null);
298    }
299
300  }
301
302/* end of source file */