View Javadoc

1   /*
2    *  XNap - A P2P framework and client.
3    *
4    *  See the file AUTHORS for copyright information.
5    *
6    *  This program is free software; you can redistribute it and/or modify
7    *  it under the terms of the GNU General Public License as published by
8    *  the Free Software Foundation.
9    *
10   *  This program is distributed in the hope that it will be useful,
11   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   *  GNU General Public License for more details.
14   *
15   *  You should have received a copy of the GNU General Public License
16   *  along with this program; if not, write to the Free Software
17   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18   */
19  
20  package org.xnap.gui;
21  
22  import java.awt.Color;
23  
24  import javax.swing.Icon;
25  import javax.swing.JLabel;
26  import javax.swing.border.EmptyBorder;
27  
28  import org.xnap.event.StatusListener;
29  import org.xnap.gui.util.IconHelper;
30  import org.xnap.util.Scheduler;
31  
32  /***
33   * Provides a little panel that can dock into the status bar.
34   *
35   * @see StatusBar
36   */
37  public class StatusPanel extends JLabel implements StatusListener
38  {
39  
40      //--- Constant(s) ---
41  
42      public static Icon ICON_ERROR = IconHelper.getStatusBarIcon("flag.png"); 
43      
44      public static Icon ICON_OK = IconHelper.getStatusBarIcon("ok.png");
45  
46      //--- Data field(s) ---
47  
48      private long defaultTimeout;
49  
50  	private String description;
51  
52      //--- Constructor(s) ---
53  
54      /***
55       * Sets up a <code>JPanel</code> containing a <code>JLabel</code>.
56       * This label is then used to display status messages produced by the
57       * application. 
58       *
59       * @param defaultTimeout Each message will be displayed this long
60       */
61      public StatusPanel(long defaultTimeout)
62      {
63  		this.defaultTimeout = defaultTimeout;
64  
65  		setBorder(new EmptyBorder(2, 5, 2, 5));
66  		setForeground(Color.black);
67  
68  		clear();
69      }
70  
71      /***
72       * Constructs a new status panel that displays <code>icon</code> and
73       * does not have a timeout for status messsages.
74       */
75      public StatusPanel(Icon icon)
76      {
77  		this(-1);
78  
79  		setIcon(icon);
80      }
81  
82      /***
83       * Constructs a new status panel that does not have a timeout for 
84       * status messsages.
85       */
86      public StatusPanel()
87      {
88  		this(-1);
89      }
90  
91      //--- Method(s) ---
92      
93      /***
94       * Clear the current status message.
95       */
96      public void clear()
97      {
98  		super.setText(" ");
99  		super.setToolTipText(null);
100     }
101 
102 	public String getDescription()
103 	{
104 		return description;
105 	}
106 
107 	public void setDescription(String description)
108 	{
109 		this.description = description;
110 	}
111 
112     /***
113      * Invokes {@link #setText(String) setText(status)}.
114      */
115     public void setStatus(String status)
116     {
117 		setText(status);
118     }
119 
120     /***
121      * Sets the status message.
122      *
123      * @param status the message to be displayed.
124      * @param timeout the message will be cleared in this many milli seconds
125      */
126     public void setText(String status, long timeout) 
127     {
128         super.setText(status);
129 		super.setToolTipText(status);
130 
131 		if (timeout > 0) {
132 			Scheduler.run(timeout, new Cleaner());
133 		}
134     }
135 
136     /***
137      * Sets the status message.
138      *
139      * @param status the message to be displayed.
140      */
141     public void setText(String status) 
142     {
143 		setText(status, defaultTimeout);
144     }
145 
146     //--- Inner Class(es) ---
147     
148     private class Cleaner implements Runnable
149     {
150 
151 		public void run()
152 		{
153 			clear();
154 		}
155     } 
156 
157 }
158