jdk/src/java.desktop/share/classes/sun/applet/AppletPanel.java
author aivanov
Thu, 19 Feb 2015 12:50:32 +0000
changeset 29260 77797a65dc63
parent 28231 b608ffcaed74
child 29922 7b9c1e1532cf
permissions -rw-r--r--
8056915: Focus lost in applet when browser window is minimized and restored Reviewed-by: ant, dtitov, dcherepanov
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
29260
77797a65dc63 8056915: Focus lost in applet when browser window is minimized and restored
aivanov
parents: 28231
diff changeset
     2
 * Copyright (c) 1995, 2015, 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: 3111
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: 3111
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: 3111
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3111
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3111
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.applet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.applet.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.awt.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.lang.ref.WeakReference;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.lang.reflect.InvocationTargetException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.lang.reflect.Method;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.net.JarURLConnection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.net.SocketPermission;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.net.URL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.util.Locale;
16092
129d7d8a7399 7192977: Issue in toolkit thread
bagiras
parents: 5506
diff changeset
    41
import sun.awt.AWTAccessor;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import sun.awt.AppContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import sun.awt.EmbeddedFrame;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import sun.awt.SunToolkit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import sun.misc.MessageUtils;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import sun.misc.PerformanceLogger;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import sun.misc.Queue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import sun.security.util.SecurityConstants;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * Applet panel class. The panel manages and manipulates the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * applet as it is being loaded. It forks a separate thread in a new
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * thread group to call the applet's init(), start(), stop(), and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * destroy() methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * @author      Arthur van Hoff
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 */
23642
3ac83ad3fb6a 8037174: Fix serial lint warnings in sun.applet
darcy
parents: 22584
diff changeset
    58
@SuppressWarnings("serial") // JDK implementation class
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
public
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
abstract class AppletPanel extends Panel implements AppletStub, Runnable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * The applet (if loaded).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    Applet applet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * Applet will allow initialization.  Should be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * set to false if loading a serialized applet
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * that was pickled in the init=true state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    protected boolean doInit = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * The classloader for the applet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     */
3111
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2645
diff changeset
    78
    protected AppletClassLoader loader;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    /* applet event ids */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    public final static int APPLET_DISPOSE = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    public final static int APPLET_LOAD = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    public final static int APPLET_INIT = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    public final static int APPLET_START = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    public final static int APPLET_STOP = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    public final static int APPLET_DESTROY = 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    public final static int APPLET_QUIT = 6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    public final static int APPLET_ERROR = 7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    /* send to the parent to force relayout */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    public final static int APPLET_RESIZE = 51234;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    /* sent to a (distant) parent to indicate that the applet is being
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * loaded or as completed loading
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    public final static int APPLET_LOADING = 51235;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    public final static int APPLET_LOADING_COMPLETED = 51236;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * The current status. One of:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     *    APPLET_DISPOSE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     *    APPLET_LOAD,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     *    APPLET_INIT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     *    APPLET_START,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     *    APPLET_STOP,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     *    APPLET_DESTROY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     *    APPLET_ERROR.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    protected int status;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * The thread for the applet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     */
3111
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2645
diff changeset
   114
    protected Thread handler;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * The initial applet size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    Dimension defaultAppletSize = new Dimension(10, 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * The current applet size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    Dimension currentAppletSize = new Dimension(10, 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    MessageUtils mu = new MessageUtils();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * The thread to use during applet loading
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    Thread loaderThread = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * Flag to indicate that a loading has been cancelled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    boolean loadAbortRequest = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    /* abstract classes */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    abstract protected String getCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    abstract protected String getJarFiles();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    abstract protected String getSerializedObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
23667
37f0fbe26e84 8028050: Typo correction needed s/Classlaoder/Classloader/
serb
parents: 23642
diff changeset
   145
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    abstract public int    getWidth();
23667
37f0fbe26e84 8028050: Typo correction needed s/Classlaoder/Classloader/
serb
parents: 23642
diff changeset
   147
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    abstract public int    getHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    abstract public boolean hasInitialFocus();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    private static int threadGroupNumber = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    protected void setupAppletAppContext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        // do nothing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * Creates a thread to run the applet. This method is called
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * each time an applet is loaded and reloaded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    synchronized void createAppletThread() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        // Create a thread group for the applet, and start a new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        // thread to load the applet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        String nm = "applet-" + getCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        loader = getClassLoader(getCodeBase(), getClassLoaderCacheKey());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        loader.grab(); // Keep this puppy around!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        // 4668479: Option to turn off codebase lookup in AppletClassLoader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        // during resource requests. [stanley.ho]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        String param = getParameter("codebase_lookup");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        if (param != null && param.equals("false"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            loader.setCodebaseLookup(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            loader.setCodebaseLookup(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        ThreadGroup appletGroup = loader.getThreadGroup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        handler = new Thread(appletGroup, this, "thread " + nm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        // set the context class loader for this thread
25768
b10ec9701a7e 8042872: Fix raw and unchecked warnings in sun.applet
henryjen
parents: 23667
diff changeset
   182
        AccessController.doPrivileged(new PrivilegedAction<Object>() {
23667
37f0fbe26e84 8028050: Typo correction needed s/Classlaoder/Classloader/
serb
parents: 23642
diff changeset
   183
                @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                public Object run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                    handler.setContextClassLoader(loader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        handler.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    void joinAppletThread() throws InterruptedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        if (handler != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            handler.join();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            handler = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    void release() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        if (loader != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            loader.release();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            loader = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * Construct an applet viewer and start the applet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    public void init() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            // Get the width (if any)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            defaultAppletSize.width = getWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            currentAppletSize.width = defaultAppletSize.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            // Get the height (if any)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            defaultAppletSize.height = getHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            currentAppletSize.height = defaultAppletSize.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        } catch (NumberFormatException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            // Turn on the error flag and let TagAppletPanel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            // do the right thing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            status = APPLET_ERROR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            showAppletStatus("badattribute.exception");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            showAppletLog("badattribute.exception");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            showAppletException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        setLayout(new BorderLayout());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        createAppletThread();
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
     * Minimum size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     */
23667
37f0fbe26e84 8028050: Typo correction needed s/Classlaoder/Classloader/
serb
parents: 23642
diff changeset
   236
    @Override
28231
b608ffcaed74 8066621: Suppress deprecation warnings in java.desktop module
darcy
parents: 25859
diff changeset
   237
    @SuppressWarnings("deprecation")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    public Dimension minimumSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        return new Dimension(defaultAppletSize.width,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                             defaultAppletSize.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * Preferred size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     */
23667
37f0fbe26e84 8028050: Typo correction needed s/Classlaoder/Classloader/
serb
parents: 23642
diff changeset
   246
    @Override
28231
b608ffcaed74 8066621: Suppress deprecation warnings in java.desktop module
darcy
parents: 25859
diff changeset
   247
    @SuppressWarnings("deprecation")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    public Dimension preferredSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        return new Dimension(currentAppletSize.width,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                             currentAppletSize.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    private AppletListener listeners;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * AppletEvent Queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     */
25768
b10ec9701a7e 8042872: Fix raw and unchecked warnings in sun.applet
henryjen
parents: 23667
diff changeset
   258
    private Queue<Integer> queue = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    synchronized public void addAppletListener(AppletListener l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        listeners = AppletEventMulticaster.add(listeners, l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    synchronized public void removeAppletListener(AppletListener l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        listeners = AppletEventMulticaster.remove(listeners, l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * Dispatch event to the listeners..
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    public void dispatchAppletEvent(int id, Object argument) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        //System.out.println("SEND= " + id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        if (listeners != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            AppletEvent evt = new AppletEvent(this, id, argument);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            listeners.appletStateChanged(evt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * Send an event. Queue it for execution by the handler thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    public void sendEvent(int id) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        synchronized(this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            if (queue == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                //System.out.println("SEND0= " + id);
25768
b10ec9701a7e 8042872: Fix raw and unchecked warnings in sun.applet
henryjen
parents: 23667
diff changeset
   287
                queue = new Queue<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            }
438
2ae294e4518c 6613529: Avoid duplicate object creation within JDK packages
dav
parents: 2
diff changeset
   289
            Integer eventId = Integer.valueOf(id);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            queue.enqueue(eventId);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            notifyAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        if (id == APPLET_QUIT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                joinAppletThread(); // Let the applet event handler exit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            // AppletClassLoader.release() must be called by a Thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            // not within the applet's ThreadGroup
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            if (loader == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                loader = getClassLoader(getCodeBase(), getClassLoaderCacheKey());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            release();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * Get an event from the queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    synchronized AppletEvent getNextEvent() throws InterruptedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        while (queue == null || queue.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            wait();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        }
25768
b10ec9701a7e 8042872: Fix raw and unchecked warnings in sun.applet
henryjen
parents: 23667
diff changeset
   314
        Integer eventId = queue.dequeue();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        return new AppletEvent(this, eventId.intValue(), null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    boolean emptyEventQueue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        if ((queue == null) || (queue.isEmpty()))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * This kludge is specific to get over AccessControlException thrown during
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * Applet.stop() or destroy() when static thread is suspended.  Set a flag
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * in AppletClassLoader to indicate that an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * AccessControlException for RuntimePermission "modifyThread" or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * "modifyThreadGroup" had occurred.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     private void setExceptionStatus(AccessControlException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     Permission p = e.getPermission();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     if (p instanceof RuntimePermission) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
         if (p.getName().startsWith("modifyThread")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
             if (loader == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                 loader = getClassLoader(getCodeBase(), getClassLoaderCacheKey());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
             loader.setExceptionStatus();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * Execute applet events.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * Here is the state transition diagram
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     *   Note: (XXX) is the action
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     *         APPLET_XXX is the state
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     *  (applet code loaded) --> APPLET_LOAD -- (applet init called)--> APPLET_INIT -- (
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     *   applet start called) --> APPLET_START -- (applet stop called) -->APPLET_STOP --(applet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     *   destroyed called) --> APPLET_DESTROY -->(applet gets disposed) -->
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     *   APPLET_DISPOSE -->....
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * In the legacy lifecycle model. The applet gets loaded, inited and started. So it stays
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * in the APPLET_START state unless the applet goes away(refresh page or leave the page).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * So the applet stop method called and the applet enters APPLET_STOP state. Then if the applet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * is revisited, it will call applet start method and enter the APPLET_START state and stay there.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * In the modern lifecycle model. When the applet first time visited, it is same as legacy lifecycle
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * model. However, when the applet page goes away. It calls applet stop method and enters APPLET_STOP
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * state and then applet destroyed method gets called and enters APPLET_DESTROY state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * This code is also called by AppletViewer. In AppletViewer "Restart" menu, the applet is jump from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * APPLET_STOP to APPLET_DESTROY and to APPLET_INIT .
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * Also, the applet can jump from APPLET_INIT state to APPLET_DESTROY (in Netscape/Mozilla case).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
         * Same as APPLET_LOAD to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * APPLET_DISPOSE since all of this are triggered by browser.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     */
23667
37f0fbe26e84 8028050: Typo correction needed s/Classlaoder/Classloader/
serb
parents: 23642
diff changeset
   372
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        Thread curThread = Thread.currentThread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        if (curThread == loaderThread) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            // if we are in the loader thread, cause
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            // loading to occur.  We may exit this with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            // status being APPLET_DISPOSE, APPLET_ERROR,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            // or APPLET_LOAD
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            runLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        boolean disposed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        while (!disposed && !curThread.isInterrupted()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            AppletEvent evt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                evt = getNextEvent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                showAppletStatus("bail");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
            //showAppletStatus("EVENT = " + evt.getID());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                switch (evt.getID()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                  case APPLET_LOAD:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                      if (!okToLoad()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                          break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                      // This complexity allows loading of applets to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                      // interruptable.  The actual thread loading runs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                      // in a separate thread, so it can be interrupted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                      // without harming the applet thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                      // So that we don't have to worry about
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                      // concurrency issues, the main applet thread waits
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                      // until the loader thread terminates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                      // (one way or another).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                      if (loaderThread == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                          // REMIND: do we want a name?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                          //System.out.println("------------------- loading applet");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                          setLoaderThread(new Thread(this));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                          loaderThread.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                          // we get to go to sleep while this runs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                          loaderThread.join();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                          setLoaderThread(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                      } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                          // REMIND: issue an error -- this case should never
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                          // occur.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                      break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                  case APPLET_INIT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                    // AppletViewer "Restart" will jump from destroy method to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                    // init, that is why we need to check status w/ APPLET_DESTROY
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                      if (status != APPLET_LOAD && status != APPLET_DESTROY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                          showAppletStatus("notloaded");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                          break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                      applet.resize(defaultAppletSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                      if (doInit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                          if (PerformanceLogger.loggingEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                              PerformanceLogger.setTime("Applet Init");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                              PerformanceLogger.outputLog();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                          applet.init();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                      //Need the default(fallback) font to be created in this AppContext
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                      Font f = getFont();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                      if (f == null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                          "dialog".equals(f.getFamily().toLowerCase(Locale.ENGLISH)) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                          f.getSize() == 12 && f.getStyle() == Font.PLAIN) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                          setFont(new Font(Font.DIALOG, Font.PLAIN, 12));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                      doInit = true;    // allow restarts
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                      // Validate the applet in event dispatch thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                      // to avoid deadlock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                      try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                          final AppletPanel p = this;
16092
129d7d8a7399 7192977: Issue in toolkit thread
bagiras
parents: 5506
diff changeset
   454
                          Runnable r = new Runnable() {
23667
37f0fbe26e84 8028050: Typo correction needed s/Classlaoder/Classloader/
serb
parents: 23642
diff changeset
   455
                              @Override
16092
129d7d8a7399 7192977: Issue in toolkit thread
bagiras
parents: 5506
diff changeset
   456
                              public void run() {
129d7d8a7399 7192977: Issue in toolkit thread
bagiras
parents: 5506
diff changeset
   457
                                  p.validate();
129d7d8a7399 7192977: Issue in toolkit thread
bagiras
parents: 5506
diff changeset
   458
                              }
129d7d8a7399 7192977: Issue in toolkit thread
bagiras
parents: 5506
diff changeset
   459
                          };
129d7d8a7399 7192977: Issue in toolkit thread
bagiras
parents: 5506
diff changeset
   460
                          AWTAccessor.getEventQueueAccessor().invokeAndWait(applet, r);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                      catch(InterruptedException ie) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                      catch(InvocationTargetException ite) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                      status = APPLET_INIT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                      showAppletStatus("inited");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                      break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                  case APPLET_START:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                      if (status != APPLET_INIT && status != APPLET_STOP) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                          showAppletStatus("notinited");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
                          break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                      applet.resize(currentAppletSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
                      applet.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
                      // Validate and show the applet in event dispatch thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                      // to avoid deadlock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
                      try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                          final AppletPanel p = this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
                          final Applet a = applet;
16092
129d7d8a7399 7192977: Issue in toolkit thread
bagiras
parents: 5506
diff changeset
   485
                          Runnable r = new Runnable() {
23667
37f0fbe26e84 8028050: Typo correction needed s/Classlaoder/Classloader/
serb
parents: 23642
diff changeset
   486
                              @Override
16092
129d7d8a7399 7192977: Issue in toolkit thread
bagiras
parents: 5506
diff changeset
   487
                              public void run() {
129d7d8a7399 7192977: Issue in toolkit thread
bagiras
parents: 5506
diff changeset
   488
                                  p.validate();
129d7d8a7399 7192977: Issue in toolkit thread
bagiras
parents: 5506
diff changeset
   489
                                  a.setVisible(true);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
16092
129d7d8a7399 7192977: Issue in toolkit thread
bagiras
parents: 5506
diff changeset
   491
                                  // Fix for BugTraq ID 4041703.
129d7d8a7399 7192977: Issue in toolkit thread
bagiras
parents: 5506
diff changeset
   492
                                  // Set the default focus for an applet.
129d7d8a7399 7192977: Issue in toolkit thread
bagiras
parents: 5506
diff changeset
   493
                                  if (hasInitialFocus()) {
129d7d8a7399 7192977: Issue in toolkit thread
bagiras
parents: 5506
diff changeset
   494
                                      setDefaultFocus();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
                                  }
16092
129d7d8a7399 7192977: Issue in toolkit thread
bagiras
parents: 5506
diff changeset
   496
                              }
129d7d8a7399 7192977: Issue in toolkit thread
bagiras
parents: 5506
diff changeset
   497
                          };
129d7d8a7399 7192977: Issue in toolkit thread
bagiras
parents: 5506
diff changeset
   498
                          AWTAccessor.getEventQueueAccessor().invokeAndWait(applet, r);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
                      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
                      catch(InterruptedException ie) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
                      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
                      catch(InvocationTargetException ite) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
                      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
                      status = APPLET_START;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
                      showAppletStatus("started");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                      break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
                  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
                case APPLET_STOP:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
                    if (status != APPLET_START) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
                        showAppletStatus("notstarted");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
                    status = APPLET_STOP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
                    // Hide the applet in event dispatch thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
                    // to avoid deadlock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
                        final Applet a = applet;
16092
129d7d8a7399 7192977: Issue in toolkit thread
bagiras
parents: 5506
diff changeset
   521
                        Runnable r = new Runnable() {
23667
37f0fbe26e84 8028050: Typo correction needed s/Classlaoder/Classloader/
serb
parents: 23642
diff changeset
   522
                            @Override
16092
129d7d8a7399 7192977: Issue in toolkit thread
bagiras
parents: 5506
diff changeset
   523
                            public void run() {
129d7d8a7399 7192977: Issue in toolkit thread
bagiras
parents: 5506
diff changeset
   524
                                a.setVisible(false);
129d7d8a7399 7192977: Issue in toolkit thread
bagiras
parents: 5506
diff changeset
   525
                            }
129d7d8a7399 7192977: Issue in toolkit thread
bagiras
parents: 5506
diff changeset
   526
                        };
129d7d8a7399 7192977: Issue in toolkit thread
bagiras
parents: 5506
diff changeset
   527
                        AWTAccessor.getEventQueueAccessor().invokeAndWait(applet, r);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
                    catch(InterruptedException ie) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
                    catch(InvocationTargetException ite) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
                    // During Applet.stop(), any AccessControlException on an involved Class remains in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
                    // the "memory" of the AppletClassLoader.  If the same instance of the ClassLoader is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
                    // reused, the same exception will occur during class loading.  Set the AppletClassLoader's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
                    // exceptionStatusSet flag to allow recognition of what had happened
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
                    // when reusing AppletClassLoader object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                        applet.stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
                    } catch (java.security.AccessControlException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
                        setExceptionStatus(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
                        // rethrow exception to be handled as it normally would be.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                        throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
                    showAppletStatus("stopped");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
                case APPLET_DESTROY:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
                    if (status != APPLET_STOP && status != APPLET_INIT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
                        showAppletStatus("notstopped");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
                    status = APPLET_DESTROY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
                    // During Applet.destroy(), any AccessControlException on an involved Class remains in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
                    // the "memory" of the AppletClassLoader.  If the same instance of the ClassLoader is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
                    // reused, the same exception will occur during class loading.  Set the AppletClassLoader's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
                    // exceptionStatusSet flag to allow recognition of what had happened
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
                    // when reusing AppletClassLoader object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
                        applet.destroy();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
                    } catch (java.security.AccessControlException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
                        setExceptionStatus(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
                        // rethrow exception to be handled as it normally would be.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
                        throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
                    showAppletStatus("destroyed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
                case APPLET_DISPOSE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
                    if (status != APPLET_DESTROY && status != APPLET_LOAD) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
                        showAppletStatus("notdestroyed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
                    status = APPLET_DISPOSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
16092
129d7d8a7399 7192977: Issue in toolkit thread
bagiras
parents: 5506
diff changeset
   579
                    try {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
                        final Applet a = applet;
16092
129d7d8a7399 7192977: Issue in toolkit thread
bagiras
parents: 5506
diff changeset
   581
                        Runnable r = new Runnable() {
23667
37f0fbe26e84 8028050: Typo correction needed s/Classlaoder/Classloader/
serb
parents: 23642
diff changeset
   582
                            @Override
16092
129d7d8a7399 7192977: Issue in toolkit thread
bagiras
parents: 5506
diff changeset
   583
                            public void run() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
                                remove(a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
                            }
16092
129d7d8a7399 7192977: Issue in toolkit thread
bagiras
parents: 5506
diff changeset
   586
                        };
129d7d8a7399 7192977: Issue in toolkit thread
bagiras
parents: 5506
diff changeset
   587
                        AWTAccessor.getEventQueueAccessor().invokeAndWait(applet, r);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
                    catch(InterruptedException ie)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
                    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
                    catch(InvocationTargetException ite)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
                    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
                    applet = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
                    showAppletStatus("disposed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
                    disposed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
                case APPLET_QUIT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
                status = APPLET_ERROR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
                if (e.getMessage() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
                    showAppletStatus("exception2", e.getClass().getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
                                     e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
                    showAppletStatus("exception", e.getClass().getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
                showAppletException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
            } catch (ThreadDeath e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
                showAppletStatus("death");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
            } catch (Error e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
                status = APPLET_ERROR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
                if (e.getMessage() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
                    showAppletStatus("error2", e.getClass().getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
                                     e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
                    showAppletStatus("error", e.getClass().getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
                showAppletException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
            clearLoadAbortRequest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
     * Gets most recent focus owner component associated with the given window.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
     * It does that without calling Window.getMostRecentFocusOwner since it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
     * provides its own logic contradicting with setDefautlFocus. Instead, it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
     * calls KeyboardFocusManager directly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
    private Component getMostRecentFocusOwnerForWindow(Window w) {
25768
b10ec9701a7e 8042872: Fix raw and unchecked warnings in sun.applet
henryjen
parents: 23667
diff changeset
   636
        Method meth = AccessController.doPrivileged(
b10ec9701a7e 8042872: Fix raw and unchecked warnings in sun.applet
henryjen
parents: 23667
diff changeset
   637
            new PrivilegedAction<Method>() {
23667
37f0fbe26e84 8028050: Typo correction needed s/Classlaoder/Classloader/
serb
parents: 23642
diff changeset
   638
                @Override
25768
b10ec9701a7e 8042872: Fix raw and unchecked warnings in sun.applet
henryjen
parents: 23667
diff changeset
   639
                public Method run() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
                    Method meth = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
                    try {
23667
37f0fbe26e84 8028050: Typo correction needed s/Classlaoder/Classloader/
serb
parents: 23642
diff changeset
   642
                        meth = KeyboardFocusManager.class.getDeclaredMethod(
37f0fbe26e84 8028050: Typo correction needed s/Classlaoder/Classloader/
serb
parents: 23642
diff changeset
   643
                                "getMostRecentFocusOwner",
25768
b10ec9701a7e 8042872: Fix raw and unchecked warnings in sun.applet
henryjen
parents: 23667
diff changeset
   644
                                new Class<?>[]{Window.class});
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
                        meth.setAccessible(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
                    } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
                        // Must never happen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
                        e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
                    return meth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
        if (meth != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
            // Meth refers static method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
                return (Component)meth.invoke(null, new Object[] {w});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
                // Must never happen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
                e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
        // Will get here if exception was thrown or meth is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
        return w.getMostRecentFocusOwner();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
     * Fix for BugTraq ID 4041703.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
     * Set the focus to a reasonable default for an Applet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
    private void setDefaultFocus() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
        Component toFocus = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
        Container parent = getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
        if(parent != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
            if (parent instanceof Window) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
                toFocus = getMostRecentFocusOwnerForWindow((Window)parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
                if (toFocus == parent || toFocus == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
                    toFocus = parent.getFocusTraversalPolicy().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
                        getInitialComponent((Window)parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
            } else if (parent.isFocusCycleRoot()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
                toFocus = parent.getFocusTraversalPolicy().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
                    getDefaultComponent(parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
        if (toFocus != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
            if (parent instanceof EmbeddedFrame) {
29260
77797a65dc63 8056915: Focus lost in applet when browser window is minimized and restored
aivanov
parents: 28231
diff changeset
   689
                // JDK-8056915: Try to request focus to the embedder first and
77797a65dc63 8056915: Focus lost in applet when browser window is minimized and restored
aivanov
parents: 28231
diff changeset
   690
                // activate the embedded frame through it
77797a65dc63 8056915: Focus lost in applet when browser window is minimized and restored
aivanov
parents: 28231
diff changeset
   691
                if (!((EmbeddedFrame) parent).requestFocusToEmbedder()) {
77797a65dc63 8056915: Focus lost in applet when browser window is minimized and restored
aivanov
parents: 28231
diff changeset
   692
                    // Otherwise activate the embedded frame directly
77797a65dc63 8056915: Focus lost in applet when browser window is minimized and restored
aivanov
parents: 28231
diff changeset
   693
                    ((EmbeddedFrame) parent).synthesizeWindowActivation(true);
77797a65dc63 8056915: Focus lost in applet when browser window is minimized and restored
aivanov
parents: 28231
diff changeset
   694
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
            // EmbeddedFrame might have focus before the applet was added.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
            // Thus after its activation the most recent focus owner will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
            // restored. We need the applet's initial focusabled component to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
            // be focused here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
            toFocus.requestFocusInWindow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
     * Load the applet into memory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
     * Runs in a seperate (and interruptible) thread from the rest of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
     * applet event processing so that it can be gracefully interrupted from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
     * things like HotJava.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
     */
28231
b608ffcaed74 8066621: Suppress deprecation warnings in java.desktop module
darcy
parents: 25859
diff changeset
   710
    @SuppressWarnings("deprecation")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
    private void runLoader() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
        if (status != APPLET_DISPOSE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
            showAppletStatus("notdisposed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
        dispatchAppletEvent(APPLET_LOADING, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
        // REMIND -- might be cool to visually indicate loading here --
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
        // maybe do animation?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
        status = APPLET_LOAD;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
        // Create a class loader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
        loader = getClassLoader(getCodeBase(), getClassLoaderCacheKey());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
        // Load the archives if present.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
        // REMIND - this probably should be done in a separate thread,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
        // or at least the additional archives (epll).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
        String code = getCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
        // setup applet AppContext
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
        // this must be called before loadJarFiles
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
        setupAppletAppContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
            loadJarFiles(loader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
            applet = createApplet(loader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
        } catch (ClassNotFoundException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
            status = APPLET_ERROR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
            showAppletStatus("notfound", code);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
            showAppletLog("notfound", code);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
            showAppletException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
        } catch (InstantiationException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
            status = APPLET_ERROR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
            showAppletStatus("nocreate", code);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
            showAppletLog("nocreate", code);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
            showAppletException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
        } catch (IllegalAccessException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
            status = APPLET_ERROR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
            showAppletStatus("noconstruct", code);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
            showAppletLog("noconstruct", code);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
            showAppletException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
            // sbb -- I added a return here
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
            status = APPLET_ERROR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
            showAppletStatus("exception", e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
            showAppletException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
        } catch (ThreadDeath e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
            status = APPLET_ERROR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
            showAppletStatus("death");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
        } catch (Error e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
            status = APPLET_ERROR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
            showAppletStatus("error", e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
            showAppletException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
            // notify that loading is no longer going on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
            dispatchAppletEvent(APPLET_LOADING_COMPLETED, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
        // Fixed #4508194: NullPointerException thrown during
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
        // quick page switch
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
        if (applet != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
            // Stick it in the frame
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
            applet.setStub(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
            applet.hide();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
            add("Center", applet);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
            showAppletStatus("loaded");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
            validate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
    protected Applet createApplet(final AppletClassLoader loader) throws ClassNotFoundException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
                                                                         IllegalAccessException, IOException, InstantiationException, InterruptedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
        final String serName = getSerializedObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
        String code = getCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
        if (code != null && serName != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
            System.err.println(amh.getMessage("runloader.err"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
//          return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
            throw new InstantiationException("Either \"code\" or \"object\" should be specified, but not both.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
        if (code == null && serName == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
            String msg = "nocode";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
            status = APPLET_ERROR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
            showAppletStatus(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
            showAppletLog(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
            repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
        if (code != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
            applet = (Applet)loader.loadCode(code).newInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
            doInit = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
            // serName is not null;
21785
fc0bfa7d9d95 8027992: FileInputStream and BufferedInputStream should be closed in sun.applet.*
pchelko
parents: 16092
diff changeset
   813
            try (InputStream is = AccessController.doPrivileged(
fc0bfa7d9d95 8027992: FileInputStream and BufferedInputStream should be closed in sun.applet.*
pchelko
parents: 16092
diff changeset
   814
                    (PrivilegedAction<InputStream>)() -> loader.getResourceAsStream(serName));
fc0bfa7d9d95 8027992: FileInputStream and BufferedInputStream should be closed in sun.applet.*
pchelko
parents: 16092
diff changeset
   815
                 ObjectInputStream ois = new AppletObjectInputStream(is, loader)) {
fc0bfa7d9d95 8027992: FileInputStream and BufferedInputStream should be closed in sun.applet.*
pchelko
parents: 16092
diff changeset
   816
fc0bfa7d9d95 8027992: FileInputStream and BufferedInputStream should be closed in sun.applet.*
pchelko
parents: 16092
diff changeset
   817
                applet = (Applet) ois.readObject();
fc0bfa7d9d95 8027992: FileInputStream and BufferedInputStream should be closed in sun.applet.*
pchelko
parents: 16092
diff changeset
   818
                doInit = false; // skip over the first init
fc0bfa7d9d95 8027992: FileInputStream and BufferedInputStream should be closed in sun.applet.*
pchelko
parents: 16092
diff changeset
   819
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
        // Determine the JDK level that the applet targets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
        // This is critical for enabling certain backward
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
        // compatibility switch if an applet is a JDK 1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
        // applet. [stanley.ho]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
        findAppletJDKLevel(applet);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
        if (Thread.interrupted()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
                status = APPLET_DISPOSE; // APPLET_ERROR?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
                applet = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
                // REMIND: This may not be exactly the right thing: the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
                // status is set by the stop button and not necessarily
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
                // here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
                showAppletStatus("death");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
                Thread.currentThread().interrupt(); // resignal interrupt
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
        return applet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
    protected void loadJarFiles(AppletClassLoader loader) throws IOException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
                                                                 InterruptedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
        // Load the archives if present.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
        // REMIND - this probably should be done in a separate thread,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
        // or at least the additional archives (epll).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
        String jarFiles = getJarFiles();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
        if (jarFiles != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
            StringTokenizer st = new StringTokenizer(jarFiles, ",", false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
            while(st.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
                String tok = st.nextToken().trim();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
                    loader.addJar(tok);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
                } catch (IllegalArgumentException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
                    // bad archive name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
     * Request that the loading of the applet be stopped.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
    protected synchronized void stopLoading() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
        // REMIND: fill in the body
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
        if (loaderThread != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
            //System.out.println("Interrupting applet loader thread: " + loaderThread);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
            loaderThread.interrupt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
            setLoadAbortRequest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
    protected synchronized boolean okToLoad() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
        return !loadAbortRequest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
    protected synchronized void clearLoadAbortRequest() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
        loadAbortRequest = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
    protected synchronized void setLoadAbortRequest() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
        loadAbortRequest = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
    private synchronized void setLoaderThread(Thread loaderThread) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
        this.loaderThread = loaderThread;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
     * Return true when the applet has been started.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
     */
23667
37f0fbe26e84 8028050: Typo correction needed s/Classlaoder/Classloader/
serb
parents: 23642
diff changeset
   899
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
    public boolean isActive() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
        return status == APPLET_START;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
    private EventQueue appEvtQ = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
     * Is called when the applet wants to be resized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
     */
23667
37f0fbe26e84 8028050: Typo correction needed s/Classlaoder/Classloader/
serb
parents: 23642
diff changeset
   909
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
    public void appletResize(int width, int height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
        currentAppletSize.width = width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
        currentAppletSize.height = height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
        final Dimension currentSize = new Dimension(currentAppletSize.width,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
                                                    currentAppletSize.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
        if(loader != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
            AppContext appCtxt = loader.getAppContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
            if(appCtxt != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
                appEvtQ = (java.awt.EventQueue)appCtxt.get(AppContext.EVENT_QUEUE_KEY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
        final AppletPanel ap = this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
        if (appEvtQ != null){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
            appEvtQ.postEvent(new InvocationEvent(Toolkit.getDefaultToolkit(),
23667
37f0fbe26e84 8028050: Typo correction needed s/Classlaoder/Classloader/
serb
parents: 23642
diff changeset
   925
                                                  new Runnable() {
37f0fbe26e84 8028050: Typo correction needed s/Classlaoder/Classloader/
serb
parents: 23642
diff changeset
   926
                                                      @Override
37f0fbe26e84 8028050: Typo correction needed s/Classlaoder/Classloader/
serb
parents: 23642
diff changeset
   927
                                                      public void run() {
37f0fbe26e84 8028050: Typo correction needed s/Classlaoder/Classloader/
serb
parents: 23642
diff changeset
   928
                                                          if (ap != null) {
37f0fbe26e84 8028050: Typo correction needed s/Classlaoder/Classloader/
serb
parents: 23642
diff changeset
   929
                                                              ap.dispatchAppletEvent(
37f0fbe26e84 8028050: Typo correction needed s/Classlaoder/Classloader/
serb
parents: 23642
diff changeset
   930
                                                                      APPLET_RESIZE,
37f0fbe26e84 8028050: Typo correction needed s/Classlaoder/Classloader/
serb
parents: 23642
diff changeset
   931
                                                                      currentSize);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
                                                          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
                                                      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
                                                  }));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
23667
37f0fbe26e84 8028050: Typo correction needed s/Classlaoder/Classloader/
serb
parents: 23642
diff changeset
   938
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
    public void setBounds(int x, int y, int width, int height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
        super.setBounds(x, y, width, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
        currentAppletSize.width = width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
        currentAppletSize.height = height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
    public Applet getApplet() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
        return applet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
     * Status line. Called by the AppletPanel to provide
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
     * feedback on the Applet's state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
    protected void showAppletStatus(String status) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
        getAppletContext().showStatus(amh.getMessage(status));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
    protected void showAppletStatus(String status, Object arg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
        getAppletContext().showStatus(amh.getMessage(status, arg));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
    protected void showAppletStatus(String status, Object arg1, Object arg2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
        getAppletContext().showStatus(amh.getMessage(status, arg1, arg2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
     * Called by the AppletPanel to print to the log.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
    protected void showAppletLog(String msg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
        System.out.println(amh.getMessage(msg));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
    protected void showAppletLog(String msg, Object arg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
        System.out.println(amh.getMessage(msg, arg));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
     * Called by the AppletPanel to provide
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
     * feedback when an exception has happened.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
    protected void showAppletException(Throwable t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
        t.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
        repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
     * Get caching key for classloader cache
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
    public String getClassLoaderCacheKey()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
        /**
23667
37f0fbe26e84 8028050: Typo correction needed s/Classlaoder/Classloader/
serb
parents: 23642
diff changeset
   990
         * Fixed #4501142: Classloader sharing policy doesn't
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
         * take "archive" into account. This will be overridden
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
         * by Java Plug-in.                     [stanleyh]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
        return getCodeBase().toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
     * The class loaders
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
     */
25768
b10ec9701a7e 8042872: Fix raw and unchecked warnings in sun.applet
henryjen
parents: 23667
diff changeset
  1000
    private static HashMap<String, AppletClassLoader> classloaders = new HashMap<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
     * Flush a class loader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
    public static synchronized void flushClassLoader(String key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
        classloaders.remove(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
     * Flush all class loaders.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
    public static synchronized void flushClassLoaders() {
25768
b10ec9701a7e 8042872: Fix raw and unchecked warnings in sun.applet
henryjen
parents: 23667
diff changeset
  1013
        classloaders = new HashMap<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
     * This method actually creates an AppletClassLoader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
     * It can be override by subclasses (such as the Plug-in)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
     * to provide different classloaders.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
    protected AppletClassLoader createClassLoader(final URL codebase) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
        return new AppletClassLoader(codebase);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
     * Get a class loader. Create in a restricted context
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
    synchronized AppletClassLoader getClassLoader(final URL codebase, final String key) {
25768
b10ec9701a7e 8042872: Fix raw and unchecked warnings in sun.applet
henryjen
parents: 23667
diff changeset
  1030
        AppletClassLoader c = classloaders.get(key);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
        if (c == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
            AccessControlContext acc =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
                getAccessControlContext(codebase);
25768
b10ec9701a7e 8042872: Fix raw and unchecked warnings in sun.applet
henryjen
parents: 23667
diff changeset
  1034
            c = AccessController.doPrivileged(
b10ec9701a7e 8042872: Fix raw and unchecked warnings in sun.applet
henryjen
parents: 23667
diff changeset
  1035
                    new PrivilegedAction<AppletClassLoader>() {
23667
37f0fbe26e84 8028050: Typo correction needed s/Classlaoder/Classloader/
serb
parents: 23642
diff changeset
  1036
                        @Override
25768
b10ec9701a7e 8042872: Fix raw and unchecked warnings in sun.applet
henryjen
parents: 23667
diff changeset
  1037
                        public AppletClassLoader run() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
                            AppletClassLoader ac = createClassLoader(codebase);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
                            /* Should the creation of the classloader be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
                             * within the class synchronized block?  Since
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
                             * this class is used by the plugin, take care
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
                             * to avoid deadlocks, or specialize
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
                             * AppletPanel within the plugin.  It may take
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
                             * an arbitrary amount of time to create a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
                             * class loader (involving getting Jar files
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
                             * etc.) and may block unrelated applets from
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
                             * finishing createAppletThread (due to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
                             * class synchronization). If
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
                             * createAppletThread does not finish quickly,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
                             * the applet cannot process other messages,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
                             * particularly messages such as destroy
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
                             * (which timeout when called from the browser).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
                             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
                            synchronized (getClass()) {
25768
b10ec9701a7e 8042872: Fix raw and unchecked warnings in sun.applet
henryjen
parents: 23667
diff changeset
  1055
                                AppletClassLoader res = classloaders.get(key);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
                                if (res == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
                                    classloaders.put(key, ac);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
                                    return ac;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
                                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
                                    return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
                    },acc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
        return c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
     * get the context for the AppletClassLoader we are creating.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
     * the context is granted permission to create the class loader,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
     * connnect to the codebase, and whatever else the policy grants
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
     * to all codebases.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
    private AccessControlContext getAccessControlContext(final URL codebase) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
25768
b10ec9701a7e 8042872: Fix raw and unchecked warnings in sun.applet
henryjen
parents: 23667
diff changeset
  1077
        PermissionCollection perms = AccessController.doPrivileged(
b10ec9701a7e 8042872: Fix raw and unchecked warnings in sun.applet
henryjen
parents: 23667
diff changeset
  1078
                new PrivilegedAction<PermissionCollection>() {
23667
37f0fbe26e84 8028050: Typo correction needed s/Classlaoder/Classloader/
serb
parents: 23642
diff changeset
  1079
                    @Override
25768
b10ec9701a7e 8042872: Fix raw and unchecked warnings in sun.applet
henryjen
parents: 23667
diff changeset
  1080
                    public PermissionCollection run() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
                        Policy p = java.security.Policy.getPolicy();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
                        if (p != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
                            return p.getPermissions(new CodeSource(null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
                                                                   (java.security.cert.Certificate[]) null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
                            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
                });
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
        if (perms == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
            perms = new Permissions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
        //XXX: this is needed to be able to create the classloader itself!
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
        perms.add(SecurityConstants.CREATE_CLASSLOADER_PERMISSION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
        Permission p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
        java.net.URLConnection urlConnection = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
            urlConnection = codebase.openConnection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
            p = urlConnection.getPermission();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
        } catch (java.io.IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
            p = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
        if (p != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
            perms.add(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
        if (p instanceof FilePermission) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
            String path = p.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
            int endIndex = path.lastIndexOf(File.separatorChar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
            if (endIndex != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
                path = path.substring(0, endIndex+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
                if (path.endsWith(File.separator)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
                    path += "-";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
                perms.add(new FilePermission(path,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
                                             SecurityConstants.FILE_READ_ACTION));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
            URL locUrl = codebase;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
            if (urlConnection instanceof JarURLConnection) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
                locUrl = ((JarURLConnection)urlConnection).getJarFileURL();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
            String host = locUrl.getHost();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
            if (host != null && (host.length() > 0))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
                perms.add(new SocketPermission(host,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
                                               SecurityConstants.SOCKET_CONNECT_ACCEPT_ACTION));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
        ProtectionDomain domain =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
            new ProtectionDomain(new CodeSource(codebase,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
                                                (java.security.cert.Certificate[]) null), perms);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
        AccessControlContext acc =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
            new AccessControlContext(new ProtectionDomain[] { domain });
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
        return acc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
    public Thread getAppletHandlerThread() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
        return handler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
    public int getAppletWidth() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
        return currentAppletSize.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
    public int getAppletHeight() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
        return currentAppletSize.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
    public static void changeFrameAppContext(Frame frame, AppContext newAppContext)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
        // Fixed #4754451: Applet can have methods running on main
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
        // thread event queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
        // The cause of this bug is that the frame of the applet
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
        // is created in main thread group. Thus, when certain
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
        // AWT/Swing events are generated, the events will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
        // dispatched through the wrong event dispatch thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
        // To fix this, we rearrange the AppContext with the frame,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
        // so the proper event queue will be looked up.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
        // Swing also maintains a Frame list for the AppContext,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
        // so we will have to rearrange it as well.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
        // Check if frame's AppContext has already been set properly
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
        AppContext oldAppContext = SunToolkit.targetToAppContext(frame);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
        if (oldAppContext == newAppContext)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
        // Synchronization on Window.class is needed for locking the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
        // critical section of the window list in AppContext.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
        synchronized (Window.class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
        {
25768
b10ec9701a7e 8042872: Fix raw and unchecked warnings in sun.applet
henryjen
parents: 23667
diff changeset
  1183
            WeakReference<Window> weakRef = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
            // Remove frame from the Window list in wrong AppContext
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
                // Lookup current frame's AppContext
25768
b10ec9701a7e 8042872: Fix raw and unchecked warnings in sun.applet
henryjen
parents: 23667
diff changeset
  1187
                @SuppressWarnings("unchecked")
b10ec9701a7e 8042872: Fix raw and unchecked warnings in sun.applet
henryjen
parents: 23667
diff changeset
  1188
                Vector<WeakReference<Window>> windowList =
b10ec9701a7e 8042872: Fix raw and unchecked warnings in sun.applet
henryjen
parents: 23667
diff changeset
  1189
                    (Vector<WeakReference<Window>>)oldAppContext.get(Window.class);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
                if (windowList != null) {
25768
b10ec9701a7e 8042872: Fix raw and unchecked warnings in sun.applet
henryjen
parents: 23667
diff changeset
  1191
                    for (WeakReference<Window> ref : windowList) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
                        if (ref.get() == frame) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
                            weakRef = ref;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
                    // Remove frame from wrong AppContext
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
                    if (weakRef != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
                        windowList.remove(weakRef);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
            // Put the frame into the applet's AppContext map
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
            SunToolkit.insertTargetMapping(frame, newAppContext);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
            // Insert frame into the Window list in the applet's AppContext map
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
            {
25768
b10ec9701a7e 8042872: Fix raw and unchecked warnings in sun.applet
henryjen
parents: 23667
diff changeset
  1208
                @SuppressWarnings("unchecked")
b10ec9701a7e 8042872: Fix raw and unchecked warnings in sun.applet
henryjen
parents: 23667
diff changeset
  1209
                Vector<WeakReference<Window>> windowList =
b10ec9701a7e 8042872: Fix raw and unchecked warnings in sun.applet
henryjen
parents: 23667
diff changeset
  1210
                    (Vector<WeakReference<Window>>)newAppContext.get(Window.class);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
                if (windowList == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
                    windowList = new Vector<WeakReference<Window>>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
                    newAppContext.put(Window.class, windowList);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
                // use the same weakRef here as it is used elsewhere
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
                windowList.add(weakRef);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
    // Flag to indicate if applet is targeted for JDK 1.1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
    private boolean jdk11Applet = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
    // Flag to indicate if applet is targeted for JDK 1.2.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
    private boolean jdk12Applet = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
     * Determine JDK level of an applet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
    private void findAppletJDKLevel(Applet applet)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
        // To determine the JDK level of an applet, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
        // most reliable way is to check the major version
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
        // of the applet class file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
        // synchronized on applet class object, so calling from
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
        // different instances of the same applet will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
        // serialized.
25768
b10ec9701a7e 8042872: Fix raw and unchecked warnings in sun.applet
henryjen
parents: 23667
diff changeset
  1239
        Class<?> appletClass = applet.getClass();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
        synchronized(appletClass)  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
            // Determine if the JDK level of an applet has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
            // checked before.
22584
eed64ee05369 8032733: Fix cast lint warnings in client libraries
darcy
parents: 21785
diff changeset
  1244
            Boolean jdk11Target = loader.isJDK11Target(appletClass);
eed64ee05369 8032733: Fix cast lint warnings in client libraries
darcy
parents: 21785
diff changeset
  1245
            Boolean jdk12Target = loader.isJDK12Target(appletClass);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
            // if applet JDK level has been checked before, retrieve
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
            // value and return.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
            if (jdk11Target != null || jdk12Target != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
                jdk11Applet = (jdk11Target == null) ? false : jdk11Target.booleanValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
                jdk12Applet = (jdk12Target == null) ? false : jdk12Target.booleanValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
            String name = appletClass.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
            // first convert any '.' to '/'
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
            name = name.replace('.', '/');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
            // append .class
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
            final String resourceName = name + ".class";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
            byte[] classHeader = new byte[8];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
21785
fc0bfa7d9d95 8027992: FileInputStream and BufferedInputStream should be closed in sun.applet.*
pchelko
parents: 16092
diff changeset
  1265
            try (InputStream is = AccessController.doPrivileged(
fc0bfa7d9d95 8027992: FileInputStream and BufferedInputStream should be closed in sun.applet.*
pchelko
parents: 16092
diff changeset
  1266
                    (PrivilegedAction<InputStream>) () -> loader.getResourceAsStream(resourceName))) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
                // Read the first 8 bytes of the class file
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
                int byteRead = is.read(classHeader, 0, 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
                // return if the header is not read in entirely
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
                // for some reasons.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
                if (byteRead != 8)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
            catch (IOException e)   {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
            // Check major version in class file header
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
            int major_version = readShort(classHeader, 6);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
            // Major version in class file is as follows:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
            //   45 - JDK 1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
            //   46 - JDK 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
            //   47 - JDK 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
            //   48 - JDK 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
            //   49 - JDK 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
            if (major_version < 46)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
                jdk11Applet = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
            else if (major_version == 46)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
                jdk12Applet = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
            // Store applet JDK level in AppContext for later lookup,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
            // e.g. page switch.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
            loader.setJDK11Target(appletClass, jdk11Applet);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
            loader.setJDK12Target(appletClass, jdk12Applet);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
     * Return true if applet is targeted to JDK 1.1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
    protected boolean isJDK11Applet()   {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
        return jdk11Applet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
     * Return true if applet is targeted to JDK1.2.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
    protected boolean isJDK12Applet()   {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
        return jdk12Applet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
     * Read short from byte array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
    private int readShort(byte[] b, int off)    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
        int hi = readByte(b[off]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
        int lo = readByte(b[off + 1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
        return (hi << 8) | lo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
    private int readByte(byte b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
        return ((int)b) & 0xFF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
    private static AppletMessageHandler amh = new AppletMessageHandler("appletpanel");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
}