JXStatusBar.java
上传用户:zhengdagz
上传日期:2014-03-06
资源大小:1956k
文件大小:6k
源码类别:

xml/soap/webservice

开发平台:

Java

  1. /*
  2.  * $Id: JXStatusBar.java,v 1.2 2005/10/10 18:01:52 rbair Exp $
  3.  *
  4.  * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
  5.  * Santa Clara, California 95054, U.S.A. All rights reserved.
  6.  *
  7.  * This library is free software; you can redistribute it and/or
  8.  * modify it under the terms of the GNU Lesser General Public
  9.  * License as published by the Free Software Foundation; either
  10.  * version 2.1 of the License, or (at your option) any later version.
  11.  * 
  12.  * This library is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15.  * Lesser General Public License for more details.
  16.  * 
  17.  * You should have received a copy of the GNU Lesser General Public
  18.  * License along with this library; if not, write to the Free Software
  19.  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  20.  */
  21. package org.jdesktop.swingx;
  22. import java.awt.Dimension;
  23. import java.awt.Font;
  24. import java.awt.FontMetrics;
  25. import java.util.logging.Level;
  26. import javax.swing.BorderFactory;
  27. import javax.swing.Box;
  28. import javax.swing.BoxLayout;
  29. import javax.swing.JLabel;
  30. import javax.swing.JPanel;
  31. import javax.swing.JProgressBar;
  32. import javax.swing.SwingConstants;
  33. import org.jdesktop.swingx.event.MessageEvent;
  34. import org.jdesktop.swingx.event.MessageListener;
  35. import org.jdesktop.swingx.event.ProgressEvent;
  36. import org.jdesktop.swingx.event.ProgressListener;
  37. /**
  38.  * A component which is a container for displaying messages. There
  39.  * are several regions in which information about the running application
  40.  * may be placed.
  41.  * <p>
  42.  * You may set the messages directly using <code>setText</code>,
  43.  * <code>setTrailingMessage</code> or <code>setLeadingMessage</code>.
  44.  * Alternatively, you can register the status bar as a
  45.  * <code>MessageListener</code> on a <code>MessageSource</code>
  46.  * and messages will be placed according to type.
  47.  *
  48.  * @author Mark Davidson
  49.  */
  50. public class JXStatusBar extends JPanel implements MessageListener,
  51.   ProgressListener {
  52.     private JLabel leadingLabel;
  53.     private JLabel trailingLabel;
  54.     private JProgressBar progressBar;
  55.     private Dimension preferredSize;
  56.     public JXStatusBar() {
  57.         super();
  58. setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
  59.         setBorder(BorderFactory.createLoweredBevelBorder());
  60. leadingLabel = (JLabel) add(new JLabel("", SwingConstants.LEADING));
  61. add(Box.createHorizontalGlue());
  62. progressBar = (JProgressBar)add(new JProgressBar());
  63. progressBar.setVisible(false);
  64. trailingLabel = (JLabel) add(new JLabel("", SwingConstants.TRAILING));
  65.         Font font = leadingLabel.getFont().deriveFont(Font.PLAIN);
  66.         leadingLabel.setFont(font);
  67.         trailingLabel.setFont(font);
  68. this.setFont(font);
  69.         preferredSize = new Dimension(getWidth("    "), 2 * getFontHeight());
  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 messageText) {
  77. setLeadingMessage(messageText);
  78.     }
  79.     public String getText() {
  80. return getLeadingMessage();
  81.     }
  82.     /**
  83.      * Places the message in the leading area.
  84.      *
  85.      * @param messageText the text to place
  86.      */
  87.     public void setLeadingMessage(String messageText) {
  88. leadingLabel.setText(messageText);
  89.     }
  90.     public String getLeadingMessage() {
  91. return leadingLabel.getText();
  92.     }
  93.     /**
  94.      * Places the message in the trailing area.
  95.      *
  96.      * @param messageText the text to place
  97.      */
  98.     public void setTrailingMessage(String messageText) {
  99. trailingLabel.setText(messageText);
  100.     }
  101.     public String getTrailingMessage() {
  102. return trailingLabel.getText();
  103.     }
  104.     /**
  105.      * Returns the string width
  106.      * @param s the string
  107.      * @return the string width
  108.      */
  109.     protected int getWidth(String s) {
  110. FontMetrics fm = this.getFontMetrics(this.getFont());
  111. if (fm == null) {
  112.     return 0;
  113. }
  114. return fm.stringWidth(s);
  115.     }
  116.     /**
  117.      * Returns the height of a line of text
  118.      * @return the height of a line of text
  119.      */
  120.     protected int getFontHeight() {
  121. FontMetrics fm = this.getFontMetrics(this.getFont());
  122. if (fm == null) {
  123.     return 0;
  124. }
  125. return fm.getHeight();
  126.     }
  127.     /**
  128.      * Returns the perferred size
  129.      * @return the preferred size
  130.      */
  131.     public Dimension getPreferredSize() {
  132. return preferredSize;
  133.     }
  134.     /**
  135.      * MessageListener implementation. This handles many of the message types.
  136.      */
  137.     public void message(MessageEvent evt) {
  138. Level level = evt.getLevel();
  139. if (level == Level.FINE) {
  140.     // transient messages are sent to the leading label.
  141.     setLeadingMessage(evt.getMessage());
  142. }
  143. else /*if (level == Level.INFO)*/ {
  144.     // persisent messages are sent to the trailing label.
  145.     setTrailingMessage(evt.getMessage());
  146. }
  147. // Message categories like SEVERE, WARNING and exceptions should
  148. // probably be logged. Perhap even INFO messages should be passed to
  149. // to the logger.
  150. /*
  151. Throwable t = evt.getThrowable();
  152. if (t != null) {
  153.     // how do we want to handle exceptions?
  154. }
  155. */
  156.     }
  157.     // ProgressListener implementation
  158.     /**
  159.      * Indicates the begining of a long operation.
  160.      */
  161.     public void progressStarted(ProgressEvent evt) {
  162. // Set up the progress bar to handle a new progress event.
  163. boolean indeterminate = evt.isIndeterminate();
  164. progressBar.setIndeterminate(indeterminate);
  165. if (indeterminate == false) {
  166.     progressBar.setValue(evt.getMinimum());
  167.     progressBar.setMinimum(evt.getMinimum());
  168.     progressBar.setMaximum(evt.getMaximum());
  169. }
  170. progressBar.setVisible(true);
  171.     }
  172.     /**
  173.      * Handles a ProgressEvent
  174.      */
  175.     public void progressIncremented(ProgressEvent evt) {
  176. progressBar.setValue(evt.getProgress());
  177.     }
  178.     /**
  179.      * Indicates the end of a long operation.
  180.      */
  181.     public void progressEnded(ProgressEvent evt) {
  182. progressBar.setVisible(false);
  183.     }
  184. }