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 javax.swing.*;
027: import javax.swing.text.*;
028: import houston.*;
029:
030: public class StatusPanel extends JPanel implements StatusListener {
031: static Logger T = Logger.getLogger(StatusPanel.class);
032:
033: private SimpleAttributeSet _errorStyle;
034:
035: private JTextPane _output;
036: private SimpleAttributeSet _plainStyle;
037: private SimpleAttributeSet _warningStyle;
038:
039: public StatusPanel() {
040: setLayout(new BorderLayout());
041: setBorder(BorderFactory.createEtchedBorder());
042:
043: _output = new JTextPane();
044: _output.setEditable(false);
045:
046: _errorStyle = new SimpleAttributeSet();
047: StyleConstants.setForeground(_errorStyle, Color.red);
048: StyleConstants.setBold(_errorStyle, true);
049:
050: _warningStyle = new SimpleAttributeSet();
051: StyleConstants.setForeground(_warningStyle, Color.orange);
052: StyleConstants.setBold(_warningStyle, true);
053:
054: _plainStyle = new SimpleAttributeSet();
055: StyleConstants.setForeground(_plainStyle, Color.black);
056: StyleConstants.setBold(_plainStyle, false);
057:
058: Status.addListener(this );
059:
060: add(new JScrollPane(_output), BorderLayout.CENTER);
061: }
062:
063: public void clear() {
064: _output.setText("");
065: }
066:
067: public void error(String msg) {
068: Toolkit.getDefaultToolkit().beep();
069: appendText(msg, _errorStyle);
070: }
071:
072: public void fatal(String msg) {
073: Toolkit.getDefaultToolkit().beep();
074: appendText(msg, _errorStyle);
075: }
076:
077: public void hint(String msg) {
078: appendText(msg, _plainStyle);
079: }
080:
081: public void info(String msg) {
082: appendText(msg, _plainStyle);
083: }
084:
085: public void info(int level, String msg) {
086: appendText(msg, _plainStyle);
087: }
088:
089: public void warning(String msg) {
090: appendText(msg, _warningStyle);
091: }
092:
093: private void appendText(String text, AttributeSet style) {
094: Document doc = _output.getDocument();
095: text += "\n";
096: try {
097: doc.insertString(doc.getLength(), text, style);
098: } catch (BadLocationException bex) {
099: T.error("*** failed to append status message: "
100: + bex.toString());
101: }
102: _output.setCaretPosition(doc.getLength());
103: }
104:
105: }
|