View Javadoc

1   /* -------------------------------------------------------------------
2    * Java source file for the class BranchGroupUtil
3    * 
4    * Copyright (c), 2003, Masahiro Takatsuka.
5    * All Rights Researved.
6    * 
7    * Original Author: Masahiro Takatsuka (masa@jbeans.net)
8    * $Author: takatsukam $
9    * 
10   * $Date: 2004/03/03 11:53:06 $
11   * 
12   * $Id: BranchGroupUtil.java,v 1.3 2004/03/03 11:53:06 takatsukam Exp $
13   * 
14   * Reference:		Document no:
15   * ___				___
16   * 
17   * To Do:
18   * ___
19   * 
20  ------------------------------------------------------------------- */
21  
22  /* --------------------------- Package ---------------------------- */
23  package net.jbeans.j3d.universe;
24  
25  /* ------------------ Import classes (packages) ------------------- *//package-summary/html">class="comment"> ------------------ Import classes (packages) ------------------- *//package-summary.html">class="comment">/* ------------------ Import classes (packages) ------------------- *//package-summary.html">class="comment"> ------------------ Import classes (packages) ------------------- */
26  import java.util.*;
27  import javax.media.j3d.*;
28  
29  import net.jbeans.util.debug.*;
30  
31  /*====================================================================
32                 Implementation of class BranchGroupUtil                
33  ====================================================================*/
34  /***
35   * BranchGroupUtil provides some utility methods for Locale
36   * and World.
37   * 
38   * @version $Revision: 1.3 $
39   * @author Masahiro Takatsuka (masa@jbeans.net)
40   */
41  
42  /*
43   * this class is ok to be package. no need to be serialized.
44   */
45  class BranchGroupUtil {
46  	private static final boolean DEBUG = Debug.getDebugFlag(BranchGroupUtil.class);
47  	
48  	private BranchGroupUtil() {
49  		super();
50  	}
51  	
52    	static final void setViewCapability(Node node) {
53  		if (node instanceof Group) {
54  			node.setCapability( Group.ALLOW_CHILDREN_READ );
55  			Enumeration enum = ((Group)node).getAllChildren();
56  			while (enum.hasMoreElements()) {
57  				setViewCapability( (Node)enum.nextElement() );
58  			}
59  			if (node instanceof Behavior) {
60  				node.setCapability( Behavior.ALLOW_BOUNDS_READ );
61  			}
62  		} else if (node instanceof Shape3D) {
63  			node.setCapability( Shape3D.ALLOW_GEOMETRY_READ );
64  		} else if (node instanceof Light) {
65  			node.setCapability( Light.ALLOW_INFLUENCING_BOUNDS_READ );
66  		} else if (node instanceof Fog) {
67  			node.setCapability( Fog.ALLOW_INFLUENCING_BOUNDS_READ );
68  		} else if (node instanceof Sound) {
69  			node.setCapability( Sound.ALLOW_SCHEDULING_BOUNDS_READ );
70  		} else {
71  			if (DEBUG) {
72  				System.out.println("BranchGroupUtil.setViewCapability() : " + node.getClass());
73  			}
74  		}
75      }
76  	
77  	/***
78  	 * Adds a new BranchGroup object.
79  	 */
80  	static final BranchGroup addBranchGroup(BranchGroupDetachable root,
81  											BranchGroup bg) {
82  		Vector detachBG = root.getDetachableBranchGroups();
83  		//int nextBranchGroup = root.getNextBranchGroup();
84  		
85  		setViewCapability(bg);
86  		
87  		/* newBG will be detached from newParentBG to allow capabilities to
88  		   be updated.
89  		   We need newParentBG so the ordering remains constant in the graph
90  		*/
91  		BranchGroup newBG = new net.jbeans.j3d.scenegraph.BranchGroup();
92  
93  //  		detachBG.add(nextBranchGroup, newBG);
94  //  		newBG.addChild(bg);
95  //  		bg.setUserData(newBG);	// bg need this to detach itself.
96  		
97  //  		root.setNextBranchGroup(nextBranchGroup++);
98  
99  		detachBG.add(newBG);
100 		newBG.addChild(bg);
101 		bg.setUserData(newBG);	// bg needs this to detach itself.
102 		
103 		return newBG;
104     }
105 
106 	/***
107 	 * Detaches the specified BranchGroup object.
108 	 */
109     static final void detachBranchGroup(BranchGroupDetachable root,
110 										BranchGroup subRoot) {
111 		boolean fail = true;
112 		Vector detachBG = root.getDetachableBranchGroups();
113 		
114         for (int i = 0; i < detachBG.size(); i++) {
115 			Group g = (Group) detachBG.get(i);
116 			if (g.getChild(0) == subRoot) {
117 				((BranchGroup) (((BranchGroup) g).getChild(0))).detach();
118 				((BranchGroup) g).detach();
119 				detachBG.remove(g);
120 				i--;
121 				fail = false;
122 			}
123 		}
124 
125 		if (fail) {
126 			if (DEBUG) {
127 				System.out.println("The specified BranchGroup was not in the scenegraph.");
128 			}
129 		}
130 	}
131 }
132