home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: Java / Java.zip / telnet4j.zip / appWrapper.java < prev    next >
Text File  |  1997-07-08  |  8KB  |  257 lines

  1. /**
  2.  * appWrapper -- applet/application wrapper
  3.  * --
  4.  * $Id: appWrapper.java,v 1.8 1997/07/08 09:34:38 leo Exp $
  5.  * $timestamp: Tue Jul  8 11:31:11 1997 by Matthias L. Jugel :$
  6.  *
  7.  * This file is part of "The Java Telnet Applet".
  8.  *
  9.  * This is free software; you can redistribute it and/or modify
  10.  * it under the terms of the GNU General Public License as published by
  11.  * the Free Software Foundation; either version 2, or (at your option)
  12.  * any later version.
  13.  *
  14.  * "The Java Telnet Applet" is distributed in the hope that it will be 
  15.  * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17.  * GNU General Public License for more details.
  18.  * 
  19.  * You should have received a copy of the GNU General Public License
  20.  * along with this software; see the file COPYING.  If not, write to the
  21.  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  22.  * Boston, MA 02111-1307, USA.
  23.  */
  24.  
  25. import java.applet.Applet;
  26. import java.applet.AppletStub;
  27.  
  28. import java.awt.Frame;
  29. import java.awt.Event;
  30. import java.awt.Panel;
  31. import java.awt.Button;
  32. import java.awt.BorderLayout;
  33. import java.awt.Graphics;
  34. import java.awt.Color;
  35. import java.awt.FontMetrics;
  36.  
  37. /**
  38.  * The appWrapper is thought to make the applet itself independent from
  39.  * the original context. This is necessary to be able to detach the applet
  40.  * from the web browsers window without disconnecting it from events.
  41.  * Note: This applet should work with any applet without changes.
  42.  *
  43.  * <DL>
  44.  * <DT><B><PRE><PARAM NAME="applet" VALUE="<I>applet</I>"></PRE></B>
  45.  * <DD>Defines the applet to be loaded by the appWrapper. State the applet 
  46.  *     class name without ".class"!<P>
  47.  * <DT><B><PRE><PARAM NAME="startButton" VALUE="<I>text</I>"></PRE></B>
  48.  * <DD>If this parameter is set the applet is not loaded until the user presses
  49.  *     the button. This decreases first time download delay. The <I>text</I>
  50.  *     given as value to the parameter is shown on the button. While loading
  51.  *     the applet the message "Loading ..." is shown on the button.<P>
  52.  * <DT><B><PRE><PARAM NAME="stopButton" VALUE="<I>text</I>"></PRE></B>
  53.  * <DD>This parameter defines the button text when the applet is loaded. When 
  54.  *     pressing the button while the applet is running this causes the applet
  55.  *     window to be destroyed and the applet is stopped.<P>
  56.  * <DT><B><PRE><PARAM NAME="frameTitle" VALUE="<I>text</I>"></PRE></B>
  57.  * <DD>The <I>frameTitle</I> is the text that is shown in the title bar of the
  58.  *     applet window.<P>
  59.  * </DL>
  60.  * @version $Id: appWrapper.java,v 1.8 1997/07/08 09:34:38 leo Exp $
  61.  * @author  Matthias L. Jugel
  62.  */
  63. public class appWrapper extends Applet implements AppletStub, Runnable
  64. {
  65.   Thread loader = null;
  66.   
  67.   String appletName = null;
  68.   Applet applet = null;
  69.  
  70.   Button startButton = null;
  71.   String startLabel, stopLabel, frameTitle;
  72.  
  73.   frame f;
  74.   
  75.   /**
  76.    * Applet initialization. We load the class giving in parameter "applet"
  77.    * and set the stub corresponding to ours. Thus we are able to give
  78.    * it access to the parameters and any applet specific context.
  79.    */
  80.   public void init() {
  81.  
  82.     // get the applet parameter
  83.     if((appletName = getParameter("applet")) == null) {
  84.       showStatus("appWrapper: missing applet parameter, nothing loaded");
  85.       System.err.println("appWrapper: missing applet parameter");
  86.       return;
  87.     }
  88.  
  89.     setLayout(new BorderLayout());
  90.  
  91.     // get the button and title parameters
  92.     if((startLabel = getParameter("startButton")) == null)
  93.       run();
  94.     else {
  95.       startButton = new Button(getParameter("startButton"));
  96.       add("Center", startButton);
  97.       if((stopLabel = getParameter("stopButton")) == null)
  98.         stopLabel = "STOP!";
  99.       if((frameTitle = getParameter("frameTitle")) == null)
  100.         frameTitle = "The Java Telnet Applet";
  101.     }
  102.     
  103.   }
  104.   
  105.   /**
  106.    * Load the applet finally. When using a button this creates a new frame
  107.    * to put the applet in.
  108.    */
  109.   public void run() {
  110.     if(applet == null) try {
  111.       applet = (Applet)Class.forName(getParameter("applet")).newInstance();
  112.       applet.setStub(this);
  113.     } catch(Exception e) {
  114.       System.err.println("appWrapper: could not load "+appletName);
  115.       e.printStackTrace();
  116.       return;
  117.     } else {
  118.       System.err.println("appWrapper: applet already loaded");
  119.       return;
  120.     }
  121.  
  122.     if(startButton == null) {
  123.       add("Center", applet);
  124.       applet.init();
  125.     } else {
  126.       f = new frame(frameTitle);
  127.       f.setLayout(new BorderLayout());
  128.       f.add("Center", applet);
  129.       applet.init();
  130.       f.resize(applet.minimumSize());
  131.       f.pack();
  132.       f.show();
  133.     }
  134.     applet.start();
  135.  
  136.     if(startButton != null)
  137.       startButton.setLabel(stopLabel);
  138.     
  139.     // stop loader thread
  140.     if(loader != null) {
  141.       while(loader != null) {
  142.         if(f == null || !f.isVisible()) {
  143.           startButton.setLabel(startLabel);
  144.           loader.stop();
  145.           loader = null;
  146.         }
  147.         try { loader.sleep(5000); }
  148.         catch(InterruptedException e) {}
  149.       }
  150.     }
  151.   }
  152.  
  153.   /**
  154.    * This method is called when the applet want's to be resized.
  155.    * @param width the width of the applet
  156.    * @param height the height of the applet
  157.    */
  158.   public void appletResize(int width, int height) {
  159.     System.err.println("appWrapper: appletResize()");
  160.     if(applet != null) applet.resize(width, height);
  161.   }
  162.  
  163.   /**
  164.    * Give information about the applet.
  165.    */
  166.   public String getAppletInfo()
  167.   {
  168.     String info = "appWrapper: $Id: appWrapper.java,v 1.8 1997/07/08 09:34:38 leo Exp $\n";
  169.     if(applet != null)
  170.       info += applet.getAppletInfo();
  171.     return info;
  172.   }
  173.   
  174.   /**
  175.    * Give information about the appWrapper and the applet loaded.
  176.    */
  177.   public String[][] getParameterInfo()
  178.   {
  179.     String info[][];
  180.     String wrapper[][] = {
  181.       {"applet",   "String",   "appWrapper: Applet to load"},
  182.     };
  183.     if(applet != null) {
  184.       String tmp[][] = applet.getParameterInfo();
  185.       info = new String[tmp.length + 1][3];
  186.       System.arraycopy(tmp, 0, info, 1, tmp.length);
  187.     }
  188.     else info = new String[1][3];
  189.     System.arraycopy(wrapper, 0, info, 0, 1);
  190.       
  191.     return info;
  192.   }
  193.  
  194.   /**
  195.    * Write a message to the applet area.
  196.    */
  197.   public void paint(Graphics g) 
  198.   {
  199.     String message;
  200.     if(applet != null) 
  201.       message = "Click to reattach the Applet!";
  202.     else message = "The was no applet load (maybe an error)!";
  203.  
  204.     
  205.     int width = size().width / 2 - 
  206.       (getFontMetrics(getFont())).stringWidth(message) / 2;
  207.     int height = size().height / 2;
  208.     
  209.     g.setColor(Color.red);
  210.     g.drawString(message, width, height);
  211.   }
  212.   
  213.   /**
  214.    * reshape the applet and ourself
  215.    */
  216.   public void reshape(int x, int y, int w, int h)
  217.   {
  218.     if(applet != null) applet.reshape(x, y, w, h);
  219.     super.reshape(x, y, w, h);
  220.   }
  221.  
  222.   /**
  223.    * Handle button events. When pressed it either creates the new applet
  224.    * window or destoys it.
  225.    */
  226.   public boolean handleEvent(Event evt) 
  227.   {
  228.     if(evt.target == startButton && evt.id == Event.ACTION_EVENT) {
  229.       if(applet == null) {
  230.         startButton.setLabel("Loading ...");
  231.         (loader = new Thread(this)).start();
  232.       } else {
  233.         if(applet.getParent() instanceof Frame) {
  234.           Frame frame = (Frame)applet.getParent();
  235.           frame.hide();
  236.           frame.dispose();
  237.         }
  238.         applet.stop();
  239.         applet.destroy();
  240.         applet = null;
  241.         startButton.setLabel(startLabel);
  242.       }
  243.       return true;
  244.     }
  245.     if(evt.id == Event.MOUSE_UP && applet.getParent() instanceof Frame) {
  246.       Frame frame = (Frame)applet.getParent();
  247.       frame.hide();
  248.       frame.dispose();
  249.       add("Center", applet);
  250.       validate();
  251.       layout();
  252.       return true;
  253.     }
  254.     return false;
  255.   }
  256. }
  257.