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