001: /*
002: * $Header: /cvs/j3dfly/J3dEditor/src/org/jdesktop/j3dedit/scenegrapheditor/sourcecontrol/ClassGenerator.java,v 1.1 2005/04/20 22:21:20 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 java.util.List;
021: import org.jdesktop.j3dedit.scenegraph.SGLocale;
022: import org.jdesktop.j3dedit.scenegraph.SGObject;
023: import org.jdesktop.j3dedit.J3dEditContext;
024: import java.util.ArrayList;
025: import com.sun.j3d.utils.universe.ViewingPlatform;
026: import java.util.Iterator;
027:
028: /**
029: *
030: * @author Jan Becicka
031: * @version
032: */
033: public class ClassGenerator {
034:
035: List fields;
036: List methods;
037: NamePool namePool;
038:
039: /** Holds value of property className. */
040: private String className;
041:
042: /** Holds value of property locale. */
043: private SGLocale locale;
044:
045: private J3dEditContext context;
046:
047: /** Creates new ClassGenerator */
048: public ClassGenerator(J3dEditContext context, SGLocale locale,
049: String className) {
050: this .context = context;
051: fields = new ArrayList();
052: methods = new ArrayList();
053:
054: this .locale = locale;
055: this .className = className;
056: namePool = new NamePool();
057: }
058:
059: public void create() {
060: for (int i = 0; i < locale.numChildren(); i++) {
061: SGObject child = locale.getChild(i);
062: if (!(child.getJ3dSceneGraphObject() instanceof ViewingPlatform)) {
063: createSceneGraph(child);
064: }
065: }
066: }
067:
068: private void createSceneGraph(SGObject o) {
069: Traverser t = new Traverser(context, o, namePool);
070: t.traverse();
071: fields.addAll(t.getFields());
072: methods.add(new MethodWithBody(namePool.getObjectName(t
073: .getRoot().getJ3dSceneGraphObject()), t
074: .getConstructionCodeLines(), t
075: .getCustomizationCodeLines()));
076: }
077:
078: public String toString() {
079: return getHeader() + getFields() + getMethods() + getFooter();
080: }
081:
082: private String getHeader() {
083: StringBuffer buf = new StringBuffer();
084: buf.append("import java.applet.Applet;\n");
085: buf.append("import java.awt.BorderLayout;\n");
086: buf.append("import java.awt.event.*;\n");
087: buf.append("import java.awt.GraphicsConfiguration;\n");
088: buf.append("import com.sun.j3d.utils.applet.MainFrame;\n");
089: buf.append("import com.sun.j3d.utils.geometry.ColorCube;\n");
090: buf.append("import com.sun.j3d.utils.universe.*;\n");
091: buf.append("import javax.media.j3d.*;\n");
092: buf.append("import javax.vecmath.*;\n\n\n");
093: buf.append("public class " + className
094: + " extends Applet {\n\n");
095: buf.append("public " + className + "() {\n");
096: buf.append("}\n\n");
097: buf.append("private SimpleUniverse u = null;\n");
098: return buf.toString();
099: }
100:
101: private String getFields() {
102: Iterator f = fields.iterator();
103: StringBuffer b = new StringBuffer();
104:
105: while (f.hasNext()) {
106: b.append(f.next());
107: }
108: return b.toString();
109: }
110:
111: private String getMethods() {
112: Iterator m = methods.iterator();
113: StringBuffer b = new StringBuffer();
114:
115: while (m.hasNext()) {
116: b.append(m.next());
117: }
118:
119: return b.toString();
120: }
121:
122: private String appendScenes() {
123: Iterator i = methods.iterator();
124: StringBuffer b = new StringBuffer();
125: MethodWithBody m;
126: while (i.hasNext()) {
127: m = (MethodWithBody) i.next();
128: b.append("BranchGroup " + m.getName() + " = "
129: + m.getMethodName() + "();\n");
130: b.append("u.addBranchGraph(" + m.getName() + ");\n");
131: }
132: return b.toString();
133:
134: }
135:
136: private String getFooter() {
137: StringBuffer b = new StringBuffer();
138: b.append("public void init() {\n");
139: b.append("setLayout(new BorderLayout());\n");
140: b
141: .append("GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();\n");
142: b.append("Canvas3D c = new Canvas3D(config);\n");
143: b.append("add(\"Center\", c);\n");
144: b.append("u = new SimpleUniverse(c);\n");
145: b
146: .append("u.getViewingPlatform().setNominalViewingTransform();\n");
147: b.append(appendScenes());
148: b.append("}\n");
149: b.append("public void destroy() {\n");
150: b.append("u.removeAllLocales();\n");
151: b.append("}\n");
152: b.append("public static void main(String[] args) {\n");
153: b.append("new MainFrame(new " + className + "(), 256, 256);\n");
154: b.append("}\n");
155: b.append("}\n");
156:
157: return b.toString();
158: }
159:
160: /** Getter for property className.
161: * @return Value of property className.
162: */
163: public String getClassName() {
164: return className;
165: }
166:
167: /** Setter for property className.
168: * @param className New value of property className.
169: */
170: public void setClassName(String className) {
171: this .className = className;
172: }
173:
174: /** Getter for property locale.
175: * @return Value of property locale.
176: */
177: public SGLocale getLocale() {
178: return locale;
179: }
180:
181: }
|