View Javadoc

1   /*
2    *  XNap Commons
3    *
4    *  Copyright (C) 2005  Steffen Pingel
5    *
6    *  This library is free software; you can redistribute it and/or
7    *  modify it under the terms of the GNU Lesser General Public
8    *  License as published by the Free Software Foundation; either
9    *  version 2.1 of the License, or (at your option) any later version.
10   *
11   *  This library is distributed in the hope that it will be useful,
12   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   *  Lesser General Public License for more details.
15   *
16   *  You should have received a copy of the GNU Lesser General Public
17   *  License along with this library; if not, write to the Free Software
18   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19   */
20  package org.xnap.commons.gui;
21  
22  import java.awt.BorderLayout;
23  import java.awt.Color;
24  import java.awt.Dimension;
25  import java.awt.Font;
26  import java.awt.Toolkit;
27  import java.awt.event.MouseAdapter;
28  import java.awt.event.MouseEvent;
29  import javax.swing.Box;
30  import javax.swing.BoxLayout;
31  import javax.swing.Icon;
32  import javax.swing.JLabel;
33  import javax.swing.JProgressBar;
34  import javax.swing.JWindow;
35  import javax.swing.SwingConstants;
36  import javax.swing.SwingUtilities;
37  import javax.swing.UIManager;
38  import org.xnap.commons.gui.util.GUIHelper;
39  
40  /***
41   * Displays a splash window at the center of the screen.
42   * <p>
43   * The following colors are used as background and foreground colors:
44   * <pre>
45   * UIManager.getColor("TextField.background");
46   * UIManager.getColor("TextField.foreground");
47   * </pre>
48   * 
49   * @author Steffen Pingel
50   */
51  public class SplashWindow extends JWindow
52  {
53  
54  	private static SplashWindow instance = null;
55      private JProgressBar progressBar;
56  	private JLabel statusLabel;
57  	private JLabel imageLabel;
58      private static Object lock = new Object();
59  
60      private SplashWindow(String text, Icon icon)
61      {
62      	Color bg = UIManager.getColor("TextField.background");
63      	Color fg = UIManager.getColor("TextField.foreground");
64  
65  		Box box = new ContainerBox(BoxLayout.Y_AXIS);
66  		setBackground(bg);
67  		getContentPane().setBackground(bg);
68  		getContentPane().setLayout(new BorderLayout());
69  		getContentPane().add(box, BorderLayout.CENTER);
70  		
71  		imageLabel = new JLabel(text);
72  		imageLabel.setForeground(fg);
73  		imageLabel.setIcon(icon);
74  		imageLabel.setHorizontalAlignment(SwingConstants.CENTER);
75  		imageLabel.setVerticalAlignment(SwingConstants.CENTER);
76  		imageLabel.setHorizontalTextPosition(SwingConstants.CENTER);
77  		imageLabel.setVerticalTextPosition(SwingConstants.BOTTOM);
78  		imageLabel.setAlignmentX(0.5f);
79  		box.add(imageLabel);
80  
81  		progressBar = new JProgressBar(0, 100);
82  		progressBar.setBackground(Color.white);
83  		progressBar.setBorder(GUIHelper.createEmptyBorder(5));
84  		progressBar.setAlignmentX(0.5f);
85  		box.add(progressBar);
86  
87  		statusLabel = new JLabel(" ");
88  		statusLabel.setForeground(fg);
89  		statusLabel.setFont(new Font("Dialog", Font.PLAIN, 10));
90  		statusLabel.setHorizontalTextPosition(SwingConstants.CENTER);
91  		statusLabel.setAlignmentX(0.5f);
92  		box.add(statusLabel);
93  
94  		pack();
95  		centerOnScreen();
96  		
97  		// close on mouse click
98  		addMouseListener(new MouseAdapter() {
99  				public void mousePressed(MouseEvent event)
100 				{
101 					synchronized (lock) {
102 						lock.notify();
103 					}
104 				}
105 		});
106     }
107 
108     public void centerOnScreen()
109 	{
110 		// center on screen
111 		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
112 		Dimension windowSize = getPreferredSize();
113 		setLocation(screenSize.width / 2 - (windowSize.width / 2),
114 					screenSize.height / 2 - (windowSize.height / 2));
115 	}
116 
117 	public static SplashWindow createInstance(String text, Icon icon)
118     {
119     	if (instance == null) {
120 			instance = new SplashWindow(text, icon);
121     	}
122     	return instance;
123     }
124     
125     /***
126      * The splash window will be closed automatically after 
127      * <code>timeout</code> milli seconds.
128      * 
129      * @param timeout automatic closing will be disabled, if less or equal
130      *                than 0
131      */
132     public static void showSplashWindow(String text, Icon icon, final long timeout)
133     {
134     	createInstance(text, icon).show(timeout);
135     }
136 
137     /***
138      * Closes the window if shown on screen.
139      */
140     public static void closeSplashWindow()
141     {
142 		if (instance != null) {
143 			synchronized (lock) {
144 				lock.notify();
145 			}
146 		}
147     }	
148 
149     public static SplashWindow getInstance()
150     {
151     	return instance;
152     }
153         
154 	public JProgressBar getProgressBar()
155 	{
156 		return progressBar;
157 	}
158 
159 	public JLabel getStatusLabel()
160 	{
161 		return statusLabel;
162 	}
163     
164 	public JLabel getImageLabel()
165 	{
166 		return imageLabel;
167 	}
168 	
169     /***
170      * 
171      */
172     public static int getProgress()
173     {
174 		return (instance != null) ? instance.progressBar.getValue() : 0;
175     }
176 
177     /***
178      * Increases the progress by <code>delta</code> percent and sets the 
179      * status text to <code>newValue</code>.
180      */
181     public static void incProgress(int delta, String text)
182     {
183 		if (instance != null) {
184 			incProgress(delta);
185 			setText(text);
186 		}
187     }
188     
189     /***
190      * Increases the progress by <code>delta</code> percent.
191      */
192     public static void incProgress(int delta)
193     {
194 		if (instance != null) {
195 			instance.progressBar.setValue(instance.progressBar.getValue() + delta);
196 		}
197     }
198 
199     /***
200      * Sets the progress to <code>progress</code> and the status text to
201      * <code>text</code>.
202      */
203     public static void setProgress(int progress, String text)
204     {
205 		setProgress(progress);
206 		setText(text);
207     }
208 
209     /***
210      * Sets the progress to <code>newValue</code> percent.
211      */
212     public static void setProgress(int newValue)
213     {
214 		if (instance != null) {
215 			instance.progressBar.setValue(newValue);
216 		}
217     }
218 
219     /***
220      * Sets the status text to <code>newValue</code>.
221      */
222     public static void setText(String newValue)
223     {
224 		if (instance != null) {
225 			instance.statusLabel.setText(newValue);
226 		}
227     }
228 
229     public void show(long timeout)
230     {
231     	Thread t = new Thread(new CloseRunner(timeout), "SplashWindowWaiter");
232     	t.start();
233    		
234    		setVisible(true);
235 	}
236     
237 	public static void updateComponentTree()
238 	{
239 		if (instance != null) {
240 			SwingUtilities.updateComponentTreeUI(instance);
241 		}
242 	}
243 
244 	public static class CloseRunner implements Runnable
245 	{
246 		long timeout;
247 
248 		public CloseRunner(long timeout)
249 		{
250 			this.timeout = timeout;
251 		}
252 
253 		public void run()
254 		{
255 			try {
256 				synchronized (lock) {
257 					if (timeout > 0) {
258 						lock.wait(timeout);
259 					}
260 					else {
261 						lock.wait();
262 					}
263 				}
264 			}
265 			catch(InterruptedException e) {
266 			}
267 			
268 			Runnable runner = new Runnable()
269 				{
270 					public void run()
271 					{
272 						instance.setVisible(false);
273 						instance.dispose();
274 						instance = null;
275 					}
276 				};
277 			SwingUtilities.invokeLater(runner);
278 		}
279 	}
280 	/***
281 	 * Inner subclass of Box to override updateUI. 
282 	 */
283 	private class ContainerBox extends Box
284 	{
285 		public ContainerBox(int axis)
286 		{
287 			super(axis);
288 		}
289 		
290 		@Override
291 		public void updateUI() 
292 		{
293 			super.updateUI();
294 	    	Color bg = UIManager.getColor("TextField.background");
295 	    	Color fg = UIManager.getColor("TextField.foreground");
296 	    	
297 			SplashWindow.this.setBackground(bg);
298 			getContentPane().setBackground(bg);
299 			imageLabel.setForeground(fg);
300 			statusLabel.setForeground(fg);
301 			statusLabel.setBackground(bg);
302 		}
303 	}
304 }