home *** CD-ROM | disk | FTP | other *** search
/ Late Night VRML 2.0 with Java CD-ROM / code.zip / Ch12 / VrmlGenerator.java < prev    next >
Text File  |  1997-01-05  |  3KB  |  134 lines

  1. // VRML Generator
  2. // (c) Justin Couch 1996
  3. //
  4. // From Chapter 13 Late Night VRML 2.0 and Java
  5. //
  6. // The basic applet class for the VRML generator
  7. // At the moment this also runs itself as a thread Eventually I would like to
  8. // move all of the code out to another class and start that class as a separate
  9. // thread.
  10.  
  11. import java.applet.*;
  12. import java.awt.*;
  13. import java.net.*;
  14. import java.lang.*;
  15. import netscape.javascript.JSObject;
  16. import vrml.external.Browser;
  17. import ui.EditWindow;
  18. import ui.OutputWindow;
  19. import ui.ImageButton;
  20.  
  21. public class VrmlGenerator extends Applet implements Runnable
  22. {
  23.     public Thread    timeout;
  24.     private static final int NUM_RETRIES = 10;
  25.  
  26.     private    VrmlScene    Scene;
  27.     private EditWindow    VrmlWin;
  28.     private OutputWindow    VrmlOut;
  29.     private String        CodeBase;
  30.  
  31.     public VrmlGenerator()
  32.     {
  33.     }
  34.  
  35.     public void init()
  36.     {
  37.         //CodeBase = this.getCodeBase();
  38.         CodeBase = getParameter("top_directory");
  39.  
  40.         // create the button bar on the bottom
  41.         setLayout(new GridLayout(1,6));
  42.  
  43.         add(new Button("Show Editor"));
  44.         add(new Button("Write VRML"));
  45.         add(new Button("Show Output"));
  46.  
  47.         VrmlOut = new OutputWindow();
  48.         VrmlWin = new EditWindow(CodeBase);
  49.     }
  50.  
  51.     public void start()
  52.     {
  53.         timeout = new Thread(this);
  54.         timeout.start();
  55.     }
  56.  
  57.     public void stop()
  58.     {
  59.         timeout.stop();
  60.     }
  61.  
  62.     // now that the applet is ready,
  63.     // hopefully the browser is ready to go as well.
  64.     public void run()
  65.     {
  66.         int    i;
  67.         Browser    browser = null;
  68.  
  69.         System.out.println("Attempting to get Browser handle");
  70.  
  71.         // first try the proper method. If this returns null , try the old
  72.         // javascript method
  73.         browser = Browser.getBrowser();
  74.  
  75.         if(browser == null)
  76.         {
  77.             System.out.println("static getBrowser() call failed, attempting Javascript method");
  78.  
  79.             JSObject win    = JSObject.getWindow(this);
  80.  
  81.             if(win != null)
  82.             {
  83.                 JSObject doc    = (JSObject)win.getMember("document");
  84.                 JSObject embeds = (JSObject)doc.getMember("embeds");
  85.  
  86.                 // wait until we can get a connection to the VRML browser.
  87.                 // If we cannot get one on the current attempt then go to sleep for one
  88.                 // second and then try again.
  89.                 for(i = 0; i < NUM_RETRIES; i++)
  90.                 {
  91.                     browser = (Browser)embeds.getSlot(0);
  92.                     if(browser != null)
  93.                     {
  94.                         System.out.println("Browser handle is available");
  95.                         break;
  96.                     }
  97.                     else
  98.                         try
  99.                         {
  100.                             Thread.sleep(500);
  101.                         }
  102.                         catch(InterruptedException e) {}
  103.                 }
  104.             }
  105.         }
  106.  
  107.         // if after all this and the browser is still null - stuff'em
  108.         if(browser == null)
  109.                 System.out.println("Browser not available");
  110.  
  111.         Scene = new VrmlScene(VrmlOut, browser);
  112.  
  113.         VrmlWin.show(Scene, browser);
  114.     }
  115.  
  116.     public boolean action(Event e, Object arg)
  117.     {
  118.         if("Show Editor".equals(arg))
  119.             VrmlWin.show();
  120.         else if("Write VRML".equals(arg))
  121.         {
  122.             Scene.writeToFile();
  123.             VrmlOut.show();
  124.         }
  125.         else if("Show Output".equals(arg))
  126.             VrmlOut.show();
  127.         else
  128.             return false;
  129.  
  130.         return true;
  131.     }
  132. }
  133.  
  134.