001    package net.provision.soap;
002    
003    import java.awt.*;
004    import java.awt.event.*;
005    
006    import java.net.*;
007    
008    import javax.swing.*;
009    
010    
011    /**
012     * This creates a borderless, frameless, splash screen. It will be centered on-screen
013     * and will stay shown for a set number of seconds. It can also be set to allow
014     * clicking on it to dismiss it.
015     */
016    public class Splash extends JWindow {
017       boolean done = false;
018    
019       /**
020        * Default constructor
021        *
022        * @param imageName The name of the file to load. This can be an external file or a
023        *        file on the classpath (in a jar, etc.).
024        * @param theWaitTime The time (in seconds) to display the splash screen
025        * @param allowClick Wheher or not to allow clicking the screen to dismiss it
026        */
027       public Splash(String imageName, int theWaitTime, boolean allowClick) {
028          super();
029    
030          //convert to seconds (1000 miliseconds = 1 second)
031          final int waitTime = 1000 * theWaitTime;
032    
033          //load the image
034          ImageIcon image;
035    
036          //first try and load it from the classpath
037          URL imageURL = getClass().getResource(imageName);
038    
039          if(imageURL != null)
040             image = new ImageIcon(imageURL);
041          else //try and load it from a local drive
042    
043             image = new ImageIcon(imageName);
044    
045          JLabel l = new JLabel(image);
046          getContentPane().add(l, BorderLayout.CENTER);
047          pack();
048    
049          //now, center it on-screen
050          Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
051          Dimension labelSize = l.getPreferredSize();
052          setLocation((screenSize.width / 2) - (labelSize.width / 2),
053             (screenSize.height / 2) - (labelSize.height / 2));
054    
055          //if allowing clicks, add a Mouse listener
056          if(allowClick) {
057             addMouseListener(new MouseAdapter() {
058                   /**
059                    * Used to detect a click on the splash screen
060                    *
061                    * @param e The mouse event
062                    */
063                   public void mousePressed(MouseEvent e) {
064                      done = true;
065                      setVisible(false);
066                      dispose();
067                   }
068                });
069          }
070    
071          final Runnable closerRunner = new Runnable() {
072                public void run() {
073                   setVisible(false);
074                   dispose();
075                }
076             };
077    
078          Runnable waitRunner = new Runnable() {
079                public void run() {
080                   try {
081                      Thread.sleep(waitTime);
082                      done = true;
083                      SwingUtilities.invokeAndWait(closerRunner);
084                   } catch(Exception e) {
085                      e.printStackTrace();
086    
087                      // can catch InvocationTargetException
088                      // can catch InterruptedException
089                   }
090                }
091             };
092    
093          setVisible(true);
094    
095          Thread splashThread = new Thread(waitRunner, "SplashThread");
096          splashThread.start();
097    
098          while(!done) {
099          }
100       }
101    }