package ca.ucalgary.seahawk.gui.splash;

/*
 * @(#)Splasher.java  2.0  January 31, 2004
 *
 * Copyright (c) 2003-2004 Werner Randelshofer
 * Staldenmattweg 2, Immensee, CH-6405, Switzerland.
 * All rights reserved.
 *
 * This software is in the public domain.
 */

import javax.swing.JApplet;

/**
 * Modified to support both applet and application modes.
 * The point of the code here is to load a splash screen quickly, while the 
 * real program code is loaded via a class loader from disk or the Web.
 *
 * @author Paul Gordon gordonp@ucalgary.ca
 */
public class SeahawkSplasher extends JApplet{

    String[] passedAppletArgs = null;

    /**
     * Method invoked when running as an applet.
     * The applet tag may have a "url" parameter, which specifies the location to load in the first tab.
     */
    public void init(){
	System.err.println("Called applet init");
        SplashWindow.splash(getClass().getClassLoader().getResource("ca/ucalgary/seahawk/resources/images/seahawk_splash.jpg"), 
                            "Seahawk - Applet Version");
	SplashWindow.setTitlePosition(135, 50);
	SplashWindow.setStatusPosition(135, 140);
	SplashWindow.setStatus("Loading applet code across the network...");
	String[] args = new String[0];
	String initialURL = getParameter("url");
	if(initialURL != null){
	    args = new String[1];
	    args[0] = initialURL;
	}

	passedAppletArgs = args;

    }

    public void start(){
	System.err.println("Called applet start");
	final String[] finalArgs = passedAppletArgs;
	
	// Execute a job on another thread: this allows start() to return nicely
	try {
	    new Thread(){
		    public void run() {
			SplashWindow.invokeStaticMethod("ca.ucalgary.seahawk.gui.MobyContentGUI", 
							"setDefaultAppCloseOperation", 
							javax.swing.JFrame.DISPOSE_ON_CLOSE);
			SplashWindow.invokeMain("ca.ucalgary.seahawk.gui.MobyContentGUI", finalArgs);
			SplashWindow.disposeSplash();
		    }
		}.start();
	} catch (Exception e) {
	    System.err.println("Invoking ca.ucalgary.seahawk.gui.MobyContentGUI's " +
			       "main method didn't successfully complete");
	}
    }

    public void stop(){
	// Nothing special to stop Seahawk
	System.err.println("Called applet stop");
    }

    public void destroy(){
	// Usually called when the web page with the applet is unloaded
	System.err.println("Called applet destroy");
	
	SplashWindow.invokeStaticMethod("ca.ucalgary.seahawk.gui.MobyContentGUI", 
					"destroy");
	super.destroy();
    }

    /**
     * Shows the splash screen, launches the application and then disposes
     * the splash screen.
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        SplashWindow.splash(SeahawkSplasher.class.getResource("ca/ucalgary/seahawk/resources/images/seahawk_splash.jpg"), 
			    "Seahawk - Application Version");
	SplashWindow.setTitlePosition(135, 50);
	SplashWindow.setStatusPosition(135, 140);
        SplashWindow.setStatus("Loading application code from the file system...");
        SplashWindow.invokeMain("ca.ucalgary.seahawk.gui.MobyContentGUI", args);
        SplashWindow.disposeSplash();
    }
    
}
