1 23 package org.enhydra.kelp.common.codegen; 24 25 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 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 import java.awt.Component ; 52 import java.io.File ; 53 import java.io.FileInputStream ; 54 import java.io.FilenameFilter ; 55 import java.util.Enumeration ; 56 import java.util.Properties ; 57 import java.util.Vector ; 58 import java.util.ResourceBundle ; 59 import javax.swing.JOptionPane ; 60 61 abstract public class ProjectGenUtil { 63 64 static ResourceBundle res = 66 ResourceBundle.getBundle("org.enhydra.kelp.common.Res"); private final static String EXCEPTION_CREATE_GENERATOR = 68 "ProjectGenUtil.createGenerator()"; 70 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 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 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 136 public OtterProject getProject() { 137 return project; 138 } 139 140 148 public File [] generate() throws CodeGenException { 149 File [] files = new File [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 181 public File [] invokeWizard(Component owner) throws CodeGenException { 182 File [] files = new File [0]; 183 184 files = codeGen.invokeWizard(owner); 185 option = codeGen.getOption(); 186 if (option == JOptionPane.CANCEL_OPTION) { 187 188 } else if (files != null) { 190 importFiles(files); 191 } 192 return files; 193 } 194 195 200 public int getOption() { 201 return option; 202 } 203 204 207 212 private String getProjectSourcePath() throws CodeGenException { 213 String path = new String (); 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 [][] map = new String [0][2]; 226 String pack = new String (); 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 249 public void importFiles(File [] 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 [] path = new String [1]; 259 260 path[0] = paths.getSourcePath(); 261 project.setSourcePathArray(path); 262 } 263 } 264 265 271 protected void initOptions() throws GeneratorException { 272 File file = null; 273 PathHandle phDest = null; 274 String projectName = new String (); 275 String pack = new String (); 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 296 private String getPackage() throws GeneratorException { 297 OptionSet optionSet; 298 String pack; 299 300 optionSet = getGenerator().getOptionSet(); 301 pack = optionSet.lookup(ProjectOptionSet.PACKAGE).getValue(); 302 return pack; 303 } 304 305 } 306 | Popular Tags |