home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1998 January / PCO0198.ISO / 1&1 / java.z / java_301 / netscape / applet / EmbeddedAppletFrame.class (.txt) < prev    next >
Encoding:
Java Class File  |  1996-10-20  |  11.4 KB  |  387 lines

  1. package netscape.applet;
  2.  
  3. import java.applet.Applet;
  4. import java.applet.AppletContext;
  5. import java.applet.AppletStub;
  6. import java.awt.BorderLayout;
  7. import java.awt.Component;
  8. import java.awt.Container;
  9. import java.awt.Dimension;
  10. import java.awt.Event;
  11. import java.awt.Frame;
  12. import java.awt.Window;
  13. import java.io.PrintStream;
  14. import java.net.URL;
  15. import java.util.Enumeration;
  16. import java.util.Hashtable;
  17.  
  18. class EmbeddedAppletFrame extends Frame implements AppletStub, Runnable {
  19.    int pData;
  20.    Integer appletID;
  21.    MozillaAppletContext context;
  22.    long timestamp;
  23.    boolean inHistory = false;
  24.    URL documentURL;
  25.    URL codebaseURL;
  26.    URL archiveURL;
  27.    Hashtable atts;
  28.    Applet applet;
  29.    int status;
  30.    Thread handler;
  31.    static final int APPLET_DISPOSE = 0;
  32.    static final int APPLET_LOAD = 1;
  33.    static final int APPLET_INIT = 2;
  34.    static final int APPLET_START = 3;
  35.    static final int APPLET_STOP = 4;
  36.    static final int APPLET_DESTROY = 5;
  37.    static final int APPLET_RESIZE = 51234;
  38.    Dimension appletSize = new Dimension(100, 100);
  39.    AppletClassLoader classLoader;
  40.    Event queue;
  41.    static Event shutdownEvents = new Event(new Event(new Event((Object)null, 0, (Object)null), 5, (Object)null), 4, (Object)null);
  42.    String currentStatus = "";
  43.    boolean noisy = true;
  44.    String errorReason;
  45.  
  46.    EmbeddedAppletFrame(URL documentURL, URL codebaseURL, URL archiveURL, Hashtable atts, MozillaAppletContext context, Integer appletID) {
  47.       this.appletID = appletID;
  48.       this.pData = appletID;
  49.       this.context = context;
  50.       this.documentURL = documentURL;
  51.       this.codebaseURL = codebaseURL;
  52.       this.archiveURL = archiveURL;
  53.       this.atts = atts;
  54.       ((Container)this).setLayout(new BorderLayout());
  55.       String att = this.getParameter("width");
  56.       if (att != null) {
  57.          this.appletSize.width = Integer.valueOf(att);
  58.       }
  59.  
  60.       att = this.getParameter("height");
  61.       if (att != null) {
  62.          this.appletSize.height = Integer.valueOf(att);
  63.       }
  64.  
  65.    }
  66.  
  67.    protected void finalize() {
  68.       this.classLoader.releaseClassLoader();
  69.       this.classLoader = null;
  70.    }
  71.  
  72.    void dumpState(PrintStream out, int i) {
  73.       MozillaAppletContext.indent(out, i);
  74.       out.println("EmbeddedAppletFrame id=" + this.pData + " documentURL=" + this.documentURL);
  75.       MozillaAppletContext.indent(out, i);
  76.       out.println("  codebaseURL=" + this.codebaseURL + " status=" + this.statusToString(this.status));
  77.       MozillaAppletContext.indent(out, i);
  78.       out.println("  handler=" + this.handler);
  79.       Enumeration e = this.atts.keys();
  80.  
  81.       while(e.hasMoreElements()) {
  82.          String key = (String)e.nextElement();
  83.          String value = (String)this.atts.get(key);
  84.          MozillaAppletContext.indent(out, i + 1);
  85.          out.println(key + " = " + value);
  86.       }
  87.  
  88.    }
  89.  
  90.    String statusToString(int status) {
  91.       switch (status) {
  92.          case 0:
  93.             return "dispose";
  94.          case 1:
  95.             return "load";
  96.          case 2:
  97.             return "init";
  98.          case 3:
  99.             return "start";
  100.          case 4:
  101.             return "stop";
  102.          case 5:
  103.             return "destroy";
  104.          default:
  105.             return Integer.toString(status, 10);
  106.       }
  107.    }
  108.  
  109.    public Dimension minimumSize() {
  110.       return new Dimension(this.appletSize.width, this.appletSize.height);
  111.    }
  112.  
  113.    public Dimension preferredSize() {
  114.       return this.minimumSize();
  115.    }
  116.  
  117.    void sendEvent(int id) {
  118.       this.sendEvent(new Event((Object)null, id, (Object)null));
  119.    }
  120.  
  121.    synchronized void sendEvent(Event evt) {
  122.       if (this.queue == null) {
  123.          evt.target = this.queue;
  124.          this.queue = evt;
  125.          this.notifyAll();
  126.       } else {
  127.          Event q;
  128.          for(q = this.queue; q.target != null; q = (Event)q.target) {
  129.          }
  130.  
  131.          q.target = evt;
  132.       }
  133.    }
  134.  
  135.    synchronized Event getNextEvent() throws InterruptedException {
  136.       while(this.queue == null) {
  137.          this.wait();
  138.       }
  139.  
  140.       Event evt = this.queue;
  141.       this.queue = (Event)this.queue.target;
  142.       evt.target = this;
  143.       return evt;
  144.    }
  145.  
  146.    public void start() {
  147.       SecurityManager.setScopePermission();
  148.       this.handler = new Thread(new AppletThreadGroup("applet-" + this.atts.get("code"), this), this);
  149.       SecurityManager.resetScopePermission();
  150.       this.handler.start();
  151.    }
  152.  
  153.    public void run() {
  154.       this.classLoader = AppletClassLoader.getClassLoader(this.context, this.codebaseURL, this.archiveURL, this.getParameter("mayscript") != null);
  155.  
  156.       while(true) {
  157.          Event evt;
  158.          try {
  159.             evt = this.getNextEvent();
  160.          } catch (InterruptedException e) {
  161.             this.showAppletException(e, "interrupted, bailing out...");
  162.             return;
  163.          }
  164.  
  165.          try {
  166.             switch (evt.id) {
  167.                case 0:
  168.                   if (this.status == 1) {
  169.                      this.status = 0;
  170.                      ((Container)this).remove(this.applet);
  171.                      this.applet = null;
  172.                      this.rightState("disposed");
  173.                      ((Frame)this).dispose();
  174.                      return;
  175.                   }
  176.  
  177.                   this.wrongState("can't dispose", "applet not destroyed");
  178.                   break;
  179.                case 1:
  180.                   if (this.status != 0) {
  181.                      this.wrongState("can't load", "not disposed");
  182.                   } else {
  183.                      String code = this.getParameter("code");
  184.                      if (code.endsWith(".class")) {
  185.                         code = code.substring(0, code.length() - 6).replace('/', '.');
  186.                      }
  187.  
  188.                      if (code.endsWith(".java")) {
  189.                         code = code.substring(0, code.length() - 5).replace('/', '.');
  190.                      }
  191.  
  192.                      try {
  193.                         this.applet = (Applet)this.classLoader.loadClass(code).newInstance();
  194.                      } catch (VerifyError e) {
  195.                         this.showAppletException(e, "class " + ((Throwable)e).getMessage() + " got a security violation: method verification error");
  196.                         continue;
  197.                      } catch (SecurityException e) {
  198.                         this.showAppletException(e, "class " + code + " got a security violation: " + ((Throwable)e).getMessage());
  199.                         continue;
  200.                      } catch (ClassNotFoundException e) {
  201.                         this.showAppletException(e, "class " + code + " not found");
  202.                         continue;
  203.                      } catch (InstantiationException e) {
  204.                         this.showAppletException(e, "class " + code + " can't be instantiated");
  205.                         continue;
  206.                      } catch (IllegalAccessException e) {
  207.                         this.showAppletException(e, "class " + code + " is not public or has no public constructor");
  208.                         continue;
  209.                      } catch (Exception e) {
  210.                         this.showAppletException(e, "exception: " + ((Throwable)e).toString());
  211.                         continue;
  212.                      } catch (ThreadDeath var11) {
  213.                         this.showAppletStatus("killed");
  214.                         return;
  215.                      } catch (Error e) {
  216.                         this.showAppletException(e, "error: " + ((Throwable)e).toString());
  217.                         continue;
  218.                      }
  219.  
  220.                      this.applet.setStub(this);
  221.                      this.applet.hide();
  222.                      ((Container)this).add("Center", this.applet);
  223.                      this.status = 1;
  224.                      this.rightState("loaded");
  225.                      ((Container)this).validate();
  226.                   }
  227.                   break;
  228.                case 2:
  229.                   if (this.status != 1) {
  230.                      this.wrongState("can't init", "applet not loaded");
  231.                   } else {
  232.                      ((Window)this).pack();
  233.                      ((Window)this).show();
  234.                      this.applet.resize(this.appletSize);
  235.                      this.applet.init();
  236.                      ((Container)this).validate();
  237.                      this.status = 2;
  238.                      this.rightState("initialized");
  239.                   }
  240.                   break;
  241.                case 3:
  242.                   if (this.status != 2) {
  243.                      this.wrongState("can't start", "applet not initialized");
  244.                   } else {
  245.                      this.status = 3;
  246.                      this.timestamp = System.currentTimeMillis();
  247.                      this.applet.resize(this.appletSize);
  248.                      this.applet.start();
  249.                      this.applet.show();
  250.                      ((Container)this).validate();
  251.                      this.context.mochaOnLoad(0);
  252.                      this.rightState("running");
  253.                   }
  254.                   break;
  255.                case 4:
  256.                   if (this.status != 3) {
  257.                      this.wrongState("can't stop", "applet not started");
  258.                   } else {
  259.                      this.status = 2;
  260.                      this.timestamp = System.currentTimeMillis();
  261.                      this.applet.hide();
  262.                      SecurityManager.setScopePermission();
  263.                      this.applet.stop();
  264.                      SecurityManager.resetScopePermission();
  265.                      this.rightState("stopped");
  266.                   }
  267.                   break;
  268.                case 5:
  269.                   if (this.status != 2) {
  270.                      this.wrongState("can't destroy", "applet not stopped");
  271.                   } else {
  272.                      this.status = 1;
  273.                      SecurityManager.setScopePermission();
  274.                      this.applet.destroy();
  275.                      SecurityManager.resetScopePermission();
  276.                      ((Component)this).hide();
  277.                      this.rightState("destroyed");
  278.                   }
  279.             }
  280.          } catch (SecurityException e) {
  281.             this.showAppletException(e, "security violation: " + ((Throwable)e).getMessage());
  282.          } catch (Exception e) {
  283.             this.showAppletException(e, "exception: " + ((Throwable)e).toString());
  284.          } catch (ThreadDeath var15) {
  285.             this.showAppletStatus("killed");
  286.             return;
  287.          } catch (Error e) {
  288.             this.showAppletException(e, "error: " + ((Throwable)e).toString());
  289.          }
  290.       }
  291.    }
  292.  
  293.    public boolean isActive() {
  294.       return this.status == 3;
  295.    }
  296.  
  297.    public String getParameter(String name) {
  298.       return (String)this.atts.get(name.toLowerCase());
  299.    }
  300.  
  301.    public URL getDocumentBase() {
  302.       return this.documentURL;
  303.    }
  304.  
  305.    public URL getCodeBase() {
  306.       return this.codebaseURL;
  307.    }
  308.  
  309.    public AppletContext getAppletContext() {
  310.       return this.context;
  311.    }
  312.  
  313.    public void appletResize(int width, int height) {
  314.       this.appletSize.width = width;
  315.       this.appletSize.height = height;
  316.       ((Component)this).postEvent(new Event(this, 51234, this.preferredSize()));
  317.    }
  318.  
  319.    protected void showAppletStatus(String status) {
  320.       if (this.applet == null) {
  321.          this.currentStatus = "Applet " + status;
  322.       } else {
  323.          String name = this.applet.getParameter("name");
  324.          if (name == null) {
  325.             name = this.applet.getClass().getName().toString();
  326.          }
  327.  
  328.          this.currentStatus = "Applet " + name + " " + status;
  329.       }
  330.  
  331.       this.getAppletContext().showStatus(this.currentStatus);
  332.    }
  333.  
  334.    protected void showAppletLog(String str) {
  335.       if (this.noisy) {
  336.          String longStr;
  337.          if (this.applet == null) {
  338.             longStr = "# Applet log: " + str;
  339.          } else {
  340.             String name = this.applet.getParameter("name");
  341.             if (name == null) {
  342.                name = this.applet.getClass().getName().toString();
  343.             }
  344.  
  345.             longStr = "# Applet " + name + " " + " log: " + str;
  346.          }
  347.  
  348.          System.err.println(longStr);
  349.       }
  350.  
  351.    }
  352.  
  353.    protected void rightState(String message) {
  354.       this.errorReason = null;
  355.       this.showAppletStatus(message);
  356.    }
  357.  
  358.    protected void wrongState(String message, String newReason) {
  359.       this.showAppletStatus(message + ": " + (this.errorReason != null ? this.errorReason : newReason));
  360.    }
  361.  
  362.    protected void showAppletException(Throwable t, String message) {
  363.       if (this.noisy) {
  364.          this.context.mochaOnLoad(-1);
  365.          if (message == null) {
  366.             message = t.toString();
  367.          }
  368.  
  369.          this.errorReason = message;
  370.          System.err.println("# Applet exception: " + message);
  371.          t.printStackTrace();
  372.          this.showAppletStatus(message);
  373.       }
  374.  
  375.    }
  376.  
  377.    public boolean mouseEnter(Event evt, int x, int y) {
  378.       this.getAppletContext().showStatus(this.currentStatus);
  379.       return true;
  380.    }
  381.  
  382.    public boolean mouseExit(Event evt, int x, int y) {
  383.       this.getAppletContext().showStatus("");
  384.       return true;
  385.    }
  386. }
  387.