home *** CD-ROM | disk | FTP | other *** search
/ S283 Planetary Science &n…he Search for Life DVD 2 / DVD-ROM.iso / install / jre1_3 / lib / rt.jar / sun / applet / AppletPanel.class (.txt) < prev    next >
Encoding:
Java Class File  |  1979-12-31  |  12.3 KB  |  615 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.awt.Window;
  12. import java.io.File;
  13. import java.io.FilePermission;
  14. import java.io.IOException;
  15. import java.io.InputStream;
  16. import java.io.ObjectInputStream;
  17. import java.net.SocketPermission;
  18. import java.net.URL;
  19. import java.security.AccessControlContext;
  20. import java.security.AccessController;
  21. import java.security.CodeSource;
  22. import java.security.Permission;
  23. import java.security.PermissionCollection;
  24. import java.security.Permissions;
  25. import java.security.ProtectionDomain;
  26. import java.security.cert.Certificate;
  27. import java.util.Hashtable;
  28. import java.util.StringTokenizer;
  29. import sun.misc.MessageUtils;
  30. import sun.misc.Queue;
  31.  
  32. public abstract class AppletPanel extends Panel implements AppletStub, Runnable {
  33.    Applet applet;
  34.    protected boolean doInit = true;
  35.    AppletClassLoader loader;
  36.    public static final int APPLET_DISPOSE = 0;
  37.    public static final int APPLET_LOAD = 1;
  38.    public static final int APPLET_INIT = 2;
  39.    public static final int APPLET_START = 3;
  40.    public static final int APPLET_STOP = 4;
  41.    public static final int APPLET_DESTROY = 5;
  42.    public static final int APPLET_QUIT = 6;
  43.    public static final int APPLET_ERROR = 7;
  44.    public static final int APPLET_RESIZE = 51234;
  45.    public static final int APPLET_LOADING = 51235;
  46.    public static final int APPLET_LOADING_COMPLETED = 51236;
  47.    protected int status;
  48.    Thread handler;
  49.    Dimension defaultAppletSize = new Dimension(10, 10);
  50.    Dimension currentAppletSize = new Dimension(10, 10);
  51.    // $FF: renamed from: mu sun.misc.MessageUtils
  52.    MessageUtils field_0 = new MessageUtils();
  53.    Thread loaderThread = null;
  54.    boolean loadAbortRequest = false;
  55.    private static int threadGroupNumber = 0;
  56.    private AppletListener listeners;
  57.    private Queue queue = null;
  58.    private static Hashtable classloaders = new Hashtable();
  59.    private static AppletMessageHandler amh = new AppletMessageHandler("appletpanel");
  60.  
  61.    protected abstract String getCode();
  62.  
  63.    protected abstract String getJarFiles();
  64.  
  65.    protected abstract String getSerializedObject();
  66.  
  67.    public abstract int getWidth();
  68.  
  69.    public abstract int getHeight();
  70.  
  71.    synchronized void createAppletThread() {
  72.       String var1 = "applet-" + this.getCode();
  73.       this.loader = this.getClassLoader(this.getCodeBase());
  74.       this.loader.grab();
  75.       ThreadGroup var2 = this.loader.getThreadGroup();
  76.       this.handler = new Thread(var2, this, "thread " + var1);
  77.       AccessController.doPrivileged(new 1(this));
  78.       this.handler.start();
  79.    }
  80.  
  81.    void joinAppletThread() throws InterruptedException {
  82.       if (this.handler != null) {
  83.          this.handler.join();
  84.          this.handler = null;
  85.       }
  86.  
  87.    }
  88.  
  89.    void release() {
  90.       if (this.loader != null) {
  91.          this.loader.release();
  92.          this.loader = null;
  93.       }
  94.  
  95.    }
  96.  
  97.    public void init() {
  98.       try {
  99.          this.defaultAppletSize.width = this.getWidth();
  100.          this.currentAppletSize.width = this.defaultAppletSize.width;
  101.          this.defaultAppletSize.height = this.getHeight();
  102.          this.currentAppletSize.height = this.defaultAppletSize.height;
  103.       } catch (NumberFormatException var2) {
  104.          this.status = 7;
  105.          this.showAppletStatus("badattribute.exception");
  106.          this.showAppletLog("badattribute.exception");
  107.          this.showAppletException(var2);
  108.       }
  109.  
  110.       ((Container)this).setLayout(new BorderLayout());
  111.       this.createAppletThread();
  112.    }
  113.  
  114.    public Dimension minimumSize() {
  115.       return new Dimension(this.defaultAppletSize.width, this.defaultAppletSize.height);
  116.    }
  117.  
  118.    public Dimension preferredSize() {
  119.       return new Dimension(this.currentAppletSize.width, this.currentAppletSize.height);
  120.    }
  121.  
  122.    public synchronized void addAppletListener(AppletListener var1) {
  123.       this.listeners = AppletEventMulticaster.add(this.listeners, var1);
  124.    }
  125.  
  126.    public synchronized void removeAppletListener(AppletListener var1) {
  127.       this.listeners = AppletEventMulticaster.remove(this.listeners, var1);
  128.    }
  129.  
  130.    public void dispatchAppletEvent(int var1, Object var2) {
  131.       if (this.listeners != null) {
  132.          AppletEvent var3 = new AppletEvent(this, var1, var2);
  133.          this.listeners.appletStateChanged(var3);
  134.       }
  135.  
  136.    }
  137.  
  138.    public void sendEvent(int var1) {
  139.       synchronized(this) {
  140.          if (this.queue == null) {
  141.             this.queue = new Queue();
  142.          }
  143.  
  144.          Integer var3 = new Integer(var1);
  145.          this.queue.enqueue(var3);
  146.          this.notifyAll();
  147.       }
  148.  
  149.       if (var1 == 6) {
  150.          try {
  151.             this.joinAppletThread();
  152.          } catch (InterruptedException var5) {
  153.          }
  154.  
  155.          if (this.loader == null) {
  156.             this.loader = this.getClassLoader(this.getCodeBase());
  157.          }
  158.  
  159.          this.release();
  160.       }
  161.  
  162.    }
  163.  
  164.    synchronized AppletEvent getNextEvent() throws InterruptedException {
  165.       while(this.queue == null || this.queue.isEmpty()) {
  166.          this.wait();
  167.       }
  168.  
  169.       Integer var1 = (Integer)this.queue.dequeue();
  170.       return new AppletEvent(this, var1, (Object)null);
  171.    }
  172.  
  173.    boolean emptyEventQueue() {
  174.       return this.queue == null || this.queue.isEmpty();
  175.    }
  176.  
  177.    public void run() {
  178.       Thread var1 = Thread.currentThread();
  179.       if (var1 == this.loaderThread) {
  180.          this.runLoader();
  181.       } else {
  182.          for(boolean var2 = false; !var2 && !var1.isInterrupted(); this.clearLoadAbortRequest()) {
  183.             AppletEvent var3;
  184.             try {
  185.                var3 = this.getNextEvent();
  186.             } catch (InterruptedException var7) {
  187.                this.showAppletStatus("bail");
  188.                return;
  189.             }
  190.  
  191.             try {
  192.                switch (var3.getID()) {
  193.                   case 0:
  194.                      if (this.status != 1) {
  195.                         this.showAppletStatus("notdestroyed");
  196.                      } else {
  197.                         this.status = 0;
  198.                         ((Container)this).remove(this.applet);
  199.                         this.applet = null;
  200.                         this.showAppletStatus("disposed");
  201.                         var2 = true;
  202.                      }
  203.                      break;
  204.                   case 1:
  205.                      if (this.okToLoad() && this.loaderThread == null) {
  206.                         this.setLoaderThread(new Thread(this));
  207.                         this.loaderThread.start();
  208.                         this.loaderThread.join();
  209.                         this.setLoaderThread((Thread)null);
  210.                      }
  211.                      break;
  212.                   case 2:
  213.                      if (this.status != 1) {
  214.                         this.showAppletStatus("notloaded");
  215.                      } else {
  216.                         this.applet.resize(this.defaultAppletSize);
  217.                         if (this.doInit) {
  218.                            this.applet.init();
  219.                         }
  220.  
  221.                         this.doInit = true;
  222.                         ((Container)this).validate();
  223.                         this.status = 2;
  224.                         this.showAppletStatus("inited");
  225.                      }
  226.                      break;
  227.                   case 3:
  228.                      if (this.status != 2) {
  229.                         this.showAppletStatus("notinited");
  230.                      } else {
  231.                         this.applet.resize(this.currentAppletSize);
  232.                         this.applet.start();
  233.                         ((Container)this).validate();
  234.                         this.applet.show();
  235.                         this.setDefaultFocus();
  236.                         this.status = 3;
  237.                         this.showAppletStatus("started");
  238.                      }
  239.                      break;
  240.                   case 4:
  241.                      if (this.status != 3) {
  242.                         this.showAppletStatus("notstarted");
  243.                      } else {
  244.                         this.status = 2;
  245.                         this.applet.hide();
  246.                         this.applet.stop();
  247.                         this.showAppletStatus("stopped");
  248.                      }
  249.                      break;
  250.                   case 5:
  251.                      if (this.status != 2) {
  252.                         this.showAppletStatus("notstopped");
  253.                      } else {
  254.                         this.status = 1;
  255.                         this.applet.destroy();
  256.                         this.showAppletStatus("destroyed");
  257.                      }
  258.                      break;
  259.                   case 6:
  260.                      return;
  261.                }
  262.             } catch (Exception var8) {
  263.                if (((Throwable)var8).getMessage() != null) {
  264.                   this.showAppletStatus("exception2", var8.getClass().getName(), ((Throwable)var8).getMessage());
  265.                } else {
  266.                   this.showAppletStatus("exception", var8.getClass().getName());
  267.                }
  268.  
  269.                this.showAppletException(var8);
  270.             } catch (ThreadDeath var9) {
  271.                this.showAppletStatus("death");
  272.                return;
  273.             } catch (Error var10) {
  274.                if (((Throwable)var10).getMessage() != null) {
  275.                   this.showAppletStatus("error2", var10.getClass().getName(), ((Throwable)var10).getMessage());
  276.                } else {
  277.                   this.showAppletStatus("error", var10.getClass().getName());
  278.                }
  279.  
  280.                this.showAppletException(var10);
  281.             }
  282.          }
  283.  
  284.       }
  285.    }
  286.  
  287.    private void setDefaultFocus() {
  288.       if (((Component)this).getParent() instanceof Window) {
  289.          Window var1 = (Window)((Component)this).getParent();
  290.          if (var1.getFocusOwner() != null) {
  291.             this.doSetDefaultFocus();
  292.          }
  293.       }
  294.  
  295.    }
  296.  
  297.    private void doSetDefaultFocus() {
  298.       if (this.applet != null) {
  299.          if (!this.activateFocus(this.applet)) {
  300.             Object var1;
  301.             Component var2;
  302.             for(var1 = this.applet; var1 instanceof Container && ((Container)var1).getComponentCount() > 0; var1 = var2) {
  303.                var2 = ((Container)var1).getComponent(0);
  304.                if (!var2.isVisible() || !var2.isEnabled()) {
  305.                   break;
  306.                }
  307.             }
  308.  
  309.             ((Component)var1).requestFocus();
  310.          }
  311.       }
  312.    }
  313.  
  314.    private boolean activateFocus(Container var1) {
  315.       int var2 = var1.getComponentCount();
  316.  
  317.       for(int var3 = 0; var3 < var2; ++var3) {
  318.          Component var4 = var1.getComponent(var3);
  319.          if (var4.isVisible() && var4.isEnabled() && var4.isFocusTraversable()) {
  320.             var4.requestFocus();
  321.             return true;
  322.          }
  323.  
  324.          if (var4 instanceof Container && var4.isVisible() && var4.isEnabled() && this.activateFocus((Container)var4)) {
  325.             return true;
  326.          }
  327.       }
  328.  
  329.       return false;
  330.    }
  331.  
  332.    private void runLoader() {
  333.       if (this.status != 0) {
  334.          this.showAppletStatus("notdisposed");
  335.       } else {
  336.          this.dispatchAppletEvent(51235, (Object)null);
  337.          this.status = 1;
  338.          this.loader = this.getClassLoader(this.getCodeBase());
  339.          String var1 = this.getCode();
  340.  
  341.          label118: {
  342.             try {
  343.                this.loadJarFiles(this.loader);
  344.                this.applet = this.createApplet(this.loader);
  345.                break label118;
  346.             } catch (ClassNotFoundException var17) {
  347.                this.status = 7;
  348.                this.showAppletStatus("notfound", var1);
  349.                this.showAppletLog("notfound", var1);
  350.                this.showAppletException(var17);
  351.             } catch (InstantiationException var18) {
  352.                this.status = 7;
  353.                this.showAppletStatus("nocreate", var1);
  354.                this.showAppletLog("nocreate", var1);
  355.                this.showAppletException(var18);
  356.                return;
  357.             } catch (IllegalAccessException var19) {
  358.                this.status = 7;
  359.                this.showAppletStatus("noconstruct", var1);
  360.                this.showAppletLog("noconstruct", var1);
  361.                this.showAppletException(var19);
  362.                return;
  363.             } catch (Exception var20) {
  364.                this.status = 7;
  365.                this.showAppletStatus("exception", ((Throwable)var20).getMessage());
  366.                this.showAppletException(var20);
  367.                return;
  368.             } catch (ThreadDeath var21) {
  369.                this.status = 7;
  370.                this.showAppletStatus("death");
  371.                return;
  372.             } catch (Error var22) {
  373.                this.status = 7;
  374.                this.showAppletStatus("error", ((Throwable)var22).getMessage());
  375.                this.showAppletException(var22);
  376.                return;
  377.             } finally {
  378.                this.dispatchAppletEvent(51236, (Object)null);
  379.             }
  380.  
  381.             return;
  382.          }
  383.  
  384.          this.applet.setStub(this);
  385.          this.applet.hide();
  386.          ((Container)this).add("Center", this.applet);
  387.          this.showAppletStatus("loaded");
  388.          ((Container)this).validate();
  389.       }
  390.    }
  391.  
  392.    protected Applet createApplet(AppletClassLoader var1) throws ClassNotFoundException, IllegalAccessException, IOException, InstantiationException, InterruptedException {
  393.       String var2 = this.getSerializedObject();
  394.       String var3 = this.getCode();
  395.       if (var3 != null && var2 != null) {
  396.          System.err.println(amh.getMessage("runloader.err"));
  397.          return null;
  398.       } else {
  399.          if (var3 == null && var2 == null) {
  400.             String var4 = "nocode";
  401.             this.status = 7;
  402.             this.showAppletStatus(var4);
  403.             this.showAppletLog(var4);
  404.             ((Component)this).repaint();
  405.          }
  406.  
  407.          if (var3 != null) {
  408.             this.applet = (Applet)var1.loadCode(var3).newInstance();
  409.             this.doInit = true;
  410.          } else {
  411.             InputStream var11 = (InputStream)AccessController.doPrivileged(new 2(this, var1, var2));
  412.             AppletObjectInputStream var5 = new AppletObjectInputStream(var11, var1);
  413.             Object var6 = ((ObjectInputStream)var5).readObject();
  414.             this.applet = (Applet)var6;
  415.             this.doInit = false;
  416.          }
  417.  
  418.          if (Thread.interrupted()) {
  419.             label58:
  420.             try {
  421.                this.status = 0;
  422.                this.applet = null;
  423.                this.showAppletStatus("death");
  424.             } finally {
  425.                break label58;
  426.             }
  427.  
  428.             Thread.currentThread().interrupt();
  429.             return null;
  430.          } else {
  431.             return this.applet;
  432.          }
  433.       }
  434.    }
  435.  
  436.    protected void loadJarFiles(AppletClassLoader var1) throws IOException, InterruptedException {
  437.       String var2 = this.getJarFiles();
  438.       if (var2 != null) {
  439.          StringTokenizer var3 = new StringTokenizer(var2, ",", false);
  440.  
  441.          while(var3.hasMoreTokens()) {
  442.             String var4 = var3.nextToken().trim();
  443.  
  444.             try {
  445.                var1.addJar(var4);
  446.             } catch (IllegalArgumentException var6) {
  447.             }
  448.          }
  449.       }
  450.  
  451.    }
  452.  
  453.    protected synchronized void stopLoading() {
  454.       if (this.loaderThread != null) {
  455.          this.loaderThread.interrupt();
  456.       } else {
  457.          this.setLoadAbortRequest();
  458.       }
  459.  
  460.    }
  461.  
  462.    protected synchronized boolean okToLoad() {
  463.       return !this.loadAbortRequest;
  464.    }
  465.  
  466.    protected synchronized void clearLoadAbortRequest() {
  467.       this.loadAbortRequest = false;
  468.    }
  469.  
  470.    protected synchronized void setLoadAbortRequest() {
  471.       this.loadAbortRequest = true;
  472.    }
  473.  
  474.    private synchronized void setLoaderThread(Thread var1) {
  475.       this.loaderThread = var1;
  476.    }
  477.  
  478.    public boolean isActive() {
  479.       return this.status == 3;
  480.    }
  481.  
  482.    public void appletResize(int var1, int var2) {
  483.       this.currentAppletSize.width = var1;
  484.       this.currentAppletSize.height = var2;
  485.       Dimension var3 = new Dimension(this.currentAppletSize.width, this.currentAppletSize.height);
  486.       this.dispatchAppletEvent(51234, var3);
  487.    }
  488.  
  489.    public void setBounds(int var1, int var2, int var3, int var4) {
  490.       super.setBounds(var1, var2, var3, var4);
  491.       this.currentAppletSize.width = var3;
  492.       this.currentAppletSize.height = var4;
  493.    }
  494.  
  495.    public Applet getApplet() {
  496.       return this.applet;
  497.    }
  498.  
  499.    protected void showAppletStatus(String var1) {
  500.       this.getAppletContext().showStatus(amh.getMessage(var1));
  501.    }
  502.  
  503.    protected void showAppletStatus(String var1, Object var2) {
  504.       this.getAppletContext().showStatus(amh.getMessage(var1, var2));
  505.    }
  506.  
  507.    protected void showAppletStatus(String var1, Object var2, Object var3) {
  508.       this.getAppletContext().showStatus(amh.getMessage(var1, var2, var3));
  509.    }
  510.  
  511.    protected void showAppletLog(String var1) {
  512.       System.out.println(amh.getMessage(var1));
  513.    }
  514.  
  515.    protected void showAppletLog(String var1, Object var2) {
  516.       System.out.println(amh.getMessage(var1, var2));
  517.    }
  518.  
  519.    protected void showAppletException(Throwable var1) {
  520.       var1.printStackTrace();
  521.       ((Component)this).repaint();
  522.    }
  523.  
  524.    public static synchronized void flushClassLoader(URL var0) {
  525.       classloaders.remove(var0);
  526.    }
  527.  
  528.    public static synchronized void flushClassLoaders() {
  529.       classloaders = new Hashtable();
  530.    }
  531.  
  532.    protected AppletClassLoader createClassLoader(URL var1) {
  533.       return new AppletClassLoader(var1);
  534.    }
  535.  
  536.    synchronized AppletClassLoader getClassLoader(URL var1) {
  537.       AppletClassLoader var2 = (AppletClassLoader)classloaders.get(var1);
  538.       if (var2 == null) {
  539.          AccessControlContext var3 = this.getAccessControlContext(var1);
  540.          var2 = (AppletClassLoader)AccessController.doPrivileged(new 3(this, var1), var3);
  541.       }
  542.  
  543.       return var2;
  544.    }
  545.  
  546.    private AccessControlContext getAccessControlContext(URL var1) {
  547.       Object var2 = (PermissionCollection)AccessController.doPrivileged(new 4(this));
  548.       if (var2 == null) {
  549.          var2 = new Permissions();
  550.       }
  551.  
  552.       ((PermissionCollection)var2).add(new RuntimePermission("createClassLoader"));
  553.  
  554.       Permission var3;
  555.       try {
  556.          var3 = var1.openConnection().getPermission();
  557.       } catch (IOException var6) {
  558.          var3 = null;
  559.       }
  560.  
  561.       if (var3 instanceof FilePermission) {
  562.          String var4 = var3.getName();
  563.          int var5 = var4.lastIndexOf(File.separatorChar);
  564.          ((PermissionCollection)var2).add(var3);
  565.          if (var5 != -1) {
  566.             var4 = var4.substring(0, var5 + 1);
  567.             if (var4.endsWith(File.separator)) {
  568.                var4 = var4 + "-";
  569.             }
  570.  
  571.             ((PermissionCollection)var2).add(new FilePermission(var4, "read"));
  572.          }
  573.       } else {
  574.          String var8 = var1.getHost();
  575.          if (var8 == null) {
  576.             var8 = "localhost";
  577.          }
  578.  
  579.          ((PermissionCollection)var2).add(new SocketPermission(var8, "connect, accept"));
  580.          if (var3 != null) {
  581.             ((PermissionCollection)var2).add(var3);
  582.          }
  583.       }
  584.  
  585.       ProtectionDomain var9 = new ProtectionDomain(new CodeSource(var1, (Certificate[])null), (PermissionCollection)var2);
  586.       AccessControlContext var10 = new AccessControlContext(new ProtectionDomain[]{var9});
  587.       return var10;
  588.    }
  589.  
  590.    public Thread getAppletHandlerThread() {
  591.       return this.handler;
  592.    }
  593.  
  594.    public int getAppletWidth() {
  595.       return this.currentAppletSize.width;
  596.    }
  597.  
  598.    public int getAppletHeight() {
  599.       return this.currentAppletSize.height;
  600.    }
  601.  
  602.    public abstract AppletContext getAppletContext();
  603.  
  604.    public abstract String getParameter(String var1);
  605.  
  606.    public abstract URL getCodeBase();
  607.  
  608.    public abstract URL getDocumentBase();
  609.  
  610.    // $FF: synthetic method
  611.    static Hashtable access$000() {
  612.       return classloaders;
  613.    }
  614. }
  615.