home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 July / Chip_1998-07_cd.bin / zkuste / JBuilder / BDK / Win / bdk_sep97.exe / _SETUP.1 / BeanWrapper.java < prev    next >
Encoding:
Java Source  |  1997-09-10  |  1.8 KB  |  84 lines

  1.  
  2. package sunw.wrapper;
  3.  
  4. /**
  5.  * Some beans are themselves applets.  These beans can be run directly
  6.  * as applets from within HTML documents.
  7.  *
  8.  * However, some beans are not applets.  This simple Warpper applet
  9.  * allwos you run run an arbitrary named bean class inside a Wrapper on an
  10.  * HTML page.  It takes a single parameter "BEAN" that gives a bean name
  11.  * such as "sunw.demo.juggler.Juggler".
  12.  */
  13.  
  14. import java.applet.Applet;
  15. import java.awt.*;
  16. import java.beans.Beans;
  17.  
  18. public class BeanWrapper extends Applet {
  19.     private Component bean;
  20.     private String error;
  21.  
  22.     public void init() {
  23.     setLayout(null);
  24.     String beanName = getParameter("BEAN");
  25.     if (beanName == null) {
  26.         error = "no \"BEAN\" parameter defined";
  27.         System.err.println(error);
  28.         showStatus(error);
  29.         return;
  30.     }
  31.     try {
  32.         ClassLoader cl = this.getClass().getClassLoader();
  33.         Object o = Beans.instantiate(cl, beanName);
  34.         bean = (Component) o;
  35.     } catch (Exception ex) {
  36.         error = "Couldn't instantiate bean " + ex;
  37.         System.err.println(error);
  38.         showStatus(error);
  39.     }
  40.     }
  41.  
  42.     public void start() {
  43.     if (bean == null) {
  44.         repaint();
  45.         return;
  46.     }
  47.     removeAll();
  48.     add(bean);
  49.     bean.setSize(getSize());
  50.     if (Beans.isInstanceOf(bean, Applet.class)) {
  51.         Applet apl = (Applet) Beans.getInstanceOf(bean, Applet.class);
  52.         apl.start();
  53.     }
  54.     }
  55.  
  56.     public void stop() {
  57.     if (bean == null) {
  58.         return;
  59.     }
  60.     if (Beans.isInstanceOf(bean, Applet.class)) {
  61.         Applet apl = (Applet) Beans.getInstanceOf(bean, Applet.class);
  62.         apl.stop();
  63.     }
  64.     }
  65.  
  66.     public void destroy() {
  67.     if (bean == null) {
  68.         return;
  69.     }
  70.     if (Beans.isInstanceOf(bean, Applet.class)) {
  71.         Applet apl = (Applet) Beans.getInstanceOf(bean, Applet.class);
  72.         apl.destroy();
  73.     }
  74.     }
  75.  
  76.     public void paint(Graphics g) {
  77.     if (error != null) {
  78.         g.drawString(error, 10, 30);
  79.         return;
  80.     }
  81.     }
  82.  
  83. }
  84.