001: /*
002: * $Header: /cvs/j3dfly/J3dEditor/src/org/jdesktop/j3dedit/scenegrapheditor/sourcecontrol/SourceCodeControl.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 Paul Byrne.
012: * Portions created by Paul Byrne are Copyright (C) 2002.
013: * All Rights Reserved.
014: *
015: * Contributor(s): Paul Byrne.
016: *
017: **/
018: package org.jdesktop.j3dedit.scenegrapheditor.sourcecontrol;
019:
020: import java.io.*;
021: import java.util.*;
022:
023: /**
024: * Controls the generation of source code for the Java3D applications
025: *
026: * This initial version is very limited in terms of it's formating
027: * capabilities. Futher work is required and probably some extension to the
028: * interface.
029: *
030: * @Author Paul Byrne
031: * @(#)SourceCodeControl.java 1.4 99/04/07 23:04:56
032: */
033: public class SourceCodeControl {
034:
035: private String className = new String("className");
036:
037: private Stack methodStack;
038: private Stack tmpVarStack;
039:
040: private ArrayList globalVar;
041: private ArrayList methods;
042: private TreeSet imports; // All the required imports
043:
044: private int tmpVar = 0; // Count of temporary variables
045:
046: public SourceCodeControl() {
047: methods = new ArrayList();
048: methodStack = new Stack();
049: tmpVarStack = new Stack();
050: imports = new TreeSet();
051: globalVar = new ArrayList();
052:
053: imports.add("java.applet.Applet");
054: imports.add("javax.media.j3d.*");
055: imports.add("com.sun.j3d.utils.applet.MainFrame");
056: }
057:
058: /**
059: * Returns the name of this class
060: */
061: public String getClassName() {
062: return className;
063: }
064:
065: /**
066: * Set the name of the current class
067: */
068: public void setClassName(String className) {
069: this .className = className;
070: }
071:
072: /**
073: * Add a method to the package and make it the current method
074: */
075: public void newCurrentMethod(CodeMethod method) {
076: if (methods.contains(method))
077: throw new RuntimeException(
078: "Duplicate Method Name in SourceCodeControl "
079: + method);
080: methods.add(method);
081: methodStack.push(method);
082:
083: tmpVarStack.push(new Integer(tmpVar));
084: tmpVar = 0;
085: }
086:
087: /**
088: * Finish the current method
089: * The new current method will be the previous current method ie
090: * the current method stack will be popped
091: */
092: public void completeCurrentMethod() {
093: methodStack.pop();
094: tmpVar = ((Integer) tmpVarStack.pop()).intValue();
095: }
096:
097: /**
098: * Returns a temporary variable name.
099: * Takes into account the current scope so variable names are
100: * reused
101: *
102: * Needs improving so that variable names represent types
103: */
104: public String getTmpVariableName() {
105: if (tmpVar > 0)
106: return "tmp" + tmpVar++;
107: else {
108: tmpVar++;
109: return "tmp";
110: }
111: }
112:
113: /**
114: * Add the line to the current method
115: */
116: public void addLine(int indent, String line) {
117: ((CodeMethod) methodStack.peek()).addLine(indent, line);
118: }
119:
120: /**
121: * Add an import statement
122: *
123: * Duplicates will be removed
124: */
125: public void addImport(String name) {
126: imports.add(name);
127: }
128:
129: /**
130: * Add a global variable
131: */
132: public void addGlobalVariable(String code) {
133: globalVar.add(code);
134: }
135:
136: /**
137: * Produce the require indent in the code
138: * output stream
139: */
140: void doIndent(PrintStream out, int indent) {
141: for (int i = 0; i < indent; i++)
142: out.print(" ");
143: }
144:
145: /**
146: * Output a formated line of code
147: */
148: void outputLine(PrintStream out, int indent, String line) {
149: doIndent(out, indent);
150: out.println(line);
151: }
152:
153: void outputLine(PrintStream out) {
154: out.println();
155: }
156:
157: private void writeHeader(PrintStream out) {
158: out.println("// Java3D generated by Java3D world builder");
159: out.println("// World builder by Paul Byrne");
160: out.println("// Paul.Byrne@uk.sun.com");
161: out.println();
162: }
163:
164: private void writeImports(PrintStream out) {
165:
166: Iterator it = imports.iterator();
167:
168: while (it.hasNext()) {
169: out.println("import " + it.next() + ";");
170: }
171:
172: }
173:
174: private void writeGlobalVariables(PrintStream out) {
175: Iterator it = globalVar.iterator();
176:
177: while (it.hasNext()) {
178: out.println(" " + it.next() + ";");
179: }
180: }
181:
182: private void writeMethods(PrintStream out) {
183:
184: Iterator it = methods.iterator();
185: CodeMethod method;
186:
187: while (it.hasNext()) {
188: method = (CodeMethod) it.next();
189: method.writeMethod(out);
190: }
191: }
192:
193: public void generateCode() {
194: generateCode(System.out);
195: }
196:
197: public void generateCode(OutputStream outStream) {
198: PrintStream out = new PrintStream(outStream);
199:
200: writeHeader(out);
201: writeImports(out);
202: outputLine(out, 0, "public class " + className
203: + " extends Applet {");
204:
205: writeGlobalVariables(out);
206:
207: outputLine(out, 1, "public " + className + "() {");
208: outputLine(out, 2, "buildUniverse();");
209: outputLine(out, 1, "}");
210:
211: writeMethods(out);
212:
213: outputLine(out, 1, "public static void main( String[] args ) {");
214: outputLine(out, 2, "new MainFrame( new " + className
215: + "(), 256, 256 );");
216: outputLine(out, 1, "}");
217:
218: outputLine(out, 0, "}");
219: }
220:
221: }
|