home *** CD-ROM | disk | FTP | other *** search
/ BUG 15 / BUGCD1998_06.ISO / aplic / jbuilder / jhelp.z / AppletButton.java < prev    next >
Text File  |  1997-07-30  |  5KB  |  158 lines

  1. //TO DO: Close all windows when you leave the page?
  2.  
  3. import java.awt.*;
  4. import java.util.*;
  5. import java.applet.Applet;
  6.  
  7. public class AppletButton extends Applet implements Runnable {
  8.     int frameNumber = 1;
  9.     String windowClass;
  10.     String buttonText;
  11.     String windowTitle;
  12.     int requestedWidth = 0;
  13.     int requestedHeight = 0;
  14.     Button button;
  15.     Thread windowThread;
  16.     Label label;
  17.     boolean pleaseCreate = false;
  18.  
  19.     public void init() {
  20.         windowClass = getParameter("WINDOWCLASS");
  21.         if (windowClass == null) {
  22.             windowClass = "TestWindow";
  23.         }
  24.  
  25.         buttonText = getParameter("BUTTONTEXT");
  26.         if (buttonText == null) {
  27.             buttonText = "Click here to bring up a " + windowClass;
  28.         }
  29.  
  30.         windowTitle = getParameter("WINDOWTITLE");
  31.         if (windowTitle == null) {
  32.             windowTitle = windowClass;
  33.         }
  34.  
  35.         String windowWidthString = getParameter("WINDOWWIDTH");
  36.         if (windowWidthString != null) {
  37.             try {
  38.                 requestedWidth = Integer.parseInt(windowWidthString);
  39.             } catch (NumberFormatException e) {
  40.                 //Use default width.
  41.             }
  42.         }
  43.  
  44.         String windowHeightString = getParameter("WINDOWHEIGHT");
  45.         if (windowHeightString != null) {
  46.             try {
  47.                 requestedHeight = Integer.parseInt(windowHeightString);
  48.             } catch (NumberFormatException e) {
  49.                 //Use default height.
  50.             }
  51.         }
  52.  
  53.         setLayout(new GridLayout(2,0));
  54.         add(button = new Button(buttonText));
  55.         button.setFont(new Font("Helvetica", Font.PLAIN, 14));
  56.  
  57.         add(label = new Label("", Label.CENTER));
  58.     }
  59.  
  60.     public void start() {
  61.         if (windowThread == null) {
  62.             windowThread = new Thread(this, "Bringing Up " + windowClass);
  63.             windowThread.start();
  64.         }
  65.     }
  66.  
  67.     public synchronized void run() {
  68.         Class windowClassObject = null;
  69.         Class tmp = null;
  70.         String name = null;
  71.         
  72.         // Make sure the window class exists and is really a Frame.
  73.         // This has the added benefit of pre-loading the class,
  74.         // which makes it much quicker for the first window to come up.
  75.         try {
  76.             windowClassObject = Class.forName(windowClass);
  77.         } catch (Exception e) {
  78.             // The specified class isn't anywhere that we can find.
  79.             label.setText("Can't create window: Couldn't find class "
  80.                               + windowClass);
  81.             button.disable();
  82.             return;
  83.         }
  84.  
  85.         // Find out whether the class is a Frame.
  86.         for (tmp = windowClassObject, name = tmp.getName();
  87.              !( name.equals("java.lang.Object") ||
  88.                 name.equals("java.awt.Frame") ); ) {
  89.             tmp = tmp.getSuperclass();
  90.             name = tmp.getName();
  91.         }
  92.         if ((name == null) || name.equals("java.lang.Object")) {
  93.             //We can't run; ERROR; print status, never bring up window
  94.             label.setText("Can't create window: "
  95.                               + windowClass +
  96.                           " isn't a Frame subclass.");
  97.             button.disable();
  98.             return;
  99.         } else if (name.equals("java.awt.Frame")) { 
  100.             //Everything's OK. Wait until we're asked to create a window.
  101.             while (windowThread != null) {
  102.                 while (pleaseCreate == false) {
  103.                     try {
  104.                         wait();
  105.                     } catch (InterruptedException e) {
  106.                     }
  107.                 }
  108.  
  109.                 //We've been asked to bring up a window.
  110.                 pleaseCreate = false;
  111.                 Frame window = null;
  112.                 try {
  113.                     window = (Frame)windowClassObject.newInstance();
  114.                 } catch (Exception e) {
  115.                     label.setText("Couldn't create instance of class "
  116.                                   + windowClass);
  117.                     button.disable();
  118.                     return;
  119.                 }
  120.                 if (frameNumber == 1) {
  121.                     window.setTitle(windowTitle);
  122.                 } else {
  123.                     window.setTitle(windowTitle + ": " + frameNumber);
  124.                 }
  125.                 frameNumber++;
  126.  
  127.                 //Set the window's size.
  128.                 window.pack();
  129.                 if ((requestedWidth > 0) | (requestedHeight > 0)) {
  130.                     window.resize(Math.max(requestedWidth,
  131.                                            window.size().width),
  132.                                   Math.max(requestedHeight,
  133.                                            window.size().height));
  134.                 }
  135.  
  136.                 window.show();
  137.                 label.setText("");
  138.             }
  139.         }
  140.     }
  141.                 
  142.     public synchronized boolean action(Event event, Object what) {
  143.         if (event.target instanceof Button) {
  144.             //signal the window thread to build a window
  145.             label.setText("Please wait while the window comes up...");
  146.             pleaseCreate = true;
  147.             notify();
  148.         } 
  149.         return true;
  150.     }
  151. }
  152.  
  153. class TestWindow extends Frame {
  154.     public TestWindow() {
  155.         resize(300, 300);
  156.     }
  157. }
  158.