Splash.java
001 /*
002  *  Copyright (c) 1995-2010, The University of Sheffield. See the file
003  *  COPYRIGHT.txt in the software or at http://gate.ac.uk/gate/COPYRIGHT.txt
004  *
005  *  This file is part of GATE (see http://gate.ac.uk/), and is free
006  *  software, licenced under the GNU Library General Public License,
007  *  Version 2, June 1991 (in the distribution as file licence.html,
008  *  and also available at http://gate.ac.uk/gate/licence.html).
009  *
010  *  Valentin Tablan 28/01/2001
011  *
012  *  $Id: Splash.java 12155 2010-01-12 14:01:15Z thomas_heitz $
013  *
014  */
015 package gate.gui;
016 
017 import java.awt.*;
018 import java.awt.event.MouseAdapter;
019 import java.awt.event.MouseEvent;
020 
021 import javax.swing.*;
022 import javax.swing.border.EtchedBorder;
023 
024 /**
025  * A splash screen.
026  * A splash screen is an image that appears on the screen while an application
027  * initialises. The implementation uses a {@link java.awt.Window} (a Frame with
028  * no decorations such as bar or buttons) and can either display a JComponent
029  * as content or an image.
030  * When clicked it is hidden.
031  */
032 public class Splash extends JWindow {
033 
034   /**
035    * Constructor from owner, GraphicsConfiguration and content.
036    */
037   public Splash(Window owner, GraphicsConfiguration gc, final JComponent content) {
038     super(owner, gc);
039     getContentPane().setLayout(new BorderLayout());
040     content.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.RAISED));
041     getContentPane().add(content, BorderLayout.CENTER);
042     content.addMouseListener(new MouseAdapter() {
043       public void mouseClicked(MouseEvent e) {
044         // when the content pane is clicked, hide it
045         setVisible(false);
046       }
047     });
048     validate();
049     pack();
050   }
051 
052   public Splash(Window owner,  JComponent content) {
053     this(owner, null, content);
054   }
055   
056   /**
057    * Constructor from image.
058    */
059   public Splash(String imageResourcePath) {
060     this(null, imageResourcePath);
061   }
062 
063   /**
064    * Constructor from content.
065    */
066   public Splash(JComponent content) {
067     this(null, content);
068   }
069 
070   /**
071    * Constructor from owner and image.
072    */
073   public Splash(Window owner, String imageResourcePath) {
074     this(owner,
075         new JLabel(new ImageIcon(Splash.class.getResource(imageResourcePath))));
076   }
077 
078   /**
079    * Displays the splash screen centered in the owner's space or centered on
080    * the screen if no owner or owner not shown.
081    */
082   public void showSplash(){
083     Dimension ownerSize;
084     Point ownerLocation;
085     Rectangle screenBounds = getGraphicsConfiguration().getBounds();
086     if(getOwner() == null){
087       ownerSize = screenBounds.getSize();
088       ownerLocation = screenBounds.getLocation();
089     }else{
090       ownerSize = getOwner().getSize();
091       ownerLocation = getOwner().getLocation();
092       if(ownerSize.height == ||
093          ownerSize.width == ||
094          !getOwner().isVisible()){
095         ownerSize = screenBounds.getSize();
096         ownerLocation = screenBounds.getLocation();        
097       }
098     }
099     //Center the window
100     Dimension frameSize = getSize();
101     if (frameSize.height > ownerSize.height)
102       frameSize.height = ownerSize.height;
103     if (frameSize.width > ownerSize.width)
104       frameSize.width = ownerSize.width;
105     setLocation(ownerLocation.x + (ownerSize.width - frameSize.width2,
106                 ownerLocation.y + (ownerSize.height - frameSize.height2);
107     super.setVisible(true);
108   }
109 }