HtmlStatusPanel.java :  » Development » Houston » houston » swing » Java Open Source

Java Open Source » Development » Houston 
Houston » houston » swing » HtmlStatusPanel.java
/*
** 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
** Lesser General Public License as published by the Free Software Foundation.
** Version 2.1 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 java.awt.datatransfer.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.text.*;
import javax.swing.text.html.*;
import houston.*;

public class HtmlStatusPanel extends JPanel implements StatusListener
{
   private final static String END_MARKER_ID = "201";
   static Logger T = Logger.getLogger( HtmlStatusPanel.class );
   private JEditorPane _browser;

   private int _counter = 0;
   private JScrollPane _scrollPane;

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

      _browser = new JEditorPane();
      _browser.setEditable( false );
      _browser.setContentType( "text/html" );

      // note: clear sets up JEditorPane
      clear();

      Status.addListener( this );

      _scrollPane = new JScrollPane( _browser );
      add( _scrollPane, BorderLayout.CENTER );
   }

   public void clear()
   {
      // todo: isn't setText supposed to be thread-safe?

      SwingUtilities.invokeLater(
         new Runnable()
         {
            public void run()
            {
               String html = "<html><head></head><body><table border=0><tr><td><p id=\"" + END_MARKER_ID + "\"></td></tr></table></body></html>";
               _browser.setText( html );
               T.debug( "html=" + html );

               /*
                *  HTMLDocument htmlDoc   = (HTMLDocument) _browser.getDocument();
                *  htmlDoc.dump( System.out );
                */
            }
         } );
   }

   public void error( String msg )
   {
      Toolkit.getDefaultToolkit().beep();
      message( "<font color=red>" + msg + "</font>" );
      T.error( msg );
   }

   public void fatal( String msg )
   {
      Toolkit.getDefaultToolkit().beep();
      message( "<font color=red>" + msg + "</font>" );
      T.fatal( msg );
   }

//
// implementation of the StatusListener Interface
//

   public void hint( String msg )
   {
      message( "<font color=grey>" + msg + "</font>" );
      T.debug( msg );
   }

   public void info( String msg )
   {
      message( msg );
      T.debug( msg );
   }

   public void info( int level, String msg )
   {
      message( level, msg );
      T.debug( msg );
   }

   public void warning( String msg )
   {
      message( "<font color=#ff8429>" + msg + "</font>" );
      T.warning( msg );
   }

   private void insertHtml( final String html )
   {
      SwingUtilities.invokeLater(
         new Runnable()
         {
            public void run()
            {
               try
               {
                  HTMLDocument htmlDoc = ( HTMLDocument ) _browser.getDocument();
                  Element endMarker = htmlDoc.getElement( END_MARKER_ID );
                  if( endMarker == null )
                  {
                     T.error( "endMarker not found" );
                  }
                  htmlDoc.insertBeforeEnd( endMarker, html );
               }
               catch( Exception ioex )
               {
                  T.error( "insertBeforeEnd failed: " + ioex.toString() );
               }
            }
         } );
   }

   private void message( int level, String msg )
   {
      _counter++;

      // String line = "<h" + level + ">" + msg + "</h" + level + ">";
      String line = "<b>" + msg + "</b><br>";

      insertHtml( line );
   }

   private void message( String msg )
   {
      _counter++;

      // String line = msg;
      String line = msg + "<br>";

      insertHtml( line );
   }

}
// end of class
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.