1 7 8 package org.jdesktop.swing; 9 10 import java.awt.Dimension ; 11 import java.awt.Font ; 12 import java.awt.FontMetrics ; 13 14 import java.util.logging.Level ; 15 16 import javax.swing.BorderFactory ; 17 import javax.swing.Box ; 18 import javax.swing.BoxLayout ; 19 import javax.swing.JLabel ; 20 import javax.swing.JPanel ; 21 import javax.swing.JProgressBar ; 22 import javax.swing.SwingConstants ; 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 42 public class JXStatusBar extends JPanel implements MessageListener, 43 ProgressListener { 44 private JLabel leadingLabel; 45 private JLabel trailingLabel; 46 private JProgressBar progressBar; 47 48 private Dimension preferredSize; 49 50 public JXStatusBar() { 51 super(); 52 setLayout(new BoxLayout (this, BoxLayout.X_AXIS)); 53 setBorder(BorderFactory.createLoweredBevelBorder()); 54 55 leadingLabel = (JLabel ) add(new JLabel ("", SwingConstants.LEADING)); 56 add(Box.createHorizontalGlue()); 57 58 progressBar = (JProgressBar )add(new JProgressBar ()); 59 progressBar.setVisible(false); 60 61 trailingLabel = (JLabel ) add(new JLabel ("", SwingConstants.TRAILING)); 62 63 Font font = leadingLabel.getFont().deriveFont(Font.PLAIN); 64 leadingLabel.setFont(font); 65 trailingLabel.setFont(font); 66 this.setFont(font); 67 68 preferredSize = new Dimension (getWidth(" "), 2 * getFontHeight()); 69 } 70 71 76 public void setText(String messageText) { 77 setLeadingMessage(messageText); 78 } 79 80 public String getText() { 81 return getLeadingMessage(); 82 } 83 84 89 public void setLeadingMessage(String messageText) { 90 leadingLabel.setText(messageText); 91 } 92 93 public String getLeadingMessage() { 94 return leadingLabel.getText(); 95 } 96 97 102 public void setTrailingMessage(String messageText) { 103 trailingLabel.setText(messageText); 104 } 105 106 public String getTrailingMessage() { 107 return trailingLabel.getText(); 108 } 109 110 111 116 protected int getWidth(String s) { 117 FontMetrics fm = this.getFontMetrics(this.getFont()); 118 if (fm == null) { 119 return 0; 120 } 121 return fm.stringWidth(s); 122 } 123 124 128 protected int getFontHeight() { 129 FontMetrics fm = this.getFontMetrics(this.getFont()); 130 if (fm == null) { 131 return 0; 132 } 133 return fm.getHeight(); 134 } 135 136 140 public Dimension getPreferredSize() { 141 return preferredSize; 142 } 143 144 147 public void message(MessageEvent evt) { 148 Level level = evt.getLevel(); 149 150 if (level == Level.FINE) { 151 setLeadingMessage(evt.getMessage()); 153 } 154 else { 155 setTrailingMessage(evt.getMessage()); 157 } 158 159 168 } 169 170 172 175 public void progressStarted(ProgressEvent evt) { 176 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 190 public void progressIncremented(ProgressEvent evt) { 191 progressBar.setValue(evt.getProgress()); 192 } 193 194 197 public void progressEnded(ProgressEvent evt) { 198 progressBar.setVisible(false); 199 } 200 } 201 | Popular Tags |