1 /*
   2  * Copyright (c) 1995, 2005, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.applet;
  27 
  28 import java.util.*;
  29 import java.io.*;
  30 import java.net.URL;
  31 import java.net.MalformedURLException;
  32 import java.awt.*;
  33 import java.applet.*;
  34 
  35 
  36 /**
  37  * Sample applet panel class. The panel manages and manipulates the
  38  * applet as it is being loaded. It forks a seperate thread in a new
  39  * thread group to call the applet's init(), start(), stop(), and
  40  * destroy() methods.
  41  *
  42  * @author      Arthur van Hoff
  43  */
  44 class AppletViewerPanel extends AppletPanel {
  45 
  46     /* Are we debugging? */
  47     static boolean debug = false;
  48 
  49     /**
  50      * The document url.
  51      */
  52     URL documentURL;
  53 
  54     /**
  55      * The base url.
  56      */
  57     URL baseURL;
  58 
  59     /**
  60      * The attributes of the applet.
  61      */
  62     Hashtable<String, String> atts;
  63 
  64     /*
  65      * JDK 1.1 serialVersionUID
  66      */
  67     private static final long serialVersionUID = 8890989370785545619L;
  68 
  69     /**
  70      * Construct an applet viewer and start the applet.
  71      */
  72     AppletViewerPanel(URL documentURL, Hashtable<String, String> atts) {
  73         this.documentURL = documentURL;
  74         this.atts = atts;
  75 
  76         String att = getParameter("codebase");
  77         if (att != null) {
  78             if (!att.endsWith("/")) {
  79                 att += "/";
  80             }
  81             try {
  82                 baseURL = new URL(documentURL, att);
  83             } catch (MalformedURLException e) {
  84             }
  85         }
  86         if (baseURL == null) {
  87             String file = documentURL.getFile();
  88             int i = file.lastIndexOf('/');
  89             if (i >= 0 && i < file.length() - 1) {
  90                 try {
  91                     baseURL = new URL(documentURL, file.substring(0, i + 1));
  92                 } catch (MalformedURLException e) {
  93                 }
  94             }
  95         }
  96 
  97         // when all is said & done, baseURL shouldn't be null
  98         if (baseURL == null)
  99                 baseURL = documentURL;
 100 
 101 
 102     }
 103 
 104     /**
 105      * Get an applet parameter.
 106      */
 107     public String getParameter(String name) {
 108         return atts.get(name.toLowerCase());
 109     }
 110 
 111     /**
 112      * Get the document url.
 113      */
 114     public URL getDocumentBase() {
 115         return documentURL;
 116 
 117     }
 118 
 119     /**
 120      * Get the base url.
 121      */
 122     public URL getCodeBase() {
 123         return baseURL;
 124     }
 125 
 126     /**
 127      * Get the width.
 128      */
 129     public int getWidth() {
 130         String w = getParameter("width");
 131         if (w != null) {
 132             return Integer.valueOf(w).intValue();
 133         }
 134         return 0;
 135     }
 136 
 137 
 138     /**
 139      * Get the height.
 140      */
 141     public int getHeight() {
 142         String h = getParameter("height");
 143         if (h != null) {
 144             return Integer.valueOf(h).intValue();
 145         }
 146         return 0;
 147     }
 148 
 149     /**
 150      * Get initial_focus
 151      */
 152     public boolean hasInitialFocus()
 153     {
 154 
 155         // 6234219: Do not set initial focus on an applet
 156         // during startup if applet is targeted for
 157         // JDK 1.1/1.2. [stanley.ho]
 158         if (isJDK11Applet() || isJDK12Applet())
 159             return false;
 160 
 161         String initialFocus = getParameter("initial_focus");
 162 
 163         if (initialFocus != null)
 164         {
 165             if (initialFocus.toLowerCase().equals("false"))
 166                 return false;
 167         }
 168 
 169         return true;
 170     }
 171 
 172     /**
 173      * Get the code parameter
 174      */
 175     public String getCode() {
 176         return getParameter("code");
 177     }
 178 
 179 
 180     /**
 181      * Return the list of jar files if specified.
 182      * Otherwise return null.
 183      */
 184     public String getJarFiles() {
 185         return getParameter("archive");
 186     }
 187 
 188     /**
 189      * Return the value of the object param
 190      */
 191     public String getSerializedObject() {
 192         return getParameter("object");// another name?
 193     }
 194 
 195 
 196     /**
 197      * Get the applet context. For now this is
 198      * also implemented by the AppletPanel class.
 199      */
 200     @SuppressWarnings("deprecation")
 201     public AppletContext getAppletContext() {
 202         return (AppletContext)getParent();
 203     }
 204 
 205     static void debug(String s) {
 206         if(debug)
 207             System.err.println("AppletViewerPanel:::" + s);
 208     }
 209 
 210     static void debug(String s, Throwable t) {
 211         if(debug) {
 212             t.printStackTrace();
 213             debug(s);
 214         }
 215     }
 216 }