001: /*
002: * $Header: /cvs/j3dfly/J3dEditor/src/org/jdesktop/j3dedit/scenegrapheditor/sourcecontrol/Traverser.java,v 1.1 2005/04/20 22:21:21 paulby Exp $
003: *
004: * Sun Public License Notice
005: *
006: * The contents of this file are subject to the Sun Public License Version
007: * 1.0 (the "License"). You may not use this file except in compliance with
008: * the License. A copy of the License is available at http://www.sun.com/
009: *
010: * The Original Code is the Java 3D(tm) Scene Graph Editor.
011: * The Initial Developer of the Original Code is Jan Becicka.
012: * Portions created by Jan Becicka are Copyright (C) 2002.
013: * All Rights Reserved.
014: *
015: * Contributor(s): Jan Becicka.
016: *
017: **/
018: package org.jdesktop.j3dedit.scenegrapheditor.sourcecontrol;
019:
020: import org.jdesktop.j3dedit.scenegraph.SGObject;
021: import java.util.ArrayList;
022: import java.util.LinkedList;
023: import java.util.List;
024: import java.util.Iterator;
025: import java.lang.StringBuffer;
026: import java.lang.reflect.Constructor;
027: import java.lang.reflect.InvocationTargetException;
028: import org.jdesktop.j3dedit.J3dEditContext;
029: import java.util.Vector;
030: import java.util.Enumeration;
031: import org.jdesktop.j3dedit.scenegraph.SGLocale;
032: import org.jdesktop.j3dedit.scenegrapheditor.nodeeditors.panels.TextureExtraData;
033: import org.jdesktop.j3dedit.scenegraph.SGTexture;
034: import org.jdesktop.j3dedit.scenegraph.SGGroup;
035:
036: /** Traverser for Scene Graph
037: * @author Jan Becicka
038: * @version 0.1
039: */
040: public class Traverser {
041:
042: /** Holds value of property root. */
043: private SGObject root;
044:
045: /** Holds value of property namePool. */
046: private NamePool namePool;
047:
048: /** Holds value of property fields. */
049: private ArrayList fields;
050:
051: /** Holds value of property constructionCodeLines. */
052: private LinkedList constructionCodeLines;
053:
054: /** Holds value of property customizationCodeLines. */
055: private LinkedList customizationCodeLines;
056:
057: /** Holds value of property needToProcess. */
058: private Vector needToProcess;
059:
060: private Object currentBean;
061:
062: private J3dEditContext context;
063:
064: /** Creates new Traverser
065: * @param root */
066: public Traverser(J3dEditContext context, SGObject root) {
067: // TODO We can get context from root.getContext() so it should
068: // be removed from the constructor
069: this (context, root, new NamePool());
070: }
071:
072: public Traverser(J3dEditContext context, SGObject root, NamePool np) {
073: // TODO We can get context from root.getContext() so it should
074: // be removed from the constructor
075: this .context = context;
076: setRoot(root);
077: namePool = np;
078: needToProcess = new Vector();
079: fields = new ArrayList();
080: constructionCodeLines = new LinkedList();
081: customizationCodeLines = new LinkedList();
082: putNames();
083: }
084:
085: /** Put names from SGObject tree into NamePool
086: */
087: private void putNames() {
088: SGObject currentNode;
089: LinkedList l = new LinkedList();
090: l.add(root);
091: while (!l.isEmpty()) {
092: currentNode = (SGObject) l.getFirst();
093: l.removeFirst();
094: if (currentNode.getNodeName() != null) {
095: namePool.put(currentNode.getJ3dSceneGraphObject(),
096: currentNode.getNodeName());
097: }
098: if (currentNode instanceof SGTexture) {
099: namePool.put(currentNode.getJ3dSceneGraphObject(),
100: ((TextureExtraData) currentNode
101: .getSGObjectExtraData()).getImageURL()
102: .toString());
103: }
104: if (currentNode instanceof SGGroup) {
105: for (int i = 0; i < ((SGGroup) currentNode)
106: .numChildren(); i++) {
107: l.add(((SGGroup) currentNode).getChild(i));
108: }
109: }
110: }
111: }
112:
113: private void initNTP() {
114: if (root instanceof SGLocale) {
115: needToProcess.add(((SGLocale) root).getLocale());
116: } else {
117: needToProcess.add(root.getJ3dSceneGraphObject());
118: }
119:
120: }
121:
122: /** traverses the structure
123: */
124: public void traverse() {
125: initNTP();
126: BeanCodeGenerator g = null;
127: Delegator d = new Delegator(context, namePool);
128: Enumeration ntpe = needToProcess.elements();
129: while (ntpe.hasMoreElements()) {
130: currentBean = ntpe.nextElement();
131: g = d.getBeanCodeGenerator(currentBean);
132:
133: addNeedToProcessBeans(g.getNeedToProcessBeans());
134: addFields(g.getDeclarationCode());
135: addConstructionCode(g.getConstructionCode());
136: addCustomizationCode(g.getCustomizationCode());
137: }
138: }
139:
140: /** Inserts beans which need to be processed
141: */
142: private void addNeedToProcessBeans(List l) {
143: //pokud tam jeste neni, tak ho tam pridam
144: Iterator i = l.iterator();
145: while (i.hasNext()) {
146: Object o = i.next();
147: if (!needToProcess.contains(o)) {
148: needToProcess.add(o);
149: }
150: }
151: }
152:
153: /** addFields, which wants to be declared
154: * @param f */
155: private void addFields(String f) {
156: if (f != null) {
157: fields.add(f);
158: }
159: }
160:
161: /** Add construction code e.g. <CODE>t = new TransformGroup()</CODE>
162: * @param c
163: */
164: private void addConstructionCode(String c) {
165: if (c != null) {
166: if (currentBean instanceof javax.media.j3d.Behavior) {
167: constructionCodeLines.addLast(c);
168: } else {
169: constructionCodeLines.addFirst(c);
170: }
171: }
172: }
173:
174: /** adds customization code e.g. <CODE>t.setTransform();</CODE>
175: */
176: private void addCustomizationCode(String c) {
177: if (c != null) {
178: customizationCodeLines.addFirst(c);
179: }
180: }
181:
182: /** Getter for property root.
183: * @return Value of property root.
184: */
185: public SGObject getRoot() {
186: return root;
187: }
188:
189: /** Setter for property root.
190: * @param root New value of property root.
191: */
192: public void setRoot(SGObject root) {
193: this .root = root;
194: }
195:
196: /** Getter for property namePool.
197: * @return Value of property namePool.
198: */
199: public NamePool getNamePool() {
200: return namePool;
201: }
202:
203: /** Getter for property fields.
204: * @return Value of property fields.
205: */
206: public ArrayList getFields() {
207: return fields;
208: }
209:
210: /** Getter for property constructionCodeLines.
211: * @return Value of property constructionCodeLines.
212: */
213: public LinkedList getConstructionCodeLines() {
214: return constructionCodeLines;
215: }
216:
217: /** Getter for property customizationCodeLines.
218: * @return Value of property customizationCodeLines.
219: */
220: public LinkedList getCustomizationCodeLines() {
221: return customizationCodeLines;
222: }
223:
224: /** Getter for property needToProcess.
225: * @return Value of property needToProcess.
226: */
227: public Vector getNeedToProcess() {
228: return needToProcess;
229: }
230:
231: /**
232: * @return */
233: public String toString() {
234: Iterator f = fields.iterator();
235: Iterator c = constructionCodeLines.iterator();
236: Iterator s = customizationCodeLines.iterator();
237:
238: StringBuffer b = new StringBuffer();
239:
240: while (f.hasNext()) {
241: b.append(f.next());
242: }
243:
244: while (c.hasNext()) {
245: b.append(c.next());
246: }
247:
248: while (s.hasNext()) {
249: b.append(s.next());
250: }
251:
252: return b.toString();
253: }
254: }
|