changeset 585:6c63976cd18b

2008-02-02 Francis Kung <fkung@redhat.com> * tools/netx/jnlp/JNLPFile.java: Added empty protected constructor. * tools/netx/jnlp/Launcher.java (launch(JNLPFile)): Delegate to new method. (launch(JNLPFile, Container)): New method. (launchApplet): Added Container parameter. (createApplet): Likewise. (TgThread): Added private Container field. (TgThread.constructor(JNLPFile)): Delegate to new constructor. (TgThread.constructor(JNLPFile, Container)): New method. (TgThread.run): Launch applet with container argument. * tools/netx/jnlp/runtime/AppletEnvironment.java (Frame): Renamed field to... (Container): New field. (AppletEnvironment(JNLPFile, AppletInstance, Container)): New method. (AppletEnvironment(JNLPFile, AppletInstance)): Delegate to new method. (getAppletFrame): Return Container instead of Frame. (startApplet): Replace Frame with Container. (appletResize): Likewise. (getParameter): Add lower-case check. *tools/netx/jnlp/runtime/AppletInstance.java (AppletInstance(JNLPFile, ThreadGroup, ClassLoader, Applet, Container)): New method. (setResizable): Only resize if the container is a Frame. (isResizable): Return false if container is not a Frame.
author Francis Kung <fkung@redhat.com>
date Sat, 02 Feb 2008 17:13:16 -0500
parents 49ff51214afc
children 94b86ca839e4
files tools/netx/jnlp/JNLPFile.java tools/netx/jnlp/Launcher.java tools/netx/jnlp/Parser.java tools/netx/jnlp/runtime/AppletEnvironment.java tools/netx/jnlp/runtime/AppletInstance.java
diffstat 5 files changed, 116 insertions(+), 175 deletions(-) [+]
line wrap: on
line diff
--- a/tools/netx/jnlp/JNLPFile.java	Fri Feb 01 09:07:04 2008 -0500
+++ b/tools/netx/jnlp/JNLPFile.java	Sat Feb 02 17:13:16 2008 -0500
@@ -103,6 +103,11 @@
         }
     }
 
+    /**
+     * Empty stub, allowing child classes to override the constructor
+     */
+    protected JNLPFile() {
+    }
 
     /**
      * Create a JNLPFile from a URL.
--- a/tools/netx/jnlp/Launcher.java	Fri Feb 01 09:07:04 2008 -0500
+++ b/tools/netx/jnlp/Launcher.java	Sat Feb 02 17:13:16 2008 -0500
@@ -18,6 +18,7 @@
 package netx.jnlp;
 
 import java.applet.*;
+import java.awt.Container;
 import java.io.*;
 import java.net.*;
 import java.util.*;
@@ -132,14 +133,34 @@
 
     /**
      * Launches a JNLP file by calling the launch method for the
-     * appropriate file type.
+     * appropriate file type.  The application will be started in
+     * a new window.
      *
      * @param file the JNLP file to launch
      * @return the application instance
      * @throws LaunchException if an error occurred while launching (also sent to handler)
      */
     public ApplicationInstance launch(JNLPFile file) throws LaunchException {
-        TgThread tg = new TgThread(file);
+        return launch(file, null);
+    }
+
+    /**
+     * Launches a JNLP file inside the given container if it is an applet.  Specifying a
+     * container has no effect for Applcations and Installers.
+     *
+     * @param file the JNLP file to launch
+     * @param cont the container in which to place the application, if it is an applet
+     * @return the application instance
+     * @throws LaunchException if an error occurred while launching (also sent to handler)
+     */
+    public ApplicationInstance launch(JNLPFile file, Container cont) throws LaunchException {
+        TgThread tg;
+
+        if (cont == null)
+          tg = new TgThread(file);
+        else
+          tg = new TgThread(file, cont);
+
         tg.start();
 
         try {
@@ -157,7 +178,7 @@
             handler.launchCompleted(tg.getApplication());
 
         return tg.getApplication();
-    }
+    }    
 
     /**
      * Launches a JNLP file by calling the launch method for the
@@ -330,12 +351,12 @@
      * @param file the JNLP file
      * @param enableCodeBase whether to add the codebase URL to the classloader
      */
-    protected ApplicationInstance launchApplet(JNLPFile file, boolean enableCodeBase) throws LaunchException {
+    protected ApplicationInstance launchApplet(JNLPFile file, boolean enableCodeBase, Container cont) throws LaunchException {
         if (!file.isApplet())
             throw launchError(new LaunchException(file, null, R("LSFatal"), R("LCClient"), R("LNotApplet"), R("LNotAppletInfo")));
 
         try {
-            AppletInstance applet= createApplet(file, enableCodeBase);
+            AppletInstance applet= createApplet(file, enableCodeBase, cont);
             applet.initialize();
 
             applet.getAppletEnvironment().startApplet(); // this should be a direct call to applet instance
@@ -363,7 +384,7 @@
      *
      * @param enableCodeBase whether to add the code base URL to the classloader
      */
-    protected AppletInstance createApplet(JNLPFile file, boolean enableCodeBase) throws LaunchException {
+    protected AppletInstance createApplet(JNLPFile file, boolean enableCodeBase, Container cont) throws LaunchException {
         try {
             JNLPClassLoader loader = JNLPClassLoader.getInstance(file, updatePolicy);
 
@@ -376,7 +397,12 @@
             Class appletClass = loader.loadClass(appletName);
             Applet applet = (Applet) appletClass.newInstance();
 
-            AppletInstance appletInstance = new AppletInstance(file, group, loader, applet);
+            AppletInstance appletInstance;
+            if (cont == null)
+              appletInstance = new AppletInstance(file, group, loader, applet);
+            else
+              appletInstance = new AppletInstance(file, group, loader, applet, cont);
+
             group.setApplication(appletInstance);
             loader.setApplication(appletInstance);
 
@@ -452,11 +478,17 @@
         private JNLPFile file;
         private ApplicationInstance application;
         private LaunchException exception;
+        private Container cont;
 
         TgThread(JNLPFile file) {
+            this(file, null);
+        }
+
+        TgThread(JNLPFile file, Container cont) {
             super(createThreadGroup(file), file.getTitle());
 
             this.file = file;
+            this.cont = cont;
         }
 
         public void run() {
@@ -467,7 +499,7 @@
                 if (file.isApplication())
                     application = launchApplication(file);
                 else if (file.isApplet())
-                    application = launchApplet(file, true); // enable applet code base
+                    application = launchApplet(file, true, cont); // enable applet code base
                 else if (file.isInstaller())
                     application = launchInstaller(file);
                 else 
--- a/tools/netx/jnlp/Parser.java	Fri Feb 01 09:07:04 2008 -0500
+++ b/tools/netx/jnlp/Parser.java	Sat Feb 02 17:13:16 2008 -0500
@@ -958,144 +958,4 @@
     }
 
 }
-
-// this class makes assumptions on how parser calls methods (such
-// as getFirstChild->getNextChild only called by a single loop at
-// a time, so no need for an iterator).
-
-/**
- * This class converts the NanoXML's XMLElement nodes into the
- * regular XML Node interface (for the methods used by Parser).
- */
-/* NANO */
-class Node {
-    private XMLElement xml;
-    private Node next;
-    private Node children[];
-
-    Node(XMLElement xml) {
-        this.xml = xml;
-    }
-
-    Node getFirstChild() {
-        if (children == null)
-            getChildNodes();
-
-        if (children.length == 0)
-            return null;
-        else
-            return children[0];
-    }
-
-    Node getNextSibling() {
-        return next;
-    }
-
-    void normalize() {
-    }
-
-    String getNodeValue() {
-        return xml.getContent();
-    }
-
-    Node[] getChildNodes() {
-        if (children == null) {
-            List list = new ArrayList();
-
-            for (Enumeration e = xml.enumerateChildren(); e.hasMoreElements();)
-                list.add( new Node((XMLElement)e.nextElement()) );
-
-            children = (Node[]) list.toArray( new Node[list.size()] );
-
-            for (int i=0; i < children.length-1; i++)
-                children[i].next = children[i+1];
-        }
-
-        return children;
-    }
-
-    String getAttribute(String name) {
-        return (String)xml.getAttribute(name);
-    }
-
-    String getNodeName() {
-        if (xml.getName() == null)
-            return "";
-        else
-            return xml.getName();
-    }
-
-    public String toString() {
-        return getNodeName();
-    }
-}
-
-/**
- * This class converts the TinyXML's ParsedXML nodes into the
- * regular XML Node interface (for the methods used by Parser).
- */
-/* TINY 
-class Node {
-    private ParsedXML tinyNode;
-    private Node next;
-    private Node children[];
-
-    Node(ParsedXML tinyNode) {
-        this.tinyNode = tinyNode;
-    }
-
-    Node getFirstChild() {
-        if (children == null)
-            getChildNodes();
-
-        if (children.length == 0)
-            return null;
-        else
-            return children[0];
-    }
-
-    Node getNextSibling() {
-        return next;
-    }
-
-    void normalize() {
-    }
-
-    String getNodeValue() {
-        return tinyNode.getContent();
-    }
-
-    Node[] getChildNodes() {
-        if (children == null) {
-            List list = new ArrayList();
-
-            for (Enumeration e = tinyNode.elements(); e.hasMoreElements();) {
-                list.add( new Node((ParsedXML)e.nextElement()) );
-            }
-            children = (Node[]) list.toArray( new Node[list.size()] );
-
-            for (int i=0; i < children.length-1; i++)
-                children[i].next = children[i+1];
-        }
-
-        return children;
-    }
-
-    String getAttribute(String name) {
-        return tinyNode.getAttribute(name);
-    }
-
-    String getNodeName() {
-        if (tinyNode.getName() == null)
-            return "";
-        else
-            return tinyNode.getName();
-    }
-
-    public String toString() {
-        return getNodeName();
-    }
-}
-*/
-
-
+
--- a/tools/netx/jnlp/runtime/AppletEnvironment.java	Fri Feb 01 09:07:04 2008 -0500
+++ b/tools/netx/jnlp/runtime/AppletEnvironment.java	Sat Feb 02 17:13:16 2008 -0500
@@ -27,6 +27,7 @@
 import javax.swing.*;
 import netx.jnlp.*;
 import netx.jnlp.util.*;
+import sun.applet.AppletViewerPanel;
 
 /**
  * The applet environment including stub, context, and frame.  The
@@ -51,8 +52,8 @@
     /** the parameters */
     private Map parameters;
 
-    /** the applet frame */
-    private Frame frame;
+    /** the applet container */
+    private Container cont;
 
     /** weak references to the audio clips */
     private WeakList weakClips = new WeakList();
@@ -68,17 +69,26 @@
      * Create a new applet environment for the applet specified by
      * the JNLP file.
      */
-    public AppletEnvironment(JNLPFile file, final AppletInstance appletInstance) {
+    public AppletEnvironment(JNLPFile file, final AppletInstance appletInstance, Container cont) {
         this.file = file;
         this.appletInstance = appletInstance;
         this.applet = appletInstance.getApplet();
 
         parameters = file.getApplet().getParameters();
-        frame = new Frame(file.getApplet().getName() + " - Applet");
+        this.cont = cont;
+    }
+
+    /**
+     * Create a new applet environment for the applet specified by
+     * the JNLP file, in a new frame.
+     */
+    public AppletEnvironment(JNLPFile file, final AppletInstance appletInstance) {
+        this(file, appletInstance, null);
+
+        Frame frame = new Frame(file.getApplet().getName() + " - Applet");
         frame.setResizable(false);
 
         appletInstance.addWindow(frame);
-
         // may not need this once security manager can close windows
         // that do not have app code on the stack
         WindowListener closer = new WindowAdapter() {
@@ -88,6 +98,7 @@
             }
         };
         frame.addWindowListener(closer);
+        this.cont = frame;
     }
 
     /**
@@ -119,8 +130,9 @@
      * Returns the frame that contains the applet.  Disposing this
      * frame will destroy the applet.
      */
-    public Frame getAppletFrame() {
-        return frame;
+    public Container getAppletFrame() {
+        // TODO: rename this method to getAppletContainer ?
+        return cont;
     }
 
     /**
@@ -137,27 +149,35 @@
         try {
             AppletDesc appletDesc = file.getApplet();
 
-            applet.setStub(this);
+            if (cont instanceof AppletStub)
+                applet.setStub((AppletStub)cont);
+            else
+                applet.setStub(this);
+
+            cont.setLayout(new BorderLayout());
+            cont.add("Center", applet);
+            cont.validate();
 
-            frame.setLayout(new BorderLayout());
-            frame.add(applet);
-            frame.validate();
-            frame.pack(); // cause insets to be calculated
+            // This is only needed if the applet is in its own frame.
+            if (cont instanceof Frame) {
+                Frame frame = (Frame) cont;
+                frame.pack(); // cause insets to be calculated
 
-            Insets insets = frame.getInsets();
-            frame.setSize(appletDesc.getWidth() + insets.left + insets.right,
-                          appletDesc.getHeight() + insets.top + insets.bottom);
-
+                Insets insets = frame.getInsets();
+                frame.setSize(appletDesc.getWidth() + insets.left + insets.right,
+                              appletDesc.getHeight() + insets.top + insets.bottom);
+            }
+    
             // do first because some applets need to be displayed before
             // starting (they use Component.getImage or something)
-            frame.show();
+            cont.show();
 
             applet.init();
             applet.start();
 
-            frame.invalidate(); // this should force the applet to
-            frame.validate();   // the correct size and to repaint
-            frame.repaint();
+            cont.invalidate(); // this should force the applet to
+            cont.validate();   // the correct size and to repaint
+            cont.repaint();
         }
         catch (Exception ex) {
             if (JNLPRuntime.isDebug())
@@ -273,10 +293,13 @@
     public void appletResize(int width, int height) {
         checkDestroyed();
 
-        Insets insets = frame.getInsets();
+        if (cont instanceof Frame) {
+            Frame frame = (Frame) cont;
+            Insets insets = frame.getInsets();
 
-        frame.setSize(width + insets.left + insets.right,
-                      height + insets.top + insets.bottom);
+            frame.setSize(width + insets.left + insets.right,
+                          height + insets.top + insets.bottom);
+        }
     }
 
     public AppletContext getAppletContext() {
@@ -297,10 +320,16 @@
         return file.getApplet().getDocumentBase();
     }
 
+    // FIXME: Sun's applet code forces all parameters to lower case.
+    // Does Netx's JNLP code do the same, so we can remove the first lookup?
     public String getParameter(String name) {
         checkDestroyed();
 
-        return (String) parameters.get(name);
+        String s = (String) parameters.get(name);
+        if (s != null)
+            return s;
+
+        return (String) parameters.get(name.toLowerCase());
     }
 
     public boolean isActive() {
--- a/tools/netx/jnlp/runtime/AppletInstance.java	Fri Feb 01 09:07:04 2008 -0500
+++ b/tools/netx/jnlp/runtime/AppletInstance.java	Sat Feb 02 17:13:16 2008 -0500
@@ -60,18 +60,33 @@
     }
 
     /**
+     * 
+     */
+    public AppletInstance(JNLPFile file, ThreadGroup group, ClassLoader loader, Applet applet, Container cont) {
+        super(file, group, loader);
+        this.applet = applet;
+        this.environment = new AppletEnvironment(file, this, cont);
+    }
+
+    /**
      * Sets whether the applet is resizable or not.  Applets default
      * to being not resizable.
      */
     public void setResizable(boolean resizable) {
-        environment.getAppletFrame().setResizable(resizable);
+        Container c = environment.getAppletFrame();
+        if (c instanceof Frame)
+            ((Frame) c).setResizable(resizable);
     }
 
     /**
      * Returns whether the applet is resizable.
      */
     public boolean isResizable() {
-        return environment.getAppletFrame().isResizable();
+        Container c = environment.getAppletFrame();
+        if (c instanceof Frame)
+            return ((Frame) c).isResizable();
+
+        return false;
     }
 
     /**