home *** CD-ROM | disk | FTP | other *** search
/ Symantec Visual Cafe for Java 2.5 / symantec-visual-cafe-2.5-database-dev-edition.iso / VPage / Java.bin / CLASSES.ZIP / sun / applet / AppletPanel.class (.txt) < prev    next >
Encoding:
Java Class File  |  1997-07-08  |  11.0 KB  |  469 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.Panel;
  11. import java.io.IOException;
  12. import java.io.InputStream;
  13. import java.io.ObjectInputStream;
  14. import java.net.URL;
  15. import java.util.Hashtable;
  16. import java.util.StringTokenizer;
  17. import sun.misc.MessageUtils;
  18. import sun.misc.Queue;
  19.  
  20. public abstract class AppletPanel extends Panel implements AppletStub, Runnable {
  21.    static boolean debug;
  22.    Applet applet;
  23.    protected boolean doInit = true;
  24.    AppletClassLoader loader;
  25.    public static final int APPLET_DISPOSE = 0;
  26.    public static final int APPLET_LOAD = 1;
  27.    public static final int APPLET_INIT = 2;
  28.    public static final int APPLET_START = 3;
  29.    public static final int APPLET_STOP = 4;
  30.    public static final int APPLET_DESTROY = 5;
  31.    public static final int APPLET_QUIT = 6;
  32.    public static final int APPLET_ERROR = 7;
  33.    public static final int APPLET_RESIZE = 51234;
  34.    public static final int APPLET_LOADING = 51235;
  35.    public static final int APPLET_LOADING_COMPLETED = 51236;
  36.    protected int status;
  37.    Thread handler;
  38.    Dimension appletSize = new Dimension(100, 100);
  39.    // $FF: renamed from: mu sun.misc.MessageUtils
  40.    MessageUtils field_0 = new MessageUtils();
  41.    Thread loaderThread;
  42.    boolean loadAbortRequest = false;
  43.    private AppletListener listeners;
  44.    private Queue queue;
  45.    private static Hashtable classloaders = new Hashtable();
  46.    private static AppletMessageHandler amh = new AppletMessageHandler("appletpanel");
  47.  
  48.    protected abstract String getCode();
  49.  
  50.    protected abstract int getWidth();
  51.  
  52.    protected abstract int getHeight();
  53.  
  54.    protected abstract String getJarFiles();
  55.  
  56.    protected abstract String getSerializedObject();
  57.  
  58.    public void init() {
  59.       ((Container)this).setLayout(new BorderLayout());
  60.  
  61.       try {
  62.          this.appletSize.width = this.getWidth();
  63.          this.appletSize.height = this.getHeight();
  64.       } catch (NumberFormatException var3) {
  65.          this.status = 7;
  66.          this.showAppletStatus("badattribute.exception");
  67.          this.showAppletLog("badattribute.exception");
  68.          this.showAppletException(var3);
  69.       }
  70.  
  71.       String var1 = "applet-" + this.getCode();
  72.       this.loader = this.getClassLoader(this.getCodeBase());
  73.       ThreadGroup var2 = this.loader.getThreadGroup();
  74.       this.handler = new Thread(var2, this, "thread " + var1);
  75.       this.handler.start();
  76.    }
  77.  
  78.    public Dimension minimumSize() {
  79.       return new Dimension(this.appletSize.width, this.appletSize.height);
  80.    }
  81.  
  82.    public Dimension preferredSize() {
  83.       return this.minimumSize();
  84.    }
  85.  
  86.    public synchronized void addAppletListener(AppletListener var1) {
  87.       this.listeners = AppletEventMulticaster.add(this.listeners, var1);
  88.    }
  89.  
  90.    public synchronized void removeAppletListener(AppletListener var1) {
  91.       this.listeners = AppletEventMulticaster.remove(this.listeners, var1);
  92.    }
  93.  
  94.    public void dispatchAppletEvent(int var1, Object var2) {
  95.       if (this.listeners != null) {
  96.          AppletEvent var3 = new AppletEvent(this, var1, var2);
  97.          this.listeners.appletStateChanged(var3);
  98.       }
  99.  
  100.    }
  101.  
  102.    public synchronized void sendEvent(int var1) {
  103.       if (this.queue == null) {
  104.          this.queue = new Queue();
  105.       }
  106.  
  107.       Integer var2 = new Integer(var1);
  108.       this.queue.enqueue(var2);
  109.       this.notifyAll();
  110.    }
  111.  
  112.    synchronized AppletEvent getNextEvent() throws InterruptedException {
  113.       while(this.queue == null || this.queue.isEmpty()) {
  114.          this.wait();
  115.       }
  116.  
  117.       Integer var1 = (Integer)this.queue.dequeue();
  118.       return new AppletEvent(this, var1, (Object)null);
  119.    }
  120.  
  121.    public void run() {
  122.       Thread var1 = Thread.currentThread();
  123.       if (var1 == this.loaderThread) {
  124.          this.runLoader();
  125.       } else {
  126.          while(true) {
  127.             AppletEvent var2;
  128.             try {
  129.                var2 = this.getNextEvent();
  130.             } catch (InterruptedException var4) {
  131.                this.showAppletStatus("bail");
  132.                return;
  133.             }
  134.  
  135.             try {
  136.                switch (var2.getID()) {
  137.                   case 0:
  138.                      if (this.status != 1) {
  139.                         this.showAppletStatus("notdestroyed");
  140.                      } else {
  141.                         this.status = 0;
  142.                         ((Container)this).remove(this.applet);
  143.                         this.applet = null;
  144.                         this.showAppletStatus("disposed");
  145.                      }
  146.                      break;
  147.                   case 1:
  148.                      if (this.okToLoad() && this.loaderThread == null) {
  149.                         this.setLoaderThread(new Thread(this));
  150.                         this.loaderThread.start();
  151.                         this.loaderThread.join();
  152.                         this.setLoaderThread((Thread)null);
  153.                      }
  154.                      break;
  155.                   case 2:
  156.                      if (this.status != 1) {
  157.                         this.showAppletStatus("notloaded");
  158.                      } else {
  159.                         this.applet.resize(this.appletSize);
  160.                         if (this.doInit) {
  161.                            this.applet.init();
  162.                         }
  163.  
  164.                         this.doInit = true;
  165.                         ((Container)this).validate();
  166.                         this.status = 2;
  167.                         this.showAppletStatus("inited");
  168.                      }
  169.                      break;
  170.                   case 3:
  171.                      if (this.status != 2) {
  172.                         this.showAppletStatus("notinited");
  173.                      } else {
  174.                         this.applet.resize(this.appletSize);
  175.                         this.applet.start();
  176.                         ((Container)this).validate();
  177.                         this.applet.show();
  178.                         this.status = 3;
  179.                         this.showAppletStatus("started");
  180.                      }
  181.                      break;
  182.                   case 4:
  183.                      if (this.status != 3) {
  184.                         this.showAppletStatus("notstarted");
  185.                      } else {
  186.                         this.status = 2;
  187.                         this.applet.hide();
  188.                         this.applet.stop();
  189.                         this.showAppletStatus("stopped");
  190.                      }
  191.                      break;
  192.                   case 5:
  193.                      if (this.status != 2) {
  194.                         this.showAppletStatus("notstopped");
  195.                      } else {
  196.                         this.status = 1;
  197.                         this.applet.destroy();
  198.                         this.showAppletStatus("destroyed");
  199.                      }
  200.                      break;
  201.                   case 6:
  202.                      this.loader.getThreadGroup().stop();
  203.                      return;
  204.                }
  205.             } catch (Exception var5) {
  206.                if (((Throwable)var5).getMessage() != null) {
  207.                   this.showAppletStatus("exception2", var5.getClass().getName(), ((Throwable)var5).getMessage());
  208.                } else {
  209.                   this.showAppletStatus("exception", var5.getClass().getName());
  210.                }
  211.  
  212.                this.showAppletException(var5);
  213.             } catch (ThreadDeath var6) {
  214.                this.showAppletStatus("death");
  215.                return;
  216.             } catch (Error var7) {
  217.                if (((Throwable)var7).getMessage() != null) {
  218.                   this.showAppletStatus("error2", var7.getClass().getName(), ((Throwable)var7).getMessage());
  219.                } else {
  220.                   this.showAppletStatus("error", var7.getClass().getName());
  221.                }
  222.  
  223.                this.showAppletException(var7);
  224.             }
  225.  
  226.             this.clearLoadAbortRequest();
  227.          }
  228.       }
  229.    }
  230.  
  231.    private void runLoader() {
  232.       if (this.status != 0) {
  233.          this.showAppletStatus("notdisposed");
  234.       } else {
  235.          this.dispatchAppletEvent(51235, (Object)null);
  236.          this.status = 1;
  237.          this.loader = this.getClassLoader(this.getCodeBase());
  238.          String var1 = this.getCode();
  239.  
  240.          label119: {
  241.             try {
  242.                this.loadJarFiles(this.loader);
  243.                this.applet = this.createApplet(this.loader);
  244.                break label119;
  245.             } catch (ClassNotFoundException var12) {
  246.                this.status = 7;
  247.                this.showAppletStatus("notfound", var1);
  248.                this.showAppletLog("notfound", var1);
  249.                this.showAppletException(var12);
  250.             } catch (InstantiationException var13) {
  251.                this.status = 7;
  252.                this.showAppletStatus("nocreate", var1);
  253.                this.showAppletLog("nocreate", var1);
  254.                this.showAppletException(var13);
  255.                return;
  256.             } catch (IllegalAccessException var14) {
  257.                this.status = 7;
  258.                this.showAppletStatus("noconstruct", var1);
  259.                this.showAppletLog("noconstruct", var1);
  260.                this.showAppletException(var14);
  261.                return;
  262.             } catch (Exception var15) {
  263.                this.status = 7;
  264.                this.showAppletStatus("exception", ((Throwable)var15).getMessage());
  265.                this.showAppletException(var15);
  266.                return;
  267.             } catch (ThreadDeath var16) {
  268.                this.status = 7;
  269.                this.showAppletStatus("death");
  270.                return;
  271.             } catch (Error var17) {
  272.                this.status = 7;
  273.                this.showAppletStatus("error", ((Throwable)var17).getMessage());
  274.                this.showAppletException(var17);
  275.                return;
  276.             } finally {
  277.                this.dispatchAppletEvent(51236, (Object)null);
  278.             }
  279.  
  280.             return;
  281.          }
  282.  
  283.          this.applet.setStub(this);
  284.          this.applet.hide();
  285.          ((Container)this).add("Center", this.applet);
  286.          this.showAppletStatus("loaded");
  287.          ((Container)this).validate();
  288.       }
  289.    }
  290.  
  291.    protected Applet createApplet(AppletClassLoader var1) throws ClassNotFoundException, IllegalAccessException, IOException, InstantiationException, InterruptedException {
  292.       String var2 = this.getSerializedObject();
  293.       String var3 = this.getCode();
  294.       if (var3 != null && var2 != null) {
  295.          System.err.println(amh.getMessage("runloader.err"));
  296.          return null;
  297.       } else {
  298.          if (var3 == null && var2 == null) {
  299.             String var4 = "nocode";
  300.             this.status = 7;
  301.             this.showAppletStatus(var4);
  302.             this.showAppletLog(var4);
  303.             ((Component)this).repaint();
  304.          }
  305.  
  306.          if (var3 != null) {
  307.             this.applet = (Applet)var1.loadCode(var3).newInstance();
  308.             this.doInit = true;
  309.          } else {
  310.             InputStream var9 = var1.getResourceAsStream(var2);
  311.             AppletObjectInputStream var5 = new AppletObjectInputStream(var9, var1);
  312.             Object var6 = ((ObjectInputStream)var5).readObject();
  313.             this.applet = (Applet)var6;
  314.             this.doInit = false;
  315.          }
  316.  
  317.          if (Thread.interrupted()) {
  318.             try {
  319.                this.status = 0;
  320.                this.applet = null;
  321.                this.showAppletStatus("death");
  322.             } finally {
  323.                ;
  324.             }
  325.  
  326.             Thread.currentThread().interrupt();
  327.             return null;
  328.          } else {
  329.             return this.applet;
  330.          }
  331.       }
  332.    }
  333.  
  334.    protected void loadJarFiles(AppletClassLoader var1) throws IOException, InterruptedException {
  335.       debug("AppletPanel:CTOR: get list of JarFiles");
  336.       String var2 = this.getJarFiles();
  337.       if (var2 == null) {
  338.          debug("No jar files specified");
  339.       } else {
  340.          debug("archive parameter is \"" + var2 + "\"");
  341.          AppletResourceLoader var3 = var1.getResourceLoader();
  342.          StringTokenizer var4 = new StringTokenizer(var2, ",", false);
  343.  
  344.          while(var4.hasMoreTokens()) {
  345.             String var5 = var4.nextToken().trim();
  346.             debug("load archive for codebase " + this.getCodeBase() + ", file " + var5);
  347.             var3.loadJar(this.getCodeBase(), var5);
  348.          }
  349.  
  350.       }
  351.    }
  352.  
  353.    protected synchronized void stopLoading() {
  354.       if (this.loaderThread != null) {
  355.          this.loaderThread.interrupt();
  356.       } else {
  357.          this.setLoadAbortRequest();
  358.       }
  359.    }
  360.  
  361.    protected synchronized boolean okToLoad() {
  362.       return !this.loadAbortRequest;
  363.    }
  364.  
  365.    protected synchronized void clearLoadAbortRequest() {
  366.       this.loadAbortRequest = false;
  367.    }
  368.  
  369.    protected synchronized void setLoadAbortRequest() {
  370.       this.loadAbortRequest = true;
  371.    }
  372.  
  373.    private synchronized void setLoaderThread(Thread var1) {
  374.       this.loaderThread = var1;
  375.    }
  376.  
  377.    public boolean isActive() {
  378.       return this.status == 3;
  379.    }
  380.  
  381.    public void appletResize(int var1, int var2) {
  382.       this.appletSize.width = var1;
  383.       this.appletSize.height = var2;
  384.       this.dispatchAppletEvent(51234, this.preferredSize());
  385.    }
  386.  
  387.    public Applet getApplet() {
  388.       return this.applet;
  389.    }
  390.  
  391.    protected void showAppletStatus(String var1) {
  392.       this.getAppletContext().showStatus(amh.getMessage(var1));
  393.    }
  394.  
  395.    protected void showAppletStatus(String var1, Object var2) {
  396.       this.getAppletContext().showStatus(amh.getMessage(var1, var2));
  397.    }
  398.  
  399.    protected void showAppletStatus(String var1, Object var2, Object var3) {
  400.       this.getAppletContext().showStatus(amh.getMessage(var1, var2, var3));
  401.    }
  402.  
  403.    protected void showAppletLog(String var1) {
  404.       System.out.println(amh.getMessage(var1));
  405.    }
  406.  
  407.    protected void showAppletLog(String var1, Object var2) {
  408.       System.out.println(amh.getMessage(var1, var2));
  409.    }
  410.  
  411.    protected void showAppletException(Throwable var1) {
  412.       var1.printStackTrace();
  413.       ((Component)this).repaint();
  414.    }
  415.  
  416.    public static synchronized void flushClassLoader(URL var0) {
  417.       classloaders.remove(var0);
  418.    }
  419.  
  420.    public static synchronized void flushClassLoaders() {
  421.       classloaders = new Hashtable();
  422.    }
  423.  
  424.    synchronized AppletClassLoader getClassLoader(URL var1) {
  425.       AppletClassLoader var2 = (AppletClassLoader)classloaders.get(var1);
  426.       if (var2 == null) {
  427.          var2 = new AppletClassLoader(var1);
  428.          classloaders.put(var1, var2);
  429.       }
  430.  
  431.       return var2;
  432.    }
  433.  
  434.    public Thread getAppletHandlerThread() {
  435.       return this.handler;
  436.    }
  437.  
  438.    public int getAppletWidth() {
  439.       return this.appletSize.width;
  440.    }
  441.  
  442.    public int getAppletHeight() {
  443.       return this.appletSize.height;
  444.    }
  445.  
  446.    static void debug(String var0) {
  447.       if (debug) {
  448.          System.err.println("AppletPanel:::" + var0);
  449.       }
  450.  
  451.    }
  452.  
  453.    static void debug(String var0, Throwable var1) {
  454.       if (debug) {
  455.          var1.printStackTrace();
  456.          debug(var0);
  457.       }
  458.  
  459.    }
  460.  
  461.    public abstract URL getDocumentBase();
  462.  
  463.    public abstract URL getCodeBase();
  464.  
  465.    public abstract String getParameter(String var1);
  466.  
  467.    public abstract AppletContext getAppletContext();
  468. }
  469.