/*
 * COPYRIGHT (c) 1998-1999 Jan-Henrik Haukeland, http://www.tildeslash.com/
 */

package ts.biff;


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**  
 * Displays a Splash Image in a window at the center of the screen The
 * window will have an area at the bottom of the screen for displaying
 * optional messages. 
 *
 * <H3>SYNOPSIS</H3>
 *  <PRE>
 *   Splash splash= Splash.createSplash();
 *   splash.showSplash();
 *   splash.print("Loading classes..");
 *    <I>&lt; Instantiate appliaction classes &gt;</I>
 *   splash.print("Setting up configuration..");
 *    <I>&lt; do configuration stuff &gt;</I>
 *   .
 *   .
 *    <I>and so on</I>
 *   splash.closeSplash();
 *   </PRE>
 * @author  Jan-Henrik Haukeland &lt;hauk@tildeslash.com&gt;
 * @version $Id: Splash.java,v 1.15 2001/08/13 12:48:28 hauk Exp $
 */
public final class Splash 
{

  private static SplashWindow mySplashWindow= null;
  private static Font         myFont= Font.decode("helvetica-plain-12");
  private static Color        myColor= Color.black;
  private static Image        myImage= null;;

  static 
  {
    try {
      myImage= Toolkit.getDefaultToolkit().
	  getImage(Class.forName("java.lang.Object").
		   getResource("/ts/resources/logo.gif"));
    } catch (Exception e) {
      System.err.println("\n\nFailed to initialize architecture"+
			 "\nReason: Could not load default Logo, "+
			 e.getMessage());
      System.exit(1);
    }
  }


  
  private Splash() { }


  public static Splash createSplash()
  {
    return new Splash();
  }


  public void show()
  {
    mySplashWindow= new SplashWindow();
    mySplashWindow.show();
    mySplashWindow.toFront();
  }
  

  public void close()
  {
    mySplashWindow.setVisible(false);
    mySplashWindow.dispose();
  }


  public void print(String s)
  {
    mySplashWindow.print(s);
  }


  public static final void center(Window window)
  {
    window.pack();
    Dimension d= Toolkit.getDefaultToolkit().getScreenSize();
    Dimension w= window.getSize();
    window.setLocation((d.width - w.width) / 2, (d.height - w.height) / 2);
  }


  public void setFont(Font f)
  {
    myFont= f;
  }


  public void setTextColor(Color c)
  {
    myColor= c;
  }



 
  static class SplashWindow extends JWindow
  {
    SplashComponent comp= new SplashComponent();
    JLabel label=         new JLabel();
    int offSet=           10;
    
    public SplashWindow()
    {
      super(new Frame());
      getContentPane().add(comp);
      Splash.center(this);

      label.setOpaque(false);
      getLayeredPane().add(label, JLayeredPane.MODAL_LAYER);
    }

    public void print(String s)
    {
      label.setFont(myFont);
      label.setForeground(myColor);
      FontMetrics fm= getFontMetrics(myFont);
      int w= SwingUtilities.computeStringWidth(fm,s);
      int h= fm.getMaxAscent();
      label.setBounds(offSet, myImage.getHeight(null)-(h+offSet), w, h+offSet);
      label.setText(s);
      comp.repaint();
    }
  }


 
  static class SplashComponent extends JComponent
  {
    {
      super.setDoubleBuffered(true);
    }

    Image image= new ImageIcon(myImage).getImage();
    
    public void paint(Graphics g)
    {
      g.drawImage(image, 0, 0, null);
    }

    public Dimension getPreferredSize()
    {
      return new Dimension(image.getWidth(null), image.getHeight(null));
    }
  }


 
  public static void main(String[] a)
  {
    try {

    final Splash p= Splash.createSplash();
    p.show();

    mySplashWindow.addMouseListener(new MouseAdapter() {
      public void mouseClicked(MouseEvent e) {
	p.close();
	System.exit(0);
      }
    });

    p.setFont(Font.decode("SansSerif-plain-12"));
    p.setTextColor(Color.black);

    p.print("Man is part of a game he cannot alter");
    Thread.sleep(1000);
    p.print("but waiting for his turn to come");
    Thread.sleep(1000);
    p.print("He is free to be");
    Thread.sleep(500);
    p.setTextColor(Color.decode("#CCCCFF"));
    p.setFont(Font.decode("SansSerif-bold-12"));
    p.print("Lars Haukeland 1991");
    
    }
    catch (Exception e) {
 	System.out.println("Error: "+e.getMessage());
 	e.printStackTrace();
	System.exit(1);
    }
  }

}
