001: /*
002: * $Header: /cvs/j3dfly/J3dEditor/src/org/jdesktop/j3dedit/scenegrapheditor/sourcecontrol/CodeMethod.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 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.util.ArrayList;
021: import java.io.PrintStream;
022:
023: /**
024: * Class representing a method in the source code generated by the builder
025: */
026: public class CodeMethod {
027:
028: private String name;
029: private String returnType;
030: private String params;
031:
032: private int methodIndent;
033:
034: private ArrayList lines;
035:
036: public CodeMethod(int indent, String name, String returnType,
037: String params) {
038: this .name = name;
039: this .returnType = returnType;
040: this .params = params;
041: methodIndent = indent;
042: lines = new ArrayList();
043: }
044:
045: /**
046: * Returns the name of this method
047: */
048: public String getName() {
049: return name;
050: }
051:
052: /**
053: * Store the line with the current indent index
054: */
055: public void addLine(int indent, String line) {
056: lines.add(new CodeLine(indent, line));
057: }
058:
059: public String toString() {
060: return name + "(" + params + ")";
061: }
062:
063: public void writeMethod(PrintStream out) {
064: doIndent(out, methodIndent);
065: out.println("public " + returnType + " " + name + "( " + params
066: + " ) {");
067: for (int i = 0; i < lines.size(); i++)
068: ((CodeLine) lines.get(i)).writeLine(methodIndent, out);
069:
070: doIndent(out, methodIndent);
071: out.println("}");
072: out.println();
073: }
074:
075: /**
076: * Produce the require indent in the code
077: * output stream
078: */
079: void doIndent(PrintStream out, int indent) {
080: for (int i = 0; i < indent; i++)
081: out.print(" ");
082: }
083: }
084:
085: class CodeLine {
086:
087: private String line;
088: private int indent;
089:
090: public CodeLine(int indent, String line) {
091: this .indent = indent;
092: this .line = line;
093: }
094:
095: /**
096: * Returns the indent of the line
097: * indent of 0 is hard against left margin
098: */
099: public int getIndent() {
100: return indent;
101: }
102:
103: /**
104: * Returns the text of the line without leading indent
105: */
106: public String getLine() {
107: return line;
108: }
109:
110: public void writeLine(int methodIndent, PrintStream out) {
111: doIndent(out, methodIndent + indent);
112: out.println(line);
113: }
114:
115: /**
116: * Produce the require indent in the code
117: * output stream
118: */
119: void doIndent(PrintStream out, int indent) {
120: for (int i = 0; i < indent; i++)
121: out.print(" ");
122: }
123: }
|