1 22 23 package houston.swing; 24 25 import java.awt.*; 26 import javax.swing.*; 27 import javax.swing.text.*; 28 import houston.*; 29 30 public class StatusPanel extends JPanel implements StatusListener 31 { 32 static Logger T = Logger.getLogger( StatusPanel.class ); 33 34 private SimpleAttributeSet _errorStyle; 35 36 private JTextPane _output; 37 private SimpleAttributeSet _plainStyle; 38 private SimpleAttributeSet _warningStyle; 39 40 public StatusPanel() 41 { 42 setLayout( new BorderLayout() ); 43 setBorder( BorderFactory.createEtchedBorder() ); 44 45 _output = new JTextPane(); 46 _output.setEditable( false ); 47 48 _errorStyle = new SimpleAttributeSet(); 49 StyleConstants.setForeground( _errorStyle, Color.red ); 50 StyleConstants.setBold( _errorStyle, true ); 51 52 _warningStyle = new SimpleAttributeSet(); 53 StyleConstants.setForeground( _warningStyle, Color.orange ); 54 StyleConstants.setBold( _warningStyle, true ); 55 56 _plainStyle = new SimpleAttributeSet(); 57 StyleConstants.setForeground( _plainStyle, Color.black ); 58 StyleConstants.setBold( _plainStyle, false ); 59 60 Status.addListener( this ); 61 62 add( new JScrollPane( _output ), BorderLayout.CENTER ); 63 } 64 65 public void clear() 66 { 67 _output.setText( "" ); 68 } 69 70 public void error( String msg ) 71 { 72 Toolkit.getDefaultToolkit().beep(); 73 appendText( msg, _errorStyle ); 74 } 75 76 public void fatal( String msg ) 77 { 78 Toolkit.getDefaultToolkit().beep(); 79 appendText( msg, _errorStyle ); 80 } 81 82 public void hint( String msg ) 83 { 84 appendText( msg, _plainStyle ); 85 } 86 87 public void info( String msg ) 88 { 89 appendText( msg, _plainStyle ); 90 } 91 92 public void info( int level, String msg ) 93 { 94 appendText( msg, _plainStyle ); 95 } 96 97 public void warning( String msg ) 98 { 99 appendText( msg, _warningStyle ); 100 } 101 102 103 private void appendText( String text, AttributeSet style ) 104 { 105 Document doc = _output.getDocument(); 106 text += "\n"; 107 try 108 { 109 doc.insertString( doc.getLength(), text, style ); 110 } 111 catch( BadLocationException bex ) 112 { 113 T.error( "*** failed to append status message: " + bex.toString() ); 114 } 115 _output.setCaretPosition( doc.getLength() ); 116 } 117 118 } 119 | Popular Tags |