jdk/src/share/classes/java/awt/SplashScreen.java
author ohair
Tue, 28 Dec 2010 15:53:50 -0800
changeset 7668 d4a77089c587
parent 6823 cff6c85026c4
child 7775 c75bf08f7e0c
permissions -rw-r--r--
6962318: Update copyright year Reviewed-by: xdono
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
7668
d4a77089c587 6962318: Update copyright year
ohair
parents: 6823
diff changeset
     2
 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3938
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3938
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3938
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3938
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3938
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package java.awt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.image.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.net.URL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.net.URLConnection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.File;
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2
diff changeset
    32
import sun.util.logging.PlatformLogger;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import sun.awt.image.SunWritableRaster;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * The splash screen can be created at application startup, before the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * Java Virtual Machine (JVM) starts. The splash screen is displayed as an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * undecorated window containing an image. You can use GIF, JPEG, and PNG files
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * for the image. Animation (for GIF) and transparency (for GIF, PNG) are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * supported. The window is positioned at the center of the screen (the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * position on multi-monitor systems is not specified - it is platform and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * implementation dependent).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * The window is closed automatically as soon as the first window is displayed by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * Swing/AWT (may be also closed manually using the Java API, see below).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * There are two ways to show the native splash screen:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * <UL>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * <LI>If your application is run from the command line or from a shortcut,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * use the "-splash:" Java application  launcher option to show a splash screen.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * <BR>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * For example:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * <PRE>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * java -splash:filename.gif Test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * </PRE>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * <LI>If your application is packaged in a jar file, you can use the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * "SplashScreen-Image" option in a manifest file to show a splash screen.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * Place the image in the jar archive and specify the path in the option.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * The path should not have a leading slash.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * <BR>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * For example, in the <code>manifest.mf</code> file:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * <PRE>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * Manifest-Version: 1.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * Main-Class: Test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * SplashScreen-Image: filename.gif
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * </PRE>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * The command line interface has higher precedence over the manifest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * setting.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * </UL>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * The {@code SplashScreen} class provides the API for controlling the splash
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * screen. This class may be used to close the splash screen, change the splash
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * screen image, get the image position/size and paint in the splash screen. It
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * cannot be used to create the splash screen; you should use the command line or manifest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * file option for that.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * This class cannot be instantiated. Only a single instance of this class
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * can exist, and it may be obtained using the {@link #getSplashScreen()}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * static method. In case the splash screen has not been created at
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * application startup via the command line or manifest file option,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * the <code>getSplashScreen</code> method returns <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * @author Oleg Semenov
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
public final class SplashScreen {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    SplashScreen(long ptr) { // non-public constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        splashPtr = ptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * Returns the {@code SplashScreen} object used for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * Java startup splash screen control.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * @throws UnsupportedOperationException if the splash screen feature is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     *         supported by the current toolkit
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * @throws HeadlessException if {@code GraphicsEnvironment.isHeadless()}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     *         returns true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * @return the {@link SplashScreen} instance, or <code>null</code> if there is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     *         none or it has already been closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    public static  SplashScreen getSplashScreen() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        synchronized (SplashScreen.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            if (GraphicsEnvironment.isHeadless()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                throw new HeadlessException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            // SplashScreen class is now a singleton
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            if (!wasClosed && theInstance == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                java.security.AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                        new sun.security.action.LoadLibraryAction("splashscreen"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                long ptr = _getInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                if (ptr != 0 && _isVisible(ptr)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                    theInstance = new SplashScreen(ptr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            return theInstance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * Changes the splash screen image. The new image is loaded from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * specified URL; GIF, JPEG and PNG image formats are supported.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * The method returns after the image has finished loading and the window
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * has been updated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * The splash screen window is resized according to the size of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * the image and is centered on the screen.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * @param imageURL the non-<code>null</code> URL for the new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     *        splash screen image
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * @throws NullPointerException if {@code imageURL} is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * @throws IOException if there was an error while loading the image
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * @throws IllegalStateException if the splash screen has already been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     *         closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    public void setImageURL(URL imageURL) throws NullPointerException, IOException, IllegalStateException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        checkVisible();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        URLConnection connection = imageURL.openConnection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        connection.connect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        int length = connection.getContentLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        java.io.InputStream stream = connection.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        byte[] buf = new byte[length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        int off = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        while(true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            // check for available data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            int available = stream.available();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            if (available <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                // no data available... well, let's try reading one byte
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                // we'll see what happens then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                available = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            // check for enough room in buffer, realloc if needed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            // the buffer always grows in size 2x minimum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            if (off + available > length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                length = off*2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                if (off + available > length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                    length = available+off;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                byte[] oldBuf = buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                buf = new byte[length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                System.arraycopy(oldBuf, 0, buf, 0, off);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            // now read the data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            int result = stream.read(buf, off, available);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            if (result < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            off += result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        synchronized(SplashScreen.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            checkVisible();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            if (!_setImageData(splashPtr, buf)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                throw new IOException("Bad image format or i/o error when loading image");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            this.imageURL = imageURL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    private void checkVisible() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        if (!isVisible()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            throw new IllegalStateException("no splash screen available");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * Returns the current splash screen image.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * @return URL for the current splash screen image file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * @throws IllegalStateException if the splash screen has already been closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    public URL getImageURL() throws IllegalStateException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        synchronized (SplashScreen.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            checkVisible();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            if (imageURL == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                    String fileName = _getImageFileName(splashPtr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                    String jarName = _getImageJarName(splashPtr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                    if (fileName != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                        if (jarName != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                            imageURL = new URL("jar:"+(new File(jarName).toURL().toString())+"!/"+fileName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                            imageURL = new File(fileName).toURL();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                catch(java.net.MalformedURLException e) {
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2
diff changeset
   206
                    if (log.isLoggable(PlatformLogger.FINE)) {
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2
diff changeset
   207
                        log.fine("MalformedURLException caught in the getImageURL() method", e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            return imageURL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * Returns the bounds of the splash screen window as a {@link Rectangle}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * This may be useful if, for example, you want to replace the splash
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * screen with your window at the same location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * You cannot control the size or position of the splash screen.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * The splash screen size is adjusted automatically when the image changes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * @return a {@code Rectangle} containing the splash screen bounds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * @throws IllegalStateException if the splash screen has already been closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    public Rectangle getBounds() throws IllegalStateException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        synchronized (SplashScreen.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            checkVisible();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            return _getBounds(splashPtr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * Returns the size of the splash screen window as a {@link Dimension}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * This may be useful if, for example,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * you want to draw on the splash screen overlay surface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * You cannot control the size or position of the splash screen.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * The splash screen size is adjusted automatically when the image changes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * @return a {@link Dimension} object indicating the splash screen size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * @throws IllegalStateException if the splash screen has already been closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    public Dimension getSize() throws IllegalStateException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        return getBounds().getSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * Creates a graphics context (as a {@link Graphics2D} object) for the splash
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * screen overlay image, which allows you to draw over the splash screen.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * Note that you do not draw on the main image but on the image that is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * displayed over the main image using alpha blending. Also note that drawing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * on the overlay image does not necessarily update the contents of splash
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * screen window. You should call {@code update()} on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * <code>SplashScreen</code> when you want the splash screen to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * updated immediately.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * @return graphics context for the splash screen overlay surface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * @throws IllegalStateException if the splash screen has already been closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    public Graphics2D createGraphics() throws IllegalStateException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        synchronized (SplashScreen.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            if (image==null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                Dimension dim = getSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                image = new BufferedImage(dim.width, dim.height, BufferedImage.TYPE_INT_ARGB);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            return image.createGraphics();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * Updates the splash window with current contents of the overlay image.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * @throws IllegalStateException if the overlay image does not exist;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     *         for example, if {@code createGraphics} has never been called,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     *         or if the splash screen has already been closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    public void update() throws IllegalStateException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        BufferedImage image;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        synchronized (SplashScreen.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            checkVisible();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            image = this.image;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        if (image == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            throw new IllegalStateException("no overlay image available");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        DataBuffer buf = image.getRaster().getDataBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        if (!(buf instanceof DataBufferInt)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            throw new AssertionError("Overlay image DataBuffer is of invalid type == "+buf.getClass().getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        int numBanks = buf.getNumBanks();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        if (numBanks!=1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            throw new AssertionError("Invalid number of banks =="+numBanks+" in overlay image DataBuffer");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        if (!(image.getSampleModel() instanceof SinglePixelPackedSampleModel)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            throw new AssertionError("Overlay image has invalid sample model == "+image.getSampleModel().getClass().getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        SinglePixelPackedSampleModel sm = (SinglePixelPackedSampleModel)image.getSampleModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        int scanlineStride = sm.getScanlineStride();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        Rectangle rect = image.getRaster().getBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        // Note that we steal the data array here, but just for reading
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        // so we do not need to mark the DataBuffer dirty...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        int[] data = SunWritableRaster.stealData((DataBufferInt)buf, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        synchronized(SplashScreen.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            checkVisible();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            _update(splashPtr, data, rect.x, rect.y, rect.width, rect.height, scanlineStride);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * Hides the splash screen, closes the window, and releases all associated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * resources.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * @throws IllegalStateException if the splash screen has already been closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    public void close() throws IllegalStateException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        synchronized (SplashScreen.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            checkVisible();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            _close(splashPtr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            image = null;
6823
cff6c85026c4 6990352: SplashScreen.getSplashScreen() does not return null for implicitly closed splash screen
anthony
parents: 5506
diff changeset
   321
            SplashScreen.markClosed();
cff6c85026c4 6990352: SplashScreen.getSplashScreen() does not return null for implicitly closed splash screen
anthony
parents: 5506
diff changeset
   322
        }
cff6c85026c4 6990352: SplashScreen.getSplashScreen() does not return null for implicitly closed splash screen
anthony
parents: 5506
diff changeset
   323
    }
cff6c85026c4 6990352: SplashScreen.getSplashScreen() does not return null for implicitly closed splash screen
anthony
parents: 5506
diff changeset
   324
cff6c85026c4 6990352: SplashScreen.getSplashScreen() does not return null for implicitly closed splash screen
anthony
parents: 5506
diff changeset
   325
    static void markClosed() {
cff6c85026c4 6990352: SplashScreen.getSplashScreen() does not return null for implicitly closed splash screen
anthony
parents: 5506
diff changeset
   326
        synchronized (SplashScreen.class) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            wasClosed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            theInstance = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * Determines whether the splash screen is visible. The splash screen may
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * be hidden using {@link #close()}, it is also hidden automatically when
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * the first AWT/Swing window is made visible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * @return true if the splash screen is visible (has not been closed yet),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     *         false otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    public boolean isVisible() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        synchronized (SplashScreen.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            return !wasClosed && _isVisible(splashPtr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    private BufferedImage image; // overlay image
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    private final long splashPtr; // pointer to native Splash structure
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    private static boolean wasClosed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    private URL imageURL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * The instance reference for the singleton.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * (<code>null</code> if no instance exists yet.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * @see #getSplashScreen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * @see #close
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    private static SplashScreen theInstance = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2
diff changeset
   363
    private static final PlatformLogger log = PlatformLogger.getLogger("java.awt.SplashScreen");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    private native static void _update(long splashPtr, int[] data, int x, int y, int width, int height, int scanlineStride);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    private native static boolean _isVisible(long splashPtr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    private native static Rectangle _getBounds(long splashPtr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    private native static long _getInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    private native static void _close(long splashPtr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    private native static String _getImageFileName(long splashPtr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    private native static String _getImageJarName(long SplashPtr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    private native static boolean _setImageData(long SplashPtr, byte[] data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
};