KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jdesktop > swing > JXStatusBar


1 /*
2  * $Id: JXStatusBar.java,v 1.1 2004/07/28 21:21:10 aim Exp $
3  *
4  * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
5  * Santa Clara, California 95054, U.S.A. All rights reserved.
6  */

7
8 package org.jdesktop.swing;
9
10 import java.awt.Dimension JavaDoc;
11 import java.awt.Font JavaDoc;
12 import java.awt.FontMetrics JavaDoc;
13
14 import java.util.logging.Level JavaDoc;
15
16 import javax.swing.BorderFactory JavaDoc;
17 import javax.swing.Box JavaDoc;
18 import javax.swing.BoxLayout JavaDoc;
19 import javax.swing.JLabel JavaDoc;
20 import javax.swing.JPanel JavaDoc;
21 import javax.swing.JProgressBar JavaDoc;
22 import javax.swing.SwingConstants JavaDoc;
23
24 import org.jdesktop.swing.event.MessageEvent;
25 import org.jdesktop.swing.event.MessageListener;
26 import org.jdesktop.swing.event.ProgressEvent;
27 import org.jdesktop.swing.event.ProgressListener;
28
29 /**
30  * A component which is a container for displaying messages. There
31  * are several regions in which information about the running application
32  * may be placed.
33  * <p>
34  * You may set the messages directly using <code>setText</code>,
35  * <code>setTrailingMessage</code> or <code>setLeadingMessage</code>.
36  * Alternatively, you can register the status bar as a
37  * <code>MessageListener</code> on a <code>MessageSource</code>
38  * and messages will be placed according to type.
39  *
40  * @author Mark Davidson
41  */

42 public class JXStatusBar extends JPanel JavaDoc implements MessageListener,
43                           ProgressListener {
44     private JLabel JavaDoc leadingLabel;
45     private JLabel JavaDoc trailingLabel;
46     private JProgressBar JavaDoc progressBar;
47
48     private Dimension JavaDoc preferredSize;
49
50     public JXStatusBar() {
51         super();
52     setLayout(new BoxLayout JavaDoc(this, BoxLayout.X_AXIS));
53         setBorder(BorderFactory.createLoweredBevelBorder());
54
55     leadingLabel = (JLabel JavaDoc) add(new JLabel JavaDoc("", SwingConstants.LEADING));
56     add(Box.createHorizontalGlue());
57
58     progressBar = (JProgressBar JavaDoc)add(new JProgressBar JavaDoc());
59     progressBar.setVisible(false);
60
61     trailingLabel = (JLabel JavaDoc) add(new JLabel JavaDoc("", SwingConstants.TRAILING));
62
63         Font JavaDoc font = leadingLabel.getFont().deriveFont(Font.PLAIN);
64         leadingLabel.setFont(font);
65         trailingLabel.setFont(font);
66     this.setFont(font);
67
68         preferredSize = new Dimension JavaDoc(getWidth(" "), 2 * getFontHeight());
69     }
70
71     /**
72      * Sets non-transient message text in leading message position on
73      * status bar.
74      * @param messageText the message to display on the status bar
75      */

76     public void setText(String JavaDoc messageText) {
77     setLeadingMessage(messageText);
78     }
79
80     public String JavaDoc getText() {
81     return getLeadingMessage();
82     }
83
84     /**
85      * Places the message in the leading area.
86      *
87      * @param messageText the text to place
88      */

89     public void setLeadingMessage(String JavaDoc messageText) {
90     leadingLabel.setText(messageText);
91     }
92
93     public String JavaDoc getLeadingMessage() {
94     return leadingLabel.getText();
95     }
96
97     /**
98      * Places the message in the trailing area.
99      *
100      * @param messageText the text to place
101      */

102     public void setTrailingMessage(String JavaDoc messageText) {
103     trailingLabel.setText(messageText);
104     }
105
106     public String JavaDoc getTrailingMessage() {
107     return trailingLabel.getText();
108     }
109
110
111     /**
112      * Returns the string width
113      * @param s the string
114      * @return the string width
115      */

116     protected int getWidth(String JavaDoc s) {
117     FontMetrics JavaDoc fm = this.getFontMetrics(this.getFont());
118     if (fm == null) {
119         return 0;
120     }
121     return fm.stringWidth(s);
122     }
123
124     /**
125      * Returns the height of a line of text
126      * @return the height of a line of text
127      */

128     protected int getFontHeight() {
129     FontMetrics JavaDoc fm = this.getFontMetrics(this.getFont());
130     if (fm == null) {
131         return 0;
132     }
133     return fm.getHeight();
134     }
135
136     /**
137      * Returns the perferred size
138      * @return the preferred size
139      */

140     public Dimension JavaDoc getPreferredSize() {
141     return preferredSize;
142     }
143
144     /**
145      * MessageListener implementation. This handles many of the message types.
146      */

147     public void message(MessageEvent evt) {
148     Level JavaDoc level = evt.getLevel();
149
150     if (level == Level.FINE) {
151         // transient messages are sent to the leading label.
152
setLeadingMessage(evt.getMessage());
153     }
154     else /*if (level == Level.INFO)*/ {
155         // persisent messages are sent to the trailing label.
156
setTrailingMessage(evt.getMessage());
157     }
158
159     // Message categories like SEVERE, WARNING and exceptions should
160
// probably be logged. Perhap even INFO messages should be passed to
161
// to the logger.
162
/*
163     Throwable t = evt.getThrowable();
164     if (t != null) {
165         // how do we want to handle exceptions?
166     }
167     */

168     }
169
170     // ProgressListener implementation
171

172     /**
173      * Indicates the begining of a long operation.
174      */

175     public void progressStarted(ProgressEvent evt) {
176     // Set up the progress bar to handle a new progress event.
177
boolean indeterminate = evt.isIndeterminate();
178     progressBar.setIndeterminate(indeterminate);
179     if (indeterminate == false) {
180         progressBar.setValue(evt.getMinimum());
181         progressBar.setMinimum(evt.getMinimum());
182         progressBar.setMaximum(evt.getMaximum());
183     }
184     progressBar.setVisible(true);
185     }
186
187     /**
188      * Handles a ProgressEvent
189      */

190     public void progressIncremented(ProgressEvent evt) {
191     progressBar.setValue(evt.getProgress());
192     }
193
194     /**
195      * Indicates the end of a long operation.
196      */

197     public void progressEnded(ProgressEvent evt) {
198     progressBar.setVisible(false);
199     }
200 }
201
Popular Tags