1 19 20 package org.netbeans.modules.apisupport.project.ui.wizard.project; 21 22 import java.io.IOException ; 23 import java.io.InputStream ; 24 import java.io.OutputStream ; 25 import java.net.URL ; 26 import java.util.ArrayList ; 27 import java.util.Collection ; 28 import java.util.Collections ; 29 import java.util.HashMap ; 30 import java.util.Iterator ; 31 import java.util.Set ; 32 import java.util.zip.ZipEntry ; 33 import java.util.zip.ZipOutputStream ; 34 import org.netbeans.api.project.Project; 35 import org.netbeans.api.project.ProjectUtils; 36 import org.netbeans.api.project.SourceGroup; 37 import org.netbeans.api.project.Sources; 38 import org.netbeans.api.queries.SharabilityQuery; 39 import org.netbeans.api.queries.VisibilityQuery; 40 import org.netbeans.modules.apisupport.project.CreatedModifiedFiles; 41 import org.netbeans.modules.apisupport.project.ManifestManager; 42 import org.netbeans.modules.apisupport.project.NbModuleProject; 43 import org.netbeans.modules.apisupport.project.Util; 44 import org.netbeans.modules.apisupport.project.layers.LayerUtils; 45 import org.netbeans.modules.apisupport.project.spi.NbModuleProvider; 46 import org.netbeans.modules.apisupport.project.ui.wizard.BasicWizardIterator; 47 import org.openide.ErrorManager; 48 import org.openide.WizardDescriptor; 49 import org.openide.filesystems.FileLock; 50 import org.openide.filesystems.FileObject; 51 import org.openide.filesystems.FileSystem; 52 import org.openide.filesystems.FileUtil; 53 54 59 final class NewProjectIterator extends BasicWizardIterator { 60 61 private NewProjectIterator.DataModel data; 62 63 public static final String [] MODULES = { 64 "org.openide.filesystems", "org.openide.loaders", "org.openide.dialogs", "org.openide.util", "org.netbeans.modules.projectuiapi", "org.netbeans.modules.projectapi", "org.openide.awt" }; 72 73 private NewProjectIterator() { }; 74 75 public static NewProjectIterator createIterator() { 76 return new NewProjectIterator(); 77 } 78 79 public Set instantiate() throws IOException { 80 CreatedModifiedFiles cmf = data.getCreatedModifiedFiles(); 81 cmf.run(); 82 return getCreatedFiles(cmf, data.getProject()); 83 } 84 85 protected BasicWizardIterator.Panel[] createPanels(WizardDescriptor wiz) { 86 data = new NewProjectIterator.DataModel(wiz); 87 return new BasicWizardIterator.Panel[] { 88 new SelectProjectPanel(wiz, data), 89 new NameAndLocationPanel(wiz, data) 90 }; 91 } 92 93 public void uninitialize(WizardDescriptor wiz) { 94 super.uninitialize(wiz); 95 data = null; 96 } 97 98 static final class DataModel extends BasicWizardIterator.BasicDataModel { 99 100 private Project template; 101 private String name; 102 private String displayName; 103 private String category; 104 105 private CreatedModifiedFiles files; 106 107 DataModel(WizardDescriptor wiz) { 108 super(wiz); 109 } 110 111 public CreatedModifiedFiles getCreatedModifiedFiles() { 112 return getFiles(); 113 } 114 115 public void setCreatedModifiedFiles(CreatedModifiedFiles files) { 116 this.setFiles(files); 117 } 118 119 public Project getTemplate() { 120 return template; 121 } 122 123 public void setTemplate(Project template) { 124 this.template = template; 125 } 126 127 public String getName() { 128 return name; 129 } 130 131 public void setName(String name) { 132 this.name = name; 133 } 134 135 public String getDisplayName() { 136 return displayName; 137 } 138 139 public void setDisplayName(String displayName) { 140 this.displayName = displayName; 141 } 142 143 public String getCategory() { 144 return category; 145 } 146 147 public void setCategory(String category) { 148 this.category = category; 149 } 150 151 public CreatedModifiedFiles getFiles() { 152 return files; 153 } 154 155 public void setFiles(CreatedModifiedFiles files) { 156 this.files = files; 157 } 158 159 } 160 161 public static void generateFileChanges(DataModel model) { 162 CreatedModifiedFiles fileChanges = new CreatedModifiedFiles(model.getProject()); 163 Project project = model.getProject(); 164 NbModuleProvider moduleInfo = model.getModuleInfo(); 165 final String category = model.getCategory(); 166 final String displayName = model.getDisplayName(); 167 final String name = model.getName(); 168 final String packageName = model.getPackageName(); 169 170 HashMap replaceTokens = new HashMap (); 171 replaceTokens.put("@@CATEGORY@@", category); replaceTokens.put("@@DISPLAYNAME@@", displayName); replaceTokens.put("@@TEMPLATENAME@@", name); replaceTokens.put("@@PACKAGENAME@@", packageName); 176 177 final String descName = getRelativePath(moduleInfo.getResourceDirectoryPath(false), packageName, 179 name, "Description.html"); URL template = NewProjectIterator.class.getResource("templateDescription.html"); fileChanges.add(fileChanges.createFileWithSubstitutions(descName, template, replaceTokens)); 183 184 for (int i = 0; i < MODULES.length; i++) { 186 fileChanges.add(fileChanges.addModuleDependency(MODULES[i])); 187 } 188 189 fileChanges.add(fileChanges.bundleKeyDefaultBundle(category + "/" + name + "Project.zip", displayName)); String bundlePath = getRelativePath(moduleInfo.getResourceDirectoryPath(false), packageName, "", "Bundle.properties"); fileChanges.add(fileChanges.bundleKey(bundlePath, "LBL_CreateProjectStep", "Name and Location")); 193 FileObject xml = LayerUtils.layerForProject(project).getLayerFile(); 195 FileObject parent = xml != null ? xml.getParent() : null; 196 Set externalFiles = Collections.singleton(LayerUtils.findGeneratedName(parent, name + "Project.zip")); fileChanges.add(fileChanges.layerModifications( 200 new CreateProjectZipOperation(model.getTemplate(), name, packageName, 201 category, ManifestManager.getInstance(Util.getManifest(moduleInfo.getManifestFile()), false)),externalFiles)); 202 203 final String iteratorName = getRelativePath(moduleInfo.getSourceDirectoryPath(), packageName, 205 name, "WizardIterator.java"); template = NewProjectIterator.class.getResource("templateWizardIterator.javx"); fileChanges.add(fileChanges.createFileWithSubstitutions(iteratorName, template, replaceTokens)); 209 final String panelName = getRelativePath(moduleInfo.getSourceDirectoryPath(), packageName, 210 name, "WizardPanel.java"); template = NewProjectIterator.class.getResource("templateWizardPanel.javx"); fileChanges.add(fileChanges.createFileWithSubstitutions(panelName, template, replaceTokens)); 214 215 final String formName = getRelativePath(moduleInfo.getSourceDirectoryPath(), packageName, 216 name, "PanelVisual.form"); template = NewProjectIterator.class.getResource("templatePanelVisual.frmx"); fileChanges.add(fileChanges.createFileWithSubstitutions(formName, template, replaceTokens)); 220 221 final String panelVisName = getRelativePath(moduleInfo.getSourceDirectoryPath(), packageName, 222 name, "PanelVisual.java"); template = NewProjectIterator.class.getResource("templatePanelVisual.javx"); fileChanges.add(fileChanges.createFileWithSubstitutions(panelVisName, template, replaceTokens)); 226 227 228 model.setCreatedModifiedFiles(fileChanges); 229 } 230 231 private static String getRelativePath(String rootPath, String fullyQualifiedPackageName, 232 String prefix, String postfix) { 233 StringBuffer sb = new StringBuffer (); 234 sb.append(rootPath).append('/'). 235 append(fullyQualifiedPackageName.replace('.','/')). 236 append('/').append(prefix).append(postfix); 237 return sb.toString(); 238 } 239 240 private static void createProjectZip(OutputStream target, Project source) throws IOException { 241 Sources srcs = ProjectUtils.getSources(source); SourceGroup[] grps = srcs.getSourceGroups(Sources.TYPE_GENERIC); 244 SourceGroup group = grps[0]; 245 Collection files = new ArrayList (); 246 collectFiles(group.getRootFolder(), files, 247 SharabilityQuery.getSharability(FileUtil.toFile(group.getRootFolder()))); 248 createZipFile(target, group.getRootFolder(), files); 249 } 250 251 private static void collectFiles(FileObject parent, Collection accepted, int parentSharab) { 252 FileObject[] fos = parent.getChildren(); 253 for (int i = 0; i < fos.length; i++) { 254 if (!VisibilityQuery.getDefault().isVisible(fos[i])) { 255 continue; 258 } 259 int sharab; 260 if (parentSharab == SharabilityQuery.UNKNOWN || parentSharab == SharabilityQuery.MIXED) { 261 sharab = SharabilityQuery.getSharability(FileUtil.toFile(fos[i])); 262 } else { 263 sharab = parentSharab; 264 } 265 if (fos[i].isData() && !fos[i].isVirtual() && sharab == SharabilityQuery.SHARABLE) { 266 accepted.add(fos[i]); 267 } else if (fos[i].isFolder() && sharab != SharabilityQuery.NOT_SHARABLE) { 268 accepted.add(fos[i]); 269 collectFiles(fos[i], accepted, sharab); 270 } 271 } 272 } 273 274 private static void createZipFile(OutputStream target, FileObject root, Collection files) throws IOException { 275 ZipOutputStream str = null; 277 try { 278 str = new ZipOutputStream (target); 279 Iterator it = files.iterator(); 280 while (it.hasNext()) { 281 FileObject fo = (FileObject)it.next(); 282 String path = FileUtil.getRelativePath(root, fo); 283 if (fo.isFolder() && !path.endsWith("/")) { 284 path = path + "/"; 285 } 286 ZipEntry entry = new ZipEntry (path); 287 str.putNextEntry(entry); 288 if (fo.isData()) { 289 InputStream in = null; 290 try { 291 in = fo.getInputStream(); 292 FileUtil.copy(in, str); 293 } finally { 294 if (in != null) { 295 in.close(); 296 } 297 } 298 } 299 str.closeEntry(); 300 } 301 } finally { 302 if (str != null) { 303 str.close(); 304 } 305 } 306 } 307 308 static class CreateProjectZipOperation implements CreatedModifiedFiles.LayerOperation { 309 310 private final String name; 311 private final String packageName; 312 private final Project templateProject; 313 private final String category; 314 private final ManifestManager manifestManager; 315 316 public CreateProjectZipOperation(Project template, String name, String packageName, 317 String category, ManifestManager manifestManager) { 318 this.packageName = packageName; 319 this.name = name; 320 this.category = category; 321 this.manifestManager = manifestManager; 322 templateProject = template; 323 } 324 325 public void run(FileSystem layer) throws IOException { 326 FileObject folder = layer.getRoot().getFileObject(category); if (folder == null) { 328 folder = FileUtil.createFolder(layer.getRoot(), category); } 330 FileObject file = folder.createData(name + "Project", "zip"); FileLock lock = file.lock(); 332 try { 333 createProjectZip(file.getOutputStream(lock), templateProject); 334 } catch (IOException e) { 335 Util.err.notify(ErrorManager.INFORMATIONAL, e); 336 } finally { 337 lock.releaseLock(); 338 } 339 String bundlePath = manifestManager.getLocalizingBundle(); 340 String suffix = ".properties"; if (bundlePath != null && bundlePath.endsWith(suffix)) { 342 bundlePath = bundlePath.substring(0, bundlePath.length() - suffix.length()).replace('/', '.'); 343 file.setAttribute("SystemFileSystem.localizingBundle", bundlePath); } else { 345 } 347 file.setAttribute("template", Boolean.TRUE); URL descURL = new URL ("nbresloc:/" + packageName.replace('.', '/') + "/" + name + "Description.html"); file.setAttribute("instantiatingWizardURL", descURL); file.setAttribute("instantiatingIterator", "methodvalue:" + packageName + "." + name + "WizardIterator.createIterator"); } 352 } 353 354 } 355 | Popular Tags |