KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > apisupport > project > ui > wizard > project > NewProjectIterator


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.apisupport.project.ui.wizard.project;
21
22 import java.io.IOException JavaDoc;
23 import java.io.InputStream JavaDoc;
24 import java.io.OutputStream JavaDoc;
25 import java.net.URL JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.Collection JavaDoc;
28 import java.util.Collections JavaDoc;
29 import java.util.HashMap JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.Set JavaDoc;
32 import java.util.zip.ZipEntry JavaDoc;
33 import java.util.zip.ZipOutputStream JavaDoc;
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 /**
55  * Wizard for creating new project templates.
56  *
57  * @author Milos Kleint
58  */

59 final class NewProjectIterator extends BasicWizardIterator {
60     
61     private NewProjectIterator.DataModel data;
62     
63     public static final String JavaDoc[] MODULES = {
64         "org.openide.filesystems", // NOI18N
65
"org.openide.loaders", // NOI18N
66
"org.openide.dialogs", // NOI18N
67
"org.openide.util", // NOI18N
68
"org.netbeans.modules.projectuiapi", // NOI18N
69
"org.netbeans.modules.projectapi", // NOI18N
70
"org.openide.awt" // NOI18N
71
};
72     
73     private NewProjectIterator() { /* Use factory method. */ };
74     
75     public static NewProjectIterator createIterator() {
76         return new NewProjectIterator();
77     }
78     
79     public Set JavaDoc instantiate() throws IOException JavaDoc {
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 JavaDoc name;
102         private String JavaDoc displayName;
103         private String JavaDoc 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 JavaDoc getName() {
128             return name;
129         }
130         
131         public void setName(String JavaDoc name) {
132             this.name = name;
133         }
134         
135         public String JavaDoc getDisplayName() {
136             return displayName;
137         }
138         
139         public void setDisplayName(String JavaDoc displayName) {
140             this.displayName = displayName;
141         }
142         
143         public String JavaDoc getCategory() {
144             return category;
145         }
146         
147         public void setCategory(String JavaDoc 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 JavaDoc category = model.getCategory();
166         final String JavaDoc displayName = model.getDisplayName();
167         final String JavaDoc name = model.getName();
168         final String JavaDoc packageName = model.getPackageName();
169         
170         HashMap JavaDoc replaceTokens = new HashMap JavaDoc();
171         replaceTokens.put("@@CATEGORY@@", category);//NOI18N
172
replaceTokens.put("@@DISPLAYNAME@@", displayName);//NOI18N
173
replaceTokens.put("@@TEMPLATENAME@@", name);//NOI18N
174
replaceTokens.put("@@PACKAGENAME@@", packageName);//NOI18N
175

176         
177         // 1. create project description file
178
final String JavaDoc descName = getRelativePath(moduleInfo.getResourceDirectoryPath(false), packageName,
179                 name, "Description.html"); //NOI18N
180
// XXX use nbresloc URL protocol rather than NewLoaderIterator.class.getResource(...):
181
URL JavaDoc template = NewProjectIterator.class.getResource("templateDescription.html");//NOI18N
182
fileChanges.add(fileChanges.createFileWithSubstitutions(descName, template, replaceTokens));
183         
184         // 2. update project dependencies
185
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)); // NOI18N
190
String JavaDoc bundlePath = getRelativePath(moduleInfo.getResourceDirectoryPath(false), packageName, "", "Bundle.properties");//NOI18N
191
fileChanges.add(fileChanges.bundleKey(bundlePath, "LBL_CreateProjectStep", "Name and Location")); // NOI18N
192

193         // 3. create sample template
194
FileObject xml = LayerUtils.layerForProject(project).getLayerFile();
195         FileObject parent = xml != null ? xml.getParent() : null;
196         // XXX this is not fully accurate since if two ops would both create the same file,
197
// really the second one would automatically generate a uniquified name... but close enough!
198
Set JavaDoc externalFiles = Collections.singleton(LayerUtils.findGeneratedName(parent, name + "Project.zip")); // NOI18N
199
fileChanges.add(fileChanges.layerModifications(
200                 new CreateProjectZipOperation(model.getTemplate(), name, packageName,
201                 category, ManifestManager.getInstance(Util.getManifest(moduleInfo.getManifestFile()), false)),externalFiles));
202         
203         // x. generate java classes
204
final String JavaDoc iteratorName = getRelativePath(moduleInfo.getSourceDirectoryPath(), packageName,
205                 name, "WizardIterator.java"); //NOI18N
206
// XXX use nbresloc URL protocol rather than NewLoaderIterator.class.getResource(...):
207
template = NewProjectIterator.class.getResource("templateWizardIterator.javx");//NOI18N
208
fileChanges.add(fileChanges.createFileWithSubstitutions(iteratorName, template, replaceTokens));
209         final String JavaDoc panelName = getRelativePath(moduleInfo.getSourceDirectoryPath(), packageName,
210                 name, "WizardPanel.java"); //NOI18N
211
// XXX use nbresloc URL protocol rather than NewLoaderIterator.class.getResource(...):
212
template = NewProjectIterator.class.getResource("templateWizardPanel.javx");//NOI18N
213
fileChanges.add(fileChanges.createFileWithSubstitutions(panelName, template, replaceTokens));
214         
215         final String JavaDoc formName = getRelativePath(moduleInfo.getSourceDirectoryPath(), packageName,
216                 name, "PanelVisual.form"); //NOI18N
217
// XXX use nbresloc URL protocol rather than NewLoaderIterator.class.getResource(...):
218
template = NewProjectIterator.class.getResource("templatePanelVisual.frmx");//NOI18N
219
fileChanges.add(fileChanges.createFileWithSubstitutions(formName, template, replaceTokens));
220         
221         final String JavaDoc panelVisName = getRelativePath(moduleInfo.getSourceDirectoryPath(), packageName,
222                 name, "PanelVisual.java"); //NOI18N
223
// XXX use nbresloc URL protocol rather than NewLoaderIterator.class.getResource(...):
224
template = NewProjectIterator.class.getResource("templatePanelVisual.javx");//NOI18N
225
fileChanges.add(fileChanges.createFileWithSubstitutions(panelVisName, template, replaceTokens));
226         
227         
228         model.setCreatedModifiedFiles(fileChanges);
229     }
230     
231     private static String JavaDoc getRelativePath(String JavaDoc rootPath, String JavaDoc fullyQualifiedPackageName,
232             String JavaDoc prefix, String JavaDoc postfix) {
233         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
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 JavaDoc target, Project source) throws IOException JavaDoc {
241         Sources srcs = ProjectUtils.getSources(source); // #63247: don't use lookup directly
242
// assuming we got 1-sized array, should be enforced by UI.
243
SourceGroup[] grps = srcs.getSourceGroups(Sources.TYPE_GENERIC);
244         SourceGroup group = grps[0];
245         Collection JavaDoc files = new ArrayList JavaDoc();
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 JavaDoc 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                 //#66765
256
// ignore invisible files/folders.. like CVS subdirectory
257
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 JavaDoc target, FileObject root, Collection JavaDoc /* FileObject*/ files) throws IOException JavaDoc {
275         //TODO create the zip..
276
ZipOutputStream JavaDoc str = null;
277         try {
278             str = new ZipOutputStream JavaDoc(target);
279             Iterator JavaDoc it = files.iterator();
280             while (it.hasNext()) {
281                 FileObject fo = (FileObject)it.next();
282                 String JavaDoc path = FileUtil.getRelativePath(root, fo);
283                 if (fo.isFolder() && !path.endsWith("/")) {
284                     path = path + "/";
285                 }
286                 ZipEntry JavaDoc entry = new ZipEntry JavaDoc(path);
287                 str.putNextEntry(entry);
288                 if (fo.isData()) {
289                     InputStream JavaDoc 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 JavaDoc name;
311         private final String JavaDoc packageName;
312         private final Project templateProject;
313         private final String JavaDoc category;
314         private final ManifestManager manifestManager;
315         
316         public CreateProjectZipOperation(Project template, String JavaDoc name, String JavaDoc packageName,
317                 String JavaDoc 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 JavaDoc {
326             FileObject folder = layer.getRoot().getFileObject(category);// NOI18N
327
if (folder == null) {
328                 folder = FileUtil.createFolder(layer.getRoot(), category); // NOI18N
329
}
330             FileObject file = folder.createData(name + "Project", "zip"); // NOI18N
331
FileLock lock = file.lock();
332             try {
333                 createProjectZip(file.getOutputStream(lock), templateProject);
334             } catch (IOException JavaDoc e) {
335                 Util.err.notify(ErrorManager.INFORMATIONAL, e);
336             } finally {
337                 lock.releaseLock();
338             }
339             String JavaDoc bundlePath = manifestManager.getLocalizingBundle();
340             String JavaDoc suffix = ".properties"; // NOI18N
341
if (bundlePath != null && bundlePath.endsWith(suffix)) {
342                 bundlePath = bundlePath.substring(0, bundlePath.length() - suffix.length()).replace('/', '.');
343                 file.setAttribute("SystemFileSystem.localizingBundle", bundlePath); // NOI18N
344
} else {
345                 // XXX what?
346
}
347             file.setAttribute("template", Boolean.TRUE); // NOI18N
348
URL JavaDoc descURL = new URL JavaDoc("nbresloc:/" + packageName.replace('.', '/') + "/" + name + "Description.html"); // NOI18N
349
file.setAttribute("instantiatingWizardURL", descURL); // NOI18N
350
file.setAttribute("instantiatingIterator", "methodvalue:" + packageName + "." + name + "WizardIterator.createIterator"); // NOI18N
351
}
352     }
353     
354 }
355
Popular Tags