Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
SplashScreen |
|
| 2.25;2.25 |
1 | package net.sf.jabref; | |
2 | ||
3 | //import javax.swing.*; | |
4 | import java.awt.EventQueue; | |
5 | import java.awt.Frame; | |
6 | import java.awt.Graphics; | |
7 | import java.awt.Image; | |
8 | import java.awt.MediaTracker; | |
9 | import java.awt.Toolkit; | |
10 | import java.awt.Window; | |
11 | import java.net.URL; | |
12 | ||
13 | /** | |
14 | * <p>Title: </p> | |
15 | * <p>Description: </p> | |
16 | * <p>Copyright: Copyright (c) 2003</p> | |
17 | * <p>Company: </p> | |
18 | * @author not attributable | |
19 | * @version 1.0 | |
20 | */ | |
21 | ||
22 | public class SplashScreen extends Window { | |
23 | private Image splashImage; | |
24 | 958628 | private boolean paintCalled = false; |
25 | ||
26 | public SplashScreen(Frame owner) { | |
27 | 958628 | super(owner); |
28 | 958628 | URL imageURL = SplashScreen.class.getResource("/images/splash.png"); |
29 | 958628 | splashImage = Toolkit.getDefaultToolkit().createImage(imageURL); |
30 | ||
31 | // Load the image | |
32 | 958628 | MediaTracker mt = new MediaTracker(this); |
33 | 958628 | mt.addImage(splashImage,0); |
34 | try { | |
35 | 958628 | mt.waitForID(0); |
36 | 958628 | } catch(InterruptedException ie) {} |
37 | ||
38 | ||
39 | // Center the window on the screen. | |
40 | 958628 | int imgWidth = splashImage.getWidth(this); |
41 | 958628 | int imgHeight = splashImage.getHeight(this); |
42 | ||
43 | 958628 | setSize(imgWidth, imgHeight); |
44 | 958628 | setLocationRelativeTo(null); |
45 | ||
46 | /* Dimension screenDim = Toolkit.getDefaultToolkit().getScreenSize(); | |
47 | setLocation( | |
48 | (screenDim.width - imgWidth) / 2, | |
49 | (screenDim.height - imgHeight) / 2 | |
50 | ); | |
51 | */ | |
52 | ||
53 | 958628 | } |
54 | ||
55 | ||
56 | /** | |
57 | * Updates the display area of the window. | |
58 | */ | |
59 | public void update(Graphics g) { | |
60 | // Note: Since the paint method is going to draw an | |
61 | // image that covers the complete area of the component we | |
62 | // do not fill the component with its background color | |
63 | // here. This avoids flickering. | |
64 | ||
65 | 0 | g.setColor(getForeground()); |
66 | 0 | paint(g); |
67 | 0 | } |
68 | /** | |
69 | * Paints the image on the window. | |
70 | */ | |
71 | ||
72 | public void paint(Graphics g) { | |
73 | 958628 | g.drawImage(splashImage, 0, 0, this); |
74 | ||
75 | // Notify method splash that the window | |
76 | // has been painted. | |
77 | 958628 | if (! paintCalled) { |
78 | 958628 | paintCalled = true; |
79 | 958628 | synchronized (this) { notifyAll(); } |
80 | } | |
81 | 958628 | } |
82 | ||
83 | /** | |
84 | * Constructs and displays a SplashWindow.<p> | |
85 | * This method is useful for startup splashs. | |
86 | * Dispose the returned frame to get rid of the splash window.<p> | |
87 | * | |
88 | * @param splashImage The image to be displayed. | |
89 | * @return Returns the frame that owns the SplashWindow. | |
90 | */ | |
91 | ||
92 | public static Frame splash() { | |
93 | 958628 | Frame f = new Frame(); |
94 | 958628 | SplashScreen w = new SplashScreen(f); |
95 | ||
96 | // Show the window. | |
97 | 958628 | w.setVisible(true); |
98 | 958628 | w.toFront(); |
99 | ||
100 | // Note: To make sure the user gets a chance to see the | |
101 | // splash window we wait until its paint method has been | |
102 | // called at least once by the AWT event dispatcher thread. | |
103 | ||
104 | // sebwills adds: However, just in case the paint method never gets called | |
105 | // (e.g. if the splashscreen is completely obscured by an 'always on top' | |
106 | // window of some other application), we time-out after 5 seconds. | |
107 | 958628 | if (! EventQueue.isDispatchThread()) { |
108 | 958628 | synchronized (w) { |
109 | 958628 | if (! w.paintCalled) { |
110 | try { | |
111 | 958628 | w.wait(5000); |
112 | 958628 | } catch (InterruptedException e) {} |
113 | } | |
114 | 958628 | } |
115 | } | |
116 | 958628 | return f; |
117 | } | |
118 | } |