/* ======================================================================
   Parts Copyright 2006 University of Leeds, Oxford University, University of the Highlands and Islands.

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.

====================================================================== */

package org.bodington.applet;

import java.awt.BorderLayout;
import java.awt.Font;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Enumeration;

import javax.swing.JApplet;
import javax.swing.JLabel;
import javax.swing.LookAndFeel;
import javax.swing.UIDefaults;
import javax.swing.UIManager;

import org.bodington.applet.components.PackageEditFrame;

public class PackageEditApplet extends JApplet
    {
    //JButton button;
    BodingtonDesktopPane desktop;

	private void cantStart( String message )
	    {
	    getContentPane().setLayout( new BorderLayout() );
	    JLabel label = new JLabel( message );
	    label.setHorizontalAlignment( JLabel.CENTER );
	    label.setVerticalAlignment( JLabel.CENTER );
	    getContentPane().add( label, BorderLayout.CENTER );
	    }
    
	public void init()
	    {
	    System.out.println( "PackageEditApplet.init()" );
	    
	    String java_version = System.getProperty( "java.version" );
	    if ( "1.4".compareTo(java_version.trim()) > 0 )
		{
		cantStart( "This applet requires a Java Plugin version 1.4+.  One can be downloaded from http://java.sun.com/plugin/" );
		return;
		}
	    
	    LookAndFeel landf  = UIManager.getLookAndFeel();
	    System.out.println( "Look and Feel is: " );
	    System.out.println( landf.getDescription() );
	    
	    
	    UIDefaults defaults = landf.getDefaults();
	    Enumeration enumeration = defaults.keys();
	    Object value, key;
	    while ( enumeration.hasMoreElements() )
	    	{
	    	key = enumeration.nextElement();
	    	value = defaults.get( key );
	    	if ( key.toString().indexOf( "font" ) >= 0 || key.toString().indexOf( "Font" ) >= 0 )
		    	System.out.println( key.toString() + " = " + value.toString() );
	    	}
	    
	    
	    Font unicode_font = new Font( "Arial Unicode MS", Font.PLAIN, 12 );
	    System.out.println( unicode_font.toString() );
	    UIManager.put( "List.font", unicode_font );
	    UIManager.put( "Tree.font", unicode_font );
	    UIManager.put( "TextArea.font", unicode_font );
	    UIManager.put( "TextField.font", unicode_font );
	    UIManager.put( "TextPane.font", unicode_font );
	    UIManager.put( "EditorPane.font", unicode_font );
	    
	    desktop = new BodingtonDesktopPane();
	    getContentPane().add( desktop );

	    String base = getDocumentBase().toExternalForm();
	    
	    System.out.println( "Document base = " + base );
	    if ( base.endsWith( ".html" ) )
		{
		if ( base.lastIndexOf( '/' )>=0 )
		    base = base.substring( 0, base.lastIndexOf( '/' )+1 );
		else
		    {
		    cantStart( "Couldn't start the applet: can't determine the web address to publish file to." );
		    return;
		    }
		}
	    else
		{
		if ( !base.endsWith( "/" ) )
		    base = base + "/";
		}
	    URL base_fixed;
	    try
		{
		base_fixed = new URL( base );
		}
	    catch ( MalformedURLException ex )
		{
		cantStart( "Couldn't start the applet: can't determine the web address to publish file to." );
		return;
		}
	    System.out.println( "Upload base = " + base );
	    desktop.setBodingtonResourceUrl( base_fixed );
	    desktop.init();
        }
        
    public static void openFrame( PackageEditApplet applet )
        {
        if ( applet!=null )
            applet.setEnabled( false );
	
        URL manifest=null;
			InputStream in=null;

/*
        try
            {
            if ( applet!=null )
            	{
            	LoginDialog dialog = new LoginDialog();
            	dialog.setModal( true );
            	
            	
                manifest = new URL( applet.getDocumentBase(), "imsmanifest.xml" );    // add data to the source List
                System.err.println( manifest.toString() );
                URLConnection connect = manifest.openConnection();
                
            	if ( !dialog.login( connect ) )
            		{
            		applet.setEnabled( true );	
            		return;
            		}
            		
                System.err.println( "Set login header" );
                
                in = connect.getInputStream();
                if ( in == null )
               	return;
                
               //if ( connect instanceof HttpURLConnection )
               //	{
               //	HttpURLConnection httpc = (HttpURLConnection)connect;
               //	System.out.println( "return: " + httpc.getResponseCode() );
               //	}

                
                System.err.println( in.toString() );
               }
            else
            	{
                manifest = new URL( "file:///imsmanifest.xml" );    // add data to the source List
                in = manifest.openStream();
               }
            }
        catch ( MalformedURLException uex )
            {
            uex.printStackTrace();
            return;
            }
        catch ( FileNotFoundException fnfex )
            {
            fnfex.printStackTrace();
            in=null;
            }
        catch ( Exception ex )
            {
            ex.printStackTrace();
            return;
            }
            
*/
        try
            {
      		PackageEditFrame frame = new PackageEditFrame( applet.getDocumentBase() );
		           
      		if ( applet!=null )
      			frame.setUploadURL( new URL( applet.getDocumentBase(), "bs_template_uploadrawconfirm.html" ) );
		      		
      		//frame.setManifest( in );
      		frame.setSize (600, 400);
      		frame.setVisible (true);
            }
        catch ( Exception ex )
            {
            ex.printStackTrace();
            return;
            }
            
        }

	public static void main( String args[] )
		{
		openFrame( null );
        }
    }
