Source code: org/dinopolis/util/gui/SplashScreen.java
1 /***********************************************************************
2 * @(#)$RCSfile: SplashScreen.java,v $ $Revision: 1.4 $ $Date: 2003/11/18 09:34:45 $
3 *
4 * Copyright (c) 2001 IICM, Graz University of Technology
5 * Inffeldgasse 16c, A-8010 Graz, Austria.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License (LGPL)
9 * as published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this program; if not, write to the
19 * Free Software Foundation, Inc.,
20 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 ***********************************************************************/
22
23
24 package org.dinopolis.util.gui;
25
26 import java.awt.BorderLayout;
27 import java.awt.Dimension;
28 import java.awt.Toolkit;
29 import java.awt.event.MouseAdapter;
30 import java.awt.event.MouseEvent;
31 import javax.swing.Icon;
32 import javax.swing.ImageIcon;
33 import javax.swing.JLabel;
34 import javax.swing.JPanel;
35 import javax.swing.JProgressBar;
36 import javax.swing.JWindow;
37 import javax.swing.SwingUtilities;
38
39 //----------------------------------------------------------------------
40 /**
41 * This class is used to visualize a simple splash screen icon for a
42 * definable amount of time. Additionally, a progress bar and a status
43 * line can be displayed.
44 *
45 * @author Dieter Freismuth
46 * @version $Revision: 1.4 $
47 */
48
49 public class SplashScreen extends JWindow
50 {
51
52 JProgressBar progress_bar_;
53 JLabel status_line_;
54
55 //----------------------------------------------------------------------
56 /**
57 * Constructor taking an Icon that is to be displayed and the time
58 * to display this icon.
59 *
60 * @param banner the icon to display.
61 * @param display_millis the amount of milliseconds to display the
62 * banner.
63 */
64
65 public SplashScreen(Icon banner, int display_millis)
66 {
67 this(new JLabel(banner), display_millis);
68 }
69
70 //----------------------------------------------------------------------
71 /**
72 * Constructor taking an Icon that is to be displayed and the time
73 * to display this icon. Additionally, the min/max values for the
74 * progress bar can be set. If both are equal to zero, no progress
75 * bar and status line are displayed.
76 *
77 * @param banner the icon to display.
78 * @param display_millis the amount of milliseconds to display the
79 * banner.
80 * @param progress_min_value the min value for the progress bar.
81 * @param progress_max_value the max value for the progress bar.
82 */
83
84 public SplashScreen(Icon banner, int display_millis,
85 int progress_min_value, int progress_max_value)
86 {
87 this(new JLabel(banner), display_millis, progress_min_value, progress_max_value);
88 }
89
90 //----------------------------------------------------------------------
91 /**
92 * Constructor taking a String that is to be displayed and the time
93 * to display it.
94 *
95 * @param text the text to display.
96 * @param display_millis the amount of milliseconds to display the
97 * banner.
98 */
99
100 public SplashScreen(String text, int display_millis)
101 {
102 this(new JLabel(text), display_millis);
103 }
104
105 //----------------------------------------------------------------------
106 /**
107 * Constructor taking a String that is to be displayed and the time
108 * to display it. Additionally, the min/max values for the progress
109 * bar can be set. If both are equal to zero, no progress bar and
110 * status line are displayed.
111 *
112 * @param text the text to display.
113 * @param display_millis the amount of milliseconds to display the
114 * banner.
115 * @param progress_min_value the min value for the progress bar.
116 * @param progress_max_value the max value for the progress bar.
117 */
118
119 public SplashScreen(String text, int display_millis,
120 int progress_min_value, int progress_max_value)
121 {
122 this(new JLabel(text), display_millis, progress_min_value, progress_max_value);
123 }
124
125 //----------------------------------------------------------------------
126 /**
127 * Constructor taking a label that is to be displayed and the time
128 * to display it.
129 *
130 * @param label the label to display.
131 * @param display_millis the amount of milliseconds to display the
132 * banner.
133 */
134
135 public SplashScreen(JLabel label, int display_millis)
136 {
137 this(label,display_millis,0,0);
138 }
139
140
141 //----------------------------------------------------------------------
142 /**
143 * Constructor taking a label that is to be displayed and the time
144 * to display it. Additionally, the min/max values for the progress
145 * bar can be set. If both are equal to zero, no progress bar and
146 * status line are displayed.
147 *
148 * @param label the label to display.
149 * @param display_millis the amount of milliseconds to display the
150 * banner.
151 * @param progress_min_value the min value for the progress bar.
152 * @param progress_max_value the max value for the progress bar.
153 */
154
155 public SplashScreen(JLabel label, int display_millis,
156 int progress_min_value, int progress_max_value)
157 {
158 getContentPane().add(label, BorderLayout.CENTER);
159 if((progress_min_value != 0) || (progress_max_value != 0))
160 {
161 JPanel status_panel = new JPanel(new BorderLayout());
162 status_panel.add(progress_bar_ = new JProgressBar(progress_min_value,progress_max_value),
163 BorderLayout.CENTER);
164 status_panel.add(status_line_ = new JLabel(),BorderLayout.SOUTH);
165 getContentPane().add(status_panel,BorderLayout.SOUTH);
166 }
167 pack();
168 Dimension screen_size = Toolkit.getDefaultToolkit().getScreenSize();
169 Dimension label_size = getContentPane().getPreferredSize();
170 setLocation(screen_size.width/2 - (label_size.width/2),
171 screen_size.height/2 - (label_size.height/2));
172 addMouseListener(new MouseAdapter()
173 {
174 public void mousePressed(MouseEvent e)
175 {
176 close();
177 }
178 });
179
180 final Runnable closer = new Runnable()
181 {
182 public void run()
183 {
184 close();
185 }
186 };
187 final int display_millis_ = display_millis;
188 setVisible(true);
189 if (display_millis_ >= 0)
190 {
191 Runnable waiter = new Runnable()
192 {
193 public void run()
194 {
195 try
196 {
197 Thread.sleep(display_millis_);
198 SwingUtilities.invokeAndWait(closer);
199 }
200 catch(Exception e)
201 {
202 e.printStackTrace();
203 }
204 }
205 };
206 Thread splash = new Thread(waiter, "SplashThread");
207 splash.setDaemon(true);
208 splash.start();
209 }
210 }
211
212 //----------------------------------------------------------------------
213 /**
214 * Set the status and the current progress for the splash screen.
215 *
216 * @param text the text to be displayed in the status line.
217 * @param progress_value the percentage of the progress.
218 */
219 public void setStatus(String text, int progress_value)
220 {
221 setStatus(text);
222 setProgress(progress_value);
223 }
224
225 //----------------------------------------------------------------------
226 /**
227 * Set the status for the splash screen.
228 *
229 * @param text the text to be displayed in the status line.
230 */
231 public void setStatus(String text)
232 {
233 if(status_line_ != null)
234 status_line_.setText(text);
235 }
236
237 //----------------------------------------------------------------------
238 /**
239 * Set the current progress for the splash screen.
240 *
241 * @param progress_value the percentage of the progress.
242 */
243 public void setProgress(int progress_value)
244 {
245 if(progress_bar_ != null)
246 progress_bar_.setValue(progress_value);
247 }
248
249
250 //----------------------------------------------------------------------
251 /**
252 * Close the splash screen and frees all resources.
253 */
254 public void close()
255 {
256 setVisible(false);
257 dispose();
258 }
259
260 //----------------------------------------------------------------------
261 /**
262 * @param arguments the command line args, will be ignored
263 */
264
265 public static void main(String[] arguments)
266 {
267 SplashScreen splash = new SplashScreen(new ImageIcon("/filer/cdaller/texte/pic/cdaller.jpg"), 5000,0,100);
268 splash.setStatus("loading plugins...",10);
269 try
270 {
271 Thread.sleep(500);
272 }
273 catch(Exception ignore) {}
274 splash.setStatus("initialize plugins...",20);
275 try
276 {
277 Thread.sleep(500);
278 }
279 catch(Exception ignore) {}
280 splash.setStatus("start plugin 1...",30);
281 try
282 {
283 Thread.sleep(500);
284 }
285 catch(Exception ignore) {}
286 splash.setStatus("start another plugin ...",50);
287 try
288 {
289 Thread.sleep(500);
290 }
291 catch(Exception ignore) {}
292 splash.setStatus("finish first plugin...",70);
293 try
294 {
295 Thread.sleep(500);
296 }
297 catch(Exception ignore) {}
298 splash.setStatus("open window...",100);
299
300 }
301
302 }
303