home *** CD-ROM | disk | FTP | other *** search
- // VRML Generator
- // (c) Justin Couch 1996
- //
- // From Chapter 13 Late Night VRML 2.0 and Java
- //
- // The basic applet class for the VRML generator
- // At the moment this also runs itself as a thread Eventually I would like to
- // move all of the code out to another class and start that class as a separate
- // thread.
-
- import java.applet.*;
- import java.awt.*;
- import java.net.*;
- import java.lang.*;
- import netscape.javascript.JSObject;
- import vrml.external.Browser;
- import ui.EditWindow;
- import ui.OutputWindow;
- import ui.ImageButton;
-
- public class VrmlGenerator extends Applet implements Runnable
- {
- public Thread timeout;
- private static final int NUM_RETRIES = 10;
-
- private VrmlScene Scene;
- private EditWindow VrmlWin;
- private OutputWindow VrmlOut;
- private String CodeBase;
-
- public VrmlGenerator()
- {
- }
-
- public void init()
- {
- //CodeBase = this.getCodeBase();
- CodeBase = getParameter("top_directory");
-
- // create the button bar on the bottom
- setLayout(new GridLayout(1,6));
-
- add(new Button("Show Editor"));
- add(new Button("Write VRML"));
- add(new Button("Show Output"));
-
- VrmlOut = new OutputWindow();
- VrmlWin = new EditWindow(CodeBase);
- }
-
- public void start()
- {
- timeout = new Thread(this);
- timeout.start();
- }
-
- public void stop()
- {
- timeout.stop();
- }
-
- // now that the applet is ready,
- // hopefully the browser is ready to go as well.
- public void run()
- {
- int i;
- Browser browser = null;
-
- System.out.println("Attempting to get Browser handle");
-
- // first try the proper method. If this returns null , try the old
- // javascript method
- browser = Browser.getBrowser();
-
- if(browser == null)
- {
- System.out.println("static getBrowser() call failed, attempting Javascript method");
-
- JSObject win = JSObject.getWindow(this);
-
- if(win != null)
- {
- JSObject doc = (JSObject)win.getMember("document");
- JSObject embeds = (JSObject)doc.getMember("embeds");
-
- // wait until we can get a connection to the VRML browser.
- // If we cannot get one on the current attempt then go to sleep for one
- // second and then try again.
- for(i = 0; i < NUM_RETRIES; i++)
- {
- browser = (Browser)embeds.getSlot(0);
- if(browser != null)
- {
- System.out.println("Browser handle is available");
- break;
- }
- else
- try
- {
- Thread.sleep(500);
- }
- catch(InterruptedException e) {}
- }
- }
- }
-
- // if after all this and the browser is still null - stuff'em
- if(browser == null)
- System.out.println("Browser not available");
-
- Scene = new VrmlScene(VrmlOut, browser);
-
- VrmlWin.show(Scene, browser);
- }
-
- public boolean action(Event e, Object arg)
- {
- if("Show Editor".equals(arg))
- VrmlWin.show();
- else if("Write VRML".equals(arg))
- {
- Scene.writeToFile();
- VrmlOut.show();
- }
- else if("Show Output".equals(arg))
- VrmlOut.show();
- else
- return false;
-
- return true;
- }
- }
-
-