home *** CD-ROM | disk | FTP | other *** search
- package netscape.applet;
-
- import java.applet.Applet;
- import java.applet.AppletContext;
- import java.applet.AppletStub;
- import java.awt.BorderLayout;
- import java.awt.Component;
- import java.awt.Container;
- import java.awt.Dimension;
- import java.awt.Event;
- import java.awt.Frame;
- import java.awt.Window;
- import java.io.PrintStream;
- import java.net.URL;
- import java.util.Enumeration;
- import java.util.Hashtable;
-
- class EmbeddedAppletFrame extends Frame implements AppletStub, Runnable {
- int pData;
- Integer appletID;
- MozillaAppletContext context;
- long timestamp;
- boolean inHistory = false;
- URL documentURL;
- URL codebaseURL;
- URL archiveURL;
- Hashtable atts;
- Applet applet;
- int status;
- Thread handler;
- static final int APPLET_DISPOSE = 0;
- static final int APPLET_LOAD = 1;
- static final int APPLET_INIT = 2;
- static final int APPLET_START = 3;
- static final int APPLET_STOP = 4;
- static final int APPLET_DESTROY = 5;
- static final int APPLET_RESIZE = 51234;
- Dimension appletSize = new Dimension(100, 100);
- AppletClassLoader classLoader;
- Event queue;
- static Event shutdownEvents = new Event(new Event(new Event((Object)null, 0, (Object)null), 5, (Object)null), 4, (Object)null);
- String currentStatus = "";
- boolean noisy = true;
- String errorReason;
-
- EmbeddedAppletFrame(URL documentURL, URL codebaseURL, URL archiveURL, Hashtable atts, MozillaAppletContext context, Integer appletID) {
- this.appletID = appletID;
- this.pData = appletID;
- this.context = context;
- this.documentURL = documentURL;
- this.codebaseURL = codebaseURL;
- this.archiveURL = archiveURL;
- this.atts = atts;
- ((Container)this).setLayout(new BorderLayout());
- String att = this.getParameter("width");
- if (att != null) {
- this.appletSize.width = Integer.valueOf(att);
- }
-
- att = this.getParameter("height");
- if (att != null) {
- this.appletSize.height = Integer.valueOf(att);
- }
-
- }
-
- protected void finalize() {
- this.classLoader.releaseClassLoader();
- this.classLoader = null;
- }
-
- void dumpState(PrintStream out, int i) {
- MozillaAppletContext.indent(out, i);
- out.println("EmbeddedAppletFrame id=" + this.pData + " documentURL=" + this.documentURL);
- MozillaAppletContext.indent(out, i);
- out.println(" codebaseURL=" + this.codebaseURL + " status=" + this.statusToString(this.status));
- MozillaAppletContext.indent(out, i);
- out.println(" handler=" + this.handler);
- Enumeration e = this.atts.keys();
-
- while(e.hasMoreElements()) {
- String key = (String)e.nextElement();
- String value = (String)this.atts.get(key);
- MozillaAppletContext.indent(out, i + 1);
- out.println(key + " = " + value);
- }
-
- }
-
- String statusToString(int status) {
- switch (status) {
- case 0:
- return "dispose";
- case 1:
- return "load";
- case 2:
- return "init";
- case 3:
- return "start";
- case 4:
- return "stop";
- case 5:
- return "destroy";
- default:
- return Integer.toString(status, 10);
- }
- }
-
- public Dimension minimumSize() {
- return new Dimension(this.appletSize.width, this.appletSize.height);
- }
-
- public Dimension preferredSize() {
- return this.minimumSize();
- }
-
- void sendEvent(int id) {
- this.sendEvent(new Event((Object)null, id, (Object)null));
- }
-
- synchronized void sendEvent(Event evt) {
- if (this.queue == null) {
- evt.target = this.queue;
- this.queue = evt;
- this.notifyAll();
- } else {
- Event q;
- for(q = this.queue; q.target != null; q = (Event)q.target) {
- }
-
- q.target = evt;
- }
- }
-
- synchronized Event getNextEvent() throws InterruptedException {
- while(this.queue == null) {
- this.wait();
- }
-
- Event evt = this.queue;
- this.queue = (Event)this.queue.target;
- evt.target = this;
- return evt;
- }
-
- public void start() {
- SecurityManager.setScopePermission();
- this.handler = new Thread(new AppletThreadGroup("applet-" + this.atts.get("code"), this), this);
- SecurityManager.resetScopePermission();
- this.handler.start();
- }
-
- public void run() {
- this.classLoader = AppletClassLoader.getClassLoader(this.context, this.codebaseURL, this.archiveURL, this.getParameter("mayscript") != null);
-
- while(true) {
- Event evt;
- try {
- evt = this.getNextEvent();
- } catch (InterruptedException e) {
- this.showAppletException(e, "interrupted, bailing out...");
- return;
- }
-
- try {
- switch (evt.id) {
- case 0:
- if (this.status == 1) {
- this.status = 0;
- ((Container)this).remove(this.applet);
- this.applet = null;
- this.rightState("disposed");
- ((Frame)this).dispose();
- return;
- }
-
- this.wrongState("can't dispose", "applet not destroyed");
- break;
- case 1:
- if (this.status != 0) {
- this.wrongState("can't load", "not disposed");
- } else {
- String code = this.getParameter("code");
- if (code.endsWith(".class")) {
- code = code.substring(0, code.length() - 6).replace('/', '.');
- }
-
- if (code.endsWith(".java")) {
- code = code.substring(0, code.length() - 5).replace('/', '.');
- }
-
- try {
- this.applet = (Applet)this.classLoader.loadClass(code).newInstance();
- } catch (VerifyError e) {
- this.showAppletException(e, "class " + ((Throwable)e).getMessage() + " got a security violation: method verification error");
- continue;
- } catch (SecurityException e) {
- this.showAppletException(e, "class " + code + " got a security violation: " + ((Throwable)e).getMessage());
- continue;
- } catch (ClassNotFoundException e) {
- this.showAppletException(e, "class " + code + " not found");
- continue;
- } catch (InstantiationException e) {
- this.showAppletException(e, "class " + code + " can't be instantiated");
- continue;
- } catch (IllegalAccessException e) {
- this.showAppletException(e, "class " + code + " is not public or has no public constructor");
- continue;
- } catch (Exception e) {
- this.showAppletException(e, "exception: " + ((Throwable)e).toString());
- continue;
- } catch (ThreadDeath var11) {
- this.showAppletStatus("killed");
- return;
- } catch (Error e) {
- this.showAppletException(e, "error: " + ((Throwable)e).toString());
- continue;
- }
-
- this.applet.setStub(this);
- this.applet.hide();
- ((Container)this).add("Center", this.applet);
- this.status = 1;
- this.rightState("loaded");
- ((Container)this).validate();
- }
- break;
- case 2:
- if (this.status != 1) {
- this.wrongState("can't init", "applet not loaded");
- } else {
- ((Window)this).pack();
- ((Window)this).show();
- this.applet.resize(this.appletSize);
- this.applet.init();
- ((Container)this).validate();
- this.status = 2;
- this.rightState("initialized");
- }
- break;
- case 3:
- if (this.status != 2) {
- this.wrongState("can't start", "applet not initialized");
- } else {
- this.status = 3;
- this.timestamp = System.currentTimeMillis();
- this.applet.resize(this.appletSize);
- this.applet.start();
- this.applet.show();
- ((Container)this).validate();
- this.context.mochaOnLoad(0);
- this.rightState("running");
- }
- break;
- case 4:
- if (this.status != 3) {
- this.wrongState("can't stop", "applet not started");
- } else {
- this.status = 2;
- this.timestamp = System.currentTimeMillis();
- this.applet.hide();
- SecurityManager.setScopePermission();
- this.applet.stop();
- SecurityManager.resetScopePermission();
- this.rightState("stopped");
- }
- break;
- case 5:
- if (this.status != 2) {
- this.wrongState("can't destroy", "applet not stopped");
- } else {
- this.status = 1;
- SecurityManager.setScopePermission();
- this.applet.destroy();
- SecurityManager.resetScopePermission();
- ((Component)this).hide();
- this.rightState("destroyed");
- }
- }
- } catch (SecurityException e) {
- this.showAppletException(e, "security violation: " + ((Throwable)e).getMessage());
- } catch (Exception e) {
- this.showAppletException(e, "exception: " + ((Throwable)e).toString());
- } catch (ThreadDeath var15) {
- this.showAppletStatus("killed");
- return;
- } catch (Error e) {
- this.showAppletException(e, "error: " + ((Throwable)e).toString());
- }
- }
- }
-
- public boolean isActive() {
- return this.status == 3;
- }
-
- public String getParameter(String name) {
- return (String)this.atts.get(name.toLowerCase());
- }
-
- public URL getDocumentBase() {
- return this.documentURL;
- }
-
- public URL getCodeBase() {
- return this.codebaseURL;
- }
-
- public AppletContext getAppletContext() {
- return this.context;
- }
-
- public void appletResize(int width, int height) {
- this.appletSize.width = width;
- this.appletSize.height = height;
- ((Component)this).postEvent(new Event(this, 51234, this.preferredSize()));
- }
-
- protected void showAppletStatus(String status) {
- if (this.applet == null) {
- this.currentStatus = "Applet " + status;
- } else {
- String name = this.applet.getParameter("name");
- if (name == null) {
- name = this.applet.getClass().getName().toString();
- }
-
- this.currentStatus = "Applet " + name + " " + status;
- }
-
- this.getAppletContext().showStatus(this.currentStatus);
- }
-
- protected void showAppletLog(String str) {
- if (this.noisy) {
- String longStr;
- if (this.applet == null) {
- longStr = "# Applet log: " + str;
- } else {
- String name = this.applet.getParameter("name");
- if (name == null) {
- name = this.applet.getClass().getName().toString();
- }
-
- longStr = "# Applet " + name + " " + " log: " + str;
- }
-
- System.err.println(longStr);
- }
-
- }
-
- protected void rightState(String message) {
- this.errorReason = null;
- this.showAppletStatus(message);
- }
-
- protected void wrongState(String message, String newReason) {
- this.showAppletStatus(message + ": " + (this.errorReason != null ? this.errorReason : newReason));
- }
-
- protected void showAppletException(Throwable t, String message) {
- if (this.noisy) {
- this.context.mochaOnLoad(-1);
- if (message == null) {
- message = t.toString();
- }
-
- this.errorReason = message;
- System.err.println("# Applet exception: " + message);
- t.printStackTrace();
- this.showAppletStatus(message);
- }
-
- }
-
- public boolean mouseEnter(Event evt, int x, int y) {
- this.getAppletContext().showStatus(this.currentStatus);
- return true;
- }
-
- public boolean mouseExit(Event evt, int x, int y) {
- this.getAppletContext().showStatus("");
- return true;
- }
- }
-