/*
** 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.resman;

import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import java.net.*;
import java.util.*;
import javax.swing.*;
import javax.xml.transform.*;
import javax.xml.transform.stream.*;
import org.jdom.*;
import org.jdom.output.*;
import org.jdom.transform.*;
import apollo.*;
import demo.common.html.*;
import demo.common.image.*;
import demo.common.tree.*;
import houston.*;
import rachel.*;
import rachel.loader.*;

public class AppFrame extends JFrame
{

   public AppFrame()
   {
      super( "ResMan - Rachel Resource Manager Example App" );

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

      ResMan.setResourceLoader( new ClassResourceLoader( Tool.class ) );

      ImageIcon appIcon = ResMan.getIcon( "images/inform.gif" );
      setIconImage( appIcon.getImage() );

      Properties props = null;

      try
      {
         props = ResMan.getProperties( "menu.properties" );
      }
      catch( IOException ioex )
      {
         Status.error( "*** failed to load properties: " + ioex.toString() );
      }

      WebBrowser browserProps = new WebBrowser();
      browserProps.showHtmlText( propsToHtml( props ) );

      BufferedImage vanessaImg = null;
      try
      {
         vanessaImg = ResMan.getImage( "images/vanessa.jpg" );
      }
      catch( IOException ioex )
      {
         Status.error( "*** failed to load image: " + ioex.toString() );
      }

      ImagePanel imagePanel = new ImagePanel( vanessaImg );

      WebBrowser browserText = new WebBrowser();

      String strunkHtmlSnippet = "";
      try
      {
         strunkHtmlSnippet = ResMan.getText( "strunk.html" );
      }
      catch( IOException ioex )
      {
         Status.error( "*** failed to load text: " + ioex.toString() );
      }
      browserText.showHtmlText( strunkHtmlSnippet );

      WebBrowser browserStream = new WebBrowser();

      InputStream inStream = ResMan.getInputStream( "overview.xml" );
      InputStream xslStream = ResMan.getInputStream( "doc2html.xsl" );

      // setup jaxp transformer (aka xsl/t engine)
      StreamSource inSource = new StreamSource( inStream, "overview.xml" );
      StreamSource xsltSource = new StreamSource( xslStream, "doc2html.xsl" );

      JDOMResult outResult = new JDOMResult();

      try
      {
         TransformerFactory tf = TransformerFactory.newInstance();
         Transformer trans = tf.newTransformer( xsltSource );
         trans.transform( inSource, outResult );

         // get result as a string
         StringWriter outBuffer = new StringWriter();
         XMLOutputter outputter = new XMLOutputter( "", false );
         outputter.output( outResult.getDocument().getRootElement(), outBuffer );

         browserStream.showHtmlText( outBuffer.toString() );
      }
      catch( TransformerConfigurationException tcex )
      {
         Status.error( "*** failed to setup xsl/t processor: " + tcex.toString() );
      }
      catch( TransformerException tex )
      {
         Status.error( "*** failed to process xsl/t stylesheet: " + tex.toString() );
      }
      catch( IOException ioex )
      {
         Status.error( "*** failed to create html document from xml data: " + ioex.toString() );
      }

      Document doc = null;
      try
      {
         doc = ResMan.getXmlDocument( "overview.xml" );
      }
      catch( JDOMException jex )
      {
         Status.error( "*** failed to retrieve xml document: " + jex.toString() );
      }

      JTree tree = new JTree( new XmlTreeModel( doc ) );

      tree.setCellRenderer( new XmlTreeNodeRenderer() );
      tree.setEditable( false );
      tree.putClientProperty( "JTree.lineStyle", "Angled" );
      tree.setRootVisible( true );
      tree.setShowsRootHandles( true );

      JTabbedPane tab = new JTabbedPane();
      tab.addTab( "ResMan.getProperties()", new JScrollPane( browserProps.getComponent() ) );
      tab.addTab( "ResMan.getImage()", imagePanel );
      tab.addTab( "ResMan.getText()", new JScrollPane( browserText.getComponent() ) );
      tab.addTab( "ResMan.getXmlDocument()", new JScrollPane( tree ) );
      tab.addTab( "ResMan.getInputStream()", new JScrollPane( browserStream.getComponent() ) );

      getContentPane().setLayout( new BorderLayout() );
      getContentPane().add( tab, BorderLayout.CENTER );

      BasicService basic = ServiceManager.lookupBasicService();

      JMenuBar menuBar = new JMenuBar();

      JMenu helpMenu = new JMenu( "Help" );

      ImageIcon worldIcon = ResMan.getIcon( "images/world2.gif" );

      String items = props.getProperty( "help.menu", "" );
      StringTokenizer t = new StringTokenizer( items, " " );

      while( t.hasMoreTokens() )
      {
         String itemKey = t.nextToken();

         String itemText = props.getProperty( itemKey + ".text" );
         String itemUrl = props.getProperty( itemKey + ".url" );

         try
         {
            Action action = new CmdShowDocumentHousehold( itemText, worldIcon, basic, new URL( itemUrl ) );
            JMenuItem mi = helpMenu.add( action );
            mi.setToolTipText( itemUrl );
         }
         catch( MalformedURLException mex )
         {
            Status.error( "*** invalid URL " + itemUrl + ": " + mex.toString() );
         }
      }

      menuBar.add( helpMenu );
      setJMenuBar( menuBar );

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

   private String propsToHtml( Properties props )
   {
      StringBuffer buf = new StringBuffer();

      buf.append( "<html>" );

      // add properties to page

      buf.append( "<h2>menu.properties</h2>" );
      buf.append( "<table border='1'>" );
      buf.append( "<tr><th>Name<th>Value" );

      // sort properties by putting them into a tree map
      TreeMap propsSorted = new TreeMap( props );

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

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

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

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

      return buf.toString();
   }
}