001: /*
002: * Enhydra Java Application Server Project
003: *
004: * The contents of this file are subject to the Enhydra Public License
005: * Version 1.1 (the "License"); you may not use this file except in
006: * compliance with the License. You may obtain a copy of the License on
007: * the Enhydra web site ( http://www.enhydra.org/ ).
008: *
009: * Software distributed under the License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
011: * the License for the specific terms governing rights and limitations
012: * under the License.
013: *
014: * The Initial Developer of the Enhydra Application Server is Lutris
015: * Technologies, Inc. The Enhydra Application Server and portions created
016: * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
017: * All Rights Reserved.
018: *
019: * Contributor(s):
020: * Paul Mahar
021: *
022: */
023: package org.enhydra.kelp.common.codegen;
024:
025: // ToolBox
026: import org.enhydra.tool.common.PathHandle;
027: import org.enhydra.tool.codegen.ProjectGenerator;
028: import org.enhydra.tool.codegen.ProjectOptionSet;
029: import org.enhydra.tool.codegen.CodeGen;
030: import org.enhydra.tool.codegen.OptionSet;
031: import org.enhydra.tool.codegen.GeneratorException;
032: import org.enhydra.tool.codegen.wizard.CodeGenPanel;
033:
034: // AddinCore
035: import org.enhydra.kelp.common.node.OtterProject;
036: import org.enhydra.kelp.common.importer.ImportPaths;
037: import org.enhydra.kelp.common.importer.ImportTool;
038: import org.enhydra.kelp.common.ValidationException;
039:
040: // JDK
041: import java.awt.Component;
042: import java.io.File;
043: import java.util.ResourceBundle;
044: import javax.swing.JOptionPane;
045:
046: //
047: abstract public class ProjectGenUtil {
048:
049: // strings not to be resourced
050: static ResourceBundle res = ResourceBundle
051: .getBundle("org.enhydra.kelp.common.Res"); // nores
052: private final static String EXCEPTION_CREATE_GENERATOR = "ProjectGenUtil.createGenerator()"; // nores
053:
054: //
055: private OtterProject project = null;
056: private CodeGen codeGen = null;
057: private int option = JOptionPane.CANCEL_OPTION;
058:
059: public static ProjectGenerator createGenerator()
060: throws CodeGenException {
061: throw new CodeGenException(
062: ProjectGenUtil.EXCEPTION_CREATE_GENERATOR);
063: }
064:
065: /**
066: * Constructor declaration
067: *
068: *
069: */
070: public ProjectGenUtil(ProjectGenerator gen) throws CodeGenException {
071: try {
072: codeGen = new CodeGen(gen);
073: } catch (GeneratorException e) {
074: throw new CodeGenException(e);
075: }
076: }
077:
078: public ProjectGenUtil(OtterProject proj, ProjectGenerator gen)
079: throws CodeGenException {
080: this (gen);
081: init(proj);
082: }
083:
084: public CodeGen getCodeGen() {
085: return codeGen;
086: }
087:
088: public void init(OtterProject proj) throws CodeGenException {
089: project = proj;
090: try {
091: initOptions();
092: } catch (GeneratorException e) {
093: throw new CodeGenException(e);
094: }
095: }
096:
097: /**
098: * Method declaration
099: *
100: *
101: * @return
102: *
103: * @exception GeneratorException
104: */
105: public CodeGenPanel[] getWizardPanels() throws CodeGenException {
106: CodeGenPanel[] panels = null;
107:
108: try {
109: panels = getGenerator().getWizardPanels();
110: } catch (GeneratorException e) {
111: throw new CodeGenException(e);
112: }
113: return panels;
114: }
115:
116: /**
117: * Method declaration
118: *
119: *
120: * @return
121: */
122: public OtterProject getProject() {
123: return project;
124: }
125:
126: /**
127: * Method declaration
128: *
129: *
130: * @return
131: *
132: * @exception GeneratorException
133: */
134: public File[] generate() throws CodeGenException {
135: File[] files = new File[0];
136:
137: try {
138: for (int i = 0; i < getGenerator().getWizardPanels().length; i++) {
139: getGenerator().getWizardPanels()[i].writeOptionSet();
140: }
141: getGenerator().setEcho(false);
142: files = getGenerator().generate();
143: } catch (GeneratorException e) {
144: e.printStackTrace(System.err);
145: throw new CodeGenException(e);
146: }
147: importFiles(files);
148: return files;
149: }
150:
151: public ProjectGenerator getGenerator() {
152: ProjectGenerator generator = null;
153:
154: generator = (ProjectGenerator) codeGen.getSelection();
155: return generator;
156: }
157:
158: /**
159: * Method declaration
160: *
161: *
162: * @param owner
163: *
164: * @return
165: */
166: public File[] invokeWizard(Component owner) throws CodeGenException {
167: File[] files = new File[0];
168:
169: files = codeGen.invokeWizard(owner);
170: option = codeGen.getOption();
171: if (option == JOptionPane.CANCEL_OPTION) {
172:
173: // done
174: } else if (files != null) {
175: importFiles(files);
176: }
177: return files;
178: }
179:
180: /**
181: * Method declaration
182: *
183: * @return
184: */
185: public int getOption() {
186: return option;
187: }
188:
189: //
190: //
191:
192: /**
193: * Method declaration
194: *
195: * @return
196: */
197: private String getProjectSourcePath() throws CodeGenException {
198: String path = new String();
199:
200: try {
201: path = getGenerator().getProjectSourcePath();
202: } catch (GeneratorException e) {
203: throw new CodeGenException(e);
204: }
205: return path;
206: }
207:
208: protected ImportPaths getImportPaths() throws CodeGenException {
209: ImportPaths paths = null;
210: String[][] map = new String[0][2];
211: String pack = new String();
212:
213: paths = new ImportPaths();
214: try {
215: pack = getPackage();
216: paths.setRootPath(getGenerator().getDestination());
217: paths.setPackage(pack);
218: paths.setPackageMap(map);
219: paths.setSourcePath(getProjectSourcePath());
220: paths.setProject(project);
221: } catch (ValidationException e) {
222: throw new CodeGenException(e);
223: } catch (GeneratorException e) {
224: throw new CodeGenException(e);
225: }
226: return paths;
227: }
228:
229: /**
230: * Method declaration
231: *
232: * @param files
233: */
234: public void importFiles(File[] files) throws CodeGenException {
235: ImportTool tool = null;
236: ImportPaths paths = null;
237:
238: if (files.length > 0) {
239: tool = new ImportTool();
240: paths = getImportPaths();
241: tool.setPaths(getImportPaths());
242: tool.importFiles(files);
243: String[] path = new String[1];
244:
245: path[0] = paths.getSourcePath();
246: project.setSourcePathArray(path);
247: }
248: }
249:
250: /**
251: * Method declaration
252: *
253: *
254: * @param p
255: */
256: protected void initOptions() throws GeneratorException {
257: PathHandle phDest = null;
258: String projectName = new String();
259: String pack = new String();
260:
261: phDest = PathHandle.createPathHandle(project
262: .getCodeGenDefaultDestination());
263: projectName = project.getProjectRootName();
264: pack = projectName.toLowerCase();
265: getGenerator().getOptionSet().lookup(ProjectOptionSet.ROOT)
266: .setValue(phDest.getPath());
267: getGenerator().getOptionSet().lookup(ProjectOptionSet.PROJECT)
268: .setValue(projectName);
269: getGenerator().getOptionSet().lookup(ProjectOptionSet.PACKAGE)
270: .setValue(pack);
271: getGenerator().getOptionSet().lookup(ProjectOptionSet.NOCLI)
272: .setValue(true);
273: }
274:
275: /**
276: * Method declaration
277: *
278: *
279: * @return
280: *
281: * @exception GeneratorException
282: */
283: private String getPackage() throws GeneratorException {
284: OptionSet optionSet;
285: String pack;
286:
287: optionSet = getGenerator().getOptionSet();
288: pack = optionSet.lookup(ProjectOptionSet.PACKAGE).getValue();
289: return pack;
290: }
291:
292: }
|