KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > server > SplashWindow


1 package com.sslexplorer.server;
2
3 import java.awt.BorderLayout JavaDoc;
4 import java.awt.Color JavaDoc;
5 import java.awt.Component JavaDoc;
6 import java.awt.Dimension JavaDoc;
7 import java.awt.GraphicsConfiguration JavaDoc;
8 import java.awt.GraphicsDevice JavaDoc;
9 import java.awt.Image JavaDoc;
10 import java.awt.Rectangle JavaDoc;
11 import java.awt.Toolkit JavaDoc;
12 import java.awt.event.ActionEvent JavaDoc;
13 import java.awt.event.ActionListener JavaDoc;
14 import java.awt.event.MouseAdapter JavaDoc;
15 import java.awt.event.MouseEvent JavaDoc;
16
17 import javax.swing.ImageIcon JavaDoc;
18 import javax.swing.JComponent JavaDoc;
19 import javax.swing.JFrame JavaDoc;
20 import javax.swing.JLabel JavaDoc;
21 import javax.swing.JPanel JavaDoc;
22 import javax.swing.JWindow JavaDoc;
23 import javax.swing.SwingConstants JavaDoc;
24 import javax.swing.SwingUtilities JavaDoc;
25 import javax.swing.Timer JavaDoc;
26 import javax.swing.border.Border JavaDoc;
27
28 /**
29  * Swing component that can be used to show a <i>splash</i> image, useful for
30  * branding an application and displaying something pretty for the user to look
31  * while the application starts up. <br>
32  * <br>
33  * The splash window will stay visible until either the application invokes the
34  * {@link SplashWindow.dismiss}method or amount of time specified elapses -
35  * whichever is the greater.
36  *
37  * @author $Author: brett $
38  */

39 public class SplashWindow extends JWindow JavaDoc implements ActionListener JavaDoc, SwingConstants JavaDoc {
40
41     // Private instance variables
42
private JPanel JavaDoc mainPanel;
43     private Timer JavaDoc timer;
44     private boolean appDismissed;
45     private boolean timerExpired;
46
47     /**
48      * Construct a new splash
49      *
50      * @param parent parent frame
51      * @param img image to use for splash
52      * @param timeout time to wait for splash to disapper
53      */

54     public SplashWindow(JFrame JavaDoc parent, Image JavaDoc img, int timeout) {
55         this(parent, img, timeout, null);
56     }
57
58     /**
59      * Construct a new splash
60      *
61      * @param parent parent frame
62      * @param img image to use for splash
63      * @param timeout time to wait for splash to disapper
64      * @param accesoryComponent accesory component
65      */

66     public SplashWindow(JFrame JavaDoc parent, Image JavaDoc img, int timeout, JComponent JavaDoc accessoryComponent) {
67         super(parent);
68         //
69
ImageIcon JavaDoc image = new ImageIcon JavaDoc(img);
70         int w = image.getIconWidth() + 5;
71         int h = image.getIconHeight() + 5;
72         // Main panel
73
mainPanel = new JPanel JavaDoc(new BorderLayout JavaDoc());
74         mainPanel.setOpaque(false);
75         // Accesory component (if specified)
76
if (accessoryComponent != null) {
77             mainPanel.add(accessoryComponent, BorderLayout.SOUTH);
78             // Image part
79
}
80         JLabel JavaDoc p = new JLabel JavaDoc(image);
81         p.setBorder(null);
82         p.addMouseListener(new MouseAdapter JavaDoc() {
83
84             public void mouseClicked(MouseEvent JavaDoc e) {
85                 hide();
86             }
87         });
88         mainPanel.add(p, BorderLayout.CENTER);
89         // This panel
90
getContentPane().setLayout(new BorderLayout JavaDoc());
91         getContentPane().add(mainPanel);
92         // Timeout after specified time
93
timer = new Timer JavaDoc(0, null);
94         timer.setRepeats(false);
95         timer.setInitialDelay(timeout);
96         // Centre the splash
97
pack();
98         positionComponent(SwingConstants.CENTER, this);
99     }
100
101     /**
102      * Position a component on the screen (must be a
103      * <code>java.awt.Window</code> to be useful)
104      *
105      * @param p postion from <code>SwingConstants</code>
106      * @param c component
107      */

108     public static void positionComponent(int p, Component JavaDoc c) {
109
110         positionComponent(p, c, c);
111
112     }
113
114     public static void positionComponent(int p, Component JavaDoc c, Component JavaDoc o) {
115         Rectangle JavaDoc d = null;
116         /*
117          * TODO This is very lame doesnt require the component to position
118          * around, just assuming its a window.
119          */

120         try {
121
122             // #ifdef JAVA1
123
/*
124              * throw new Exception();
125              */

126
127             // #else
128
GraphicsConfiguration JavaDoc config = o.getGraphicsConfiguration();
129             GraphicsDevice JavaDoc dev = config.getDevice();
130             d = config.getBounds();
131
132             // #endif JAVA1
133
} catch (Throwable JavaDoc t) {
134         }
135         positionComponent(p, c, d);
136
137     }
138
139     public static void positionComponent(int p, Component JavaDoc c, Rectangle JavaDoc d) {
140         if (d == null) {
141             Dimension JavaDoc s = Toolkit.getDefaultToolkit().getScreenSize();
142             d = new Rectangle JavaDoc(0, 0, s != null ? s.width : 800, s != null ? s.height : 600);
143         }
144
145         switch (p) {
146             case NORTH_WEST:
147                 c.setLocation(d.x, d.y);
148                 break;
149             case NORTH:
150                 c.setLocation(d.x + (d.width - c.getSize().width) / 2, d.y);
151                 break;
152             case NORTH_EAST:
153                 c.setLocation(d.x + (d.width - c.getSize().width), d.y);
154                 break;
155             case WEST:
156                 c.setLocation(d.x, d.y + (d.height - c.getSize().height) / 2);
157                 break;
158             case SOUTH_WEST:
159                 c.setLocation(d.x, d.y + (d.height - c.getSize().height));
160                 break;
161             case EAST:
162                 c.setLocation(d.x + d.width - c.getSize().width, d.y + (d.height - c.getSize().height) / 2);
163                 break;
164             case SOUTH_EAST:
165                 c.setLocation(d.x + (d.width - c.getSize().width), d.y + (d.height - c.getSize().height) - 30);
166                 break;
167             case CENTER:
168                 c.setLocation(d.x + (d.width - c.getSize().width) / 2, d.y + (d.height - c.getSize().height) / 2);
169                 break;
170         }
171     }
172
173     /**
174      * Set the background color of the splash
175      *
176      * @param b background color
177      */

178     public void setBackground(Color JavaDoc b) {
179         getContentPane().setBackground(b);
180         mainPanel.setBackground(b);
181     }
182
183     /**
184      * Set the border
185      *
186      * @param border the border
187      */

188     public void setBorder(Border JavaDoc b) {
189         mainPanel.setBorder(b);
190         positionComponent(SwingConstants.CENTER, this);
191     }
192
193     /**
194      * Get the border
195      *
196      * @return the border
197      */

198     public Border JavaDoc getBorder() {
199         return mainPanel.getBorder();
200     }
201
202     /**
203      * Remove the splash image from the screen
204      */

205     public void dismiss() {
206         if (!timerExpired) {
207             appDismissed = true;
208         } else {
209             dismissImpl();
210         }
211     }
212
213     private void dismissImpl() {
214         SwingUtilities.invokeLater(new Runnable JavaDoc() {
215             public void run() {
216                 dispose();
217             }
218         });
219     }
220
221     /*
222      * (non-Javadoc)
223      *
224      * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
225      */

226     public void actionPerformed(ActionEvent JavaDoc evt) {
227         if (appDismissed) {
228             dismissImpl();
229         } else {
230             timerExpired = true;
231         }
232     }
233
234     /*
235      * (non-Javadoc)
236      *
237      * @see java.awt.Component#show()
238      */

239     public void show() {
240         timer.addActionListener(this);
241         timer.start();
242         super.show();
243     }
244
245     /*
246      * (non-Javadoc)
247      *
248      * @see java.awt.Component#hide()
249      */

250     public void hide() {
251         timer.stop();
252         timer.removeActionListener(this);
253         super.hide();
254     }
255 }
Popular Tags