View Javadoc

1   /*
2    *  File: Splash.java 
3    *  Copyright (c) 2004-2007  Peter Kliem (Peter.Kliem@jaret.de)
4    *  A commercial license is available, see http://www.jaret.de.
5    *
6    * All rights reserved. This program and the accompanying materials
7    * are made available under the terms of the Common Public License v1.0
8    * which accompanies this distribution, and is available at
9    * http://www.eclipse.org/legal/cpl-v10.html
10   */
11  package de.jaret.util.ui;
12  
13  import java.io.InputStream;
14  
15  import org.eclipse.swt.SWT;
16  import org.eclipse.swt.graphics.Color;
17  import org.eclipse.swt.graphics.Image;
18  import org.eclipse.swt.graphics.Rectangle;
19  import org.eclipse.swt.layout.GridData;
20  import org.eclipse.swt.layout.GridLayout;
21  import org.eclipse.swt.widgets.Display;
22  import org.eclipse.swt.widgets.Label;
23  import org.eclipse.swt.widgets.Shell;
24  
25  /**
26   * A splash screen implementation for swt. Displays an image and a textmessage (i.e. version information). The image is
27   * retrieved by <code>ClassLoader.getSystemResourceAsStream</code> so it has to be made sure that the resource is in
28   * the classpath. Usage is fairly simple: construction will show the splash, dispose if you're done with the splash.
29   * 
30   * @author Peter Kliem
31   * @version $Id: Splash.java 242 2007-02-11 21:05:07Z olk $
32   */
33  public class Splash {
34      Image _image;
35      Shell _splashShell;
36      Label _label;
37      Label _msgLabel;
38      Display _display;
39  
40      /**
41       * Constructor for the splash screen
42       * 
43       * @param rscName resource identifier for the image to be displayed
44       * @param messageString message to be displayed underneath the image
45       */
46      public Splash(String rscName, String messageString) {
47          Display display = new Display();
48          _display = display;
49          InputStream inStream = ClassLoader.getSystemResourceAsStream(rscName);
50          _image = new Image(display, inStream);
51          _splashShell = new Shell(SWT.ON_TOP);
52  
53          GridLayout gridLayout = new GridLayout();
54          gridLayout.marginWidth = 1;
55          gridLayout.marginHeight = 1;
56          gridLayout.numColumns = 1;
57          _splashShell.setLayout(gridLayout);
58  
59          // FillLayout layout = new FillLayout();
60          // _splashShell.setLayout(layout);
61  
62          GridData gd = new GridData(GridData.FILL_BOTH);
63          // gd.horizontalAlignment = GridData.FILL_BOTH;
64  
65          _label = new Label(_splashShell, SWT.NONE);
66          _label.setImage(_image);
67          _label.setLayoutData(gd);
68  
69          gd = new GridData(GridData.FILL_HORIZONTAL);
70          _msgLabel = new Label(_splashShell, SWT.NONE);
71          _msgLabel.setLayoutData(gd);
72          _msgLabel.setBackground(new Color(_display, 255, 255, 255));
73          _msgLabel.setText(messageString);
74  
75          _splashShell.pack();
76          Rectangle splashRect = _splashShell.getBounds();
77          Rectangle displayRect = display.getBounds();
78          int x = (displayRect.width - splashRect.width) / 2;
79          int y = (displayRect.height - splashRect.height) / 2;
80          _splashShell.setLocation(x, y);
81          _splashShell.open();
82      }
83  
84      public void dispose() {
85          _splashShell.close();
86          _image.dispose();
87          _label.dispose();
88      }
89  }