1 22 23 package houston.swing; 24 25 import java.awt.*; 26 import java.awt.datatransfer.*; 27 import java.awt.event.*; 28 import java.io.*; 29 import java.util.*; 30 import javax.swing.*; 31 import javax.swing.table.*; 32 import javax.swing.text.*; 33 import javax.swing.text.html.*; 34 import houston.*; 35 36 public class HtmlStatusPanel extends JPanel implements StatusListener 37 { 38 private final static String END_MARKER_ID = "201"; 39 static Logger T = Logger.getLogger( HtmlStatusPanel.class ); 40 private JEditorPane _browser; 41 42 private int _counter = 0; 43 private JScrollPane _scrollPane; 44 45 public HtmlStatusPanel() 46 { 47 setLayout( new BorderLayout() ); 48 setBorder( BorderFactory.createEtchedBorder() ); 49 50 _browser = new JEditorPane(); 51 _browser.setEditable( false ); 52 _browser.setContentType( "text/html" ); 53 54 clear(); 56 57 Status.addListener( this ); 58 59 _scrollPane = new JScrollPane( _browser ); 60 add( _scrollPane, BorderLayout.CENTER ); 61 } 62 63 public void clear() 64 { 65 67 SwingUtilities.invokeLater( 68 new Runnable () 69 { 70 public void run() 71 { 72 String html = "<html><head></head><body><table border=0><tr><td><p id=\"" + END_MARKER_ID + "\"></td></tr></table></body></html>"; 73 _browser.setText( html ); 74 T.debug( "html=" + html ); 75 76 80 } 81 } ); 82 } 83 84 public void error( String msg ) 85 { 86 Toolkit.getDefaultToolkit().beep(); 87 message( "<font color=red>" + msg + "</font>" ); 88 T.error( msg ); 89 } 90 91 public void fatal( String msg ) 92 { 93 Toolkit.getDefaultToolkit().beep(); 94 message( "<font color=red>" + msg + "</font>" ); 95 T.fatal( msg ); 96 } 97 98 102 public void hint( String msg ) 103 { 104 message( "<font color=grey>" + msg + "</font>" ); 105 T.debug( msg ); 106 } 107 108 public void info( String msg ) 109 { 110 message( msg ); 111 T.debug( msg ); 112 } 113 114 public void info( int level, String msg ) 115 { 116 message( level, msg ); 117 T.debug( msg ); 118 } 119 120 public void warning( String msg ) 121 { 122 message( "<font color=#ff8429>" + msg + "</font>" ); 123 T.warning( msg ); 124 } 125 126 private void insertHtml( final String html ) 127 { 128 SwingUtilities.invokeLater( 129 new Runnable () 130 { 131 public void run() 132 { 133 try 134 { 135 HTMLDocument htmlDoc = ( HTMLDocument ) _browser.getDocument(); 136 Element endMarker = htmlDoc.getElement( END_MARKER_ID ); 137 if( endMarker == null ) 138 { 139 T.error( "endMarker not found" ); 140 } 141 htmlDoc.insertBeforeEnd( endMarker, html ); 142 } 143 catch( Exception ioex ) 144 { 145 T.error( "insertBeforeEnd failed: " + ioex.toString() ); 146 } 147 } 148 } ); 149 } 150 151 private void message( int level, String msg ) 152 { 153 _counter++; 154 155 String line = "<b>" + msg + "</b><br>"; 157 158 insertHtml( line ); 159 } 160 161 private void message( String msg ) 162 { 163 _counter++; 164 165 String line = msg + "<br>"; 167 168 insertHtml( line ); 169 } 170 171 } 172 | Popular Tags |