home *** CD-ROM | disk | FTP | other *** search
/ Print Shop Ensemble 3 / the-print-shop-ensemble-iii.iso / worldnet / disk2 / java.z / MOZ2_01.ZIP / netscape / applet / MozillaAppletContext.class (.txt) < prev    next >
Encoding:
Java Class File  |  1996-03-08  |  14.9 KB  |  626 lines

  1. package netscape.applet;
  2.  
  3. import java.applet.Applet;
  4. import java.applet.AppletContext;
  5. import java.applet.AudioClip;
  6. import java.awt.Image;
  7. import java.awt.Toolkit;
  8. import java.awt.Window;
  9. import java.io.PrintStream;
  10. import java.net.MalformedURLException;
  11. import java.net.URL;
  12. import java.util.Enumeration;
  13. import java.util.Hashtable;
  14. import java.util.Vector;
  15. import netscape.net.URLStreamHandlerFactory;
  16. import sun.awt.image.URLImageSource;
  17.  
  18. class MozillaAppletContext implements AppletContext {
  19.    int frameMWContext;
  20.    HistoryElement history;
  21.    Hashtable appletFrames = new Hashtable();
  22.    Hashtable imageCache = new Hashtable();
  23.    Hashtable audioClipCache = new Hashtable();
  24.    Hashtable classLoaders = new Hashtable();
  25.    URL documentURL;
  26.    static int debug;
  27.    static Console console;
  28.    static int totalApplets;
  29.    static final int MAX_APPLETS = 10;
  30.    static final int APPLETS_PER_WINDOW = 5;
  31.  
  32.    void dumpState(PrintStream out, int i) {
  33.       MozillaWindow.indent(out, i);
  34.       out.println("MozillaAppletContext #frames=" + this.appletFrames.size() + " #images=" + this.imageCache.size() + " #audioClips=" + this.audioClipCache.size() + " #classLoaders=" + this.classLoaders.size() + " url=" + this.documentURL);
  35.       Enumeration e = this.appletFrames.elements();
  36.  
  37.       while(e.hasMoreElements()) {
  38.          EmbeddedAppletFrame eaf = (EmbeddedAppletFrame)e.nextElement();
  39.          eaf.dumpState(out, i + 1);
  40.       }
  41.  
  42.       if (this.imageCache.size() > 0) {
  43.          MozillaWindow.indent(out, i + 1);
  44.          out.println("image cache:");
  45.          Enumeration e = this.imageCache.keys();
  46.  
  47.          while(e.hasMoreElements()) {
  48.             URL u = (URL)e.nextElement();
  49.             MozillaWindow.indent(out, i + 2);
  50.             out.println(u.toString());
  51.          }
  52.       }
  53.  
  54.       if (this.audioClipCache.size() > 0) {
  55.          MozillaWindow.indent(out, i + 1);
  56.          out.println("audio clip cache:");
  57.          Enumeration e = this.audioClipCache.keys();
  58.  
  59.          while(e.hasMoreElements()) {
  60.             URL u = (URL)e.nextElement();
  61.             MozillaWindow.indent(out, i + 2);
  62.             out.println(u.toString());
  63.          }
  64.       }
  65.  
  66.       if (this.classLoaders.size() > 0) {
  67.          MozillaWindow.indent(out, i + 1);
  68.          out.println("class loader cache:");
  69.          Enumeration e = this.classLoaders.keys();
  70.  
  71.          while(e.hasMoreElements()) {
  72.             URL u = (URL)e.nextElement();
  73.             MozillaWindow.indent(out, i + 2);
  74.             out.println(u.toString());
  75.          }
  76.       }
  77.  
  78.    }
  79.  
  80.    void destroy() {
  81.       this.frameMWContext = 0;
  82.       this.appletFrames = null;
  83.       this.imageCache = null;
  84.       this.audioClipCache = null;
  85.    }
  86.  
  87.    void trim() {
  88.       this.destroyApplets();
  89.       this.destroy();
  90.    }
  91.  
  92.    Class loadClass(URL codebase, String code) throws ClassNotFoundException {
  93.       AppletClassLoader loader = lookupClassLoader(this, codebase);
  94.       return loader.loadClass(code);
  95.    }
  96.  
  97.    public AudioClip getAudioClip(URL url) {
  98.       AudioClip clip = lookupAudioClip(this, url);
  99.       return clip;
  100.    }
  101.  
  102.    public Image getImage(URL url) {
  103.       Image img = lookupImage(this, url);
  104.       return img;
  105.    }
  106.  
  107.    public Applet getApplet(String name) {
  108.       name = name.toLowerCase();
  109.       Enumeration e = this.appletFrames.elements();
  110.  
  111.       while(e.hasMoreElements()) {
  112.          EmbeddedAppletFrame p = (EmbeddedAppletFrame)e.nextElement();
  113.          if (name.equals(p.applet.getParameter("name"))) {
  114.             return p.applet;
  115.          }
  116.       }
  117.  
  118.       return null;
  119.    }
  120.  
  121.    public Enumeration getApplets() {
  122.       Vector v = new Vector();
  123.       Enumeration e = this.appletFrames.elements();
  124.  
  125.       while(e.hasMoreElements()) {
  126.          EmbeddedAppletFrame p = (EmbeddedAppletFrame)e.nextElement();
  127.          v.addElement(p.applet);
  128.       }
  129.  
  130.       return v.elements();
  131.    }
  132.  
  133.    public void showDocument(URL url) {
  134.       this.pShowDocument(url.toExternalForm(), url.getHost(), "_top");
  135.    }
  136.  
  137.    public void showDocument(URL url, String target) {
  138.       this.pShowDocument(url.toExternalForm(), url.getHost(), target);
  139.    }
  140.  
  141.    public void showStatus(String status) {
  142.       this.pShowStatus(status);
  143.    }
  144.  
  145.    private native void pShowDocument(String var1, String var2, String var3);
  146.  
  147.    private native void pShowStatus(String var1);
  148.  
  149.    static synchronized AppletClassLoader lookupClassLoader(MozillaAppletContext incx, URL url) {
  150.       Enumeration e = HistoryElement.elements();
  151.  
  152.       while(e.hasMoreElements()) {
  153.          HistoryElement h = (HistoryElement)e.nextElement();
  154.          MozillaAppletContext acx = h.appletContext;
  155.          if (acx != null) {
  156.             Object loader = acx.classLoaders.get(url);
  157.             if (loader != null) {
  158.                return (AppletClassLoader)loader;
  159.             }
  160.          }
  161.       }
  162.  
  163.       AppletClassLoader loader = new AppletClassLoader(incx, url);
  164.       incx.classLoaders.put(url, loader);
  165.       if (debug > 0) {
  166.          System.err.println("# New class loader: " + url);
  167.       }
  168.  
  169.       return loader;
  170.    }
  171.  
  172.    static synchronized AudioClip lookupAudioClip(MozillaAppletContext incx, URL url) {
  173.       Enumeration e = HistoryElement.elements();
  174.  
  175.       while(e.hasMoreElements()) {
  176.          HistoryElement h = (HistoryElement)e.nextElement();
  177.          MozillaAppletContext acx = h.appletContext;
  178.          if (acx != null) {
  179.             Object clip = acx.audioClipCache.get(url);
  180.             if (clip != null) {
  181.                return (AudioClip)clip;
  182.             }
  183.          }
  184.       }
  185.  
  186.       AudioClip clip = new AppletAudioClip(url);
  187.       incx.audioClipCache.put(url, clip);
  188.       if (debug > 0) {
  189.          System.err.println("# New audio clip: " + url);
  190.       }
  191.  
  192.       return clip;
  193.    }
  194.  
  195.    static synchronized Image lookupImage(MozillaAppletContext incx, URL url) {
  196.       Enumeration e = HistoryElement.elements();
  197.  
  198.       while(e.hasMoreElements()) {
  199.          HistoryElement h = (HistoryElement)e.nextElement();
  200.          MozillaAppletContext acx = h.appletContext;
  201.          if (acx != null) {
  202.             Object image = acx.imageCache.get(url);
  203.             if (image != null) {
  204.                return (Image)image;
  205.             }
  206.          }
  207.       }
  208.  
  209.       Image image;
  210.       try {
  211.          URLImageSource source = new URLImageSource(url);
  212.          image = Toolkit.getDefaultToolkit().createImage(source);
  213.          incx.imageCache.put(url, image);
  214.          if (debug > 0) {
  215.             System.err.println("# New image: " + url);
  216.          }
  217.       } catch (Exception var6) {
  218.          image = null;
  219.       }
  220.  
  221.       return image;
  222.    }
  223.  
  224.    synchronized void initApplet(int pLJAppletData, String[] argv) {
  225.       Hashtable params = new Hashtable();
  226.  
  227.       for(int i = 1; i < argv.length; ++i) {
  228.          String str = argv[i];
  229.          int j = str.indexOf(61);
  230.          if (j >= 0) {
  231.             params.put(str.substring(0, j).toLowerCase(), str.substring(j + 1));
  232.          }
  233.       }
  234.  
  235.       String dbg = (String)params.get("debug");
  236.  
  237.       try {
  238.          debug = Integer.parseInt(dbg);
  239.       } catch (Exception var15) {
  240.       }
  241.  
  242.       try {
  243.          this.documentURL = new URL(argv[0]);
  244.       } catch (MalformedURLException var16) {
  245.          if (debug > 0) {
  246.             System.err.println("# Malformed documentURL: " + argv[0]);
  247.          }
  248.  
  249.          return;
  250.       }
  251.  
  252.       String codebase = (String)params.get("codebase");
  253.       if (codebase == null) {
  254.          codebase = argv[0];
  255.          int tail = codebase.lastIndexOf(47) + 1;
  256.          codebase = codebase.substring(0, tail);
  257.       } else {
  258.          if (!codebase.endsWith("/")) {
  259.             codebase = codebase + "/";
  260.          }
  261.  
  262.          try {
  263.             URL u = new URL(this.documentURL, codebase);
  264.             codebase = u.toString();
  265.             int tail = codebase.lastIndexOf(47) + 1;
  266.             if (tail != codebase.length() - 1) {
  267.                codebase = codebase.substring(0, tail);
  268.             }
  269.          } catch (MalformedURLException var14) {
  270.          }
  271.       }
  272.  
  273.       params.put("codebase", codebase);
  274.       URL codebaseURL = this.documentURL;
  275.  
  276.       try {
  277.          codebaseURL = new URL(codebase);
  278.       } catch (MalformedURLException var13) {
  279.       }
  280.  
  281.       Integer appKey = new Integer(pLJAppletData);
  282.       EmbeddedAppletFrame frame = new EmbeddedAppletFrame(this.documentURL, codebaseURL, params, this, pLJAppletData);
  283.       this.appletFrames.put(appKey, frame);
  284.       ++totalApplets;
  285.       if (debug > 0) {
  286.          System.err.println("# Total applets " + totalApplets);
  287.       }
  288.  
  289.       if (debug > 0) {
  290.          String p = "";
  291.  
  292.          Object key;
  293.          for(Enumeration e = params.keys(); e.hasMoreElements(); p = p + (String)key + "=" + (String)params.get(key) + " ") {
  294.             key = e.nextElement();
  295.          }
  296.  
  297.          System.err.println("# New applet: " + pLJAppletData + " at " + codebaseURL + " " + p);
  298.       }
  299.  
  300.       frame.start();
  301.       ((Window)frame).pack();
  302.       ((Window)frame).show();
  303.       frame.sendEvent(1);
  304.       frame.sendEvent(2);
  305.    }
  306.  
  307.    synchronized void startApplet(int pLJAppletData) {
  308.       if (debug > 0) {
  309.          System.err.println("# startApplet: appletID=" + pLJAppletData);
  310.       }
  311.  
  312.       Integer appKey = new Integer(pLJAppletData);
  313.       EmbeddedAppletFrame frame = (EmbeddedAppletFrame)this.appletFrames.get(appKey);
  314.       if (frame != null) {
  315.          frame.sendEvent(3);
  316.          this.history.start();
  317.       } else {
  318.          if (debug > 0) {
  319.             System.err.println("# Warning: startApplet: appletID " + pLJAppletData + " not found!");
  320.          }
  321.  
  322.       }
  323.    }
  324.  
  325.    synchronized void stopApplet(int pLJAppletData) {
  326.       if (debug > 0) {
  327.          System.err.println("# stopApplet: appletID=" + pLJAppletData);
  328.       }
  329.  
  330.       Integer appKey = new Integer(pLJAppletData);
  331.       EmbeddedAppletFrame frame = (EmbeddedAppletFrame)this.appletFrames.get(appKey);
  332.       if (frame != null) {
  333.          frame.sendEvent(4);
  334.          this.history.stop();
  335.       } else {
  336.          if (debug > 0) {
  337.             System.err.println("# Warning: stopApplet: appletID " + pLJAppletData + " not found!");
  338.          }
  339.  
  340.       }
  341.    }
  342.  
  343.    synchronized void destroyApplet(int pLJAppletData) {
  344.       if (debug > 0) {
  345.          System.err.println("# destroyApplet: appletID=" + pLJAppletData);
  346.       }
  347.  
  348.       Integer appKey = new Integer(pLJAppletData);
  349.       EmbeddedAppletFrame frame = (EmbeddedAppletFrame)this.appletFrames.get(appKey);
  350.       if (frame == null) {
  351.          if (debug > 0) {
  352.             System.err.println("# Warning: destroyApplet: appletID " + pLJAppletData + " not found!");
  353.          }
  354.  
  355.       } else {
  356.          --totalApplets;
  357.          if (debug > 0) {
  358.             System.err.println("# Total applets " + totalApplets);
  359.          }
  360.  
  361.          this.appletFrames.remove(appKey);
  362.          ThreadGroup group = frame.handler.getThreadGroup();
  363.          synchronized(group){}
  364.  
  365.          try {
  366.             frame.sendEvent(4);
  367.             frame.sendEvent(5);
  368.             frame.sendEvent(0);
  369.  
  370.             try {
  371.                group.wait(1000L);
  372.             } catch (InterruptedException var12) {
  373.             }
  374.  
  375.             if (group.activeCount() > 0) {
  376.                group.stop();
  377.  
  378.                try {
  379.                   group.wait(1000L);
  380.                } catch (InterruptedException var11) {
  381.                }
  382.             }
  383.  
  384.             try {
  385.                group.destroy();
  386.             } catch (Exception var10) {
  387.             }
  388.          } catch (Throwable var13) {
  389.             throw var13;
  390.          }
  391.  
  392.       }
  393.    }
  394.  
  395.    synchronized void iconifyApplets() {
  396.       if (debug > 0) {
  397.          System.err.println("# iconifyApplets");
  398.       }
  399.  
  400.       Enumeration frames = this.appletFrames.elements();
  401.  
  402.       while(frames.hasMoreElements()) {
  403.          EmbeddedAppletFrame frame = (EmbeddedAppletFrame)frames.nextElement();
  404.          frame.sendEvent(4);
  405.          this.history.stop();
  406.          if (debug > 0) {
  407.             System.err.println("# iconifyApplet: stopping appletID " + frame.pData);
  408.          }
  409.       }
  410.  
  411.    }
  412.  
  413.    synchronized void uniconifyApplets() {
  414.       if (debug > 0) {
  415.          System.err.println("# uniconifyApplets");
  416.       }
  417.  
  418.       Enumeration frames = this.appletFrames.elements();
  419.  
  420.       while(frames.hasMoreElements()) {
  421.          EmbeddedAppletFrame frame = (EmbeddedAppletFrame)frames.nextElement();
  422.          frame.sendEvent(3);
  423.          this.history.start();
  424.          if (debug > 0) {
  425.             System.err.println("# uniconifyApplet: starting appletID " + frame.pData);
  426.          }
  427.       }
  428.  
  429.    }
  430.  
  431.    synchronized void destroyApplets() {
  432.       if (debug > 0) {
  433.          System.err.println("# destroyApplets");
  434.       }
  435.  
  436.       Enumeration e = this.appletFrames.keys();
  437.  
  438.       while(e.hasMoreElements()) {
  439.          Integer frame = (Integer)e.nextElement();
  440.          this.destroyApplet(frame);
  441.       }
  442.  
  443.    }
  444.  
  445.    static void init() {
  446.       console = new Console();
  447.       System.setProperties(new AppletProperties());
  448.       URL.setURLStreamHandlerFactory(new URLStreamHandlerFactory());
  449.       System.setSecurityManager(new AppletSecurity());
  450.    }
  451.  
  452.    static void setConsoleState(int newstate) {
  453.       setConsoleState0(newstate);
  454.    }
  455.  
  456.    static native void setConsoleState0(int var0);
  457.  
  458.    static void showConsole() {
  459.       console.show();
  460.    }
  461.  
  462.    static void hideConsole() {
  463.       console.hide();
  464.    }
  465.  
  466.    static void trimOneFrame() {
  467.       int numWindows = MozillaWindow.numWindows();
  468.       int maxApplets = Math.max(numWindows * 5, 10);
  469.       if (HistoryElement.hiddenElements != 0 && totalApplets > maxApplets) {
  470.          HistoryElement.trim();
  471.       }
  472.  
  473.    }
  474.  
  475.    static void initApplet(int parentContext, int frameContext, int historyID, int pLJAppletData, String[] argv) {
  476.       if (debug > 0) {
  477.          System.err.println("# initApplet: historyID=" + historyID + " appletID=" + pLJAppletData + " parentContext=" + parentContext + "frameContext=" + frameContext);
  478.       }
  479.  
  480.       trimOneFrame();
  481.       MozillaWindow win = MozillaWindow.lookupOrCreate(parentContext);
  482.       HistoryElement h = HistoryElement.lookup(historyID);
  483.       MozillaAppletContext acx;
  484.       if (h == null) {
  485.          MozillaFrame frame = win.lookupFrame(frameContext);
  486.          if (frame == null) {
  487.             frame = new MozillaFrame(win, frameContext);
  488.          }
  489.  
  490.          acx = new MozillaAppletContext();
  491.          h = new HistoryElement(historyID, frame, acx);
  492.       } else if (h.appletContext == null) {
  493.          acx = new MozillaAppletContext();
  494.          h.resurrect(acx);
  495.       } else {
  496.          acx = h.appletContext;
  497.       }
  498.  
  499.       acx.frameMWContext = frameContext;
  500.       acx.history = h;
  501.       acx.initApplet(pLJAppletData, argv);
  502.    }
  503.  
  504.    static void startApplet(int historyID, int pLJAppletData, int newFrameMWContext) {
  505.       if (debug > 0) {
  506.          System.err.println("# startApplet: historyID=" + historyID + " appletID=" + pLJAppletData);
  507.       }
  508.  
  509.       HistoryElement h = HistoryElement.lookup(historyID);
  510.       if (h != null) {
  511.          MozillaAppletContext acx = h.appletContext;
  512.          if (acx != null) {
  513.             acx.frameMWContext = newFrameMWContext;
  514.             h.frame.setMWContext(newFrameMWContext);
  515.             acx.startApplet(pLJAppletData);
  516.             return;
  517.          }
  518.       } else if (debug > 0) {
  519.          System.err.println("# Warning: startApplet: historyID " + historyID + " not found!");
  520.       }
  521.  
  522.    }
  523.  
  524.    static void stopApplet(int historyID, int pLJAppletData) {
  525.       if (debug > 0) {
  526.          System.err.println("# stopApplet: historyID=" + historyID + " appletID=" + pLJAppletData);
  527.       }
  528.  
  529.       HistoryElement h = HistoryElement.lookup(historyID);
  530.       if (h != null) {
  531.          MozillaAppletContext acx = h.appletContext;
  532.          if (acx != null) {
  533.             acx.stopApplet(pLJAppletData);
  534.             acx.frameMWContext = 0;
  535.             h.frame.setMWContext(0);
  536.             return;
  537.          }
  538.       } else if (debug > 0) {
  539.          System.err.println("# Warning: stopApplet: historyID " + historyID + " not found!");
  540.       }
  541.  
  542.    }
  543.  
  544.    static void destroyApplet(int historyID, int pLJAppletData) {
  545.       if (debug > 0) {
  546.          System.err.println("# destroyApplet: historyID=" + historyID + " appletID=" + pLJAppletData);
  547.       }
  548.  
  549.       HistoryElement h = HistoryElement.lookup(historyID);
  550.       if (h != null) {
  551.          MozillaAppletContext acx = h.appletContext;
  552.          if (acx != null) {
  553.             acx.destroyApplet(pLJAppletData);
  554.             if (acx.appletFrames.isEmpty()) {
  555.                if (debug > 0) {
  556.                   System.err.println("# destroy: historyID=" + historyID);
  557.                }
  558.  
  559.                acx.destroy();
  560.                h.destroy();
  561.                return;
  562.             }
  563.          } else {
  564.             h.destroy();
  565.          }
  566.       }
  567.  
  568.    }
  569.  
  570.    static void iconifyApplets(int historyID) {
  571.       if (debug > 0) {
  572.          System.err.println("# iconifyApplets: historyID=" + historyID);
  573.       }
  574.  
  575.       HistoryElement h = HistoryElement.lookup(historyID);
  576.       if (h != null) {
  577.          MozillaAppletContext acx = h.appletContext;
  578.          if (acx != null) {
  579.             acx.iconifyApplets();
  580.          }
  581.       }
  582.  
  583.    }
  584.  
  585.    static void uniconifyApplets(int historyID) {
  586.       if (debug > 0) {
  587.          System.err.println("# uniconifyApplets: historyID=" + historyID);
  588.       }
  589.  
  590.       HistoryElement h = HistoryElement.lookup(historyID);
  591.       if (h != null) {
  592.          MozillaAppletContext acx = h.appletContext;
  593.          if (acx != null) {
  594.             acx.uniconifyApplets();
  595.          }
  596.       }
  597.  
  598.    }
  599.  
  600.    static void destroyApplets(int historyID) {
  601.       if (debug > 0) {
  602.          System.err.println("# destroyApplets: historyID=" + historyID);
  603.       }
  604.  
  605.       HistoryElement h = HistoryElement.lookup(historyID);
  606.       if (h != null) {
  607.          MozillaAppletContext acx = h.appletContext;
  608.          acx.destroyApplets();
  609.          if (debug > 0) {
  610.             System.err.println("# destroyApplets: destroying historyID " + historyID);
  611.          }
  612.  
  613.          acx.destroy();
  614.       }
  615.  
  616.    }
  617.  
  618.    static void destroyAll() {
  619.       if (debug > 0) {
  620.          System.err.println("# destroyAll");
  621.       }
  622.  
  623.       HistoryElement.destroyAll();
  624.    }
  625. }
  626.