KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > kelp > common > codegen > ProjectGenUtil


1 /*
2  * Enhydra Java Application Server Project
3  *
4  * The contents of this file are subject to the Enhydra Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License on
7  * the Enhydra web site ( http://www.enhydra.org/ ).
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
11  * the License for the specific terms governing rights and limitations
12  * under the License.
13  *
14  * The Initial Developer of the Enhydra Application Server is Lutris
15  * Technologies, Inc. The Enhydra Application Server and portions created
16  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
17  * All Rights Reserved.
18  *
19  * Contributor(s):
20  * Paul Mahar
21  *
22  */

23 package org.enhydra.kelp.common.codegen;
24
25 // ToolBox
26
import org.enhydra.tool.common.ExtensionFilter;
27 import org.enhydra.tool.common.PathHandle;
28 import org.enhydra.tool.codegen.ProjectGenerator;
29 import org.enhydra.tool.codegen.ProjectOptionSet;
30 import org.enhydra.tool.codegen.CodeGen;
31 import org.enhydra.tool.codegen.OptionSet;
32 import org.enhydra.tool.codegen.GeneratorException;
33 import org.enhydra.tool.codegen.wizard.CodeGenWizard;
34 import org.enhydra.tool.codegen.wizard.CodeGenPanel;
35
36 // AddinCore
37
import org.enhydra.kelp.KelpInfo;
38 import org.enhydra.kelp.common.Constants;
39 import org.enhydra.kelp.common.map.Mapper;
40 import org.enhydra.kelp.common.node.OtterNode;
41 import org.enhydra.kelp.common.node.OtterProject;
42 import org.enhydra.kelp.common.node.OtterFolderNode;
43 import org.enhydra.kelp.common.node.OtterJavaFileNode;
44 import org.enhydra.kelp.common.node.OtterTextFileNode;
45 import org.enhydra.kelp.common.node.OtterNodeFactory;
46 import org.enhydra.kelp.common.importer.ImportPaths;
47 import org.enhydra.kelp.common.importer.ImportTool;
48 import org.enhydra.kelp.common.ValidationException;
49
50 // JDK
51
import java.awt.Component JavaDoc;
52 import java.io.File JavaDoc;
53 import java.io.FileInputStream JavaDoc;
54 import java.io.FilenameFilter JavaDoc;
55 import java.util.Enumeration JavaDoc;
56 import java.util.Properties JavaDoc;
57 import java.util.Vector JavaDoc;
58 import java.util.ResourceBundle JavaDoc;
59 import javax.swing.JOptionPane JavaDoc;
60
61 //
62
abstract public class ProjectGenUtil {
63
64     // strings not to be resourced
65
static ResourceBundle JavaDoc res =
66         ResourceBundle.getBundle("org.enhydra.kelp.common.Res"); // nores
67
private final static String JavaDoc EXCEPTION_CREATE_GENERATOR =
68         "ProjectGenUtil.createGenerator()"; // nores
69

70     //
71
private OtterProject project = null;
72     private CodeGen codeGen = null;
73     private int option = JOptionPane.CANCEL_OPTION;
74
75     public static ProjectGenerator createGenerator() throws CodeGenException {
76         throw new CodeGenException(ProjectGenUtil.EXCEPTION_CREATE_GENERATOR);
77     }
78
79     /**
80      * Constructor declaration
81      *
82      *
83      */

84     public ProjectGenUtil(ProjectGenerator gen) throws CodeGenException {
85         try {
86             codeGen = new CodeGen(gen);
87         } catch (GeneratorException e) {
88             throw new CodeGenException(e);
89         }
90     }
91
92     public ProjectGenUtil(OtterProject proj,
93                           ProjectGenerator gen) throws CodeGenException {
94         this(gen);
95         init(proj);
96     }
97
98     public CodeGen getCodeGen() {
99         return codeGen;
100     }
101
102     public void init(OtterProject proj) throws CodeGenException {
103         project = proj;
104         try {
105             initOptions();
106         } catch (GeneratorException e) {
107             throw new CodeGenException(e);
108         }
109     }
110
111     /**
112      * Method declaration
113      *
114      *
115      * @return
116      *
117      * @exception GeneratorException
118      */

119     public CodeGenPanel[] getWizardPanels() throws CodeGenException {
120         CodeGenPanel[] panels = null;
121
122         try {
123             panels = getGenerator().getWizardPanels();
124         } catch (GeneratorException e) {
125             throw new CodeGenException(e);
126         }
127         return panels;
128     }
129
130     /**
131      * Method declaration
132      *
133      *
134      * @return
135      */

136     public OtterProject getProject() {
137         return project;
138     }
139
140     /**
141      * Method declaration
142      *
143      *
144      * @return
145      *
146      * @exception GeneratorException
147      */

148     public File JavaDoc[] generate() throws CodeGenException {
149         File JavaDoc[] files = new File JavaDoc[0];
150
151         try {
152             for (int i = 0; i < getGenerator().getWizardPanels().length;
153                     i++) {
154                 getGenerator().getWizardPanels()[i].writeOptionSet();
155             }
156             getGenerator().setEcho(false);
157             files = getGenerator().generate();
158         } catch (GeneratorException e) {
159             e.printStackTrace(System.err);
160             throw new CodeGenException(e);
161         }
162         importFiles(files);
163         return files;
164     }
165
166     public ProjectGenerator getGenerator() {
167         ProjectGenerator generator = null;
168
169         generator = (ProjectGenerator) codeGen.getSelection();
170         return generator;
171     }
172
173     /**
174      * Method declaration
175      *
176      *
177      * @param owner
178      *
179      * @return
180      */

181     public File JavaDoc[] invokeWizard(Component JavaDoc owner) throws CodeGenException {
182         File JavaDoc[] files = new File JavaDoc[0];
183
184         files = codeGen.invokeWizard(owner);
185         option = codeGen.getOption();
186         if (option == JOptionPane.CANCEL_OPTION) {
187
188             // done
189
} else if (files != null) {
190             importFiles(files);
191         }
192         return files;
193     }
194
195     /**
196      * Method declaration
197      *
198      * @return
199      */

200     public int getOption() {
201         return option;
202     }
203
204     //
205
//
206

207     /**
208      * Method declaration
209      *
210      * @return
211      */

212     private String JavaDoc getProjectSourcePath() throws CodeGenException {
213         String JavaDoc path = new String JavaDoc();
214
215         try {
216             path = getGenerator().getProjectSourcePath();
217         } catch (GeneratorException e) {
218             throw new CodeGenException(e);
219         }
220         return path;
221     }
222
223     protected ImportPaths getImportPaths() throws CodeGenException {
224         ImportPaths paths = null;
225         String JavaDoc[][] map = new String JavaDoc[0][2];
226         String JavaDoc pack = new String JavaDoc();
227
228         paths = new ImportPaths();
229         try {
230             pack = getPackage();
231             paths.setRootPath(getGenerator().getDestination());
232             paths.setPackage(pack);
233             paths.setPackageMap(map);
234             paths.setSourcePath(getProjectSourcePath());
235             paths.setProject(project);
236         } catch (ValidationException e) {
237             throw new CodeGenException(e);
238         } catch (GeneratorException e) {
239             throw new CodeGenException(e);
240         }
241         return paths;
242     }
243
244     /**
245      * Method declaration
246      *
247      * @param files
248      */

249     public void importFiles(File JavaDoc[] files) throws CodeGenException {
250         ImportTool tool = null;
251         ImportPaths paths = null;
252
253         if (files.length > 0) {
254             tool = new ImportTool();
255             paths = getImportPaths();
256             tool.setPaths(getImportPaths());
257             tool.importFiles(files);
258             String JavaDoc[] path = new String JavaDoc[1];
259
260             path[0] = paths.getSourcePath();
261             project.setSourcePathArray(path);
262         }
263     }
264
265     /**
266      * Method declaration
267      *
268      *
269      * @param p
270      */

271     protected void initOptions() throws GeneratorException {
272         File JavaDoc file = null;
273         PathHandle phDest = null;
274         String JavaDoc projectName = new String JavaDoc();
275         String JavaDoc pack = new String JavaDoc();
276         int index = -1;
277
278         phDest =
279             PathHandle.createPathHandle(project.getCodeGenDefaultDestination());
280         projectName = project.getProjectRootName();
281         pack = projectName.toLowerCase();
282         getGenerator().getOptionSet().lookup(ProjectOptionSet.ROOT).setValue(phDest.getPath());
283         getGenerator().getOptionSet().lookup(ProjectOptionSet.PROJECT).setValue(projectName);
284         getGenerator().getOptionSet().lookup(ProjectOptionSet.PACKAGE).setValue(pack);
285         getGenerator().getOptionSet().lookup(ProjectOptionSet.NOCLI).setValue(true);
286     }
287
288     /**
289      * Method declaration
290      *
291      *
292      * @return
293      *
294      * @exception GeneratorException
295      */

296     private String JavaDoc getPackage() throws GeneratorException {
297         OptionSet optionSet;
298         String JavaDoc pack;
299
300         optionSet = getGenerator().getOptionSet();
301         pack = optionSet.lookup(ProjectOptionSet.PACKAGE).getValue();
302         return pack;
303     }
304
305 }
306
Popular Tags