View Javadoc

1   /*
2    * Created on Dec 20, 2004
3    * 
4    *   Magic-Project is a turn based strategy simulator
5    *   Copyright (C) 2003-2007 Fabrice Daugan
6    *
7    *   This program is free software; you can redistribute it and/or modify it 
8    * under the terms of the GNU General Public License as published by the Free 
9    * Software Foundation; either version 2 of the License, or (at your option) any
10   * later version.
11   *
12   *   This program is distributed in the hope that it will be useful, but WITHOUT 
13   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14   * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more 
15   * details.
16   *
17   *   You should have received a copy of the GNU General Public License along  
18   * with this program; if not, write to the Free Software Foundation, Inc., 
19   * 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20   */
21  package net.sf.magicproject.ui.component;
22  
23  import java.awt.Dimension;
24  import java.awt.Font;
25  import java.awt.Frame;
26  import java.awt.Toolkit;
27  import java.awt.event.KeyAdapter;
28  import java.awt.event.KeyEvent;
29  import java.awt.event.MouseAdapter;
30  import java.awt.event.MouseEvent;
31  import java.io.BufferedReader;
32  import java.io.IOException;
33  import java.io.InputStreamReader;
34  import java.io.Reader;
35  
36  import javax.swing.ImageIcon;
37  import javax.swing.JLabel;
38  import javax.swing.JScrollPane;
39  import javax.swing.JTextArea;
40  import javax.swing.JWindow;
41  import javax.swing.ScrollPaneConstants;
42  
43  import net.sf.magicproject.token.IdConst;
44  import net.sf.magicproject.tools.MToolKit;
45  import net.sf.magicproject.ui.MagicUIComponents;
46  
47  /***
48   * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
49   * @since 0.82
50   */
51  public class SplashScreen extends JWindow {
52  
53  	/***
54  	 * Create a new instance of this class.
55  	 * 
56  	 * @param filename
57  	 *          the picture filename.
58  	 * @param parent
59  	 *          the splash screen's parent.
60  	 * @param waitTime
61  	 *          the maximum time before the screen is hidden.
62  	 */
63  	public SplashScreen(String filename, Frame parent, int waitTime) {
64  		super(parent);
65  		getContentPane().setLayout(null);
66  		toFront();
67  		final JLabel l = new JLabel(new ImageIcon(filename));
68  		final Dimension labelSize = l.getPreferredSize();
69  		l.setLocation(0, 0);
70  		l.setSize(labelSize);
71  		setSize(labelSize);
72  
73  		final Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
74  		setLocation(screenSize.width / 2 - labelSize.width / 2, screenSize.height
75  				/ 2 - labelSize.height / 2);
76  
77  		final JLabel mp = new JLabel("Magic-Project");
78  		mp.setLocation(30, 305);
79  		mp.setSize(new Dimension(300, 30));
80  
81  		final JLabel version = new JLabel(IdConst.VERSION);
82  		version.setLocation(235, 418);
83  		version.setSize(new Dimension(300, 30));
84  
85  		final JTextArea disclaimer = new JTextArea();
86  		disclaimer.setEditable(false);
87  		disclaimer.setLineWrap(true);
88  		disclaimer.setWrapStyleWord(true);
89  		disclaimer.setAutoscrolls(true);
90  		disclaimer.setFont(new Font("Arial", 0, 9));
91  		disclaimer.setTabSize(2);
92  
93  		// Then try and read it locally
94  		Reader inGPL = null;
95  		try {
96  			inGPL = new BufferedReader(new InputStreamReader(MToolKit
97  					.getResourceAsStream(IdConst.FILE_LICENSE)));
98  			disclaimer.read(inGPL, "");
99  		} catch (IOException e) {
100 			e.printStackTrace();
101 		} finally {
102 			if (inGPL != null) {
103 				try {
104 					inGPL.close();
105 				} catch (IOException closeException) {
106 					// Ignore this error
107 				}
108 			}
109 		}
110 
111 		final JScrollPane disclaimerSPanel = new JScrollPane();
112 		disclaimerSPanel
113 				.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
114 		disclaimerSPanel.setViewportView(disclaimer);
115 		disclaimerSPanel.setLocation(27, 340);
116 		disclaimerSPanel.setPreferredSize(new Dimension(283, 80));
117 		disclaimerSPanel.setSize(disclaimerSPanel.getPreferredSize());
118 
119 		getContentPane().add(disclaimerSPanel);
120 		getContentPane().add(version);
121 		getContentPane().add(mp);
122 		getContentPane().add(l);
123 
124 		final int pause = waitTime;
125 		final Runnable waitRunner = new Runnable() {
126 			public void run() {
127 				try {
128 					Thread.sleep(pause);
129 					while (!bKilled) {
130 						Thread.sleep(200);
131 					}
132 				} catch (InterruptedException e) {
133 					// Ignore this error
134 				}
135 				setVisible(false);
136 				dispose();
137 				MagicUIComponents.magicForm.toFront();
138 			}
139 		};
140 
141 		// setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
142 		addMouseListener(new MouseAdapter() {
143 			@Override
144 			public void mousePressed(MouseEvent e) {
145 				setVisible(false);
146 				dispose();
147 				if (MagicUIComponents.magicForm != null)
148 					MagicUIComponents.magicForm.toFront();
149 			}
150 		});
151 		addKeyListener(new KeyAdapter() {
152 			@Override
153 			public void keyPressed(KeyEvent e) {
154 				if (e.getKeyCode() == KeyEvent.VK_SPACE) {
155 					setVisible(false);
156 					dispose();
157 					MagicUIComponents.magicForm.toFront();
158 				}
159 			}
160 		});
161 		setVisible(true);
162 		new Thread(waitRunner, "SplashThread").start();
163 	}
164 
165 	/***
166 	 * Close this splash screen.
167 	 */
168 	public void close() {
169 		bKilled = true;
170 	}
171 
172 	boolean bKilled = false;
173 
174 }