home *** CD-ROM | disk | FTP | other *** search
/ Java 1996 August / Java - Summer 1996.iso / windows / doc / appletbu.jav < prev    next >
Encoding:
Text File  |  1996-02-26  |  4.3 KB  |  145 lines

  1. /*
  2.  * Copyright (c) 1994 Sun Microsystems, Inc. All Rights Reserved.
  3.  *
  4.  * Permission to use, copy, modify, and distribute this software
  5.  * and its documentation for NON-COMMERCIAL purposes and without
  6.  * fee is hereby granted provided that this copyright notice
  7.  * appears in all copies. Please refer to the file "copyright.html"
  8.  * for further important copyright and licensing information.
  9.  *
  10.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  11.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  12.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  13.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  14.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  15.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  16.  */
  17. import java.awt.*;
  18. import java.util.*;
  19. import java.applet.Applet;
  20.  
  21. public class AppletButton extends Applet implements Runnable {
  22.     int frameNumber = 1;
  23.     String windowClass = null;
  24.     String buttonText = null;
  25.     String windowText = null;
  26.     Button button = null;
  27.     Thread windowThread = null;
  28.     Label label = null;
  29.     boolean pleaseCreate = false;
  30.  
  31.     public void init() {
  32.     windowClass = getParameter("WINDOWTYPE");
  33.     if (windowClass == null) {
  34.         windowClass = "TestWindow";
  35.     }
  36.  
  37.     buttonText = getParameter("BUTTONTEXT");
  38.     if (buttonText == null) {
  39.         buttonText = "Click here to bring up a " + windowClass;
  40.     }
  41.  
  42.     windowText = getParameter("WINDOWTEXT");
  43.     if (windowText == null) {
  44.         windowText = windowClass;
  45.     }
  46.  
  47.     setLayout(new GridLayout(2,0));
  48.     add(button = new Button(buttonText));
  49.         button.setFont(new Font("Helvetica", Font.PLAIN, 14));
  50.  
  51.     add(label = new Label("", Label.CENTER));
  52.     }
  53.  
  54.     public void start() {
  55.     if (windowThread == null) {
  56.         windowThread = new Thread(this, windowClass + " Bringup Thread");
  57.         windowThread.start();
  58.     }
  59.     }
  60.  
  61.     public synchronized void run() {
  62.     Class windowClassObject = null;
  63.     Class tmp = null;
  64.     String name = null;
  65.     
  66.     // Make sure the window class exists.
  67.     // This has the added benefit of pre-loading the class,
  68.     // which makes it much quicker for the first window to come up.
  69.     try {
  70.         windowClassObject = Class.forName(windowClass);
  71.     } catch (Exception e) {
  72.         // The specified class isn't anywhere that we can find.
  73.         label.setText("Can't create window: Couldn't find class "
  74.                   + windowClass);
  75.         button.disable();
  76.     }
  77.  
  78.     // Make sure the class is a Frame.
  79.     for (tmp = windowClassObject, name = tmp.getName();
  80.          !( name.equals("java.lang.Object") ||
  81.             name.equals("java.awt.Frame") ); ) {
  82.         tmp = tmp.getSuperclass();
  83.         name = tmp.getName();
  84.     }
  85.     if ((name == null) || name.equals("java.lang.Object")) {
  86.         //We can't run; ERROR; print status, never bring up window
  87.         label.setText("Can't create window: "
  88.                   + windowClass +
  89.               " isn't a Frame subclass.");
  90.         button.disable();
  91.     } else if (name.equals("java.awt.Frame")) { 
  92.  
  93.         //Everything's OK. Wait until we're asked to create a window.
  94.         while (windowThread != null) {
  95.             while (pleaseCreate == false) {
  96.             try {
  97.                 wait();
  98.             } catch (InterruptedException e) {
  99.             }
  100.             }
  101.  
  102.             //We've been asked to bring up a window.
  103.             pleaseCreate = false;
  104.             Frame window = null;
  105.             try {
  106.                 window = (Frame)windowClassObject.newInstance();
  107.             } catch (Exception e) {
  108.             label.setText("Couldn't create instance of class "
  109.                       + windowClass);
  110.             }
  111.         if (frameNumber == 1) {
  112.                 window.setTitle(windowText);
  113.         } else {
  114.                 window.setTitle(windowText + ": " + frameNumber);
  115.         }
  116.             frameNumber++;
  117.             window.pack();
  118.             window.show();
  119.             label.setText("");
  120.         }
  121.     }
  122.     }
  123.  
  124.     public void stop() {
  125.         windowThread.stop();
  126.         windowThread = null;
  127.     }
  128.         
  129.     public synchronized boolean action(Event event, Object what) {
  130.     if (event.target instanceof Button) {
  131.         //signal the window thread to build a window
  132.         label.setText("Please wait while the window comes up...");
  133.         pleaseCreate = true;
  134.         notify();
  135.     } 
  136.     return false;
  137.     }
  138. }
  139.  
  140. class TestWindow extends Frame {
  141.     public TestWindow() {
  142.     resize(300, 300);
  143.     }
  144. }
  145.