import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import java.net.URL;

import javax.swing.*;


/**
 * This creates a borderless, frameless, splash screen. It will be centered
 * on-screen and will stay shown for a set number of seconds.
 * It can also be set to allow clicking on it to dismiss it.
 *
 * @author Brian Pipa - http://www.pipasoft.com
 * @version 1.0
 */
public class Splash extends JWindow {
    boolean done = false;

    /**
     * Default constructor
     *
     * @param filename The name of the file to load. This can be an external
     *     file or a file on the classpath (in a jar, etc.).
     * @param theWaitTime The time (in seconds) to display the splash
     *     screen
     * @param allowClick Wheher or not to allow clicking the screen to
     *      dismiss it
     */
    public Splash(String imageName, int theWaitTime, boolean allowClick) {
        super();

        //convert to seconds (1000 miliseconds = 1 second)
        final int waitTime = 1000*theWaitTime;

        //load the image
        ImageIcon image;

        //first try and load it from the classpath
        URL imageURL = getClass().getResource(imageName);
        if (imageURL != null)
            image = new ImageIcon(imageURL);
        else//try and load it from a local drive
            image = new ImageIcon(imageName);

        JLabel l = new JLabel(image);
        getContentPane().add(l, BorderLayout.CENTER);
        pack();

        //now, center it on-screen
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension labelSize = l.getPreferredSize();
        setLocation((screenSize.width / 2) - (labelSize.width / 2),
            (screenSize.height / 2) - (labelSize.height / 2));
        //if allowing clicks, add a Mouse listener
        if (allowClick) {
            addMouseListener(new MouseAdapter() {
		    /**
		     * Used to detect a click on the splash screen
		     *
		     * @param e The mouse event
		     */
                    public void mousePressed(MouseEvent e) {
                        done = true;
                        setVisible(false);
                        dispose();
                    }
                });
        }
        final Runnable closerRunner = new Runnable() {
                public void run() {
                    setVisible(false);
                    dispose();
                }
            };

        Runnable waitRunner = new Runnable() {
                public void run() {
                    try {
                        Thread.sleep(waitTime);
                        done = true;
                        SwingUtilities.invokeAndWait(closerRunner);
                    } catch (Exception e) {
                        e.printStackTrace();

                        // can catch InvocationTargetException
                        // can catch InterruptedException
                    }
                }
            };

        setVisible(true);

        Thread splashThread = new Thread(waitRunner, "SplashThread");
        splashThread.start();

        while (!done) {}
    }

    /**
     * For testing
     *
     * @param args
     */
    public static void main(String[] args) {

        if (args.length !=2) {
            System.out.println("USAGE: java Splash image seconds");
        }


        new Splash(args[0], Integer.parseInt(args[1]), true);
        System.exit(0);
    }
}
