KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > houston > swing > StatusPanel


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 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 JavaDoc msg )
71    {
72       Toolkit.getDefaultToolkit().beep();
73       appendText( msg, _errorStyle );
74    }
75
76    public void fatal( String JavaDoc msg )
77    {
78       Toolkit.getDefaultToolkit().beep();
79       appendText( msg, _errorStyle );
80    }
81
82    public void hint( String JavaDoc msg )
83    {
84       appendText( msg, _plainStyle );
85    }
86
87    public void info( String JavaDoc msg )
88    {
89       appendText( msg, _plainStyle );
90    }
91
92    public void info( int level, String JavaDoc msg )
93    {
94       appendText( msg, _plainStyle );
95    }
96
97    public void warning( String JavaDoc msg )
98    {
99       appendText( msg, _warningStyle );
100    }
101
102
103    private void appendText( String JavaDoc 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