home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1997 August / PCO0897.ISO / browser / tonline / ie32.exe / IEXPLORE.CAB / TCLASSES.ZIP / sun / applet / AppletPanel.class (.txt) < prev    next >
Encoding:
Java Class File  |  1996-10-01  |  9.2 KB  |  377 lines

  1. package sun.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.Panel;
  12. import java.io.IOException;
  13. import java.net.URL;
  14. import java.util.Hashtable;
  15. import sun.misc.MessageUtils;
  16.  
  17. public abstract class AppletPanel extends Panel implements AppletStub, Runnable {
  18.    static String propName = "appletloader";
  19.    Applet applet;
  20.    AppletClassLoader loader;
  21.    protected int status;
  22.    Thread handler;
  23.    public static final int APPLET_DISPOSE = 0;
  24.    public static final int APPLET_LOAD = 1;
  25.    public static final int APPLET_INIT = 2;
  26.    public static final int APPLET_START = 3;
  27.    public static final int APPLET_STOP = 4;
  28.    public static final int APPLET_DESTROY = 5;
  29.    public static final int APPLET_QUIT = 6;
  30.    public static final int APPLET_ERROR = 7;
  31.    public static final int APPLET_RESIZE = 51234;
  32.    public static final int APPLET_LOADING = 51235;
  33.    public static final int APPLET_LOADING_COMPLETED = 51236;
  34.    Dimension appletSize = new Dimension(100, 100);
  35.    // $FF: renamed from: mu sun.misc.MessageUtils
  36.    MessageUtils field_0 = new MessageUtils();
  37.    Thread loaderThread;
  38.    boolean loadAbortRequest = false;
  39.    Event queue;
  40.    private static Hashtable classloaders = new Hashtable();
  41.  
  42.    public void init() {
  43.       ((Container)this).setLayout(new BorderLayout());
  44.       String var1 = this.getParameter("width");
  45.       if (var1 != null) {
  46.          this.appletSize.width = Integer.valueOf(var1);
  47.       }
  48.  
  49.       var1 = this.getParameter("height");
  50.       if (var1 != null) {
  51.          this.appletSize.height = Integer.valueOf(var1);
  52.       }
  53.  
  54.       String var2 = "applet-" + this.getParameter("code");
  55.       this.handler = new Thread(new AppletThreadGroup("group " + var2), this, "thread " + var2);
  56.       this.handler.start();
  57.    }
  58.  
  59.    public Dimension minimumSize() {
  60.       return new Dimension(this.appletSize.width, this.appletSize.height);
  61.    }
  62.  
  63.    public Dimension preferredSize() {
  64.       return this.minimumSize();
  65.    }
  66.  
  67.    public void sendEvent(int var1) {
  68.       this.sendEvent(new Event((Object)null, var1, (Object)null));
  69.    }
  70.  
  71.    protected synchronized void sendEvent(Event var1) {
  72.       if (this.queue == null) {
  73.          var1.target = this.queue;
  74.          this.queue = var1;
  75.          this.notifyAll();
  76.       } else {
  77.          Event var2;
  78.          for(var2 = this.queue; var2.target != null; var2 = (Event)var2.target) {
  79.          }
  80.  
  81.          var2.target = var1;
  82.       }
  83.    }
  84.  
  85.    synchronized Event getNextEvent() throws InterruptedException {
  86.       while(this.queue == null) {
  87.          this.wait();
  88.       }
  89.  
  90.       Event var1 = this.queue;
  91.       this.queue = (Event)this.queue.target;
  92.       var1.target = this;
  93.       return var1;
  94.    }
  95.  
  96.    public void run() {
  97.       Thread var1 = Thread.currentThread();
  98.       if (var1 == this.loaderThread) {
  99.          this.runLoader();
  100.       } else {
  101.          int var2 = var1.getPriority();
  102.          var1.setPriority(var2 + 1);
  103.  
  104.          while(true) {
  105.             Event var3;
  106.             try {
  107.                var3 = this.getNextEvent();
  108.             } catch (InterruptedException var5) {
  109.                this.showAppletStatus(propName + ".bail");
  110.                return;
  111.             }
  112.  
  113.             try {
  114.                switch (var3.id) {
  115.                   case 0:
  116.                      if (this.status != 1) {
  117.                         this.showAppletStatus(propName + ".notdestroyed");
  118.                      } else {
  119.                         this.status = 0;
  120.                         ((Container)this).remove(this.applet);
  121.                         this.showAppletStatus(propName + ".disposed");
  122.                      }
  123.                      break;
  124.                   case 1:
  125.                      if (this.okToLoad() && this.loaderThread == null) {
  126.                         this.setLoaderThread(new Thread(this));
  127.                         this.loaderThread.setPriority(var2 + 1);
  128.                         this.loaderThread.start();
  129.                         this.loaderThread.join();
  130.                         this.setLoaderThread((Thread)null);
  131.                      }
  132.                      break;
  133.                   case 2:
  134.                      if (this.status != 1) {
  135.                         this.showAppletStatus(propName + ".notloaded");
  136.                      } else {
  137.                         this.applet.resize(this.appletSize);
  138.                         this.applet.init();
  139.                         ((Container)this).validate();
  140.                         this.status = 2;
  141.                         this.showAppletStatus(propName + ".inited");
  142.                      }
  143.                      break;
  144.                   case 3:
  145.                      if (this.status != 2) {
  146.                         this.showAppletStatus(propName + ".notinited");
  147.                      } else {
  148.                         this.applet.resize(this.appletSize);
  149.                         this.applet.start();
  150.                         ((Container)this).validate();
  151.                         this.applet.show();
  152.                         this.status = 3;
  153.                         this.showAppletStatus(propName + ".started");
  154.                      }
  155.                      break;
  156.                   case 4:
  157.                      if (this.status != 3) {
  158.                         this.showAppletStatus(propName + ".notstarted");
  159.                      } else {
  160.                         this.status = 2;
  161.                         this.applet.hide();
  162.                         this.applet.stop();
  163.                         this.showAppletStatus(propName + ".stopped");
  164.                      }
  165.                      break;
  166.                   case 5:
  167.                      if (this.status != 2) {
  168.                         this.showAppletStatus(propName + ".notstopped");
  169.                      } else {
  170.                         this.status = 1;
  171.                         this.applet.destroy();
  172.                         this.showAppletStatus(propName + ".destroyed");
  173.                      }
  174.                      break;
  175.                   case 6:
  176.                      return;
  177.                }
  178.             } catch (Exception var6) {
  179.                this.showAppletStatus(MessageUtils.substProp(propName + ".exception2", var6.getClass().getName(), ((Throwable)var6).getMessage()));
  180.                this.showAppletException(var6);
  181.             } catch (ThreadDeath var7) {
  182.                this.showAppletStatus(propName + ".death");
  183.                return;
  184.             } catch (Error var8) {
  185.                this.showAppletStatus(MessageUtils.substProp(propName + ".error2", var8.getClass().getName(), ((Throwable)var8).getMessage()));
  186.                this.showAppletException(var8);
  187.             }
  188.  
  189.             this.clearLoadAbortRequest();
  190.          }
  191.       }
  192.    }
  193.  
  194.    private void runLoader() {
  195.       if (this.status != 0) {
  196.          this.showAppletStatus(propName + ".notdisposed");
  197.       } else {
  198.          ((Component)this).postEvent(new Event(this, 51235, (Object)null));
  199.          this.status = 1;
  200.  
  201.          try {
  202.             this.loader = getClassLoader(this.getCodeBase());
  203.             if (Thread.interrupted()) {
  204.                this.status = 0;
  205.                ((Component)this).postEvent(new Event(this, 51236, (Object)null));
  206.                return;
  207.             }
  208.          } catch (IOException var21) {
  209.             this.status = 7;
  210.             this.showAppletStatus(((Throwable)var21).getMessage());
  211.             this.showAppletLog(((Throwable)var21).getMessage());
  212.             this.showAppletException(var21);
  213.             ((Component)this).postEvent(new Event(this, 51236, (Object)null));
  214.             return;
  215.          }
  216.  
  217.          String var1 = this.getParameter("code");
  218.          if (var1 == null) {
  219.             String var2 = propName + ".nocode";
  220.             this.status = 7;
  221.             this.showAppletStatus(var2);
  222.             this.showAppletLog(var2);
  223.             ((Component)this).repaint();
  224.          } else if (var1.endsWith(".class")) {
  225.             var1 = var1.substring(0, var1.length() - 6).replace('/', '.');
  226.          } else if (var1.endsWith(".java")) {
  227.             var1 = var1.substring(0, var1.length() - 5).replace('/', '.');
  228.          }
  229.  
  230.          label175: {
  231.             try {
  232.                this.applet = (Applet)this.loader.loadClass(var1).newInstance();
  233.                if (!Thread.interrupted()) {
  234.                   break label175;
  235.                }
  236.  
  237.                this.status = 0;
  238.                this.applet = null;
  239.                this.showAppletStatus(propName + ".death");
  240.                return;
  241.             } catch (ClassNotFoundException var14) {
  242.                this.status = 7;
  243.                String var23 = MessageUtils.substProp(propName + ".notfound", var1);
  244.                this.showAppletStatus(var23);
  245.                this.showAppletLog(var23);
  246.                this.showAppletException(var14);
  247.             } catch (InstantiationException var15) {
  248.                this.status = 7;
  249.                String var22 = MessageUtils.substProp(propName + ".nocreate", var1);
  250.                this.showAppletStatus(var22);
  251.                this.showAppletLog(var22);
  252.                this.showAppletException(var15);
  253.                return;
  254.             } catch (IllegalAccessException var16) {
  255.                this.status = 7;
  256.                String var5 = MessageUtils.substProp(propName + ".noconstruct", var1);
  257.                this.showAppletStatus(var5);
  258.                this.showAppletLog(var5);
  259.                this.showAppletException(var16);
  260.                return;
  261.             } catch (Exception var17) {
  262.                this.status = 7;
  263.                this.showAppletStatus(MessageUtils.substProp(propName + ".exception", ((Throwable)var17).getMessage()));
  264.                this.showAppletException(var17);
  265.                return;
  266.             } catch (ThreadDeath var18) {
  267.                this.status = 7;
  268.                this.showAppletStatus(propName + ".death");
  269.                return;
  270.             } catch (Error var19) {
  271.                this.status = 7;
  272.                this.showAppletStatus(MessageUtils.substProp(propName + ".error", ((Throwable)var19).getMessage()));
  273.                this.showAppletException(var19);
  274.                return;
  275.             } finally {
  276.                ((Component)this).postEvent(new Event(this, 51236, (Object)null));
  277.             }
  278.  
  279.             return;
  280.          }
  281.  
  282.          this.applet.setStub(this);
  283.          this.applet.hide();
  284.          ((Container)this).add("Center", this.applet);
  285.          this.showAppletStatus(propName + ".loaded");
  286.          ((Container)this).validate();
  287.       }
  288.    }
  289.  
  290.    protected synchronized void stopLoading() {
  291.       if (this.loaderThread != null) {
  292.          this.loaderThread.interrupt();
  293.       } else {
  294.          this.setLoadAbortRequest();
  295.       }
  296.    }
  297.  
  298.    protected synchronized boolean okToLoad() {
  299.       return !this.loadAbortRequest;
  300.    }
  301.  
  302.    protected synchronized void clearLoadAbortRequest() {
  303.       this.loadAbortRequest = false;
  304.    }
  305.  
  306.    protected synchronized void setLoadAbortRequest() {
  307.       this.loadAbortRequest = true;
  308.    }
  309.  
  310.    private synchronized void setLoaderThread(Thread var1) {
  311.       this.loaderThread = var1;
  312.    }
  313.  
  314.    public boolean isActive() {
  315.       return this.status == 3;
  316.    }
  317.  
  318.    public void appletResize(int var1, int var2) {
  319.       this.appletSize.width = var1;
  320.       this.appletSize.height = var2;
  321.       ((Component)this).postEvent(new Event(this, 51234, this.preferredSize()));
  322.    }
  323.  
  324.    public Applet getApplet() {
  325.       return this.applet;
  326.    }
  327.  
  328.    protected void showAppletStatus(String var1) {
  329.       String var2 = System.getProperty(var1);
  330.       if (var2 == null) {
  331.          var2 = var1;
  332.       }
  333.  
  334.       this.getAppletContext().showStatus(var2);
  335.    }
  336.  
  337.    protected void showAppletLog(String var1) {
  338.       String var2 = System.getProperty(var1);
  339.       if (var2 == null) {
  340.          var2 = var1;
  341.       }
  342.  
  343.       System.out.println(var2);
  344.    }
  345.  
  346.    protected void showAppletException(Throwable var1) {
  347.       var1.printStackTrace();
  348.       ((Component)this).repaint();
  349.    }
  350.  
  351.    public static synchronized void flushClassLoader(URL var0) {
  352.       classloaders.remove(var0);
  353.    }
  354.  
  355.    public static synchronized void flushClassLoaders() {
  356.       classloaders = new Hashtable();
  357.    }
  358.  
  359.    static synchronized AppletClassLoader getClassLoader(URL var0) throws IOException {
  360.       AppletClassLoader var1 = (AppletClassLoader)classloaders.get(var0);
  361.       if (var1 == null) {
  362.          var1 = new AppletClassLoader(var0);
  363.          classloaders.put(var0, var1);
  364.       }
  365.  
  366.       return var1;
  367.    }
  368.  
  369.    public abstract URL getDocumentBase();
  370.  
  371.    public abstract URL getCodeBase();
  372.  
  373.    public abstract String getParameter(String var1);
  374.  
  375.    public abstract AppletContext getAppletContext();
  376. }
  377.