KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > houston > swing > HtmlStatusPanel


1 /*
2 ** Houston - Status and Logging Toolkit
3 ** Copyright (c) 2001, 2002, 2003 by Gerald Bauer
4 **
5 ** This program is free software.
6 **
7 ** You may redistribute it and/or modify it under the terms of the GNU
8 ** Lesser General Public License as published by the Free Software Foundation.
9 ** Version 2.1 of the license should be included with this distribution in
10 ** the file LICENSE, as well as License.html. If the license is not
11 ** included with this distribution, you may find a copy at the FSF web
12 ** site at 'www.gnu.org' or 'www.fsf.org', or you may write to the
13 ** Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139 USA.
14 **
15 ** THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY OF ANY KIND,
16 ** NOT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY. THE AUTHOR
17 ** OF THIS SOFTWARE, ASSUMES _NO_ RESPONSIBILITY FOR ANY
18 ** CONSEQUENCE RESULTING FROM THE USE, MODIFICATION, OR
19 ** REDISTRIBUTION OF THIS SOFTWARE.
20 **
21 */

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 JavaDoc 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       // note: clear sets up JEditorPane
55
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       // todo: isn't setText supposed to be thread-safe?
66

67       SwingUtilities.invokeLater(
68          new Runnable JavaDoc()
69          {
70             public void run()
71             {
72                String JavaDoc 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                /*
77                 * HTMLDocument htmlDoc = (HTMLDocument) _browser.getDocument();
78                 * htmlDoc.dump( System.out );
79                 */

80             }
81          } );
82    }
83
84    public void error( String JavaDoc msg )
85    {
86       Toolkit.getDefaultToolkit().beep();
87       message( "<font color=red>" + msg + "</font>" );
88       T.error( msg );
89    }
90
91    public void fatal( String JavaDoc msg )
92    {
93       Toolkit.getDefaultToolkit().beep();
94       message( "<font color=red>" + msg + "</font>" );
95       T.fatal( msg );
96    }
97
98 //
99
// implementation of the StatusListener Interface
100
//
101

102    public void hint( String JavaDoc msg )
103    {
104       message( "<font color=grey>" + msg + "</font>" );
105       T.debug( msg );
106    }
107
108    public void info( String JavaDoc msg )
109    {
110       message( msg );
111       T.debug( msg );
112    }
113
114    public void info( int level, String JavaDoc msg )
115    {
116       message( level, msg );
117       T.debug( msg );
118    }
119
120    public void warning( String JavaDoc msg )
121    {
122       message( "<font color=#ff8429>" + msg + "</font>" );
123       T.warning( msg );
124    }
125
126    private void insertHtml( final String JavaDoc html )
127    {
128       SwingUtilities.invokeLater(
129          new Runnable JavaDoc()
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 JavaDoc ioex )
144                {
145                   T.error( "insertBeforeEnd failed: " + ioex.toString() );
146                }
147             }
148          } );
149    }
150
151    private void message( int level, String JavaDoc msg )
152    {
153       _counter++;
154
155       // String line = "<h" + level + ">" + msg + "</h" + level + ">";
156
String JavaDoc line = "<b>" + msg + "</b><br>";
157
158       insertHtml( line );
159    }
160
161    private void message( String JavaDoc msg )
162    {
163       _counter++;
164
165       // String line = msg;
166
String JavaDoc line = msg + "<br>";
167
168       insertHtml( line );
169    }
170
171 }
172 // end of class
173
Popular Tags