001 /*
002 * Copyright 2005-2007 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025 package java.awt;
026
027 import java.io.IOException;
028 import java.awt.image.*;
029 import java.net.URL;
030 import java.net.URLConnection;
031 import java.io.File;
032 import java.util.logging.Logger;
033 import java.util.logging.Level;
034 import sun.awt.image.SunWritableRaster;
035
036 /**
037 * The splash screen can be created at application startup, before the
038 * Java Virtual Machine (JVM) starts. The splash screen is displayed as an
039 * undecorated window containing an image. You can use GIF, JPEG, and PNG files
040 * for the image. Animation (for GIF) and transparency (for GIF, PNG) are
041 * supported. The window is positioned at the center of the screen (the
042 * position on multi-monitor systems is not specified - it is platform and
043 * implementation dependent).
044 * The window is closed automatically as soon as the first window is displayed by
045 * Swing/AWT (may be also closed manually using the Java API, see below).
046 * <P>
047 * There are two ways to show the native splash screen:
048 * <P>
049 * <UL>
050 * <LI>If your application is run from the command line or from a shortcut,
051 * use the "-splash:" Java application launcher option to show a splash screen.
052 * <BR>
053 * For example:
054 * <PRE>
055 * java -splash:filename.gif Test
056 * </PRE>
057 * <LI>If your application is packaged in a jar file, you can use the
058 * "SplashScreen-Image" option in a manifest file to show a splash screen.
059 * Place the image in the jar archive and specify the path in the option.
060 * The path should not have a leading slash.
061 * <BR>
062 * For example, in the <code>manifest.mf</code> file:
063 * <PRE>
064 * Manifest-Version: 1.0
065 * Main-Class: Test
066 * SplashScreen-Image: filename.gif
067 * </PRE>
068 * The command line interface has higher precedence over the manifest
069 * setting.
070 * </UL>
071 * <p>
072 * The {@code SplashScreen} class provides the API for controlling the splash
073 * screen. This class may be used to close the splash screen, change the splash
074 * screen image, get the image position/size and paint in the splash screen. It
075 * cannot be used to create the splash screen; you should use the command line or manifest
076 * file option for that.
077 * <p>
078 * This class cannot be instantiated. Only a single instance of this class
079 * can exist, and it may be obtained using the {@link #getSplashScreen()}
080 * static method. In case the splash screen has not been created at
081 * application startup via the command line or manifest file option,
082 * the <code>getSplashScreen</code> method returns <code>null</code>.
083 *
084 * @author Oleg Semenov
085 * @since 1.6
086 */
087 public final class SplashScreen {
088
089 SplashScreen(long ptr) { // non-public constructor
090 splashPtr = ptr;
091 }
092
093 /**
094 * Returns the {@code SplashScreen} object used for
095 * Java startup splash screen control.
096 *
097 * @throws UnsupportedOperationException if the splash screen feature is not
098 * supported by the current toolkit
099 * @throws HeadlessException if {@code GraphicsEnvironment.isHeadless()}
100 * returns true
101 * @return the {@link SplashScreen} instance, or <code>null</code> if there is
102 * none or it has already been closed
103 */
104 public static SplashScreen getSplashScreen() {
105 synchronized (SplashScreen.class) {
106 if (GraphicsEnvironment.isHeadless()) {
107 throw new HeadlessException();
108 }
109 // SplashScreen class is now a singleton
110 if (!wasClosed && theInstance == null) {
111 java.security.AccessController
112 .doPrivileged(new sun.security.action.LoadLibraryAction(
113 "splashscreen"));
114 long ptr = _getInstance();
115 if (ptr != 0 && _isVisible(ptr)) {
116 theInstance = new SplashScreen(ptr);
117 }
118 }
119 return theInstance;
120 }
121 }
122
123 /**
124 * Changes the splash screen image. The new image is loaded from the
125 * specified URL; GIF, JPEG and PNG image formats are supported.
126 * The method returns after the image has finished loading and the window
127 * has been updated.
128 * The splash screen window is resized according to the size of
129 * the image and is centered on the screen.
130 *
131 * @param imageURL the non-<code>null</code> URL for the new
132 * splash screen image
133 * @throws NullPointerException if {@code imageURL} is <code>null</code>
134 * @throws IOException if there was an error while loading the image
135 * @throws IllegalStateException if the splash screen has already been
136 * closed
137 */
138 public void setImageURL(URL imageURL) throws NullPointerException,
139 IOException, IllegalStateException {
140 checkVisible();
141 URLConnection connection = imageURL.openConnection();
142 connection.connect();
143 int length = connection.getContentLength();
144 java.io.InputStream stream = connection.getInputStream();
145 byte[] buf = new byte[length];
146 int off = 0;
147 while (true) {
148 // check for available data
149 int available = stream.available();
150 if (available <= 0) {
151 // no data available... well, let's try reading one byte
152 // we'll see what happens then
153 available = 1;
154 }
155 // check for enough room in buffer, realloc if needed
156 // the buffer always grows in size 2x minimum
157 if (off + available > length) {
158 length = off * 2;
159 if (off + available > length) {
160 length = available + off;
161 }
162 byte[] oldBuf = buf;
163 buf = new byte[length];
164 System.arraycopy(oldBuf, 0, buf, 0, off);
165 }
166 // now read the data
167 int result = stream.read(buf, off, available);
168 if (result < 0) {
169 break;
170 }
171 off += result;
172 }
173 synchronized (SplashScreen.class) {
174 checkVisible();
175 if (!_setImageData(splashPtr, buf)) {
176 throw new IOException(
177 "Bad image format or i/o error when loading image");
178 }
179 this .imageURL = imageURL;
180 }
181 }
182
183 private void checkVisible() {
184 if (!isVisible()) {
185 throw new IllegalStateException(
186 "no splash screen available");
187 }
188 }
189
190 /**
191 * Returns the current splash screen image.
192 *
193 * @return URL for the current splash screen image file
194 * @throws IllegalStateException if the splash screen has already been closed
195 */
196 public URL getImageURL() throws IllegalStateException {
197 synchronized (SplashScreen.class) {
198 checkVisible();
199 if (imageURL == null) {
200 try {
201 String fileName = _getImageFileName(splashPtr);
202 String jarName = _getImageJarName(splashPtr);
203 if (fileName != null) {
204 if (jarName != null) {
205 imageURL = new URL("jar:"
206 + (new File(jarName).toURL()
207 .toString()) + "!/"
208 + fileName);
209 } else {
210 imageURL = new File(fileName).toURL();
211 }
212 }
213 } catch (java.net.MalformedURLException e) {
214 if (log.isLoggable(Level.FINE)) {
215 log
216 .log(
217 Level.FINE,
218 "MalformedURLException caught in the getImageURL() method",
219 e);
220 }
221 }
222 }
223 return imageURL;
224 }
225 }
226
227 /**
228 * Returns the bounds of the splash screen window as a {@link Rectangle}.
229 * This may be useful if, for example, you want to replace the splash
230 * screen with your window at the same location.
231 * <p>
232 * You cannot control the size or position of the splash screen.
233 * The splash screen size is adjusted automatically when the image changes.
234 *
235 * @return a {@code Rectangle} containing the splash screen bounds
236 * @throws IllegalStateException if the splash screen has already been closed
237 */
238 public Rectangle getBounds() throws IllegalStateException {
239 synchronized (SplashScreen.class) {
240 checkVisible();
241 return _getBounds(splashPtr);
242 }
243 }
244
245 /**
246 * Returns the size of the splash screen window as a {@link Dimension}.
247 * This may be useful if, for example,
248 * you want to draw on the splash screen overlay surface.
249 * <p>
250 * You cannot control the size or position of the splash screen.
251 * The splash screen size is adjusted automatically when the image changes.
252 *
253 * @return a {@link Dimension} object indicating the splash screen size
254 * @throws IllegalStateException if the splash screen has already been closed
255 */
256 public Dimension getSize() throws IllegalStateException {
257 return getBounds().getSize();
258 }
259
260 /**
261 * Creates a graphics context (as a {@link Graphics2D} object) for the splash
262 * screen overlay image, which allows you to draw over the splash screen.
263 * Note that you do not draw on the main image but on the image that is
264 * displayed over the main image using alpha blending. Also note that drawing
265 * on the overlay image does not necessarily update the contents of splash
266 * screen window. You should call {@code update()} on the
267 * <code>SplashScreen</code> when you want the splash screen to be
268 * updated immediately.
269 *
270 * @return graphics context for the splash screen overlay surface
271 * @throws IllegalStateException if the splash screen has already been closed
272 */
273 public Graphics2D createGraphics() throws IllegalStateException {
274 synchronized (SplashScreen.class) {
275 if (image == null) {
276 Dimension dim = getSize();
277 image = new BufferedImage(dim.width, dim.height,
278 BufferedImage.TYPE_INT_ARGB);
279 }
280 return image.createGraphics();
281 }
282 }
283
284 /**
285 * Updates the splash window with current contents of the overlay image.
286 *
287 * @throws IllegalStateException if the overlay image does not exist;
288 * for example, if {@code createGraphics} has never been called,
289 * or if the splash screen has already been closed
290 */
291 public void update() throws IllegalStateException {
292 BufferedImage image;
293 synchronized (SplashScreen.class) {
294 checkVisible();
295 image = this .image;
296 }
297 if (image == null) {
298 throw new IllegalStateException(
299 "no overlay image available");
300 }
301 DataBuffer buf = image.getRaster().getDataBuffer();
302 if (!(buf instanceof DataBufferInt)) {
303 throw new AssertionError(
304 "Overlay image DataBuffer is of invalid type == "
305 + buf.getClass().getName());
306 }
307 int numBanks = buf.getNumBanks();
308 if (numBanks != 1) {
309 throw new AssertionError("Invalid number of banks =="
310 + numBanks + " in overlay image DataBuffer");
311 }
312 if (!(image.getSampleModel() instanceof SinglePixelPackedSampleModel)) {
313 throw new AssertionError(
314 "Overlay image has invalid sample model == "
315 + image.getSampleModel().getClass()
316 .getName());
317 }
318 SinglePixelPackedSampleModel sm = (SinglePixelPackedSampleModel) image
319 .getSampleModel();
320 int scanlineStride = sm.getScanlineStride();
321 Rectangle rect = image.getRaster().getBounds();
322 // Note that we steal the data array here, but just for reading
323 // so we do not need to mark the DataBuffer dirty...
324 int[] data = SunWritableRaster
325 .stealData((DataBufferInt) buf, 0);
326 synchronized (SplashScreen.class) {
327 checkVisible();
328 _update(splashPtr, data, rect.x, rect.y, rect.width,
329 rect.height, scanlineStride);
330 }
331 }
332
333 /**
334 * Hides the splash screen, closes the window, and releases all associated
335 * resources.
336 *
337 * @throws IllegalStateException if the splash screen has already been closed
338 */
339 public void close() throws IllegalStateException {
340 synchronized (SplashScreen.class) {
341 checkVisible();
342 _close(splashPtr);
343 image = null;
344 wasClosed = true;
345 theInstance = null;
346 }
347 }
348
349 /**
350 * Determines whether the splash screen is visible. The splash screen may
351 * be hidden using {@link #close()}, it is also hidden automatically when
352 * the first AWT/Swing window is made visible.
353 *
354 * @return true if the splash screen is visible (has not been closed yet),
355 * false otherwise
356 */
357 public boolean isVisible() {
358 synchronized (SplashScreen.class) {
359 return !wasClosed && _isVisible(splashPtr);
360 }
361 }
362
363 private BufferedImage image; // overlay image
364
365 private final long splashPtr; // pointer to native Splash structure
366 private static boolean wasClosed = false;
367
368 private URL imageURL;
369
370 /**
371 * The instance reference for the singleton.
372 * (<code>null</code> if no instance exists yet.)
373 *
374 * @see #getSplashScreen
375 * @see #close
376 */
377 private static SplashScreen theInstance = null;
378
379 private static final Logger log = Logger
380 .getLogger("java.awt.SplashScreen");
381
382 private native static void _update(long splashPtr, int[] data,
383 int x, int y, int width, int height, int scanlineStride);
384
385 private native static boolean _isVisible(long splashPtr);
386
387 private native static Rectangle _getBounds(long splashPtr);
388
389 private native static long _getInstance();
390
391 private native static void _close(long splashPtr);
392
393 private native static String _getImageFileName(long splashPtr);
394
395 private native static String _getImageJarName(long SplashPtr);
396
397 private native static boolean _setImageData(long SplashPtr,
398 byte[] data);
399
400 };
|