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 com.jeta.forms.store.memento.FormMemento;
022: import com.jeta.swingbuilder.store.CodeModel;
023:
024: public class DefaultSourceBuilder implements SourceBuilder {
025:
026: private StringBuffer m_source = new StringBuffer();
027:
028: private int m_indent_pos = 0;
029: private String m_indent_padding = "";
030:
031: private int m_tab_size = 3;
032:
033: private int m_column = 0;
034:
035: public static String buildSource(CodeModel cgenmodel, FormMemento fm) {
036:
037: DefaultSourceBuilder builder = new DefaultSourceBuilder();
038:
039: ClassDeclarationManager decl_mgr = new ClassDeclarationManager(
040: cgenmodel);
041: decl_mgr.setPackage(cgenmodel.getPackage());
042:
043: BuilderUtils.buildConstructor(decl_mgr, cgenmodel
044: .getClassName());
045:
046: if (cgenmodel.isIncludeMain()) {
047: BuilderUtils.buildMain(decl_mgr, cgenmodel.getClassName());
048: }
049:
050: BuilderUtils.buildFillMethod(decl_mgr);
051: BuilderUtils.buildImageLoader(decl_mgr);
052: BuilderUtils.buildApplyComponentOrientation(decl_mgr);
053:
054: MethodWriter create_panel = new PanelWriter().createPanel(
055: decl_mgr, fm);
056: BuilderUtils.buildInitializer(decl_mgr, create_panel
057: .getMethodName()
058: + "()");
059:
060: decl_mgr.build(builder);
061: return builder.m_source.toString();
062: }
063:
064: public void closeBrace() {
065: print('}');
066: }
067:
068: public void dedent() {
069: m_indent_pos--;
070: if (m_indent_pos < 0)
071: m_indent_pos = 0;
072: updateIndentPadding();
073: }
074:
075: public void indent() {
076: m_indent_pos++;
077: updateIndentPadding();
078: }
079:
080: public void openBrace() {
081: println();
082: print('{');
083: }
084:
085: public void println() {
086: m_source.append('\n');
087: m_column = 0;
088: }
089:
090: public void println(String txt) {
091: print(txt);
092: println();
093: }
094:
095: public void print(String txt) {
096: if (txt != null && txt.length() > 0) {
097: if (m_column == 0) {
098: m_source.append(m_indent_padding);
099: }
100: m_source.append(txt);
101: m_column += txt.length();
102: }
103: }
104:
105: public void print(char c) {
106: if (m_column == 0) {
107: m_source.append(m_indent_padding);
108: }
109: m_source.append(c);
110: m_column++;
111: }
112:
113: /**
114: * Fills a buffer with the given character count times
115: *
116: * @param c
117: * the character to fill the string with
118: * @param count
119: * the number of characters to place in the string
120: */
121: public static void fillBuffer(StringBuffer buff, char c, int count) {
122: for (int index = 0; index < count; index++) {
123: buff.append(c);
124: }
125: }
126:
127: /**
128: * Creates a string that is filled with the given character count times
129: *
130: * @param c
131: * the character to fill the string with
132: * @param count
133: * the number of characters to place in the string
134: * @return the created string
135: */
136: public static String fillString(char c, int count) {
137: StringBuffer buff = new StringBuffer(count);
138: fillBuffer(buff, c, count);
139: return buff.toString();
140: }
141:
142: private void updateIndentPadding() {
143: if (m_indent_pos == 0)
144: m_indent_padding = "";
145: else {
146: m_indent_padding = fillString(' ', m_indent_pos
147: * m_tab_size);
148: }
149: }
150:
151: }
|