home *** CD-ROM | disk | FTP | other *** search
/ Late Night VRML 2.0 with Java CD-ROM / code.zip / Ch12 / geometry / VrmlRoot.java < prev   
Text File  |  1997-01-05  |  4KB  |  153 lines

  1. // VRML Generator
  2. // Copyright Justin Couch 1996
  3. //
  4. // Chapter 13: Late Night VRML 2.0 and Java
  5. //
  6. // The class that represents the root of the scene graph.
  7.  
  8. package geometry;
  9.  
  10. import java.io.*;
  11. import java.awt.*;
  12. import java.util.Vector;
  13. import java.util.Enumeration;
  14. import vrml.external.*;
  15. import vrml.external.field.*;
  16. import vrml.external.exception.*;
  17. import geometry.*;
  18.  
  19. public class VrmlRoot extends GroupingNode
  20. {
  21.     // constructor builds a copy of itself and is added to the scene
  22.     // graph.
  23.     public VrmlRoot()
  24.     {
  25.         Browser b = Browser.getBrowser();
  26.  
  27.         if(b != null)
  28.         {
  29.             try
  30.             {
  31.                 // loop until we can access the scene root. This should not take
  32.                 // too long since we already have access to the main browser.
  33.                 while(node == null)
  34.                     node = b.getNode("root");
  35.  
  36.                 System.out.println("Scene root obtained from the VRML browser");
  37.             }
  38.             catch(InvalidNodeException e)
  39.             {
  40.                 System.out.println("Unable to obtain the root node from the VRML world");
  41.             }
  42.             _addChildren = (EventInMFNode)node.getEventIn("addChilren");
  43.             _removeChildren = (EventInMFNode)node.getEventIn("removeChildren");
  44.             _set_children = (EventInMFNode)node.getEventIn("children");
  45.  
  46.             have_browser = true;
  47.         }
  48.         _children = new Vector();
  49.     }
  50.  
  51.     public VrmlRoot(Browser b)
  52.     {
  53.         if(b != null)
  54.         {
  55.             try
  56.             {
  57.                 // loop until we can access the scene root. This should not take
  58.                 // too long since we already have access to the main browser.
  59.                 while(node == null)
  60.                     node = b.getNode("root");
  61.  
  62.                 System.out.println("Scene root obtained from the VRML browser");
  63.             }
  64.             catch(InvalidNodeException e)
  65.             {
  66.                 System.out.println("Unable to obtain the root node from the VRML world");
  67.             }
  68.  
  69.             _addChildren = (EventInMFNode)node.getEventIn("addChildren");
  70.             _removeChildren = (EventInMFNode)node.getEventIn("removeChildren");
  71.             _set_children = (EventInMFNode)node.getEventIn("children");
  72.  
  73.             have_browser = true;
  74.         }
  75.         _children = new Vector();
  76.     }
  77.  
  78.     public void finalize()
  79.     {
  80.         // force the clean up of internally allocated objects
  81.         _children = null;
  82.     }
  83.  
  84.     public void writeToFile(PrintStream fp)
  85.     {
  86.         Enumeration e = _children.elements();
  87.  
  88.         fp.println("#VRML V2.0 utf8");
  89.         fp.println("#");
  90.         fp.println("# Generated by the VermelGen");
  91.         fp.println("# Software by The Virtual Light Company");
  92.         fp.println("# Released under the GPL ftp://ftp.sunsite.edu/pub/GNU/copyinfo.txt");
  93.         fp.println("");
  94.  
  95.         for(; e.hasMoreElements();)
  96.             ((VrmlObject)(e.nextElement())).writeToFile(fp, 0);
  97.     }
  98.  
  99.     // this needs to be here to satisfy the abstract class implementation. Just
  100.     // provides a safety buffer in case someone, somehow calls this version
  101.     // rather than the one above without the indent.
  102.     public void writeToFile(PrintStream fp, int indent)
  103.     {
  104.         writeToFile(fp);
  105.     }
  106.  
  107.     public void addChildren(VrmlObject child)
  108.     {
  109.         Node[] n = new Node[1];
  110.         n[0] = child.node;
  111.  
  112.         _children.addElement(child);
  113.         if(have_browser)
  114.             _addChildren.setValue(n);
  115.     }
  116.  
  117.     public void removeChildren(VrmlObject child)
  118.     {
  119.         Node[] n = new Node[1];
  120.         n[0] = child.node;
  121.  
  122.         _children.removeElement(child);
  123.         if(have_browser)
  124.             _addChildren.setValue(n);
  125.     }
  126.  
  127.     public void set_children(VrmlObject[] child_list)
  128.     {
  129.         int    i;
  130.         int num = child_list.length;
  131.         Node[]     node_list;
  132.  
  133.         // replace the current _children list with the new list.
  134.         // force the garbage collection
  135.         _children = null;
  136.  
  137.         // assign the new arrays.
  138.         _children = new Vector(num);
  139.         node_list = new Node[num];
  140.  
  141.         // copy the values across.
  142.         for(i = 0; i < num; i++)
  143.         {
  144.             _children.addElement(child_list[i]);
  145.             node_list[i] = child_list[i].node;
  146.         }
  147.  
  148.         // set the values in the VRML scene
  149.         if(have_browser)
  150.             _set_children.setValue(node_list);
  151.     }
  152. }
  153.