View Javadoc

1   /*
2    * $Id$
3    *
4    * The contents of this file are subject to the Mozilla Public License 
5    * Version 1.1 (the "License"); you may not use this file except in 
6    * compliance with the License. You may obtain a copy of the License at 
7    * http://www.mozilla.org/MPL/ 
8    *
9    * Software distributed under the License is distributed on an "AS IS" basis, 
10   * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
11   * for the specific language governing rights and limitations under the License.
12   *
13   * The Original Code is XML Hammer code. (org.xmlhammer.*)
14   *
15   * The Initial Developer of the Original Code is Edwin Dankert. Portions created 
16   * by the Initial Developer are Copyright (C) 2005 - 2006 the Initial Developer. 
17   * All Rights Reserved.
18   *
19   * Contributor(s): Edwin Dankert <edankert@gmail.com>
20   */
21  
22  package org.xmlhammer.gui;
23  
24  import java.awt.BorderLayout;
25  import java.awt.Color;
26  import java.awt.Dimension;
27  import java.awt.Font;
28  import java.awt.Toolkit;
29  import java.awt.event.ActionEvent;
30  import java.awt.event.ActionListener;
31  
32  import javax.swing.Box;
33  import javax.swing.ImageIcon;
34  import javax.swing.JFrame;
35  import javax.swing.JLabel;
36  import javax.swing.JPanel;
37  import javax.swing.JProgressBar;
38  import javax.swing.JWindow;
39  import javax.swing.SwingConstants;
40  import javax.swing.SwingUtilities;
41  import javax.swing.border.CompoundBorder;
42  import javax.swing.border.EmptyBorder;
43  import javax.swing.border.LineBorder;
44  
45  import org.apache.log4j.Logger;
46  import org.bounce.LinkButton;
47  import org.bounce.QButton;
48  import org.bounce.util.URIUtils;
49  import org.xmlhammer.Identity;
50  import org.xmlhammer.gui.util.ExternalApplicationLauncher;
51  
52  /***
53   * The splash screen.
54   *
55   * @version	$Revision$, $Date$
56   * @author Edwin Dankert <edankert@gmail.com>
57   */
58  public class Splash extends JWindow {
59  	private static final long serialVersionUID = -7942336483005539892L;
60  
61  	private JLabel descriptionField	= null;
62  	private JLabel versionField		= null;
63  	private LinkButton referenceField	= null;
64  	private JLabel copyrightField	= null;
65  	private JLabel statusField		= null;
66  	private JLabel titleField 		= null;
67  
68  	private JProgressBar progressBar = null;
69  	private JPanel contentPane = null;
70  	private JPanel productPanel = null;
71  	private JPanel progressPanel = null;
72  	
73  	private QButton cancelButton = null;
74  	private JPanel progressField = null;
75  
76  	/***
77  	 * Constructor for the Splash screen!
78  	 *
79  	 * @param frame the parent frame.
80  	 * @param identity the version and copyright info for the browser.
81  	 * @param icon the icon for the browser.
82  	 */
83  	public Splash( JFrame owner, Identity identity, ImageIcon icon) {
84  	
85  		// To make sure the Splash window stays 'on top'.
86  		super( owner);
87  		
88  		contentPane = new JPanel( new BorderLayout());
89  		
90  		titleField = new JLabel( identity.getTitle());
91  		Font prevFont = titleField.getFont();
92  		titleField.setFont( new Font( prevFont.getFontName(), Font.BOLD, prevFont.getSize() + 8));
93  
94  		descriptionField = new JLabel( identity.getDescription());
95  		versionField = new JLabel( "Version: "+identity.getVersion());
96  
97  		referenceField = new LinkButton( identity.getReference());
98  		referenceField.setForeground( Color.blue);
99  		referenceField.setHorizontalAlignment( SwingConstants.CENTER);
100 		referenceField.addActionListener( new ActionListener() {
101 			public void actionPerformed( ActionEvent e) {
102 				try {
103 					ExternalApplicationLauncher.getInstance().browse(URIUtils.createURI(referenceField.getText()));
104 				} catch( Exception ex) {
105                     Logger.getLogger("org.xmlhammer.gui.Splash").debug(ex);
106 				}
107 			}
108 		});
109 //		referenceField.setForeground( new Color( 0, 0, 255));
110 
111 		copyrightField = new JLabel( identity.getCopyright());
112 		copyrightField.setHorizontalAlignment( SwingConstants.CENTER);
113 
114 		JLabel iconField = new JLabel( icon);
115 		iconField.setBorder( new EmptyBorder( 0, 10, 0, 10));
116 		
117 		Box descriptionPanel = Box.createVerticalBox();
118 		descriptionPanel.add( titleField);
119 		descriptionPanel.add( descriptionField);
120 		descriptionPanel.add( versionField);
121 		
122 		productPanel = new JPanel( new BorderLayout());
123 	
124 		productPanel.add( descriptionPanel, BorderLayout.CENTER);
125 		productPanel.add( iconField, BorderLayout.EAST);
126 		productPanel.setBorder( new EmptyBorder( 5, 5, 10, 5));
127 
128 		statusField = new JLabel( "Status:");
129 		cancelButton = new QButton( "Cancel");
130 		progressBar = new JProgressBar( 0, 100);
131 		progressField = new JPanel( new BorderLayout());
132 
133 		progressField.add( progressBar, BorderLayout.CENTER);
134 		progressField.setBorder( new EmptyBorder( 5, 0, 5, 10));
135 		progressBar.setPreferredSize( new Dimension( 0, 16));
136 		progressBar.setBorderPainted( false);
137 		
138 		cancelButton.addActionListener( new ActionListener() {
139 			public void actionPerformed( ActionEvent e) {
140 				System.exit(0);
141 			}
142 		});
143 		
144 		contentPane.setBorder( new EmptyBorder( 5, 5, 5, 5));
145 		
146 		progressPanel = new JPanel( new BorderLayout());
147 		progressPanel.add( statusField, BorderLayout.NORTH);
148 		progressPanel.add( cancelButton, BorderLayout.EAST);
149 		progressPanel.add( progressField, BorderLayout.CENTER);
150 		progressPanel.add( copyrightField, BorderLayout.SOUTH);
151 
152 		copyrightField.setBorder( new EmptyBorder( 10, 0, 0, 0));
153 		progressPanel.setBorder( new EmptyBorder( 10, 0, 5, 0));
154 
155 		contentPane.add( productPanel, BorderLayout.NORTH);
156 		contentPane.add( referenceField, BorderLayout.CENTER);
157 		contentPane.add( progressPanel, BorderLayout.SOUTH);
158 		
159 		setContentPane( contentPane);
160 	}
161 	
162 	/***
163 	 * Shows the status of the creation of the main application!
164 	 *
165 	 * @param status the description of the status.
166 	 * @param percentage the percentage of the status bar.
167 	 */
168 	public void showStatus( final String status, final int percentage)  {
169 		Logger.getLogger(getClass()).debug("Splash.showStatus( "+status+", "+percentage+")");
170 
171 		SwingUtilities.invokeLater( new SplashRunner( this) {
172             @Override
173 		    public void run() {
174 				statusField.setText( status);
175 				progressBar.setValue( percentage);
176 		    }
177 		});
178 	}
179 
180 	/***
181 	 * Sets the foreground color of the splash screen.
182 	 *
183 	 * @param foreground the foreground color.
184 	 */
185     @Override
186 	public void setForeground(Color foreground) {
187 		copyrightField.setForeground( foreground);
188 		statusField.setForeground( foreground);
189 		
190 		progressBar.setForeground( foreground);
191 		progressField.setBorder( new CompoundBorder( 
192 									new EmptyBorder( 4, 0, 4, 10),
193 									new LineBorder( foreground, 1)));
194 		productPanel.setBackground( foreground);
195 		cancelButton.setForeground( foreground);
196 		cancelButton.setBorder( new CompoundBorder( 
197 									new LineBorder( foreground, 1),
198 									new EmptyBorder( 0, 10, 0, 10)));
199 		cancelButton.setPressedBackground( foreground);
200 	}
201 
202 	/***
203 	 * Sets the background color of the splash screen.
204 	 *
205 	 * @param background the background color.
206 	 */
207     @Override
208 	public void setBackground( Color background) {
209 		titleField.setForeground( background);
210 		descriptionField.setForeground( background);
211 		versionField.setForeground( background);
212 		
213 		contentPane.setBackground( background);
214 		progressBar.setBackground( background);
215 		progressPanel.setBackground( background);
216 		progressField.setBackground( background);
217 		cancelButton.setBackground( background);
218 		cancelButton.setPressedForeground( background);
219 	}
220 
221 	/***
222 	 * Shows the splash screen for a certain time, needs to be run in a thread for this.
223 	 *
224 	 * @param duration the time the splash screen should be shown for.
225 	 */
226 	public void start( final long duration)  {
227         Logger.getLogger(getClass()).debug("Splash.start( "+duration+")");
228 		
229 		// Create a runnable object ...
230 		Runnable timer = new SplashRunner( this) {
231             @Override
232 			public void run()  {
233 				try  {
234 					getSplash().start();
235 					Thread.sleep( duration);
236 					getSplash().stop();
237 				} catch ( Exception e) {
238                     Logger.getLogger("org.xmlhammer.gui.Splash").debug(e);
239 				}
240 			} 
241 		};
242 
243 		// Create and start the thread ...
244 		Thread thread = new Thread( timer);
245 		thread.start();
246 	}
247 
248 	/***
249 	 * Shows the splash screen.
250 	 **/
251 	public void start()  {
252         Logger.getLogger(getClass()).debug("Splash.start()");
253 
254 		SwingUtilities.invokeLater( new SplashRunner( this) {
255             @Override
256 		    public void run() {
257 				Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
258 
259 				int width = getSplash().getSize().width;
260 				int height = getSplash().getSize().height;
261 				
262 				getSplash().setLocation( (d.width - width)/2, (d.height - height)/2);
263 
264 				getSplash().setVisible(true);
265 		    }
266 		});
267 	}
268 
269 	/***
270 	 * Hide the splash screen.
271 	 **/
272 	public void stop()  {
273         Logger.getLogger(getClass()).debug("Splash.stop()");
274 
275 		if ( isVisible()) {
276 			SwingUtilities.invokeLater( new SplashRunner( this) {
277                 @Override
278 			    public void run() {
279 					getSplash().setVisible( false);
280 					getSplash().dispose();
281 			    }
282 			});
283 		}
284 	}
285 	
286 	/***
287 	 * Hide the splash screen after x milli seconds.
288 	 *
289 	 * @param duration the time after which the splash screen should be hidden.
290 	 **/
291 	public void stop( final long duration)  {
292         Logger.getLogger(getClass()).debug("Splash.stop( "+duration+")");
293 
294 		// Create a runnable object ...
295 		Runnable timer = new SplashRunner( this) {
296             @Override
297 			public void run()  {
298 				try  {
299 					Thread.sleep( duration);
300 					getSplash().stop();
301 				} catch ( Exception e) {
302 					e.printStackTrace();
303 				}
304 			} 
305 		};
306 
307 		// Create and start the thread ...
308 		Thread thread = new Thread( timer);
309 		thread.start();
310 	}
311 
312 	private abstract class SplashRunner implements Runnable {
313 		private Splash splash = null;
314 		
315 		/***
316 		 * Constructs a splash runner that contains the Splash object.
317 		 *
318 		 * @param the Splash screen.
319 		 **/
320 		public SplashRunner( Splash splash) {
321 			this.splash = splash;
322 		}
323 		
324 		/***
325 		 * Returns the Splash object.
326 		 *
327 		 * @return Splash.
328 		 **/
329 		public Splash getSplash() {
330 			return splash;
331 		}
332 		
333 		/***
334 		 * Abstract implementation of the run method.
335 		 **/
336 		public abstract void run();
337 	}
338 }