01: /*
02: * Copyright (C) 2005 Jeff Tassin
03: *
04: * This library is free software; you can redistribute it and/or
05: * modify it under the terms of the GNU Lesser General Public
06: * License as published by the Free Software Foundation; either
07: * version 2.1 of the License, or (at your option) any later version.
08: *
09: * This library is distributed in the hope that it will be useful,
10: * but WITHOUT ANY WARRANTY; without even the implied warranty of
11: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12: * Lesser General Public License for more details.
13: *
14: * You should have received a copy of the GNU Lesser General Public
15: * License along with this library; if not, write to the Free Software
16: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17: */
18:
19: package com.jeta.swingbuilder.codegen.builder;
20:
21: import java.util.Collection;
22: import java.util.Iterator;
23:
24: public class VariableDeclaration extends MultiParameterStatement {
25: private Class m_obj_class;
26: private String m_declaration;
27: private DeclarationManager m_decl_mgr;
28: private String m_varname;
29: private String m_initializer;
30:
31: /** Creates a member variable declaration */
32: public VariableDeclaration(DeclarationManager declMgr,
33: Class objClass, String varName, boolean local) {
34: this (declMgr, objClass, varName, local, null);
35: }
36:
37: /** Creates a member variable declaration */
38: public VariableDeclaration(DeclarationManager declMgr,
39: Class objClass, String varName, boolean local,
40: String initializer) {
41: m_decl_mgr = declMgr;
42: m_obj_class = objClass;
43: if (objClass != com.jeta.forms.gui.form.GridView.class) {
44: m_decl_mgr.addImport(m_obj_class.getName());
45: }
46: if (local)
47: m_varname = m_decl_mgr.createLocalVariable(objClass,
48: varName);
49: else
50: m_varname = m_decl_mgr.createMemberVariable(objClass,
51: varName);
52:
53: m_initializer = initializer;
54: }
55:
56: public String getVariable() {
57: return m_varname;
58: }
59:
60: protected String getInitializer() {
61: if (m_initializer == null)
62: return "new " + DeclarationHelper.trimPackage(m_obj_class);
63: else
64: return m_initializer;
65: }
66:
67: public void output(SourceBuilder builder) {
68: builder.print(DeclarationHelper.trimPackage(m_obj_class));
69: builder.print(" ");
70: builder.print(m_varname);
71: builder.print(" = ");
72: builder.print(getInitializer());
73: builder.print("(");
74: Collection params = getParameters();
75: Iterator iter = params.iterator();
76: while (iter.hasNext()) {
77: Expression expr = (Expression) iter.next();
78: expr.output(builder);
79: if (iter.hasNext())
80: builder.print(',');
81: }
82: builder.print(");");
83: builder.println();
84: }
85: }
|