/*******************************************************************************
 * Copyright (c) 2001, 2006 Mathew Nelson and Robocode contributors
 * All rights reserved. This program and the accompanying materials 
 * are made available under the terms of the Common Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.robocode.net/license/CPLv1.0.html
 * 
 * Contributors:
 *     Mathew Nelson - initial API and implementation
 *******************************************************************************/
package robocode.dialog;


import java.awt.*;
import javax.swing.*;
import robocode.manager.*;
import robocode.util.*;


/**
 * This type was generated by a SmartGuide.
 */
public class SplashScreen extends JWindow {
	EventHandler eventHandler = new EventHandler();
	private JLabel splashLabel = null;
	private JPanel splashPanel = null;
	private JPanel splashScreenContentPane = null;

	class EventHandler implements java.awt.event.WindowListener {
		public void windowActivated(java.awt.event.WindowEvent e) {}
		;
		public void windowClosed(java.awt.event.WindowEvent e) {}
		;
		public void windowClosing(java.awt.event.WindowEvent e) {
			if (e.getSource() == SplashScreen.this) { 
				splashScreenWindowClosing();
			}
		}
		;
		public void windowDeactivated(java.awt.event.WindowEvent e) {}
		;
		public void windowDeiconified(java.awt.event.WindowEvent e) {}
		;
		public void windowIconified(java.awt.event.WindowEvent e) {}
		;
		public void windowOpened(java.awt.event.WindowEvent e) {}
		;
	}


	;
	public java.awt.Image splashImage = null;
	public robocode.Robocode robocode = null;
	public boolean painted = false;
	public java.lang.String version = null;
	private RobocodeManager manager = null;

	/**
	 * SplashScreen constructor comment.
	 */
	public SplashScreen(RobocodeManager manager) {
		super();
		this.manager = manager;
		this.version = manager.getVersionManager().getVersion();
		initialize();
	}

	/**
	 * Insert the method's description here.
	 * Creation date: (9/4/2001 2:55:22 PM)
	 * @return java.awt.Image
	 */
	public Image getSplashImage() {
		Image splashImage;
		MediaTracker mt = new MediaTracker(this);

		try {
			splashImage = Toolkit.getDefaultToolkit().getImage(
					getClass().getResource("/resources/images/robocode_logo_tanks.jpg"));
		} catch (Exception e) {
			log("Exception loading splash image: " + e);
			return null;
		}
	
		mt.addImage(splashImage, 0);
		mt.statusID(0, true);
		try {
			mt.waitForID(0, 15000);
		} catch (InterruptedException e) {}

		if (mt.statusID(0, true) != MediaTracker.COMPLETE) {
			log("Error fetching splash image. ");
			return null;
		}

		return splashImage; 
	}

	/**
	 * Return the splashLabel
	 * @return javax.swing.JLabel
	 */
	public javax.swing.JLabel getSplashLabel() {
		if (splashLabel == null) {
			try {
				splashLabel = new javax.swing.JLabel();
				splashLabel.setName("SplashLabel");
				splashLabel.setText("");
				splashLabel.setForeground(Color.white);
				splashLabel.setOpaque(false);
			} catch (java.lang.Throwable e) {
				log(e);
			}
		}
		return splashLabel;
	}

	/**
	 * Return the splashLabel
	 * @return javax.swing.JLabel
	 */
	private javax.swing.JPanel getSplashPanel() {
		if (splashPanel == null) {
			try {
				splashPanel = new javax.swing.JPanel() {
					public void paintComponent(Graphics g) {
						g.drawImage(splashImage, 0, 0, null);
						g.setColor(Color.white);
						g.setFont(new java.awt.Font("Arial", 1, 12));
						FontMetrics fm = g.getFontMetrics();

						g.drawString("Version: " + version,
								splashImage.getWidth(null) - fm.stringWidth("Version: " + version),
								splashImage.getHeight(null) - 4);
					}

					public Dimension getPreferredSize() {
						return new Dimension(splashImage.getWidth(null), splashImage.getHeight(null));
					}
				};
				splashPanel.setName("splashPanel");
				splashPanel.setLayout(new BorderLayout());
				splashPanel.add(getSplashLabel(), BorderLayout.NORTH);
			} catch (java.lang.Throwable e) {
				log(e);
			}
		}
		return splashPanel;
	}

	/**
	 * Return the splashScreenContentPane
	 * @return javax.swing.JPanel
	 */
	private javax.swing.JPanel getSplashScreenContentPane() {
		if (splashScreenContentPane == null) {
			try {
				splashScreenContentPane = new javax.swing.JPanel();
				splashScreenContentPane.setName("splashScreenContentPane");
				splashScreenContentPane.setBorder(new javax.swing.border.EtchedBorder());
				splashScreenContentPane.setLayout(new java.awt.BorderLayout());
				splashScreenContentPane.add(getSplashPanel(), "Center");
			} catch (java.lang.Throwable e) {
				log(e);
			}
		}
		return splashScreenContentPane;
	}

	/**
	 * Initialize the class.
	 */
	private void initialize() {
		try {
			setName("SplashScreen");
			setSize(600, 220);
			splashImage = getSplashImage();
			setContentPane(getSplashScreenContentPane());
			addWindowListener(eventHandler);
		} catch (java.lang.Throwable e) {
			log(e);
		}
	}

	/**
	 * Insert the method's description here.
	 * Creation date: (9/19/2001 1:04:16 AM)
	 * @return boolean
	 */
	public synchronized boolean isPainted() {
		return painted;
	}

	/**
	 * Insert the method's description here.
	 * Creation date: (8/22/2001 1:41:21 PM)
	 * @param e java.lang.Exception
	 */
	public void log(String s) {
		Utils.log(s);
	}

	/**
	 * Insert the method's description here.
	 * Creation date: (8/22/2001 1:41:21 PM)
	 * @param e java.lang.Exception
	 */
	public void log(Throwable e) {
		Utils.log(e);
	}

	/**
	 * Insert the method's description here.
	 * Creation date: (9/19/2001 12:58:54 AM)
	 */
	public void paint(Graphics g) {
		super.paint(g);
		setPainted(true);
	}

	/**
	 * Insert the method's description here.
	 * Creation date: (9/19/2001 1:04:16 AM)
	 * @param newPainted boolean
	 */
	public synchronized void setPainted(boolean newPainted) {
		painted = newPainted;
	}

	/**
	 * Insert the method's description here.
	 * Creation date: (8/26/2001 5:01:33 PM)
	 */
	public void splashScreenWindowClosing() {
		this.dispose();
	}
}
