001: /*
002: * Copyright (C) 2005 Jeff Tassin
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */
018:
019: package com.jeta.swingbuilder.codegen.builder;
020:
021: import java.util.Collection;
022: import java.util.Iterator;
023: import java.util.LinkedList;
024:
025: public class MethodWriter implements DeclarationManager {
026: private DeclarationManager m_class_decl_mgr;
027:
028: private DeclarationHelper m_local_vars = new DeclarationHelper(null);
029:
030: /**
031: * A list of statements that make up this method
032: */
033: private LinkedList m_segments = new LinkedList();
034:
035: /**
036: * The BeanWriter associated with this return value of this method
037: * declaration
038: */
039: private BeanWriter m_return_result;
040:
041: /**
042: * The parameters for this method
043: */
044: private Class[] m_parameters;
045:
046: /**
047: * The name of the method
048: */
049: private String m_method_name;
050:
051: /**
052: * A list of Strings that make up the method comments. Each String object is
053: * a single comment line.
054: */
055: private LinkedList m_comments;
056:
057: private String m_access = "public";
058:
059: /**
060: * Defines a method with no parameters.
061: */
062: public MethodWriter(DeclarationManager declMgr,
063: BeanWriter beanWriter, String suggestedName) {
064: m_class_decl_mgr = declMgr;
065: m_return_result = beanWriter;
066: m_method_name = createMethodName(suggestedName);
067: }
068:
069: /**
070: * Adds a line to the comments for this method. Do NOT include any /*, *
071: * tokens because they are automatically added here.
072: */
073: public void addCommentLine(String comment) {
074: if (m_comments == null)
075: m_comments = new LinkedList();
076:
077: m_comments.add(comment);
078: }
079:
080: protected String getSignature() {
081: StringBuffer sbuff = new StringBuffer();
082: sbuff.append(getAccess());
083: sbuff.append(' ');
084: sbuff.append(DeclarationHelper.trimPackage(getReturnType()));
085: sbuff.append(' ');
086: sbuff.append(m_method_name);
087: sbuff.append("()");
088: return sbuff.toString();
089: }
090:
091: public void build(SourceBuilder builder) {
092: if (m_comments != null) {
093: builder.println("/**");
094: Iterator iter = m_comments.iterator();
095: while (iter.hasNext()) {
096: String comment = (String) iter.next();
097: builder.print(" * ");
098: builder.println(comment);
099: }
100: builder.println(" */");
101: }
102:
103: builder.print(getSignature());
104: builder.openBrace();
105: builder.println();
106: builder.indent();
107: Iterator iter = m_segments.iterator();
108: while (iter.hasNext()) {
109: CodeSegment seg = (CodeSegment) iter.next();
110: seg.output(builder);
111: }
112:
113: String returnval = getReturnVariable();
114: if (returnval != null && returnval.length() > 0) {
115: builder.print("return ");
116: builder.print(returnval);
117: builder.print(';');
118: builder.println();
119: }
120:
121: builder.dedent();
122: builder.closeBrace();
123: }
124:
125: public void addImport(String importDef) {
126: m_class_decl_mgr.addImport(importDef);
127: }
128:
129: public void addMemberVariable(Statement stmt) {
130: m_class_decl_mgr.addMemberVariable(stmt);
131: }
132:
133: public void addMethod(MethodWriter mw) {
134: m_class_decl_mgr.addMethod(mw);
135: }
136:
137: public void addStatement(Statement stmt) {
138: m_segments.add(stmt);
139: }
140:
141: public void addSegment(CodeSegment seg) {
142: m_segments.add(seg);
143: }
144:
145: public void addStatements(Collection stmts) {
146: Iterator iter = stmts.iterator();
147: while (iter.hasNext()) {
148: addStatement((Statement) iter.next());
149: }
150: }
151:
152: public String createMemberVariable(Class compClass, String compName) {
153: return m_class_decl_mgr.createMemberVariable(compClass,
154: compName);
155: }
156:
157: public String createLocalVariable(Class compClass, String compName) {
158: return m_local_vars.createVariable(compClass, compName);
159: }
160:
161: public String createMethodName(String name) {
162: return m_class_decl_mgr.createMethodName(name);
163: }
164:
165: public String getAccess() {
166: return m_access;
167: }
168:
169: public String getMethodName() {
170: return m_method_name;
171: }
172:
173: /**
174: * This returns the name of a method used for loading resources in a form
175: * such as Icons and strings.
176: */
177: public String getResourceMethod(Class resourceType) {
178: return m_class_decl_mgr.getResourceMethod(resourceType);
179: }
180:
181: public Class getReturnType() {
182: if (m_return_result != null)
183: return m_return_result.getResultType();
184: else
185: return void.class;
186: }
187:
188: public String getReturnVariable() {
189: if (m_return_result != null)
190: return m_return_result.getResultVariable();
191: else
192: return "";
193: }
194:
195: public void setAccess(String access) {
196: m_access = access;
197: }
198:
199: public void setReturnResult(BeanWriter result) {
200: m_return_result = result;
201: }
202:
203: public boolean isIncludeNonStandard() {
204: return m_class_decl_mgr.isIncludeNonStandard();
205: }
206:
207: public Object get(String name) {
208: return m_class_decl_mgr.get(name);
209: }
210:
211: public void put(String name, Object obj) {
212: m_class_decl_mgr.put(name, obj);
213: }
214:
215: }
|