/*
 * [Splash.java]
 *
 * Summary: Display splash screen shown during program load.
 *
 * Copyright: (c) 2013-2017 Roedy Green, Canadian Mind Products, http://mindprod.com
 *
 * Licence: This software may be copied and used freely for any purpose but military.
 *          http://mindprod.com/contact/nonmil.html
 *
 * Requires: JDK 1.8+
 *
 * Created with: JetBrains IntelliJ IDEA IDE http://www.jetbrains.com/idea/
 *
 * Version History:
 *  1.0 2013-01-18 initial version
 */
package com.mindprod.example;

import com.mindprod.common18.ImageViewer;

import javax.swing.JWindow;
import javax.swing.Timer;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.net.URL;
import java.util.concurrent.TimeUnit;

import static java.lang.System.*;

/**
 * Display splash screen shown during program load.
 * <p/>
 * To use it, just use new Splash() early in program load.
 *
 * @author Roedy Green, Canadian Mind Products
 * @version 1.0 2013-01-18 initial version
 * @since 2013-01-18
 */
public class Splash extends JWindow
    {
    /**
     * constructor
     *
     * @param splashImage name of image in resource, by default filed in jar under com.mindprod.example.
     *                    parameter to getResource.
     * @param splashTime  how long to display the splash screen in milliseconds.
     */
    public Splash( String splashImage, long splashTime )
        {
        // get Image as a resource, from jar or from classpath
        URL url = Splash.class.getResource( splashImage );
        Image image = Toolkit.getDefaultToolkit().getImage( url );
        // see http://mindprod.com/jgloss/image.html for ImageViewer source
        ImageViewer imageViewer = new ImageViewer( image );
        this.getContentPane().add( imageViewer, BorderLayout.CENTER );
        // Allow user to close splash window with just a click
        // A fancier version would also also support programmatic stop.
        this.addMouseListener( new MouseAdapter()
            {
            public void mousePressed( MouseEvent e )
                {
                setVisible( false );
                dispose();
                }
            } );
        this.pack();
        // put this splash window on the center of the screen.
        final Dimension screen_dim = Toolkit.getDefaultToolkit().getScreenSize();
        this.setLocation( ( screen_dim.width - this.getWidth() ) / 2, ( screen_dim.height - this.getHeight() ) / 2 );
        this.setVisible( true );
        final ActionListener splashCloser = new ActionListener()
            {
            public void actionPerformed( ActionEvent evt )
                {
                // shutdown Splash
                setVisible( false );
                dispose();
                }
            };
        Timer splashTimer = new Timer( ( int ) splashTime, splashCloser );
        splashTimer.setRepeats( false );
        splashTimer.start();
        }

    /**
     * what you do to invoke a splash Screen.
     *
     * @param args used in the usual way.
     */
    public static void main( String[] args )
        {
        new Splash( "replicator.png", TimeUnit.SECONDS.toMillis( 10 ) );
        // returns right away, leaving splash displaying for 10 seconds.
        out.println( "starting..." );
        }
    }