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

package houston.swing;

import java.awt.*;
import javax.swing.*;
import javax.swing.text.*;
import houston.*;

public class StatusPanel extends JPanel implements StatusListener
{
   static Logger T = Logger.getLogger( StatusPanel.class );

   private SimpleAttributeSet _errorStyle;

   private JTextPane _output;
   private SimpleAttributeSet _plainStyle;
   private SimpleAttributeSet _warningStyle;

   public StatusPanel()
   {
      setLayout( new BorderLayout() );
      setBorder( BorderFactory.createEtchedBorder() );

      _output = new JTextPane();
      _output.setEditable( false );

      _errorStyle = new SimpleAttributeSet();
      StyleConstants.setForeground( _errorStyle, Color.red );
      StyleConstants.setBold( _errorStyle, true );

      _warningStyle = new SimpleAttributeSet();
      StyleConstants.setForeground( _warningStyle, Color.orange );
      StyleConstants.setBold( _warningStyle, true );

      _plainStyle = new SimpleAttributeSet();
      StyleConstants.setForeground( _plainStyle, Color.black );
      StyleConstants.setBold( _plainStyle, false );

      Status.addListener( this );

      add( new JScrollPane( _output ), BorderLayout.CENTER );
   }

   public void clear()
   {
      _output.setText( "" );
   }

   public void error( String msg )
   {
      Toolkit.getDefaultToolkit().beep();
      appendText( msg, _errorStyle );
   }

   public void fatal( String msg )
   {
      Toolkit.getDefaultToolkit().beep();
      appendText( msg, _errorStyle );
   }

   public void hint( String msg )
   {
      appendText( msg, _plainStyle );
   }

   public void info( String msg )
   {
      appendText( msg, _plainStyle );
   }

   public void info( int level, String msg )
   {
      appendText( msg, _plainStyle );
   }

   public void warning( String msg )
   {
      appendText( msg, _warningStyle );
   }


   private void appendText( String text, AttributeSet style )
   {
      Document doc = _output.getDocument();
      text += "\n";
      try
      {
         doc.insertString( doc.getLength(), text, style );
      }
      catch( BadLocationException bex )
      {
         T.error( "*** failed to append status message: " + bex.toString() );
      }
      _output.setCaretPosition( doc.getLength() );
   }

}