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.open.i18n.I18NUtils;
022:
023: public class BuilderUtils {
024:
025: /**
026: * Builds the constructor
027: */
028: public static void buildConstructor(DeclarationManager declMgr,
029: String className) {
030: declMgr.addImport("javax.swing.JFrame");
031:
032: final String ctor = className;
033: MethodWriter method_writer = new MethodWriter(declMgr, null,
034: "main") {
035: protected String getSignature() {
036: return "public " + ctor + "()";
037: }
038: };
039:
040: method_writer.addCommentLine("Default constructor");
041:
042: Block block = new Block();
043: String frame_code = "initializePanel();";
044: block.addCode(frame_code);
045: method_writer.addSegment(block);
046: declMgr.addMethod(method_writer);
047: }
048:
049: /**
050: * Builds the initializer
051: */
052: public static void buildInitializer(DeclarationManager declMgr,
053: String methodExpression) {
054: MethodWriter method_writer = new MethodWriter(declMgr, null,
055: "initializePanel");
056: method_writer.setAccess("protected");
057:
058: method_writer.addCommentLine("Initializer");
059: declMgr.addImport("java.awt.BorderLayout");
060:
061: Block block = new Block();
062: String frame_code = "setLayout(new BorderLayout());" + "add("
063: + methodExpression + ", BorderLayout.CENTER);";
064:
065: block.addCode(frame_code);
066: method_writer.addSegment(block);
067: declMgr.addMethod(method_writer);
068: }
069:
070: /**
071: * Builds a 'main' method
072: */
073: public static void buildMain(DeclarationManager declMgr,
074: String className) {
075: MethodWriter method_writer = new MethodWriter(declMgr, null,
076: "main") {
077: protected String getSignature() {
078: return "public static void main(String[] args)";
079: }
080: };
081:
082: method_writer.addCommentLine("Main method for panel");
083:
084: Block block = new Block();
085: String frame_code = "JFrame frame = new JFrame();"
086: + "frame.setSize(600, 400);"
087: + "frame.setLocation(100, 100);"
088: + "frame.getContentPane().add(new " + className
089: + "());" + "frame.setVisible(true);";
090:
091: block.addCode(frame_code);
092: block.println();
093:
094: declMgr.addImport("java.awt.event.WindowAdapter");
095: declMgr.addImport("java.awt.event.WindowEvent");
096:
097: String listener_code = "frame.addWindowListener( new WindowAdapter()"
098: + "{"
099: + "public void windowClosing( WindowEvent evt )"
100: + "{" + "System.exit(0);" + "}\n});";
101: block.addCode(listener_code);
102:
103: method_writer.addSegment(block);
104: declMgr.addMethod(method_writer);
105: }
106:
107: /**
108: * Builds a 'fill' method
109: */
110: public static void buildFillMethod(DeclarationManager declMgr) {
111: MethodWriter method_writer = new MethodWriter(declMgr, null,
112: "addFillComponents") {
113: protected String getSignature() {
114: return "void addFillComponents( Container panel, int[] cols, int[] rows )";
115: }
116: };
117:
118: declMgr.addImport("java.awt.Container");
119: declMgr.addImport("java.awt.Dimension");
120: declMgr.addImport("javax.swing.Box");
121:
122: method_writer
123: .addCommentLine("Adds fill components to empty cells in the first row and first column of the grid.");
124: method_writer
125: .addCommentLine("This ensures that the grid spacing will be the same as shown in the designer.");
126: method_writer
127: .addCommentLine("@param cols an array of column indices in the first row where fill components should be added.");
128: method_writer
129: .addCommentLine("@param rows an array of row indices in the first column where fill components should be added.");
130:
131: Block block = new Block();
132: String fill_code1 = "Dimension filler = new Dimension(10,10);\n"
133: + "boolean filled_cell_11 = false;"
134: + "CellConstraints cc = new CellConstraints();"
135: + "if ( cols.length > 0 && rows.length > 0 )"
136: + "{"
137: + "if ( cols[0] == 1 && rows[0] == 1 )"
138: + "{"
139: + "/** add a rigid area */\n"
140: + "panel.add( Box.createRigidArea( filler ), cc.xy(1,1) );"
141: + "filled_cell_11 = true;" + "}\n" + "}\n\n";
142:
143: block.addCode(fill_code1);
144: method_writer.addSegment(block);
145:
146: block = new Block();
147: method_writer.addSegment(new BasicExpression(
148: "for( int index = 0; index < cols.length; index++ )"));
149: String fill_code2 = "{"
150: + "if ( cols[index] == 1 && filled_cell_11 )"
151: + "{"
152: + "continue;"
153: + "}\n"
154: + "panel.add( Box.createRigidArea( filler ), cc.xy(cols[index],1) );"
155: + "}\n\n";
156:
157: block.addCode(fill_code2);
158: method_writer.addSegment(block);
159:
160: block = new Block();
161: method_writer.addSegment(new BasicExpression(
162: "for( int index = 0; index < rows.length; index++ )"));
163: String fill_code3 = "{"
164: + "if ( rows[index] == 1 && filled_cell_11 )"
165: + "{"
166: + "continue;"
167: + "}\n"
168: + "panel.add( Box.createRigidArea( filler ), cc.xy(1,rows[index]) );"
169: + "}\n\n";
170:
171: block.addCode(fill_code3);
172: method_writer.addSegment(block);
173: declMgr.addMethod(method_writer);
174: }
175:
176: public static void buildImageLoader(DeclarationManager declMgr) {
177: MethodWriter method_writer = new MethodWriter(declMgr, null,
178: "loadImage") {
179: protected String getSignature() {
180: return "public ImageIcon loadImage( String imageName )";
181: }
182: };
183:
184: declMgr.addImport("javax.swing.ImageIcon");
185:
186: method_writer
187: .addCommentLine("Helper method to load an image file from the CLASSPATH");
188: method_writer
189: .addCommentLine("@param imageName the package and name of the file to load relative to the CLASSPATH");
190: method_writer
191: .addCommentLine("@return an ImageIcon instance with the specified image file");
192: method_writer
193: .addCommentLine("@throws IllegalArgumentException if the image resource cannot be loaded.");
194:
195: Block block = new Block();
196: String codeblock = "try"
197: + "{"
198: + "ClassLoader classloader = getClass().getClassLoader();"
199: + "java.net.URL url = classloader.getResource( imageName );"
200: + "if ( url != null )"
201: + "{"
202: + "ImageIcon icon = new ImageIcon( url );"
203: + "return icon;"
204: + "}\n"
205: + "}\n"
206: + "catch( Exception e )"
207: + "{"
208: + "e.printStackTrace();"
209: + "}\n"
210: + "throw new IllegalArgumentException( \"Unable to load image: \" + imageName );";
211:
212: block.addCode(codeblock);
213: method_writer.addSegment(block);
214: declMgr.addMethod(method_writer);
215: }
216:
217: public static void buildApplyComponentOrientation(
218: DeclarationManager declMgr) {
219: MethodWriter method_writer = new MethodWriter(declMgr, null,
220: "applyComponentOrientation") {
221: protected String getSignature() {
222: return "public void applyComponentOrientation( ComponentOrientation orientation )";
223: }
224: };
225:
226: declMgr.addImport("java.awt.ComponentOrientation");
227: declMgr.addImport("com.jeta.open.i18n.I18NUtils");
228:
229: method_writer
230: .addCommentLine("Method for recalculating the component orientation for ");
231: method_writer.addCommentLine("right-to-left Locales.");
232: method_writer
233: .addCommentLine("@param orientation the component orientation to be applied");
234:
235: Block block = new Block();
236: StringBuffer codeblock = new StringBuffer();
237: codeblock.append("// Not yet implemented...").append("\n");
238: codeblock
239: .append("// I18NUtils.applyComponentOrientation(this, orientation);");
240: codeblock
241: .append("super.applyComponentOrientation(orientation);");
242:
243: block.addCode(codeblock.toString());
244: method_writer.addSegment(block);
245: declMgr.addMethod(method_writer);
246: }
247: }
|