KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sshtools > ui > swing > SplashWindow


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

29 public class SplashWindow
30     extends JWindow JavaDoc
31     implements ActionListener JavaDoc {
32
33   // Private instance variables
34
private JPanel JavaDoc mainPanel;
35   private Timer JavaDoc timer;
36   private boolean appDismissed;
37   private boolean timerExpired;
38
39   /**
40    * Construct a new splash
41    *
42    * @param parent parent frame
43    * @param img image to use for splash
44    * @param timeout time to wait for splash to disapper
45    */

46   public SplashWindow(JFrame JavaDoc parent, Image JavaDoc img, int timeout) {
47     this(parent, img, timeout, null);
48   }
49
50   /**
51    * Construct a new splash
52    *
53    * @param parent parent frame
54    * @param img image to use for splash
55    * @param timeout time to wait for splash to disapper
56    * @param accesoryComponent accesory component
57    */

58   public SplashWindow(JFrame JavaDoc parent, Image JavaDoc img, int timeout,
59                       JComponent JavaDoc accessoryComponent) {
60     super(parent);
61     //
62
ImageIcon JavaDoc image = new ImageIcon JavaDoc(img);
63     int w = image.getIconWidth() + 5;
64     int h = image.getIconHeight() + 5;
65     // Main panel
66
mainPanel = new JPanel JavaDoc(new BorderLayout JavaDoc());
67     mainPanel.setOpaque(false);
68     // Accesory component (if specified)
69
if (accessoryComponent != null) {
70       mainPanel.add(accessoryComponent, BorderLayout.SOUTH);
71       // Image part
72
}
73     JLabel JavaDoc p = new JLabel JavaDoc(image);
74     p.setBorder(null);
75     mainPanel.add(p, BorderLayout.CENTER);
76     // This panel
77
getContentPane().setLayout(new BorderLayout JavaDoc());
78     getContentPane().add(mainPanel);
79     // Timeout after specified time
80
timer = new Timer JavaDoc(0, null);
81     timer.setRepeats(false);
82     timer.setInitialDelay(timeout);
83     // Centre the splash
84
pack();
85     UIUtil.positionComponent(SwingConstants.CENTER, this);
86   }
87
88   /**
89    * Set the background color of the splash
90    *
91    * @param b background color
92    */

93   public void setBackground(Color JavaDoc b) {
94     getContentPane().setBackground(b);
95     mainPanel.setBackground(b);
96   }
97
98   /**
99    * Set the border
100    *
101    * @param border the border
102    */

103   public void setBorder(Border JavaDoc b) {
104     mainPanel.setBorder(b);
105     UIUtil.positionComponent(SwingConstants.CENTER, this);
106   }
107
108   /**
109    * Get the border
110    *
111    * @return the border
112    */

113   public Border JavaDoc getBorder() {
114     return mainPanel.getBorder();
115   }
116
117   /**
118    * Remove the splash image from the screen
119    */

120   public void dismiss() {
121     if (!timerExpired) {
122       appDismissed = true;
123     }
124     else {
125       dismissImpl();
126     }
127   }
128
129   private void dismissImpl() {
130       SwingUtilities.invokeLater(new Runnable JavaDoc() {
131           public void run() {
132               dispose();
133               
134           }
135       });
136   }
137
138   /*
139    * (non-Javadoc)
140    *
141        * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
142    */

143   public void actionPerformed(ActionEvent JavaDoc evt) {
144     if (appDismissed) {
145       dismissImpl();
146     }
147     else {
148       timerExpired = true;
149     }
150   }
151
152   /* (non-Javadoc)
153    * @see java.awt.Component#show()
154    */

155   public void show() {
156     timer.addActionListener(this);
157     timer.start();
158     super.show();
159   }
160
161   /* (non-Javadoc)
162    * @see java.awt.Component#hide()
163    */

164   public void hide() {
165     timer.stop();
166     timer.removeActionListener(this);
167     super.hide();
168   }
169 }
Popular Tags