001: /*
002: ** Houston - Status and Logging Toolkit
003: ** Copyright (c) 2001, 2002, 2003 by Gerald Bauer
004: **
005: ** This program is free software.
006: **
007: ** You may redistribute it and/or modify it under the terms of the GNU
008: ** Lesser General Public License as published by the Free Software Foundation.
009: ** Version 2.1 of the license should be included with this distribution in
010: ** the file LICENSE, as well as License.html. If the license is not
011: ** included with this distribution, you may find a copy at the FSF web
012: ** site at 'www.gnu.org' or 'www.fsf.org', or you may write to the
013: ** Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139 USA.
014: **
015: ** THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY OF ANY KIND,
016: ** NOT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY. THE AUTHOR
017: ** OF THIS SOFTWARE, ASSUMES _NO_ RESPONSIBILITY FOR ANY
018: ** CONSEQUENCE RESULTING FROM THE USE, MODIFICATION, OR
019: ** REDISTRIBUTION OF THIS SOFTWARE.
020: **
021: */
022:
023: package houston.swing;
024:
025: import java.awt.*;
026: import java.awt.datatransfer.*;
027: import java.awt.event.*;
028: import java.io.*;
029: import java.util.*;
030: import javax.swing.*;
031: import javax.swing.table.*;
032: import javax.swing.text.*;
033: import javax.swing.text.html.*;
034: import houston.*;
035:
036: public class HtmlStatusPanel extends JPanel implements StatusListener {
037: private final static String END_MARKER_ID = "201";
038: static Logger T = Logger.getLogger(HtmlStatusPanel.class);
039: private JEditorPane _browser;
040:
041: private int _counter = 0;
042: private JScrollPane _scrollPane;
043:
044: public HtmlStatusPanel() {
045: setLayout(new BorderLayout());
046: setBorder(BorderFactory.createEtchedBorder());
047:
048: _browser = new JEditorPane();
049: _browser.setEditable(false);
050: _browser.setContentType("text/html");
051:
052: // note: clear sets up JEditorPane
053: clear();
054:
055: Status.addListener(this );
056:
057: _scrollPane = new JScrollPane(_browser);
058: add(_scrollPane, BorderLayout.CENTER);
059: }
060:
061: public void clear() {
062: // todo: isn't setText supposed to be thread-safe?
063:
064: SwingUtilities.invokeLater(new Runnable() {
065: public void run() {
066: String html = "<html><head></head><body><table border=0><tr><td><p id=\""
067: + END_MARKER_ID
068: + "\"></td></tr></table></body></html>";
069: _browser.setText(html);
070: T.debug("html=" + html);
071:
072: /*
073: * HTMLDocument htmlDoc = (HTMLDocument) _browser.getDocument();
074: * htmlDoc.dump( System.out );
075: */
076: }
077: });
078: }
079:
080: public void error(String msg) {
081: Toolkit.getDefaultToolkit().beep();
082: message("<font color=red>" + msg + "</font>");
083: T.error(msg);
084: }
085:
086: public void fatal(String msg) {
087: Toolkit.getDefaultToolkit().beep();
088: message("<font color=red>" + msg + "</font>");
089: T.fatal(msg);
090: }
091:
092: //
093: // implementation of the StatusListener Interface
094: //
095:
096: public void hint(String msg) {
097: message("<font color=grey>" + msg + "</font>");
098: T.debug(msg);
099: }
100:
101: public void info(String msg) {
102: message(msg);
103: T.debug(msg);
104: }
105:
106: public void info(int level, String msg) {
107: message(level, msg);
108: T.debug(msg);
109: }
110:
111: public void warning(String msg) {
112: message("<font color=#ff8429>" + msg + "</font>");
113: T.warning(msg);
114: }
115:
116: private void insertHtml(final String html) {
117: SwingUtilities.invokeLater(new Runnable() {
118: public void run() {
119: try {
120: HTMLDocument htmlDoc = (HTMLDocument) _browser
121: .getDocument();
122: Element endMarker = htmlDoc
123: .getElement(END_MARKER_ID);
124: if (endMarker == null) {
125: T.error("endMarker not found");
126: }
127: htmlDoc.insertBeforeEnd(endMarker, html);
128: } catch (Exception ioex) {
129: T.error("insertBeforeEnd failed: "
130: + ioex.toString());
131: }
132: }
133: });
134: }
135:
136: private void message(int level, String msg) {
137: _counter++;
138:
139: // String line = "<h" + level + ">" + msg + "</h" + level + ">";
140: String line = "<b>" + msg + "</b><br>";
141:
142: insertHtml(line);
143: }
144:
145: private void message(String msg) {
146: _counter++;
147:
148: // String line = msg;
149: String line = msg + "<br>";
150:
151: insertHtml(line);
152: }
153:
154: }
155: // end of class
|