/*
** Rachel  - Resource Loading Toolkit for Web Start/JNLP
** Copyright (c) 2001, 2002 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 demo.hello;

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.util.*;
import javax.swing.*;
import com.l2fprod.gui.*;
import com.l2fprod.gui.plaf.skin.*;
import com.l2fprod.gui.plaf.skin.impl.gtk.*;
import com.l2fprod.gui.plaf.skin.impl.kde.*;
import demo.common.html.*;
import houston.*;
import rachel.http.*;
import rachel.http.loader.*;
import rachel.url.*;
import rachel.util.*;

public class HelloRachel extends JFrame
{

   public final static int PORT = 6868;
   static Logger T = Logger.getLogger( HelloRachel.class );

   private WebBrowser _browser;

   public HelloRachel()
   {
      super( "Hello Rachel - Web Start Resource Loading Example App" );

      addWindowListener(
         new WindowAdapter()
         {
            public void windowClosing( WindowEvent e )
            {
               System.exit( 0 );
            }
         } );

      // build URLs

      URL displayIconUrl = null;
      URL appIconUrl = null;

      URL javaDocUrl = null;
      URL javaDocUrl2 = null;
      URL crossRefUrl = null;
      URL crossRefUrl2 = null;

      try
      {
         displayIconUrl = new URL( "class://demo.hello.ThemeAnchor/images/world2.gif" );
         appIconUrl = new URL( "class://demo.hello.ThemeAnchor/images/inform.gif" );

         javaDocUrl = new URL( getDocumentBase() + "/javadoc/index.html" );
         crossRefUrl = new URL( getDocumentBase() + "/crossref/index.html" );

         javaDocUrl2 = new URL( "class://demo.hello.JavaDocAnchor/javadoc/index.html" );
         crossRefUrl2 = new URL( "class://demo.hello.CrossRefAnchor/crossref/index.html" );
      }
      catch( MalformedURLException mex )
      {
         Status.error( "*** failed to create URL: " + mex.toString() );
      }

      ImageIcon appIcon = new ImageIcon( appIconUrl );
      setIconImage( appIcon.getImage() );

      _browser = new WebBrowser();

      StringBuffer html = new StringBuffer();

      html.append( "<html>" );

      // -- add system properties to page

      html.append( "<h2>System Properties</h2>" );
      html.append( "<table border='1'>" );
      html.append( "<tr><th>Name<th>Value" );

      // sort system properties by putting them into a tree map
      TreeMap props = new TreeMap( System.getProperties() );

      Iterator it = props.entrySet().iterator();
      while( it.hasNext() )
      {
         Map.Entry entry = ( Map.Entry ) it.next();

         String key = ( String ) entry.getKey();
         String value = ( String ) entry.getValue();

         html.append( "<tr><td>" + key + "<td>" + value );
      }

      html.append( "</table>" );

      html.append( "<h2>Other</h2>" );

      html.append( "ClassLoader hierachy" );
      html.append( "<ul>" );
      ClassLoader cl = getClass().getClassLoader();
      while( cl != null )
      {
         html.append( "<li>" + cl.getClass().getName() );
         cl = cl.getParent();
      }
      html.append( "</ul>" );

      html.append( "SystemClassLoader hierachy" );
      html.append( "<ul>" );
      cl = ClassLoader.getSystemClassLoader();
      while( cl != null )
      {
         html.append( "<li>" + cl.getClass().getName() );
         cl = cl.getParent();
      }
      html.append( "</ul>" );

      html.append( "<hr>" );
      html.append( "SecurityManager: " );

      SecurityManager sm = System.getSecurityManager();
      if( sm != null )
         html.append( sm.getClass().getName() );

      html.append( "</html>" );

      _browser.showHtmlText( html.toString() );

      getContentPane().setLayout( new BorderLayout() );
      getContentPane().add( new JScrollPane( _browser.getComponent() ), BorderLayout.CENTER );

      // create menu
      ImageIcon displayIcon = new ImageIcon( displayIconUrl );

      JMenuBar menuBar = new JMenuBar();

      JMenu favoritesMenu = new JMenu( "Favorites" );

      favoritesMenu.add(
            new CmdShowDocumentBuiltIn( "Java Doc @ " + javaDocUrl.toExternalForm(), displayIcon, _browser, javaDocUrl ) );

      favoritesMenu.add(
            new CmdShowDocumentBuiltIn( "Java Doc @ " + javaDocUrl2.toExternalForm(), displayIcon, _browser, javaDocUrl2 ) );

      favoritesMenu.add(
            new CmdShowDocumentBuiltIn( "Cross Reference @ " + crossRefUrl.toExternalForm(), displayIcon, _browser, crossRefUrl ) );

      favoritesMenu.add(
            new CmdShowDocumentBuiltIn( "Cross Reference @ " + crossRefUrl2.toExternalForm(), displayIcon, _browser, crossRefUrl2 ) );

      menuBar.add( favoritesMenu );
      setJMenuBar( menuBar );

      setSize( 600, 400 );
      setVisible( true );
   }

   public String getDocumentBase()
   {
      return "http://" + NetUtils.getLocalHostName() + ":" + PORT;
   }

   public static void main( String args[] )
   {
      // enable class:// urls
      URL.setURLStreamHandlerFactory( new RachelUrlFactory() );

      // set look&feel,theme,skin etc.
      try
      {
         URL themepack = new URL( "class://demo.hello.ThemeAnchor/skinlf/themes/modern.zip" );

         Skin skin = SkinLookAndFeel.loadThemePack( themepack );
         SkinLookAndFeel.setSkin( skin );
         SkinLookAndFeel lnf = new SkinLookAndFeel();
         UIManager.setLookAndFeel( lnf );
         UIManager.getLookAndFeelDefaults().put( "ClassLoader", lnf.getClass().getClassLoader() );
      }
      catch( Exception ex )
      {
         T.error( "*** failed to change look&feel: " + ex.toString() );
      }

      // register resource anchors

      WebResourceManager roots = WebResourceManager.getInstance();
      roots.addResourceLoader( new ClassResourceLoader( CrossRefAnchor.class ) );
      roots.addResourceLoader( new ClassResourceLoader( JavaDocAnchor.class ) );

      try
      {
         // start web server
         WebServer http = new WebServer( PORT, roots );
         http.start();
      }
      catch( IOException ioex )
      {
         T.error( "*** failed to start web service: " + ioex.toString() );
      }

      HelloRachel gui = new HelloRachel();
   }
}