001: /*
002: * $RCSfile: TreePrinter.java,v $
003: *
004: * @(#)TreePrinter.java 1.15 98/11/05 20:35:29
005: *
006: * Copyright (c) 1996-1998 Sun Microsystems, Inc. All Rights Reserved.
007: *
008: * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
009: * modify and redistribute this software in source and binary code form,
010: * provided that i) this copyright notice and license appear on all copies of
011: * the software; and ii) Licensee does not utilize the software in a manner
012: * which is disparaging to Sun.
013: *
014: * This software is provided "AS IS," without a warranty of any kind. ALL
015: * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
016: * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
017: * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
018: * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
019: * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
020: * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
021: * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
022: * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
023: * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
024: * POSSIBILITY OF SUCH DAMAGES.
025: *
026: * This software is not designed or intended for use in on-line control of
027: * aircraft, air traffic, aircraft navigation or aircraft communications; or in
028: * the design, construction, operation or maintenance of any nuclear
029: * facility. Licensee represents and warrants that it will not use or
030: * redistribute the Software for such purposes.
031: *
032: * $Revision: 1.2 $
033: * $Date: 2005/02/03 23:07:03 $
034: * $State: Exp $
035: */
036: /*
037: * @Author: Doug Gehringer
038: * @Author: Rick Goldberg
039: *
040: */
041: package org.jdesktop.j3d.loaders.vrml97.impl;
042:
043: import java.io.PrintStream;
044: import java.util.Enumeration;
045: import java.util.HashSet;
046: import java.util.Iterator;
047: import java.util.Set;
048:
049: import javax.media.j3d.*;
050:
051: /** Description of the Class */
052: public class TreePrinter {
053: PrintStream printStream;
054: String j3dPkg = new String("javax.media.j3d.");
055: String v97Pkg = new String("org.jdesktop.j3d.loaders.vrml97.impl.");
056:
057: /**
058: * Description of the Method
059: *
060: *@param s Description of the Parameter
061: *@param l Description of the Parameter
062: */
063: public void print(PrintStream s, Locale l) {
064: printStream = s;
065: HashSet sharedGroups = new HashSet();
066: printTree(l, 0, sharedGroups);
067: Iterator iterator = sharedGroups.iterator();
068: while (iterator.hasNext()) {
069: SharedGroup sg = (SharedGroup) iterator.next();
070: print(s, sg);
071: }
072: }
073:
074: /**
075: * Description of the Method
076: *
077: *@param l Description of the Parameter
078: */
079: public void print(Locale l) {
080: print(System.out, l);
081: }
082:
083: /**
084: * Description of the Method
085: *
086: *@param l Description of the Parameter
087: *@param graphDepth Description of the Parameter
088: *@param sharedGroups Description of the Parameter
089: */
090: private void printTree(Locale l, int graphDepth, Set sharedGroups) {
091: printNode(l, 0, sharedGroups);
092: try {
093: Enumeration e = l.getAllBranchGraphs();
094: while (e.hasMoreElements()) {
095: Object o = e.nextElement();
096: if (o instanceof Locale) {
097: printTree((Locale) o, graphDepth + 1, sharedGroups);
098: } else if (o instanceof SceneGraphObject) {
099: printTree((SceneGraphObject) o, graphDepth + 1,
100: sharedGroups);
101: } else {
102: printStream.println(o + " unknown and in tree");
103: }
104: }
105: } catch (CapabilityNotSetException e) {
106: printStream.println("No capability to read children");
107: }
108: }
109:
110: /**
111: * Description of the Method
112: *
113: *@param s Description of the Parameter
114: *@param sgo Description of the Parameter
115: */
116: public void print(PrintStream s, SceneGraphObject sgo) {
117: printStream = s;
118: HashSet sharedGroups = new HashSet();
119: printTree(sgo, 0, sharedGroups);
120: Iterator iterator = sharedGroups.iterator();
121: while (iterator.hasNext()) {
122: SharedGroup sg = (SharedGroup) iterator.next();
123: print(s, sg);
124: }
125: }
126:
127: /**
128: * Description of the Method
129: *
130: *@param sgo Description of the Parameter
131: */
132: public void print(SceneGraphObject sgo) {
133: print(System.out, sgo);
134: }
135:
136: /**
137: * Description of the Method
138: *
139: *@param sgo Description of the Parameter
140: *@param graphDepth Description of the Parameter
141: *@param sharedGroups Description of the Parameter
142: */
143: private void printTree(SceneGraphObject sgo, int graphDepth,
144: Set sharedGroups) {
145:
146: printNode(sgo, graphDepth, sharedGroups);
147: if (sgo instanceof javax.media.j3d.Group) {
148: try {
149: Enumeration e = ((javax.media.j3d.Group) sgo)
150: .getAllChildren();
151: while (e.hasMoreElements()) {
152: printTree((SceneGraphObject) (e.nextElement()),
153: graphDepth + 1, sharedGroups);
154: }
155: } catch (CapabilityNotSetException e) {
156: // Can't read handled below
157: }
158: }
159: }
160:
161: /**
162: * Description of the Method
163: *
164: *@param o Description of the Parameter
165: *@return Description of the Return Value
166: */
167: private String nodeString(Object o) {
168: String objString = o.toString();
169: if (objString.startsWith(j3dPkg)) {
170: objString = objString.substring(j3dPkg.length());
171: }
172: if (objString.startsWith(v97Pkg)) {
173: objString = objString.substring(v97Pkg.length());
174: }
175: return objString;
176: }
177:
178: /**
179: * Description of the Method
180: *
181: *@param o Description of the Parameter
182: *@param indent Description of the Parameter
183: *@param sharedGroups Description of the Parameter
184: */
185: private void printNode(Object o, int indent, Set sharedGroups) {
186: for (int i = 0; i < indent; i++) {
187: printStream.print(">");
188: }
189: printStream.print(nodeString(o) + ": ");
190: if (o instanceof SceneGraphObject) {
191: SceneGraphObject sgo = (SceneGraphObject) o;
192: int capBits = 0;
193: // TODO: how to make sure we always check all the valid bits?
194: for (int i = 0; i < 64; i++) {
195: if (sgo.getCapability(i)) {
196: capBits |= 1 << i;
197: }
198: }
199: printStream.print("capBits:Ox"
200: + Integer.toHexString(capBits));
201: if (o instanceof javax.media.j3d.Group) {
202: javax.media.j3d.Group g = (javax.media.j3d.Group) o;
203: int numChildren = 0;
204: try {
205: numChildren = g.numChildren();
206: } catch (CapabilityNotSetException e) {
207: //anyone who is using treePrinter, is debugging, so it is
208: //alright to blindly allow read. you should first detach
209: //browser.curScene, print the tree, then add it back to
210: //browser.locale when finished.
211: g
212: .setCapability(javax.media.j3d.Group.ALLOW_CHILDREN_READ);
213: numChildren = g.numChildren();
214: //System.out.println("Can't read children on group");
215: //return;
216: }
217: printStream.print(" children:" + numChildren);
218: if (o instanceof TransformGroup) {
219: Transform3D transform = new Transform3D();
220: Transform3D identity = new Transform3D();
221: TransformGroup t = (TransformGroup) o;
222: t.getTransform(transform);
223: // TODO: use getBestType() when implemented
224: if (transform.equals(identity)) {
225: printStream.print(" xform:IDENTITY ");
226: } else {
227: printStream.print(" xform:NON-IDENTITY ");
228: }
229: }
230: } else if (o instanceof Link) {
231: Link l = (Link) o;
232: SharedGroup sg = l.getSharedGroup();
233: printStream.print(" sg:" + nodeString(sg));
234: sharedGroups.add(sg);
235: } else {
236: printStream.print(": leaf");
237: }
238: }
239: printStream.println();
240: }
241: }
|