home *** CD-ROM | disk | FTP | other *** search
/ Late Night VRML 2.0 with Java CD-ROM / code.zip / Ch12 / ui / VrmlCheck.java < prev    next >
Text File  |  1997-01-04  |  12KB  |  471 lines

  1. // VRML Generator
  2. // (c) Justin Couch
  3. //
  4. // From Chapter 13: Late Night VRML 2.0 and Java
  5. //
  6. // A silent class that does the consistency checking for adding nodes in the
  7. // correct spot. If an incorrect node is attempted to be added then an
  8. // appropriate error message is posted.
  9.  
  10. package ui;
  11.  
  12. import java.awt.*;
  13. import vrml.external.Browser;
  14. import ui.dialogs.*;
  15. import VrmlScene;
  16. import VrmlTypes;
  17. import ui.VrmlTree;
  18. import ui.tree.VrmlTreeNode;
  19. import geometry.*;
  20. import exceptions.NoSelectedNodeException;
  21.  
  22. public class VrmlCheck
  23. {
  24.     private VrmlTree            screen_data;
  25.     private VrmlScene            vrml_data;
  26.     private VrmlBaseDialog[]    dialog_list;
  27.     private GeneralWarnDialog    gen_warn;
  28.  
  29.     public VrmlCheck(Browser b, Frame parent, VrmlTree screen, VrmlScene data)
  30.     {
  31.         screen_data = screen;
  32.         vrml_data = data;
  33.  
  34.         // create the warning dialog
  35.         gen_warn = new GeneralWarnDialog(parent);
  36.  
  37.         // now put the dialog list together.
  38.         createDialogs(parent);
  39.  
  40.         // the temporary hack until the static getBrowser method is
  41.         // properly implemented
  42.         for(int i = 0; i < VrmlTypes.NumberOfTypes; i ++)
  43.             if(dialog_list[i] != null)
  44.                 dialog_list[i].setBrowser(b);
  45.     }
  46.  
  47.     private void createDialogs(Frame parent)
  48.     {
  49.         dialog_list =  new VrmlBaseDialog[VrmlTypes.NumberOfTypes];
  50.  
  51.         dialog_list[VrmlTypes.Group] =
  52.                     new GroupDialog(parent, screen_data, vrml_data);
  53.         dialog_list[VrmlTypes.Transform] =
  54.                     new TransformDialog(parent, screen_data, vrml_data);
  55.  
  56.         dialog_list[VrmlTypes.Shape] =
  57.                     new ShapeDialog(parent, screen_data, vrml_data);
  58.         dialog_list[VrmlTypes.Box] =
  59.                     new BoxDialog(parent, screen_data, vrml_data);
  60.         dialog_list[VrmlTypes.Cone] =
  61.                     new ConeDialog(parent, screen_data, vrml_data);
  62.         dialog_list[VrmlTypes.Cylinder] =
  63.                     new CylinderDialog(parent, screen_data, vrml_data);
  64.         dialog_list[VrmlTypes.Sphere] =
  65.                     new SphereDialog(parent, screen_data, vrml_data);
  66.  
  67.         dialog_list[VrmlTypes.Appearance] =
  68.                     new AppearanceDialog(parent, screen_data, vrml_data);
  69.         dialog_list[VrmlTypes.Material] =
  70.                     new MaterialDialog(parent, screen_data, vrml_data);
  71.         dialog_list[VrmlTypes.TextureTransform] =
  72.                     new TextureTransformDialog(parent, screen_data, vrml_data);
  73.         dialog_list[VrmlTypes.ImageTexture] =
  74.                     new ImageTextureDialog(parent, screen_data, vrml_data);
  75.         dialog_list[VrmlTypes.MovieTexture] =
  76.                     new MovieTextureDialog(parent, screen_data, vrml_data);
  77.     }
  78.  
  79.     public void checkNode(int node_type)
  80.     {
  81.         VrmlObject    parent;
  82.  
  83.         try
  84.         {
  85.             parent  = screen_data.getVrmlParent();
  86.         }
  87.         catch(NoSelectedNodeException e)
  88.         {
  89.             gen_warn.show("You have not selected a node to add to");
  90.             return;
  91.         }
  92.  
  93.         if(node_type <= VrmlTypes.Switch)
  94.             checkGroupingNode(node_type, parent);
  95.         else if(node_type <= VrmlTypes.WorldInfo)
  96.             checkCommonNode(node_type, parent);
  97.         else if(node_type <= VrmlTypes.VisibilitySensor)
  98.             checkSensorNode(node_type, parent);
  99.         else if(node_type <= VrmlTypes.Text)
  100.             checkGeometryNode(node_type, parent);
  101.         else if(node_type <= VrmlTypes.TextureCoordinate)
  102.             checkGeoPropertyNode(node_type, parent);
  103.         else if(node_type <= VrmlTypes.TextureTransform)
  104.             checkAppearanceNode(node_type, parent);
  105.         else if(node_type <= VrmlTypes.ScalarInterpolator)
  106.             checkInterpolatorNode(node_type, parent);
  107.         else
  108.             checkBindableNode(node_type, parent);
  109.     }
  110.  
  111.     private void checkGroupingNode(int node_type, VrmlObject parent)
  112.     {
  113.         String            node_name = null;
  114.  
  115.         if(!(parent instanceof GroupingNode) && !(parent == null))
  116.         {
  117.             gen_warn.show("Need a grouping node to add a group to");
  118.             return;
  119.         }
  120.  
  121.         switch(node_type)
  122.         {
  123.             // Grouping Nodes
  124.             case VrmlTypes.Anchor:
  125.                         node_name = "Anchor";
  126.                         break;
  127.             case VrmlTypes.BillBoard:
  128.                         node_name = "Billboard";
  129.                         break;
  130.             case VrmlTypes.Collision:
  131.                         node_name = "Collision";
  132.                         break;
  133.             case VrmlTypes.Group:
  134.                         dialog_list[VrmlTypes.Group].show();
  135.                         break;
  136.             case VrmlTypes.Transform:
  137.                         dialog_list[VrmlTypes.Transform].show();
  138.                         break;
  139.  
  140.             // Special Groups
  141.             case VrmlTypes.Inline:
  142.                         node_name = "Inline";
  143.                         break;
  144.             case VrmlTypes.LOD:
  145.                         node_name = "LOD";
  146.                         break;
  147.             case VrmlTypes.Switch:
  148.                         node_name = "Switch";
  149.                         break;
  150.             default:
  151.         }
  152.     }
  153.  
  154.     private void checkCommonNode(int node_type, VrmlObject parent)
  155.     {
  156.         if(!(parent instanceof GroupingNode) && !(parent == null))
  157.         {
  158.             gen_warn.show("Need a grouping node to add to");
  159.             return;
  160.         }
  161.  
  162.         switch(node_type)
  163.         {
  164.             // Common Nodes
  165.             case VrmlTypes.AudioClip:
  166.                         break;
  167.             case VrmlTypes.DirectionalLight:
  168.                         break;
  169.             case VrmlTypes.PointLight:
  170.                         break;
  171.             case VrmlTypes.Script:
  172.                         break;
  173.             case VrmlTypes.Shape:
  174.                         dialog_list[VrmlTypes.Shape].show();
  175.                         break;
  176.             case VrmlTypes.Sound:
  177.                         break;
  178.             case VrmlTypes.SpotLight:
  179.                         break;
  180.             case VrmlTypes.WorldInfo:
  181.                         break;
  182.             default:
  183.         }
  184.     }
  185.  
  186.     private void checkSensorNode(int node_type, VrmlObject parent)
  187.     {
  188.         String node_name = null;
  189.  
  190.         if(!(parent instanceof GroupingNode))
  191.         {
  192.             gen_warn.show("Need a grouping node to add a Sensor to");
  193.             return;
  194.         }
  195.         switch(node_type)
  196.         {
  197.             case VrmlTypes.CylinderSensor:
  198.                         node_name = "CylinderSensor";
  199.                         break;
  200.             case VrmlTypes.PlaneSensor:
  201.                         node_name = "PlaneSensor";
  202.                         break;
  203.             case VrmlTypes.ProximitySensor:
  204.                         node_name = "ProximitySensor";
  205.                         break;
  206.             case VrmlTypes.SphereSensor:
  207.                         node_name = "SphereSensor";
  208.                         break;
  209.             case VrmlTypes.TimeSensor:
  210.                         node_name = "TimeSensor";
  211.                         break;
  212.             case VrmlTypes.TouchSensor:
  213.                         node_name = "TouchSensor";
  214.                         break;
  215.             case VrmlTypes.VisibilitySensor:
  216.                         node_name = "VisibilitySensor";
  217.                         break;
  218.             default:
  219.         }
  220.     }
  221.  
  222.     private void checkGeometryNode(int node_type, VrmlObject parent)
  223.     {
  224.         String node_name = null;
  225.  
  226.         // this is not a correct test for the geometric properties below.
  227.         // need to fix this.
  228.         if(!(parent instanceof Shape))
  229.         {
  230.             gen_warn.show("You need a Shape for the parent of geometry nodes");
  231.             return;
  232.         }
  233.  
  234.         // now that we have established that the parent is a shape we need to
  235.         // check if it already has geometry set
  236.         if(((Shape)parent).hasGeometry())
  237.         {
  238.             gen_warn.show("This Shape already has a geometry node allocated");
  239.             return;
  240.         }
  241.  
  242.         switch(node_type)
  243.         {
  244.             // Geometry
  245.             case VrmlTypes.Box:
  246.                         dialog_list[VrmlTypes.Box].show();
  247.                         break;
  248.             case VrmlTypes.Cone:
  249.                         dialog_list[VrmlTypes.Cone].show();
  250.                         break;
  251.             case VrmlTypes.Cylinder:
  252.                         dialog_list[VrmlTypes.Cylinder].show();
  253.                         break;
  254.             case VrmlTypes.ElevationGrid:
  255.                         node_name = "ElevationGrid";
  256.                         break;
  257.             case VrmlTypes.Extrusion:
  258.                         node_name = "Extrusion";
  259.                         break;
  260.             case VrmlTypes.IndexedFaceSet:
  261.                         node_name = "IndexedFaceSet";
  262.                         break;
  263.             case VrmlTypes.IndexedLineSet:
  264.                         node_name = "IndexedLineSet";
  265.                         break;
  266.             case VrmlTypes.PointSet:
  267.                         node_name = "PointSet";
  268.                         break;
  269.             case VrmlTypes.Sphere:
  270.                         dialog_list[VrmlTypes.Sphere].show();
  271.                         break;
  272.             case VrmlTypes.Text:
  273.                         node_name = "Text";
  274.                         break;
  275.  
  276.             default:
  277.         }
  278.     }
  279.  
  280.     private void checkGeoPropertyNode(int node_type, VrmlObject parent)
  281.     {
  282.         String node_name = null;
  283.  
  284.         // this is not a correct test for the geometric properties below.
  285.         // need to fix this.
  286.         if(!(parent instanceof Geometry))
  287.         {
  288.             System.out.println("Error not a piece of geometry for a parent");
  289.             return;
  290.         }
  291.  
  292.         switch(node_type)
  293.         {
  294.             // Geometric Properties
  295.             case VrmlTypes.Color:
  296.                         break;
  297.             case VrmlTypes.Coordinate:
  298.                         break;
  299.             case VrmlTypes.Normal:
  300.                         break;
  301.             case VrmlTypes.TextureCoordinate:
  302.                         break;
  303.             default:
  304.         }
  305.     }
  306.  
  307.     private void checkAppearanceNode(int node_type, VrmlObject parent)
  308.     {
  309.         String node_name = null;
  310.  
  311.         switch(node_type)
  312.         {
  313.             // Appearance
  314.             case VrmlTypes.Appearance:
  315.                     if(!(parent instanceof Shape))
  316.                     {
  317.                         gen_warn.show("Error not a Shape for a parent");
  318.                         return;
  319.                     }
  320.                     if(((Shape)parent).hasAppearance())
  321.                     {
  322.                         gen_warn.show("This Shape already has an Appearance");
  323.                         return;
  324.                     }
  325.                     dialog_list[VrmlTypes.Appearance].show();
  326.                     break;
  327.             case VrmlTypes.FontStyle:
  328.                     break;
  329.             case VrmlTypes.ImageTexture:
  330.                     if(!(parent instanceof Appearance))
  331.                     {
  332.                         gen_warn.show("You need an Appearance for a parent");
  333.                         return;
  334.                     }
  335.                     if(((Appearance)parent).hasTexture())
  336.                     {
  337.                         gen_warn.show("This Shape already has a Texture");
  338.                         return;
  339.                     }
  340.                     dialog_list[VrmlTypes.ImageTexture].show();
  341.                     break;
  342.             case VrmlTypes.Material:
  343.                     if(!(parent instanceof Appearance))
  344.                     {
  345.                         gen_warn.show("You need an Appearance for a parent");
  346.                         return;
  347.                     }
  348.                     if(((Appearance)parent).hasMaterial())
  349.                     {
  350.                         gen_warn.show("This Shape already has a Material");
  351.                         return;
  352.                     }
  353.                     dialog_list[VrmlTypes.Material].show();
  354.                     break;
  355.             case VrmlTypes.MovieTexture:
  356.                     if(!(parent instanceof Appearance))
  357.                     {
  358.                         gen_warn.show("You need an Appearance for a parent");
  359.                         return;
  360.                     }
  361.                     if(((Appearance)parent).hasTexture())
  362.                     {
  363.                         gen_warn.show("This Shape already has a Texture");
  364.                         return;
  365.                     }
  366.                     dialog_list[VrmlTypes.MovieTexture].show();
  367.                     break;
  368.             case VrmlTypes.PixelTexture:
  369.                     if(!(parent instanceof Appearance))
  370.                     {
  371.                         gen_warn.show("You need an Appearance for a parent");
  372.                         return;
  373.                     }
  374.                     if(((Appearance)parent).hasTexture())
  375.                     {
  376.                         gen_warn.show("This Shape already has a Texture");
  377.                         return;
  378.                     }
  379.                     break;
  380.             case VrmlTypes.TextureTransform:
  381.                     if(!(parent instanceof Appearance))
  382.                     {
  383.                         System.out.println("You need an Appearance for a parent");
  384.                         return;
  385.                     }
  386.                     if(((Appearance)parent).hasTextureTransform())
  387.                     {
  388.                         gen_warn.show("This Appearance already has a TextureTransform");
  389.                         return;
  390.                     }
  391.                     dialog_list[VrmlTypes.TextureTransform].show();
  392.                     break;
  393.             default:
  394.         }
  395.     }
  396.  
  397.     private void checkInterpolatorNode(int node_type, VrmlObject parent)
  398.     {
  399.         String node_name = null;
  400.  
  401.         switch(node_type)
  402.         {
  403.             case VrmlTypes.ColorInterpolator:
  404.                         node_name = "ColorInterpolator";
  405.                         break;
  406.             case VrmlTypes.CoordinateInterpolator:
  407.                         node_name = "CoordinateInterpolator";
  408.                         break;
  409.             case VrmlTypes.NormalInterpolator:
  410.                         node_name = "NormalInterpolator";
  411.                         break;
  412.             case VrmlTypes.OrientationInterpolator:
  413.                         node_name = "OrientationInterpolator";
  414.                         break;
  415.             case VrmlTypes.PositionInterpolator:
  416.                         node_name = "PositionInterpolator";
  417.                         break;
  418.             case VrmlTypes.ScalarInterpolator:
  419.                         node_name = "ScalarInterpolator";
  420.                         break;
  421.             default:
  422.         }
  423.     }
  424.  
  425.     private void checkBindableNode(int node_type, VrmlObject parent)
  426.     {
  427.         String node_name = null;
  428.  
  429.         switch(node_type)
  430.         {
  431.             // Bindable nodes
  432.             case VrmlTypes.BackGround:
  433.                         node_name = "BackGround";
  434.                         break;
  435.             case VrmlTypes.Fog:
  436.                         node_name = "Fog";
  437.                         break;
  438.             case VrmlTypes.NavigationInfo:
  439.                         node_name = "NavigationInfo";
  440.                         break;
  441.             case VrmlTypes.Viewpoint:
  442.                         node_name = "Viewpoint";
  443.                         break;
  444.             default:
  445.         }
  446.     }
  447.  
  448.     public void deleteNode()
  449.     {
  450.         VrmlTreeNode current = screen_data.selected;
  451.         VrmlTreeNode parent = screen_data.getParent();
  452.  
  453.         // a null parent indicates the root node. Inform the user of this.
  454.         // at the moment there is no backing out from this operation. This
  455.         // needs to be done.
  456.  
  457.         if(parent == null)
  458.             gen_warn.show("This operation is about to delete the whole scene!");
  459.  
  460.         screen_data.removeNode(parent);
  461.  
  462.         if(parent == null)
  463.             vrml_data.deleteAll();
  464.         else
  465.             vrml_data.deleteNode(parent.type,
  466.                                  parent.vrmlNode,
  467.                                  current.type,
  468.                                  current.vrmlNode);
  469.     }
  470. }
  471.