KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ixenon > free > install > SplashScreen2


1 /* $Id$
2  *
3  * Copyright (c) 1999 Xenonsoft Limited. All Rights Reserved.
4  *
5  * This software is protected by copyright. You are hereby notified from
6  * now by reading this message. This software is also the confidential
7  * and proprietary information of Xenonsoft Limited. ("Confidential
8  * Information").
9  *
10  * This software is distributed under the Xenonsoft Public end user
11  * License ("XPeL"), where the machine-readable source code is provided
12  * under the "Open Source" model.
13  * For more information, please read the file "LICENSE-XPL.txt"
14  */

15
16 // SplashScreen2.java
17

18 //
19
// Description:
20
// Application Splash Screen
21
//
22
// Author:
23
// Peter Pilgrim
24
// Wed Nov 11 00:47:31 GMT 1998
25
//
26
// RCS HEADER ``SplashScreen.java''
27
//
28
// $Author$
29
// $Date$
30
// $Source$
31
// $Revision$ $State$ $Locker$
32
//
33
// History
34
// ================================================================================
35
// $Log$
36
//
37

38 package ixenon.free.install;
39
40 // ***SWING*** (Java Foundation Classes) http://java.sun.com/products/jfc
41
import javax.swing.*;
42 import javax.swing.event.*;
43 import javax.swing.border.*; // for BevelBorder
44

45 import java.awt.*;
46 import java.awt.event.*;
47 import java.util.*;
48
49 import ixenon.free.swing.*; // for Pager
50
import ixenon.free.util.*; // for ApplicationResources
51

52
53 /**
54  * An object containing a static methods to show a title screen (also
55  * known as a `Splash' in the center of the screen for a given amount
56  * of time. The method <B>showSplash</B> makes use of application resources
57  * to find an image.
58  *
59  * @see ApplicatioResources
60  * @see ImageIcon
61  * @see Image
62  *
63  * <PRE>
64  *
65  * public static void main(String[] args)
66  * {
67  * ResourceBundle global_bundle;
68  * global_bundle = ResourceBundle.getBundle( "XsqlBundle" );
69  * ApplicationResources.createInstance( global_bundle, "Xsql" );
70  *
71  * // Throw a nice little title page up on the screen first
72  *
73  * SplashScreen2.Closure myClosure = new SplashScreen2.Closure() {
74  * public void waitAndCloseWindow() {
75  * System.out.println("window:"+window );
76  * System.out.println("duration:"+duration );
77  * System.out.print("Sleeping now Zzzzz .... " );
78  * try { Thread.sleep(duration); } catch (Exception e) {}
79  * System.out.println("*timeout* dispose now (terminating program)");
80  * SwingUtilities.invokeLater( new Runnable() {
81  * public void run() {
82  * window.setVisible(false);
83  * window.dispose();
84  * window=null;
85  * System.exit(0);
86  * }
87  * });
88  * }
89  * };
90  *
91  * showSplash(10000, "splashScreen.image", "Xenon-SQL.jpg", myClosure);
92  * }
93  *
94  * </PRE>
95  * <B>WARNING: Multiple Threads and Swing/JFC,
96  * please note the use of the SwingUtilities.invokeLater() method </B>
97  */

98 public class SplashScreen2 {
99
100     /** Default image application resource name */
101     public final static String JavaDoc IMAGE_RESOURCE="splashScreen.image";
102     /** Default image name */
103     public final static String JavaDoc IMAGE_NAME="Xenon-SQL.jpg";
104
105   
106     public static void main(String JavaDoc[] args)
107     {
108     ResourceBundle global_bundle;
109     global_bundle = ResourceBundle.getBundle( "XsqlBundle" );
110     ApplicationResources.createInstance( global_bundle, "Xsql" );
111
112     // Throw a nice little title page up on the screen first
113

114     SplashScreen2.Closure myClosure = new SplashScreen2.Closure() {
115         public void waitAndCloseWindow() {
116         System.out.println("window:"+window );
117         System.out.println("duration:"+duration );
118         System.out.print("Sleeping now Zzzzz .... " );
119         try { Thread.sleep(duration); } catch (Exception JavaDoc e) {}
120         
121         System.out.println("*timeout* dispose now (terminating program)");
122         SwingUtilities.invokeLater( new Runnable JavaDoc() {
123             public void run() {
124             window.setVisible(false);
125             window.dispose();
126             window=null;
127             System.exit(0);
128             }
129         });
130         }
131     };
132         
133     showSplash(10000, "splashScreen.image", "Xenon-SQL.jpg", myClosure);
134     }
135
136
137     /* A method to show a title screen (a so-called `splash') in the
138      * center of the screen for a given amount of time.
139      * Using default resource and image names:
140      * @see IMAGE_RESOURCE
141      * @see IMAGE_NAME
142      * Using the default closure method: @see SplashScreen2.Closure
143      *
144      * @param duration the splash's duration in milliseconds.
145      *
146      * @return JWindow the <I>realized</I> window with the splash image
147      */

148     public static JWindow showSplash(int duration)
149     {
150     return showSplash( duration, IMAGE_RESOURCE, IMAGE_NAME );
151     }
152
153     /* A method to show a title screen (a so-called `splash') in the
154      * center of the screen for a given amount of time.
155      * Using the default closure method: @see SplashScreen2.Closure
156      *
157      * @param duration the splash's duration in milliseconds.
158      * @param resName the application resource name for the splash image.
159      * @param imageName the default splash image name.
160      *
161      * @return JWindow the <I>realized</I> window with the splash image
162      */

163     public static JWindow showSplash(
164     int duration, String JavaDoc resName, String JavaDoc imageName )
165     {
166     return showSplash( duration, resName, imageName, new Closure() );
167     }
168     
169     /* A method to show a title screen (a so-called `splash') in the
170      * center of the screen for a given amount of time.
171      *
172      * @param duration the splash's duration in milliseconds.
173      * @param resName the application resource name for the splash image.
174      * @param imageName the default splash image name.
175      * @param closure a runnable object which can dispose of the splash.
176      *
177      * @return JWindow the <I>realized</I> window with the splash image
178      *
179      * For information about the closure object:
180      * @see SplashScreen2.Closure
181      */

182     public static JWindow showSplash(
183     int duration, String JavaDoc resName, String JavaDoc imageName,
184     SplashScreen2.Closure closure
185     )
186     {
187     // Create a simple JLabel with ImageIcon (our splash image)
188

189     ApplicationResources appres = ApplicationResources.getInstance();
190     String JavaDoc splashImageName =
191         appres.getResourceString( resName, imageName );
192     Image image = appres.getImage( "images", splashImageName, null );
193     ImageIcon splashImage = new ImageIcon( image );
194
195     JWindow splash = new JWindow();
196     JPanel content = (JPanel)splash.getContentPane();
197
198     // set the window's bounds, centering the window
199
int width = splashImage.getIconWidth() + 2; /* 276 */
200     int height = splashImage.getIconHeight() + 2; /* 294 */
201     Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
202     int x = (screen.width-width)/2;
203     int y = (screen.height-height)/2;
204     splash.setBounds(x,y,width,height);
205     
206     // build the splash screen
207
JLabel label = new JLabel( splashImage );
208     JLabel copyrt = new JLabel
209         ("[c] Copyright 1999, XeNoNSoFT Ltd.", JLabel.CENTER);
210     copyrt.setFont(new Font("Sans-Serif", Font.BOLD, 10));
211     content.add(label, BorderLayout.CENTER);
212     content.add(copyrt, BorderLayout.SOUTH);
213     
214     Border loweredBorder = BorderFactory.createLoweredBevelBorder();
215     Border raisedBorder = BorderFactory.createRaisedBevelBorder();
216     content.setBorder( BorderFactory.createCompoundBorder(
217         raisedBorder, loweredBorder) );
218     // content.setBorder(BorderFactory.createLineBorder(Color.black, 3));
219

220     // System.out.println( "showing splash:" +splash );
221
// System.out.println( "splash bounds:" +splash.getBounds() );
222

223     // display it
224
splash.setVisible(true);
225     
226     if (closure != null ) {
227         // Spawn a separate closure thread and set it's parameters.
228
closure.setWindow( splash );
229         closure.setDuration( duration );
230         Thread JavaDoc t1 = new Thread JavaDoc( closure );
231         t1.start();
232     }
233     
234     return (splash);
235     }
236
237
238     /*
239      * Closure runnable thread object class designed to close a splash
240      * screen object. The class can be subclassed to in the <B>run()</B>
241      * to provide an alternative behaviour.
242      *
243      * @see SplashScreen2
244      */

245     public static class Closure implements Runnable JavaDoc {
246
247     /** A reference to the splash screen's Window */
248     protected JWindow window;
249     /** the splash screen's duration time (in milliseconds) */
250     protected int duration;
251     
252     /* Default constructor */
253     public Closure()
254     {
255         window = null; duration = 1;
256     }
257     
258     /** Constructs a closure action
259      * @param window the JWindow splash screen to be disposed
260      * @param duration the splash screen's duration time (in milliseconds)
261      */

262     public Closure( JWindow window, int duration )
263     {
264         this.window = window;
265         this.duration = duration;
266     }
267     
268     /** Sets the window of the splash
269      * @param window the JWindow splash screen to be disposed
270      */

271     public void setWindow( JWindow newWindow )
272     {
273         window = newWindow;
274     }
275     
276     /** Sets the duration of the splash
277      * @param duration the splash screen's duration time (in milliseconds)
278      */

279     public void setDuration( int newDuration )
280     {
281         duration = newDuration;
282     }
283
284     /** A subclassable method to sleep and wake up and dispose
285      * of then spash window.
286      */

287     public void waitAndCloseWindow( )
288     {
289         // FIXME: SwingUtilities.invokeLater( Runnable );
290
try { Thread.sleep(duration); } catch (Exception JavaDoc e) {}
291         SwingUtilities.invokeLater( new Runnable JavaDoc() {
292         public void run() {
293             window.setVisible(false);
294             window.dispose();
295             window=null;
296         }
297         });
298     }
299     
300  
301     /** Action performed method makes the splash screen window
302      * invisible and calls dispose(false) on it
303      */

304     public void run()
305     {
306         // Sleep for a number of milliseconds then pop down the
307
// splash window.
308

309         if (window == null)
310         throw new NullPointerException JavaDoc( "splash closure has null window parameter!!");
311         waitAndCloseWindow();
312     }
313
314     /* Finalizer for this object */
315     public void finalize() throws Throwable JavaDoc
316     {
317         if (window != null ) {
318         window.dispose();
319         window = null;
320         }
321     }
322     }
323     
324 }
325
326 // fini
327
Popular Tags