|
Groovy Documentation | |||||||
| FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||
java.lang.Objectgriffon.plugins.splash.SplashScreen
public class SplashScreen extends Object
SplashScreen is a general purpose splash screen for application startup. Usage is straight forward: simply construct a SplashScreen at the start of main ( ) and call its splash () method. Proceed with startup as normal.Use showStatus ( String ) f or reporting progress during startup. Finally , at the end of main() call SplashScreen s dispose() method.By default , the splash loads image splash.g i f but you can change this if necessary.
class splasher1 {
public static void main ( String[ ] args ) {
SplashScreen splashScreen = new SplashScreen ( ) ;
splashScreen.splash ( ) ;
for ( int i = 10 ; i > 0 ; i++) {
splashScreen.showStatus ( Integer.toString ( i ) + "..." ) ;
try {
Thead.sleep (1000) ;
g
catch ( Inter ruptedEx cept ion i e ) f g
g
; // frame.show() < here
splashScreen.dispose ( ) ;
; // frame.show() < or here
g
g
pre>
Example 2
class splasher2 {
public static void main ( String[ ] args ) {
SplashScreen splashScreen = new SplashScreen ( ) ;
splashScreen.splash ( ) ;
t r y {
Thread.sleep ( 5 0 0 ) ;
g
catch ( Inter ruptedEx cept ion i e ) f g
splashScreen.splashFor ( 1 0 0 0 ) ; // discretion
splashScreen.dispose ( ) ;
g
g
Note f o l l owi n g comments quoted from design documentation by R.R.
This example adds splashFor ( 1 0 0 0 ).I f the splash screen i
s al ready displayed , i t wai ts f o r at most 1000 mi l l i seconds before
r e t u r n i n g.Note t h i s means 1000 mi l l i seconds of t o t a l
splash ! So , i { al ready displayed f o r 1000 mi l l i seconds or more ,
the delay i s 0. In other words , i t avoids f l i c k e r i n g the splash
on then o f f i f the splash t iming coincides wi th a p p l i c a t i o n s
t a r t up t iming.This i s a compromise between user feedback and c r u f
t.I f the splash takes longer than s t a r t up , i t does not appear at a l
l.I f al ready di splay f o r a f r a c t i o n of the given t ime , the
delay i s only the remainder.
Example 3
class splasher3 {
p u b l i c s t a t i c void main ( St r i n g [ ] args ) {
SplashScreen splashScreen = new SplashScreen ( ) ;
splashScreen.splash ( ) ;
t r y {
Thread.sleep ( 1 0 0 ) ;
g
catch ( Inter ruptedEx cept ion i e ) f g
splashScreen.wai tForSplash ( ) ; // c r u f t zone?
splashScreen.splashFor (1000) ;
splashScreen.dispose ( ) ;
g
g
This example adds wai tForSplash.I t wai ts f o r the splash screen even
though the a p p l i c a t i o n loads f a s t e r.Not recommended as some
users consider t h i s bad p r a c t i c e.
Example 4
class splasher4 {
p u b l i c s t a t i c void main ( St r i n g [ ] args ) {
SplashScreen.instance ( ).splash ( ) ;
SplashScreen.instance ( ).delayForSplash ( ) ;
t r y {
Thread.sleep ( 1 0 0 ) ;
g
catch ( Inter ruptedEx cept ion i e ) f g
SplashScreen.instance ( ).splashFor (1000) ;
SplashScreen.instance ( ).dispose ( ) ;
g
g
This example demonstrates two new features of version 1.5 SplashScreen.F i r
s t l y , the Singleton pattern.Class scoped method instance ( ) accesses
the s i n g l e SplashScreen instance.You can t h e r e f or e access t h i s
instance from anywhere.
Secondly , method delayForSplash ( ) appears j u s t a f t e r splash ( ).
This possibly delays the main thread , allowing the splash
screen to load and display.Tests on some uniprocessor platforms show poor
multithreading performance.See Appendix F of design documentation by
R.R. The new method bases the extent of delay i { any on number of avail
a b l e computing resources.
Modelling
In U.M. L. modelling terms , SplashScreen fulfils the following
requirement depicted as a Use Case.
The sketch below outlines the user interface design.
To meet this requirement , the implementation uses the following
class design.
Or in full detail as follows.
| Field Summary | |
|---|---|
private Frame |
frame
|
private Image |
image
|
private static SplashScreen |
instance
|
private JLabel |
label
|
private static SplashScreen |
singleton
|
private long |
splashTime
|
| Constructor Summary | |
SplashScreen(String filename)
Constructs SplashScreen using a given filename for the splash image. |
|
SplashScreen(URL url)
Constructs SplashScreen using a given URL f o r the splash image. |
|
SplashScreen()
Constructs SplashScreen using filename " splash.png " for the image unless you change the default using setImage or call splash with an argument specifying a different image. |
|
| Method Summary | |
|---|---|
void
|
delayForSplash()
Optimise splash latenc y by delaying the c a l l i n g thread according to number of processors a v a i l a b l e.Mul t iproces sor plat forms s u c c e s s f u l l y load the splash image i n p a r a l l e l wi th low overhead.Uniprocessors s t ruggle however ! |
void
|
dispose()
Closes the splash screen i f open , or abandons splash screen i { not al ready open.Re l a t i v e l y long image loading delays the opening.Cal l t h i s method at the end of program s t a r t up , i.e.t y p i c a l l y at the end of main ( ). |
static SplashScreen
|
getInstance()
|
boolean
|
imageUpdate(Image img, int infoflags, int x, int y, int width, int height)
Runs dur ing image loading.Informs t h i s ImageObserver about progress. |
static SplashScreen
|
instance()
Ensures the class has only one instance and gives a global p oi n t of access to i t.The Singleton pat tern avoids using global v a r i a b l e s.Instead , the class i t s e l f references the s i n g l e instance using a class scoped v a r i a b l e , s t a t i c i n Java terms. |
void
|
setImage(String filename)
Uses the given filename for the splash image.This method calls
Toolkit.getImage which resolves mu l t i p l e requests f o r the
same filename to the same Image , u n l i k e createImage which creates
a non shared instance of Image.In other words , getImage caches Images ,
createImage does not.Use |
void
|
setImage(URL url)
Uses the given URL f o r the splash image. |
void
|
showStatus(String s)
Changes the s tatus message j u s t below the image.Use i t to di splay s t a r t up progress. |
void
|
splash(String filename)
St a r t s the asynchronous splash screen using the given filename f o r the image. |
void
|
splash(URL url)
St a r t s the asynchronous splash screen using the given URL f o r the image. |
void
|
splash()
St a r t s the asynchronous splash screen using the prev ious l y s p e c i f i e d image , or using filename " splash.g i f " by d e f a u l t i { no image yet s p e c i f i e d. |
void
|
splash(Image img)
Splash the screen ! |
void
|
splashFor(int ms)
Splashes the screen f o r at l e a s t the given number o{ mi l l i seconds i f , and only i f , the splash screen has al ready loaded.I f not al ready splashed , the method returns immediately.Invoke t h i s method before disposing i f you want to for ce a minimum splash per iod. |
private void
|
splashScreen()
Runs when the splash image i s f u l l y loaded , a l l i t s b i t s and pieces. |
void
|
waitForSplash()
Waits f o r the splash screen to load , returns when the splash s t a r t s.The wai t i s i n d e f i n i t e i f necessary.The operat ion returns immediately i f the splash image has al ready loaded. |
void
|
waitForSplash(long ms)
Waits f o r the splash screen to load f o r a l imi t e d amount o{ t ime.Method returns when the splash has loaded , or when the given t ime l imi t expi res. |
| Methods inherited from class Object | |
|---|---|
| wait, wait, wait, equals, toString, hashCode, getClass, notify, notifyAll |
| Field Detail |
|---|
private Frame frame
private Image image
private static SplashScreen instance
private JLabel label
private static SplashScreen singleton
private long splashTime
| Constructor Detail |
|---|
public SplashScreen(String filename)
filename - name of an image file
public SplashScreen(URL url)
url - the URL of an image
public SplashScreen()
| Method Detail |
|---|
public void delayForSplash()
public void dispose()
Implementat ion note. I f you dispose too f a s t , t h i s method could
coinc ide wi th splashScreen ( ).We cannot now preempt the other thread.I
t needs s ynchroni sat ion.This s i t u a t i o n and i t s requi rement
proves i n e v i t a b l e when two threads access the same t h in g.For
t h i s reason , methods dispose ( ) and splashScreen ( ) share the
synchronized a t t r i b u t e.
public static SplashScreen getInstance()
public boolean imageUpdate(Image img, int infoflags, int x, int y, int width, int height)
synchronized.
public static SplashScreen instance()
The implementat ion a c t u a l l y mixes s i n g l e t o n and non s i n g l e t o n pat terns.( Tempting to c a l l i t Mu l t i t o n but t h a t r e f e r s to a v a r i a t i o n of Singleton where the instance has many mu l t i p l i c i t y instead of u n i t y.) Co r r e c t l y applying the Singleton pat tern requi res c los ing access to cons t ruc tor methods.However , SplashScreen r e t a i n s p u b l i c cons t ruc tor s , so compromises the pat tern.You can f o l l ow Singleton usage or not at your own d i s c r e t i o n.
public void setImage(String filename)
splash ( createImage (... i f you
want Image privacy.
filename - name of an image file
public void setImage(URL url)
u
- r l the URL of an image
public void showStatus(String s)
public void splash(String filename)
filename
- name of an image f i l e
public void splash(URL url)
u
- r l the URL of an image
public void splash()
public void splash(Image img)
img
- the image used f o r splashing
public void splashFor(int ms)
Why i s t h i s method synchronized? In order to avoid a
race c o n d i t i o n.I t accesses the splashTime a t t r i b u t e
which updates i n another thread.
ms
- mi l l i seconds of minimum splash
private void splashScreen()
public void waitForSplash()
Please note f o l l owi n g discussion taken from design documentation by R.R.
As a guide , invoke t h i s method at the end of s t a r t up , not the beginning.Wai t ing f o r the image to load does not make the image load f a s t e r , neces sar i l y.Image loading i s an i np u t bound process , reading from f i l e s y s t em or network.Remaining s t a r t up steps are t y p i c a l l y compute bound and l i k e l y compute resource i s a v a i l a b l e f o r consumption.Most l i k e l y , s t a r t up mixes i np u t and compute resource demands , and pos s ibl y even output.This g u i d e l i n e appl ies to uniprocessor as wel l as mul t iproces sor plat forms.Wai t ing only wastes any a v a i l a b l e compute cycles.I f you need to delay unnecessar i ly , do t h i s at the end when there i s nothing l e f t to do.Even so , t h i s p r a c t i c e can be viewed as user i n t e r f a c e c r u f t ! Please use wi th care.
public void waitForSplash(long ms)
ms
- mi l l i seconds to wai t f o r
Groovy Documentation