001 /* $Id: StatusBar.java 12 2007-07-27 09:48:53Z ringler $
002 * This class is part of the de.mospace.swing library.
003 * Copyright (C) 2005-2006 Moritz Ringler
004 *
005 * This program is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU General Public License
007 * as published by the Free Software Foundation; either version 2
008 * of the License, or (at your option) any later version.
009 *
010 * This program 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
013 * GNU General Public License for more details.
014 *
015 * You should have received a copy of the GNU General Public License
016 * along with this program; if not, write to the Free Software
017 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
018 */
019 package de.mospace.swing;
020
021 import java.awt.Color;
022 import java.awt.event.ActionEvent;
023 import java.awt.event.ActionListener;
024 import javax.swing.BorderFactory;
025 import javax.swing.JLabel;
026 import javax.swing.Timer;
027
028 /** A status bar that is automatically cleared after some time.
029 * @author Moritz Ringler
030 * @version $Revision: 12 $ ($Date: 2007-07-27 11:48:53 +0200 (Fr, 27 Jul 2007) $)
031 */
032 public class StatusBar extends JLabel{
033 private final Timer clearTimer;
034 private final ActionListener clearTimerActionListener;
035 private int defaultTimeout;
036 /** initial timeout */
037 final public static int INITIAL_TIMEOUT=1000;
038
039 /** Constructs a new instance of this class. Equivalent to
040 * StatusBar(StatusBar.INITIAL_TIMEOUT).
041 **/
042 public StatusBar(){
043 this(INITIAL_TIMEOUT);
044 }
045
046 /** Constructs a new instance of this class. Status bar messages
047 * are cleared after <code>timeout</code> milliseconds by default.
048 * @param timeout default timeout in milliseconds
049 **/
050 public StatusBar(int timeout){
051 this.setFont(new java.awt.Font("Dialogue", 0, 9));
052 this.setBackground(Color.white);
053 this.setOpaque(true);
054 this.setBorder(BorderFactory.createEtchedBorder());
055 this.setText(" ");
056 clearTimerActionListener = new ActionListener(){
057 public void actionPerformed(ActionEvent evt){
058 clear();
059 }
060 };
061 defaultTimeout=timeout;
062 clearTimer = new Timer(defaultTimeout,clearTimerActionListener);
063 clearTimer.setRepeats(false);
064 }
065
066 /** Shows the spefied message in the status bar for
067 * the time specified by {@link #setDefaultTimeout(int timeout)}.
068 * @param status message that will be displayed
069 */
070 public void setStatus(String status){
071 if(clearTimer.isRunning()){
072 clearTimer.stop();
073 }
074 this.setText(status);
075 clearTimer.setDelay(defaultTimeout);
076 clearTimer.start();
077 }
078
079 /**
080 * Shows the specified message and clears the status bar after
081 * the specified delay.
082 * @param status message that will be displayed
083 * @param timeout time the message will remain visible (milliseconds)
084 */
085 public void setStatus(String status, int timeout){
086 if(clearTimer.isRunning()){
087 clearTimer.stop();
088 }
089 this.setText(status);
090 if (timeout>0){
091 clearTimer.setDelay(timeout);
092 clearTimer.start();
093 }
094 }
095
096 /** Sets the default time a status bar message remains visible.
097 * @param timeout time in milliseconds
098 */
099 public void setDefaultTimeout(int timeout){
100 defaultTimeout = timeout;
101 }
102
103 /** Clears the status bar. */
104 public void clear(){
105 this.setText(" ");
106 }
107 }