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