/*
 * @(#)Splash.java
 *
 * Copyright (c) 2003-2004 Stand By Soft, Ltd. All rights reserved.
 *
 * This software is the proprietary information of Stand By Soft, Ltd.  
 * Use is subject to license terms.
 */
package com.standbysoft.demo.date;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.Window;

import javax.swing.SwingUtilities;

/**
 * Splash screen to be used when an application is launched.
 */
public final class Splash {
    private Frame parent;
    
    private MediaTracker mediaTracker;

    private Image image;
    
    private SplashWindow window ;
    
    private int steps;

    private int step;

    /**
     * Creates a splash screen from an image URL.
     
     @param path path of the image displayed in the splash screen
     */
    public Splash(String path, int steps) {
        this.steps = steps;
        parent = new Frame();
        mediaTracker = new MediaTracker(parent);
        image = Toolkit.getDefaultToolkit().getImage(this.getClass().getClassLoader().getResource(path));
    }

    /**
     * Show the actual splash screen.
     */
    public void showSplash() {
        parent.setSize(image.getWidth(null), image.getHeight(null));
        Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
        Rectangle frame = parent.getBounds();
        parent.setLocation((screen.width - frame.width2(screen.height - frame.height2);

        mediaTracker.addImage(image, 0);
        try {
            mediaTracker.waitForID(0);
        catch (InterruptedException ie) {
        }

        window = new SplashWindow(parent, image);
    }
    
    /**
     * Hide the splash screen.
     */
    public void hideSplash() {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                parent.dispose();
            }
        });
    }
    
    public void increaseStep() {
        step++;
        window.setRatio(((float)step)/steps);
        window.repaint();
    }
    
    public void increaseStep(int delta) {
        step += delta;
        window.setRatio(((float)step)/steps);
        window.repaint();
    }

    private class SplashWindow extends Window {
        private Image image;
        private Color color = new Color(0114255);
        private int thick = 2;
        private float ratio;
        
        SplashWindow(Frame parent, Image aImage) {
            super(parent);
            ratio = 0;
            this.image = aImage;
            setSize(image.getWidth(null), image.getHeight(null+ thick);
            Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
            Rectangle window = getBounds();
            setLocation((screen.width - window.width2(screen.height - window.height2);
            setVisible(true);
        }
        
        public void setRatio(float ratio) {
            this.ratio = ratio;
        }

        public void repaint() {
            super.repaint(0, getHeight() - thick, getWidth(), thick);
        }
        
        public void paint(Graphics graphics) {
            if (image != null) {
                Graphics2D g2d = (Graphics2D)graphics;
                g2d.drawImage(image, 00this);
                g2d.setStroke(new BasicStroke(thick));
                g2d.setColor(color);
                g2d.drawLine(0, getHeight()- thick / 2(int)(getWidth() * ratio), getHeight() - thick / 2);
            }
        }
    }
}