ClassGenerator.java :  » 6.0-JDK-Modules » java-3d » org » jdesktop » j3dedit » scenegrapheditor » sourcecontrol » Java Open Source

Java Open Source » 6.0 JDK Modules » java 3d 
java 3d » org » jdesktop » j3dedit » scenegrapheditor » sourcecontrol » ClassGenerator.java
/*
 *  $Header: /cvs/j3dfly/J3dEditor/src/org/jdesktop/j3dedit/scenegrapheditor/sourcecontrol/ClassGenerator.java,v 1.1 2005/04/20 22:21:20 paulby Exp $
 *
 *                         Sun Public License Notice
 *
 *  The contents of this file are subject to the Sun Public License Version
 *  1.0 (the "License"). You may not use this file except in compliance with
 *  the License. A copy of the License is available at http://www.sun.com/
 *  
 *  The Original Code is the Java 3D(tm) Scene Graph Editor.
 *  The Initial Developer of the Original Code is Jan Becicka.
 *  Portions created by Jan Becicka are Copyright (C) 2002.
 *  All Rights Reserved.
 *  
 *  Contributor(s): Jan Becicka.
 *  
 **/
package org.jdesktop.j3dedit.scenegrapheditor.sourcecontrol;

import java.util.List;
import org.jdesktop.j3dedit.scenegraph.SGLocale;
import org.jdesktop.j3dedit.scenegraph.SGObject;
import org.jdesktop.j3dedit.J3dEditContext;
import java.util.ArrayList;
import com.sun.j3d.utils.universe.ViewingPlatform;
import java.util.Iterator;

/**
 *
 * @author  Jan Becicka
 * @version
 */
public class ClassGenerator {
    
    List fields;
    List methods;
    NamePool namePool;
    
    /** Holds value of property className. */
    private String className;
    
    /** Holds value of property locale. */
    private SGLocale locale;
    
    private J3dEditContext context;
    
    /** Creates new ClassGenerator */
    public ClassGenerator( J3dEditContext context, SGLocale locale, String className) {
        this.context = context;
        fields = new ArrayList();
        methods = new ArrayList();
        
        this.locale = locale;
        this.className = className;
        namePool = new NamePool();
    }
    
    public void create(){
        for (int i=0; i < locale.numChildren(); i++){
            SGObject child = locale.getChild(i);
            if (!(child.getJ3dSceneGraphObject() instanceof ViewingPlatform)){
                createSceneGraph(child);
            }
        }
    }
    
    private void createSceneGraph(SGObject o){
        Traverser t = new Traverser( context, o, namePool);
        t.traverse();
        fields.addAll(t.getFields());
        methods.add(
          new MethodWithBody(
          namePool.getObjectName(t.getRoot().getJ3dSceneGraphObject()),
          t.getConstructionCodeLines(),
          t.getCustomizationCodeLines())
        );
    }
    
    public String toString(){
        return
        getHeader()  +
        getFields()  +
        getMethods() +
        getFooter();
    }
    
    private String getHeader(){
        StringBuffer buf = new StringBuffer();
        buf.append("import java.applet.Applet;\n");
        buf.append("import java.awt.BorderLayout;\n");
        buf.append("import java.awt.event.*;\n");
        buf.append("import java.awt.GraphicsConfiguration;\n");
        buf.append("import com.sun.j3d.utils.applet.MainFrame;\n");
        buf.append("import com.sun.j3d.utils.geometry.ColorCube;\n");
        buf.append("import com.sun.j3d.utils.universe.*;\n");
        buf.append("import javax.media.j3d.*;\n");
        buf.append("import javax.vecmath.*;\n\n\n");
        buf.append("public class " + className + " extends Applet {\n\n");
        buf.append("public " + className + "() {\n");
        buf.append("}\n\n");
        buf.append("private SimpleUniverse u = null;\n");
        return buf.toString();
    }
    
    private String getFields(){
        Iterator f = fields.iterator();
        StringBuffer b = new StringBuffer();
        
        while (f.hasNext()){
            b.append(f.next());
        }
        return b.toString();
    }
    
    private String getMethods(){
        Iterator m = methods.iterator();
        StringBuffer b = new StringBuffer();
        
        while (m.hasNext()){
            b.append(m.next());
        }
        
        return b.toString();
    }
    
    private String appendScenes(){
        Iterator i = methods.iterator();
        StringBuffer b = new StringBuffer();
        MethodWithBody m;
        while (i.hasNext()){
            m = (MethodWithBody) i.next();
            b.append("BranchGroup " + m.getName() + " = " + m.getMethodName() + "();\n");
            b.append("u.addBranchGraph(" + m.getName() + ");\n");
        }
        return b.toString();

    }
    private String getFooter(){
        StringBuffer b = new StringBuffer();
        b.append("public void init() {\n");
        b.append("setLayout(new BorderLayout());\n");
        b.append("GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();\n");
        b.append("Canvas3D c = new Canvas3D(config);\n");
        b.append("add(\"Center\", c);\n");
        b.append("u = new SimpleUniverse(c);\n");
        b.append("u.getViewingPlatform().setNominalViewingTransform();\n");
        b.append(appendScenes());
        b.append("}\n");
        b.append("public void destroy() {\n");
        b.append("u.removeAllLocales();\n");
        b.append("}\n");
        b.append("public static void main(String[] args) {\n");
        b.append("new MainFrame(new " + className + "(), 256, 256);\n");
        b.append("}\n");
        b.append("}\n");


        return b.toString();
    }
    
    /** Getter for property className.
     * @return Value of property className.
     */
    public String getClassName() {
        return className;
    }
    
    /** Setter for property className.
     * @param className New value of property className.
     */
    public void setClassName(String className) {
        this.className = className;
    }
    
    /** Getter for property locale.
     * @return Value of property locale.
     */
    public SGLocale getLocale() {
        return locale;
    }
    
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.