KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > apisupport > project > NbModuleProject


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;
21
22 import java.beans.PropertyChangeEvent JavaDoc;
23 import java.beans.PropertyChangeListener JavaDoc;
24 import java.beans.PropertyChangeSupport JavaDoc;
25 import java.io.File JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.io.InputStream JavaDoc;
28 import java.net.URI JavaDoc;
29 import java.net.URL JavaDoc;
30 import java.util.Arrays JavaDoc;
31 import java.util.Enumeration JavaDoc;
32 import java.util.HashMap JavaDoc;
33 import java.util.Iterator JavaDoc;
34 import java.util.Map JavaDoc;
35 import java.util.WeakHashMap JavaDoc;
36 import java.util.jar.Manifest JavaDoc;
37 import javax.swing.Icon JavaDoc;
38 import javax.swing.ImageIcon JavaDoc;
39 import org.netbeans.api.java.classpath.ClassPath;
40 import org.netbeans.api.java.classpath.GlobalPathRegistry;
41 import org.netbeans.api.java.project.JavaProjectConstants;
42 import org.netbeans.api.project.FileOwnerQuery;
43 import org.netbeans.api.project.Project;
44 import org.netbeans.api.project.ProjectManager;
45 import org.netbeans.api.project.ProjectInformation;
46 import org.netbeans.modules.apisupport.project.spi.NbModuleProvider.NbModuleType;
47 import org.netbeans.modules.apisupport.project.metainf.ServiceNodeHandler;
48 import org.netbeans.modules.apisupport.project.queries.ModuleProjectClassPathExtender;
49 import org.netbeans.modules.apisupport.project.ui.customizer.CustomizerProviderImpl;
50 import org.netbeans.modules.apisupport.project.ui.customizer.SingleModuleProperties;
51 import org.netbeans.spi.project.AuxiliaryConfiguration;
52 import org.netbeans.spi.project.support.ant.AntProjectEvent;
53 import org.netbeans.spi.project.support.ant.AntProjectHelper;
54 import org.netbeans.spi.project.support.ant.AntProjectListener;
55 import org.netbeans.spi.project.support.ant.EditableProperties;
56 import org.netbeans.spi.project.support.ant.GeneratedFilesHelper;
57 import org.netbeans.spi.project.support.ant.ProjectXmlSavedHook;
58 import org.netbeans.spi.project.support.ant.PropertyEvaluator;
59 import org.netbeans.spi.project.support.ant.SourcesHelper;
60 import org.netbeans.spi.project.ui.PrivilegedTemplates;
61 import org.netbeans.spi.project.ui.ProjectOpenedHook;
62 import org.netbeans.spi.queries.FileBuiltQueryImplementation;
63 import org.openide.ErrorManager;
64 import org.openide.filesystems.FileChangeAdapter;
65 import org.openide.filesystems.FileEvent;
66 import org.openide.filesystems.FileObject;
67 import org.openide.filesystems.FileUtil;
68 import org.openide.util.Lookup;
69 import org.openide.util.Mutex;
70 import org.openide.util.NbBundle;
71 import org.openide.util.Utilities;
72 import org.openide.util.lookup.Lookups;
73 import org.w3c.dom.Element JavaDoc;
74 import org.netbeans.modules.apisupport.project.queries.AccessibilityQueryImpl;
75 import org.netbeans.modules.apisupport.project.queries.UnitTestForSourceQueryImpl;
76 import org.netbeans.modules.apisupport.project.queries.SourceLevelQueryImpl;
77 import org.netbeans.modules.apisupport.project.queries.AntArtifactProviderImpl;
78 import org.netbeans.modules.apisupport.project.queries.ClassPathProviderImpl;
79 import org.netbeans.modules.apisupport.project.queries.JavadocForBinaryImpl;
80 import org.netbeans.modules.apisupport.project.queries.SourceForBinaryImpl;
81 import org.netbeans.modules.apisupport.project.queries.SubprojectProviderImpl;
82 import org.netbeans.modules.apisupport.project.spi.NbModuleProvider;
83 import org.netbeans.modules.apisupport.project.universe.NbPlatform;
84 import org.netbeans.modules.apisupport.project.universe.ModuleList;
85 import org.netbeans.modules.apisupport.project.ui.ModuleActions;
86 import org.netbeans.modules.apisupport.project.ui.ModuleLogicalView;
87 import org.netbeans.modules.apisupport.project.ui.ModuleOperations;
88 import org.netbeans.modules.apisupport.project.universe.LocalizedBundleInfo;
89 import org.netbeans.modules.apisupport.project.universe.ModuleEntry;
90 import org.netbeans.spi.project.support.LookupProviderSupport;
91 import org.netbeans.spi.project.ui.RecommendedTemplates;
92 import org.netbeans.spi.project.ui.support.UILookupMergerSupport;
93 import org.openide.modules.SpecificationVersion;
94
95 /**
96  * A NetBeans module project.
97  * @author Jesse Glick
98  */

99 public final class NbModuleProject implements Project {
100     
101     public static final String JavaDoc NB_PROJECT_ICON_PATH =
102             "org/netbeans/modules/apisupport/project/resources/module.gif"; // NOI18N
103

104     private static final Icon JavaDoc NB_PROJECT_ICON = new ImageIcon JavaDoc(
105             Utilities.loadImage(NB_PROJECT_ICON_PATH));
106     
107     public static final String JavaDoc SOURCES_TYPE_JAVAHELP = "javahelp"; // NOI18N
108

109     private final AntProjectHelper helper;
110     private final Evaluator eval;
111     private Lookup lookup;
112     private Map JavaDoc<FileObject,Element JavaDoc> extraCompilationUnits;
113     private final GeneratedFilesHelper genFilesHelper;
114     private final NbModuleProviderImpl typeProvider;
115     
116     NbModuleProject(AntProjectHelper helper) throws IOException JavaDoc {
117         this.helper = helper;
118         genFilesHelper = new GeneratedFilesHelper(helper);
119         Util.err.log("Loading project in " + getProjectDirectory());
120         if (getCodeNameBase() == null) {
121             throw new IOException JavaDoc("Misconfigured project in " + FileUtil.getFileDisplayName(getProjectDirectory()) + " has no defined <code-name-base>"); // NOI18N
122
}
123         typeProvider = new NbModuleProviderImpl();
124         if (typeProvider.getModuleType() == NbModuleProvider.NETBEANS_ORG && ModuleList.findNetBeansOrg(getProjectDirectoryFile()) == null) {
125             // #69097: preferable to throwing an assertion error later...
126
throw new IOException JavaDoc("netbeans.org-type module not in a complete netbeans.org source root: " + this); // NOI18N
127
}
128         eval = new Evaluator(this, typeProvider);
129         FileBuiltQueryImplementation fileBuilt;
130         // XXX could add globs for other package roots too
131
if (supportsUnitTests()) {
132             fileBuilt = helper.createGlobFileBuiltQuery(eval, new String JavaDoc[] {
133                 "${src.dir}/*.java", // NOI18N
134
"${test.unit.src.dir}/*.java", // NOI18N
135
}, new String JavaDoc[] {
136                 "${build.classes.dir}/*.class", // NOI18N
137
"${build.test.unit.classes.dir}/*.class", // NOI18N
138
});
139         } else {
140             fileBuilt = helper.createGlobFileBuiltQuery(eval, new String JavaDoc[] {
141                 "${src.dir}/*.java", // NOI18N
142
}, new String JavaDoc[] {
143                 "${build.classes.dir}/*.class", // NOI18N
144
});
145         }
146         final SourcesHelper sourcesHelper = new SourcesHelper(helper, eval);
147         // Temp build dir is always internal; NBM build products go elsewhere, but
148
// difficult to predict statically exactly what they are!
149
// XXX would be good to mark at least the module JAR as owned by this project
150
// (currently FOQ/SH do not support that)
151
sourcesHelper.addPrincipalSourceRoot("${src.dir}", NbBundle.getMessage(NbModuleProject.class, "LBL_source_packages"), null, null); // #56457
152
sourcesHelper.addPrincipalSourceRoot("${test.unit.src.dir}", NbBundle.getMessage(NbModuleProject.class, "LBL_unit_test_packages"), null, null); // #68727
153
sourcesHelper.addTypedSourceRoot("${src.dir}", JavaProjectConstants.SOURCES_TYPE_JAVA, NbBundle.getMessage(NbModuleProject.class, "LBL_source_packages"), null, null);
154         // XXX other principal source roots, as needed...
155
sourcesHelper.addTypedSourceRoot("${test.unit.src.dir}", JavaProjectConstants.SOURCES_TYPE_JAVA, NbBundle.getMessage(NbModuleProject.class, "LBL_unit_test_packages"), null, null);
156         sourcesHelper.addTypedSourceRoot("${test.qa-functional.src.dir}", JavaProjectConstants.SOURCES_TYPE_JAVA, NbBundle.getMessage(NbModuleProject.class, "LBL_functional_test_packages"), null, null);
157         sourcesHelper.addTypedSourceRoot("${test.qa-performance.src.dir}", JavaProjectConstants.SOURCES_TYPE_JAVA, NbBundle.getMessage(NbModuleProject.class, "LBL_performance_test_packages"), null, null);
158         // #42332: also any other misc. test dirs (just add source roots, no CP etc. for now)
159
FileObject testDir = helper.getProjectDirectory().getFileObject("test"); // NOI18N
160
if (testDir != null) {
161             Enumeration JavaDoc<? extends FileObject> kids = testDir.getChildren(false);
162             while (kids.hasMoreElements()) {
163                 FileObject testSubdir = (FileObject) kids.nextElement();
164                 if (!testSubdir.isFolder()) {
165                     continue;
166                 }
167                 String JavaDoc name = testSubdir.getNameExt();
168                 if (testDir.getFileObject("build-" + name + ".xml") == null) { // NOI18N
169
continue;
170                 }
171                 if (name.equals("unit") || name.equals("qa-functional") || name.equals("qa-performance")) { // NOI18N
172
// Already handled specially.
173
continue;
174                 }
175                 sourcesHelper.addTypedSourceRoot("test/" + name + "/src", JavaProjectConstants.SOURCES_TYPE_JAVA, NbBundle.getMessage(NbModuleProject.class, "LBL_unknown_test_packages", name), null, null);
176             }
177         }
178         if (helper.resolveFileObject("javahelp/manifest.mf") == null) { // NOI18N
179
// Special hack for core - ignore core/javahelp
180
sourcesHelper.addTypedSourceRoot("javahelp", SOURCES_TYPE_JAVAHELP, NbBundle.getMessage(NbModuleProject.class, "LBL_javahelp_packages"), null, null);
181         }
182         Iterator JavaDoc it = getExtraCompilationUnits().entrySet().iterator();
183         while (it.hasNext()) {
184             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) it.next();
185             Element JavaDoc ecu = (Element JavaDoc) entry.getValue();
186             Element JavaDoc pkgrootEl = Util.findElement(ecu, "package-root", NbModuleProjectType.NAMESPACE_SHARED); // NOI18N
187
String JavaDoc pkgrootS = Util.findText(pkgrootEl);
188             FileObject pkgroot = (FileObject) entry.getKey();
189             sourcesHelper.addTypedSourceRoot(pkgrootS, JavaProjectConstants.SOURCES_TYPE_JAVA, /* XXX should schema incl. display name? */pkgroot.getNameExt(), null, null);
190         }
191         // #56457: support external source roots too.
192
ProjectManager.mutex().postWriteRequest(new Runnable JavaDoc() {
193             public void run() {
194                 sourcesHelper.registerExternalRoots(FileOwnerQuery.EXTERNAL_ALGORITHM_TRANSIENT);
195             }
196         });
197         lookup = Lookups.fixed(new Object JavaDoc[] {
198             this,
199             new Info(),
200             helper.createAuxiliaryConfiguration(),
201             helper.createCacheDirectoryProvider(),
202             new SavedHook(),
203             new OpenedHook(),
204             new ModuleActions(this),
205             new ClassPathProviderImpl(this),
206             new SourceForBinaryImpl(this),
207             new JavadocForBinaryImpl(this),
208             new UnitTestForSourceQueryImpl(this),
209             new ModuleLogicalView(this),
210             new SubprojectProviderImpl(this),
211             fileBuilt,
212             new AccessibilityQueryImpl(this),
213             new SourceLevelQueryImpl(this),
214             helper.createSharabilityQuery(evaluator(), new String JavaDoc[0], new String JavaDoc[] {
215                 // currently these are hardcoded
216
"build", // NOI18N
217
}),
218             sourcesHelper.createSources(),
219             new AntArtifactProviderImpl(this, helper, evaluator()),
220             new CustomizerProviderImpl(this, getHelper(), evaluator()),
221             new SuiteProviderImpl(),
222             typeProvider,
223             new PrivilegedTemplatesImpl(),
224             new ModuleProjectClassPathExtender(this),
225             new LocalizedBundleInfoProvider(),
226             new ModuleOperations(this),
227             new ServiceNodeHandler(this),
228             LookupProviderSupport.createSourcesMerger(),
229             UILookupMergerSupport.createPrivilegedTemplatesMerger(),
230             UILookupMergerSupport.createRecommendedTemplatesMerger(),
231
232         });
233         lookup = LookupProviderSupport.createCompositeLookup(lookup, "Projects/org-netbeans-modules-apisupport-project/Lookup"); //NOI18N
234
}
235     
236     public String JavaDoc toString() {
237         return "NbModuleProject[" + getProjectDirectory() + "]"; // NOI18N
238
}
239     
240     public Lookup getLookup() {
241         return lookup;
242     }
243     
244     public FileObject getProjectDirectory() {
245         return helper.getProjectDirectory();
246     }
247     
248     public File JavaDoc getProjectDirectoryFile() {
249         return FileUtil.toFile(getProjectDirectory());
250     }
251     
252     /**
253      * Get the minimum harness version required to work with this module.
254      */

255     public int getMinimumHarnessVersion() {
256         if (helper.createAuxiliaryConfiguration().getConfigurationFragment(NbModuleProjectType.NAME_SHARED, NbModuleProjectType.NAMESPACE_SHARED_2, true) != null) {
257             return NbPlatform.HARNESS_VERSION_50;
258         } else {
259             return NbPlatform.HARNESS_VERSION_55u1;
260         }
261     }
262
263     /**
264      * Replacement for {@link AntProjectHelper#getPrimaryConfigurationData}
265      * taking into account the /2 -> /3 upgrade.
266      */

267     public Element JavaDoc getPrimaryConfigurationData() {
268         return ProjectManager.mutex().readAccess(new Mutex.Action<Element JavaDoc>() {
269             public Element JavaDoc run() {
270                 AuxiliaryConfiguration ac = helper.createAuxiliaryConfiguration();
271                 Element JavaDoc data = ac.getConfigurationFragment(NbModuleProjectType.NAME_SHARED, NbModuleProjectType.NAMESPACE_SHARED_2, true);
272                 if (data != null) {
273                     return Util.translateXML(data, NbModuleProjectType.NAMESPACE_SHARED);
274                 } else {
275                     return helper.getPrimaryConfigurationData(true);
276                 }
277             }
278         });
279     }
280
281     /**
282      * Replacement for {@link AntProjectHelper#putPrimaryConfigurationData}
283      * taking into account the /2 -> /3 upgrade.
284      */

285     public void putPrimaryConfigurationData(final Element JavaDoc data) {
286         ProjectManager.mutex().writeAccess(new Mutex.Action<Void JavaDoc>() {
287             public Void JavaDoc run() {
288                 AuxiliaryConfiguration ac = helper.createAuxiliaryConfiguration();
289                 if (ac.getConfigurationFragment(NbModuleProjectType.NAME_SHARED, NbModuleProjectType.NAMESPACE_SHARED_2, true) != null) {
290                     ac.putConfigurationFragment(Util.translateXML(data, NbModuleProjectType.NAMESPACE_SHARED_2), true);
291                 } else {
292                     helper.putPrimaryConfigurationData(data, true);
293                 }
294                 return null;
295             }
296         });
297     }
298
299     /** Returns a relative path to a project's source directory. */
300     public String JavaDoc getSourceDirectoryPath() {
301         return evaluator().getProperty("src.dir"); // NOI18N
302
}
303     
304     private NbModuleProvider.NbModuleType getModuleType() {
305         Element JavaDoc data = getPrimaryConfigurationData();
306         if (Util.findElement(data, "suite-component", NbModuleProjectType.NAMESPACE_SHARED) != null) { // NOI18N
307
return NbModuleProvider.SUITE_COMPONENT;
308         } else if (Util.findElement(data, "standalone", NbModuleProjectType.NAMESPACE_SHARED) != null) { // NOI18N
309
return NbModuleProvider.STANDALONE;
310         } else {
311             return NbModuleProvider.NETBEANS_ORG;
312         }
313     }
314     
315     public FileObject getManifestFile() {
316         return helper.resolveFileObject(evaluator().getProperty("manifest.mf")); // NOI18N
317
}
318     
319     public Manifest JavaDoc getManifest() {
320         return Util.getManifest(getManifestFile());
321     }
322
323     public AntProjectHelper getHelper() {
324         return helper;
325     }
326     
327     public PropertyEvaluator evaluator() {
328         return eval;
329     }
330     
331     private final Map JavaDoc<String JavaDoc,FileObject> directoryCache = new WeakHashMap JavaDoc<String JavaDoc,FileObject>();
332     
333     private FileObject getDir(String JavaDoc prop) {
334         // XXX also add a PropertyChangeListener to eval and clear the cache of changed props
335
if (directoryCache.containsKey(prop)) {
336             return directoryCache.get(prop);
337         } else {
338             String JavaDoc v = evaluator().getProperty(prop);
339             assert v != null : "No value for " + prop;
340             FileObject f = helper.resolveFileObject(v);
341             directoryCache.put(prop, f);
342             return f;
343         }
344     }
345
346     public FileObject getSourceDirectory() {
347         return getDir("src.dir"); // NOI18N
348
}
349     
350     public FileObject getTestSourceDirectory() {
351         return getDir("test.unit.src.dir"); // NOI18N
352
}
353     
354     public FileObject getFunctionalTestSourceDirectory() {
355         return getDir("test.qa-functional.src.dir"); // NOI18N
356
}
357     
358     public FileObject getPerformanceTestSourceDirectory() {
359         return getDir("test.qa-performance.src.dir"); // NOI18N
360
}
361     
362     public File JavaDoc getClassesDirectory() {
363         String JavaDoc classesDir = evaluator().getProperty("build.classes.dir"); // NOI18N
364
return helper.resolveFile(classesDir);
365     }
366     
367     public File JavaDoc getTestClassesDirectory() {
368         String JavaDoc testClassesDir = evaluator().getProperty("build.test.unit.classes.dir"); // NOI18N
369
return helper.resolveFile(testClassesDir);
370     }
371     
372     public FileObject getJavaHelpDirectory() {
373         if (helper.resolveFileObject("javahelp/manifest.mf") != null) { // NOI18N
374
// Special hack for core.
375
return null;
376         }
377         return helper.resolveFileObject("javahelp"); // NOI18N
378
}
379     
380     public File JavaDoc getModuleJarLocation() {
381         // XXX could use ModuleList here instead
382
return helper.resolveFile(evaluator().evaluate("${cluster}/${module.jar}")); // NOI18N
383
}
384     
385     public String JavaDoc getCodeNameBase() {
386         Element JavaDoc config = getPrimaryConfigurationData();
387         Element JavaDoc cnb = Util.findElement(config, "code-name-base", NbModuleProjectType.NAMESPACE_SHARED); // NOI18N
388
if (cnb != null) {
389             return Util.findText(cnb);
390         } else {
391             return null;
392         }
393     }
394     
395     public String JavaDoc getSpecVersion() {
396         //TODO shall we check for illegal cases like "none-defined" or "both-defined" here?
397
Manifest JavaDoc m = getManifest();
398         if (m != null) {
399             String JavaDoc manVersion = m.getMainAttributes().getValue("OpenIDE-Module-Specification-Version"); //NOI18N
400
if (manVersion != null) {
401                 return stripExcessZeros(manVersion);
402             }
403         }
404         return stripExcessZeros(evaluator().getProperty(SingleModuleProperties.SPEC_VERSION_BASE));
405     }
406     private static String JavaDoc stripExcessZeros(String JavaDoc spec) { // #72826
407
return spec != null ? spec.replaceAll("(\\.[0-9]+)\\.0$", "$1") : null; // NOI18N
408
}
409     
410     /**
411      * Slash-separated path inside netbeans.org CVS, or null for external modules.
412      */

413     public String JavaDoc getPathWithinNetBeansOrg() {
414         FileObject nbroot = getNbrootFileObject(null);
415         if (nbroot != null) {
416             return FileUtil.getRelativePath(nbroot, getProjectDirectory());
417         } else {
418             return null;
419         }
420     }
421     
422     private File JavaDoc getNbroot() {
423         return getNbroot(null);
424     }
425     private File JavaDoc getNbroot(PropertyEvaluator eval) {
426         File JavaDoc dir = getProjectDirectoryFile();
427         File JavaDoc nbroot = ModuleList.findNetBeansOrg(dir);
428         if (nbroot != null) {
429             return nbroot;
430         } else {
431             // OK, not it.
432
NbPlatform platform = getPlatform(eval);
433             if (platform != null) {
434                 URL JavaDoc[] roots = platform.getSourceRoots();
435                 for (int i = 0; i < roots.length; i++) {
436                     if (roots[i].getProtocol().equals("file")) { // NOI18N
437
File JavaDoc f = new File JavaDoc(URI.create(roots[i].toExternalForm()));
438                         if (ModuleList.isNetBeansOrg(f)) {
439                             return f;
440                         }
441                     }
442                 }
443             }
444             // Did not find it.
445
return null;
446         }
447     }
448     
449     public File JavaDoc getNbrootFile(String JavaDoc path) {
450         return getNbrootFile(path, null);
451     }
452     File JavaDoc getNbrootFile(String JavaDoc path, PropertyEvaluator eval) {
453         File JavaDoc nbroot = getNbroot(eval);
454         if (nbroot != null) {
455             return new File JavaDoc(nbroot, path.replace('/', File.separatorChar));
456         } else {
457             return null;
458         }
459     }
460     
461     public FileObject getNbrootFileObject(String JavaDoc path) {
462         File JavaDoc f = path != null ? getNbrootFile(path) : getNbroot();
463         if (f != null) {
464             return FileUtil.toFileObject(f);
465         } else {
466             return null;
467         }
468     }
469     
470     public ModuleList getModuleList() throws IOException JavaDoc {
471         NbPlatform p = getPlatform(false);
472         if (p == null) {
473             // #67148: have to use something... (and getEntry(codeNameBase) will certainly fail!)
474
return ModuleList.getModuleList(getProjectDirectoryFile(), NbPlatform.getDefaultPlatform().getDestDir());
475         }
476         ModuleList ml = ModuleList.getModuleList(getProjectDirectoryFile(), p.getDestDir());
477         if (ml.getEntry(getCodeNameBase()) == null) {
478             ModuleList.refresh();
479             ml = ModuleList.getModuleList(getProjectDirectoryFile());
480             if (ml.getEntry(getCodeNameBase()) == null) {
481                 // XXX try to give better diagnostics - as examples are discovered
482
Util.err.log(ErrorManager.WARNING, "Project in " + FileUtil.getFileDisplayName(getProjectDirectory()) + " does not appear to be listed in its own module list; some sort of misconfiguration (e.g. not listed in its own suite)"); // NOI18N
483
}
484         }
485         return ml;
486         /*
487         } catch (IOException e) {
488             // #60094: see if we can fix it quietly by resetting platform to default.
489             FileObject platformPropertiesFile = null;
490             if (typeProvider.getModuleType() == NbModuleProvider.STANDALONE) {
491                 platformPropertiesFile = getProjectDirectory().getFileObject("nbproject/platform.properties"); // NOI18N
492             } else if (typeProvider.getModuleType() == NbModuleProvider.SUITE_COMPONENT) {
493                 PropertyEvaluator baseEval = PropertyUtils.sequentialPropertyEvaluator(
494                         getHelper().getStockPropertyPreprovider(),
495                         new PropertyProvider[] {
496                             getHelper().getPropertyProvider("nbproject/private/suite-private.properties"), // NOI18N
497                             getHelper().getPropertyProvider("nbproject/suite.properties"), // NOI18N
498                         });
499                 String suiteDirS = baseEval.getProperty("suite.dir"); // NOI18N
500                 if (suiteDirS != null) {
501                     FileObject suiteDir = getHelper().resolveFileObject(suiteDirS);
502                     if (suiteDir != null) {
503                         platformPropertiesFile = suiteDir.getFileObject("nbproject/platform.properties"); // NOI18N
504                     }
505                 }
506             }
507             if (platformPropertiesFile != null) {
508                 try {
509                     EditableProperties ep = Util.loadProperties(platformPropertiesFile);
510                     if (!NbPlatform.PLATFORM_ID_DEFAULT.equals(ep.getProperty("nbplatform.active"))) { // NOI18N
511                         ep.setProperty("nbplatform.active", NbPlatform.PLATFORM_ID_DEFAULT); // NOI18N
512                         Util.storeProperties(platformPropertiesFile, ep);
513                     } else {
514                         // That wasn't it, never mind.
515                         throw e;
516                     }
517                 } catch (IOException e2) {
518                     Util.err.notify(ErrorManager.INFORMATIONAL, e2);
519                     // Well, throw original exception.
520                     throw e;
521                 }
522                 // Try again!
523                 return ModuleList.getModuleList(getProjectDirectoryFile());
524             }
525             throw e;
526         }
527          */

528     }
529     
530     /**
531      * Get the platform which this project is currently associated with.
532      * @param fallback if true, fall back to the default platform if necessary
533      * @return the current platform; or null if fallback is false and there is no
534      * platform specified, or an invalid platform is specified, or even if
535      * fallback is true but even the default platform is not available
536      */

537     public NbPlatform getPlatform(boolean fallback) {
538         NbPlatform p = getPlatform(null);
539         if (fallback && (p == null || !p.isValid())) {
540             p = NbPlatform.getDefaultPlatform();
541         }
542         return p;
543     }
544     
545     private NbPlatform getPlatform(PropertyEvaluator eval) {
546         File JavaDoc file = getPlatformFile(eval);
547         if (file == null) {
548             return null;
549         }
550         return NbPlatform.getPlatformByDestDir(file);
551     }
552     
553     private File JavaDoc getPlatformFile(PropertyEvaluator eval) {
554         if (eval == null) {
555             eval = evaluator();
556         }
557         String JavaDoc prop = eval.getProperty("netbeans.dest.dir"); // NOI18N
558
if (prop == null) {
559             return null;
560         }
561         return getHelper().resolveFile(prop);
562     }
563
564     /**
565      * Check whether Javadoc generation is possible.
566      */

567     public boolean supportsJavadoc() {
568         if (evaluator().getProperty("module.javadoc.packages") != null) {
569             return true;
570         }
571         Element JavaDoc config = getPrimaryConfigurationData();
572         Element JavaDoc pubPkgs = Util.findElement(config, "public-packages", NbModuleProjectType.NAMESPACE_SHARED); // NOI18N
573
if (pubPkgs == null) {
574             // Try <friend-packages> too.
575
pubPkgs = Util.findElement(config, "friend-packages", NbModuleProjectType.NAMESPACE_SHARED); // NOI18N
576
}
577         return pubPkgs != null && !Util.findSubElements(pubPkgs).isEmpty();
578     }
579     
580     public boolean supportsUnitTests() {
581         return getTestSourceDirectory() != null;
582     }
583     
584     /**
585      * Find marked extra compilation units.
586      * Gives a map from the package root to the defining XML element.
587      */

588     public Map JavaDoc<FileObject,Element JavaDoc> getExtraCompilationUnits() {
589         if (extraCompilationUnits == null) {
590             extraCompilationUnits = new HashMap JavaDoc<FileObject,Element JavaDoc>();
591             for (Element JavaDoc ecu : Util.findSubElements(getPrimaryConfigurationData())) {
592                 if (ecu.getLocalName().equals("extra-compilation-unit")) { // NOI18N
593
Element JavaDoc pkgrootEl = Util.findElement(ecu, "package-root", NbModuleProjectType.NAMESPACE_SHARED); // NOI18N
594
String JavaDoc pkgrootS = Util.findText(pkgrootEl);
595                     String JavaDoc pkgrootEval = evaluator().evaluate(pkgrootS);
596                     FileObject pkgroot = getHelper().resolveFileObject(pkgrootEval);
597                     if (pkgroot == null) {
598                         Util.err.log(ErrorManager.WARNING, "Could not find package-root " + pkgrootEval + " for " + getCodeNameBase());
599                         continue;
600                     }
601                     extraCompilationUnits.put(pkgroot, ecu);
602                 }
603             }
604         }
605         return extraCompilationUnits;
606     }
607     
608     /** Get the Java source level used for this module. Default is 1.4. */
609     public String JavaDoc getJavacSource() {
610         String JavaDoc javacSource = evaluator().getProperty("javac.source");
611         assert javacSource != null;
612         return javacSource;
613     }
614     
615     /**
616      * Run the open hook.
617      * For use from unit tests.
618      */

619     public void open() {
620         getLookup().lookup(OpenedHook.class).projectOpened();
621     }
622     
623     /**
624      * <strong>For use from unit tests only.</strong> Returns {@link
625      * LocalizedBundleInfo} for this project.
626      */

627     public LocalizedBundleInfo getBundleInfo() {
628         return getLookup().lookup(LocalizedBundleInfo.Provider.class).getLocalizedBundleInfo();
629     }
630     
631     
632     /** See issue #69440 for more details. */
633     public void setRunInAtomicAction(boolean runInAtomicAction) {
634         eval.setRunInAtomicAction(runInAtomicAction);
635     }
636         
637     private final class Info implements ProjectInformation, PropertyChangeListener JavaDoc {
638         
639         private final PropertyChangeSupport JavaDoc changeSupport = new PropertyChangeSupport JavaDoc(this);
640
641         private String JavaDoc displayName;
642         
643         Info() {}
644         
645         public String JavaDoc getName() {
646             String JavaDoc cnb = getCodeNameBase();
647             return cnb != null ? cnb : /* #70490 */getProjectDirectory().toString();
648         }
649         
650         public String JavaDoc getDisplayName() {
651             if (displayName == null) {
652                 LocalizedBundleInfo bundleInfo = getBundleInfo();
653                 if (bundleInfo != null) {
654                     displayName = bundleInfo.getDisplayName();
655                 }
656             }
657             if (/* #70490 */displayName == null) {
658                 displayName = getName();
659             }
660             assert displayName != null : NbModuleProject.this;
661             return displayName;
662         }
663         
664         private void setDisplayName(String JavaDoc newDisplayName) {
665             String JavaDoc oldDisplayName = getDisplayName();
666             displayName = newDisplayName == null ? getName() : newDisplayName;
667             firePropertyChange(ProjectInformation.PROP_DISPLAY_NAME, oldDisplayName, displayName);
668         }
669         
670         public Icon JavaDoc getIcon() {
671             return NB_PROJECT_ICON;
672         }
673         
674         public Project getProject() {
675             return NbModuleProject.this;
676         }
677         
678         public void addPropertyChangeListener(PropertyChangeListener JavaDoc pchl) {
679             changeSupport.addPropertyChangeListener(pchl);
680         }
681         
682         public void removePropertyChangeListener(PropertyChangeListener JavaDoc pchl) {
683             changeSupport.removePropertyChangeListener(pchl);
684         }
685         
686         private void firePropertyChange(String JavaDoc propName, Object JavaDoc oldValue, Object JavaDoc newValue) {
687             changeSupport.firePropertyChange(propName, oldValue, newValue);
688         }
689
690         public void propertyChange(PropertyChangeEvent JavaDoc evt) {
691             if (evt.getPropertyName() == ProjectInformation.PROP_DISPLAY_NAME) {
692                 setDisplayName((String JavaDoc) evt.getNewValue());
693             }
694         }
695         
696     }
697     
698     final class OpenedHook extends ProjectOpenedHook {
699         
700         private ClassPath[] boot, source, compile;
701
702         OpenedHook() {}
703         
704         protected void projectOpened() {
705             // write user.properties.file=$userdir/build.properties to platform-private.properties
706
if (getModuleType() == NbModuleProvider.STANDALONE) {
707                 // XXX skip this in case nbplatform.active is not defined
708
ProjectManager.mutex().writeAccess(new Mutex.Action<Void JavaDoc>() {
709                     public Void JavaDoc run() {
710                         String JavaDoc path = "nbproject/private/platform-private.properties"; // NOI18N
711
EditableProperties ep = getHelper().getProperties(path);
712                         File JavaDoc buildProperties = new File JavaDoc(System.getProperty("netbeans.user"), "build.properties"); // NOI18N
713
ep.setProperty("user.properties.file", buildProperties.getAbsolutePath()); //NOI18N
714
getHelper().putProperties(path, ep);
715                         try {
716                             ProjectManager.getDefault().saveProject(NbModuleProject.this);
717                         } catch (IOException JavaDoc e) {
718                             ErrorManager.getDefault().notify(e);
719                         }
720                         return null;
721                     }
722                 });
723             }
724             // register project's classpaths to GlobalClassPathRegistry
725
ClassPathProviderImpl cpProvider = lookup.lookup(ClassPathProviderImpl.class);
726             ClassPath[] _boot = cpProvider.getProjectClassPaths(ClassPath.BOOT);
727             assert _boot != null : "No BOOT path";
728             ClassPath[] _source = cpProvider.getProjectClassPaths(ClassPath.SOURCE);
729             assert _source != null : "No SOURCE path";
730             ClassPath[] _compile = cpProvider.getProjectClassPaths(ClassPath.COMPILE);
731             assert _compile != null : "No COMPILE path";
732             // Possible cause of #68414: do not change instance vars until after the dangerous stuff has been computed.
733
GlobalPathRegistry.getDefault().register(ClassPath.BOOT, _boot);
734             GlobalPathRegistry.getDefault().register(ClassPath.SOURCE, _source);
735             GlobalPathRegistry.getDefault().register(ClassPath.COMPILE, _compile);
736             boot = _boot;
737             source = _source;
738             compile = _compile;
739             // refresh build.xml and build-impl.xml for external modules
740
if (getModuleType() != NbModuleProvider.NETBEANS_ORG) {
741                 try {
742                     refreshBuildScripts(true);
743                 } catch (IOException JavaDoc e) {
744                     ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
745                 }
746             }
747         }
748         
749         protected void projectClosed() {
750             try {
751                 ProjectManager.getDefault().saveProject(NbModuleProject.this);
752             } catch (IOException JavaDoc e) {
753                 Util.err.notify(e);
754             }
755             
756             // XXX could discard caches, etc.
757

758             // unregister project's classpaths to GlobalClassPathRegistry
759
assert boot != null && source != null && compile != null : "#46802: project being closed which was never opened?? " + NbModuleProject.this;
760             GlobalPathRegistry.getDefault().unregister(ClassPath.BOOT, boot);
761             GlobalPathRegistry.getDefault().unregister(ClassPath.SOURCE, source);
762             GlobalPathRegistry.getDefault().unregister(ClassPath.COMPILE, compile);
763             boot = null;
764             source = null;
765             compile = null;
766         }
767         
768     }
769     
770     public void notifyDeleting() {
771         eval.removeListeners();
772     }
773         
774     private final class SavedHook extends ProjectXmlSavedHook {
775         
776         SavedHook() {}
777         
778         protected void projectXmlSaved() throws IOException JavaDoc {
779             // refresh build.xml and build-impl.xml for external modules
780
if (getModuleType() != NbModuleProvider.NETBEANS_ORG) {
781                 refreshBuildScripts(false);
782             }
783         }
784         
785     }
786     
787     private void refreshBuildScripts(boolean checkForProjectXmlModified) throws IOException JavaDoc {
788         genFilesHelper.refreshBuildScript(
789                 GeneratedFilesHelper.BUILD_IMPL_XML_PATH,
790                 NbModuleProject.class.getResource("resources/build-impl.xsl"), // NOI18N
791
checkForProjectXmlModified);
792         genFilesHelper.refreshBuildScript(
793                 GeneratedFilesHelper.BUILD_XML_PATH,
794                 NbModuleProject.class.getResource("resources/build.xsl"), // NOI18N
795
checkForProjectXmlModified);
796     }
797     
798     private final class SuiteProviderImpl implements SuiteProvider {
799
800         public File JavaDoc getSuiteDirectory() {
801             String JavaDoc suiteDir = evaluator().getProperty("suite.dir"); // NOI18N
802
return suiteDir == null ? null : helper.resolveFile(suiteDir);
803         }
804         
805     }
806     
807     private class NbModuleProviderImpl implements NbModuleProvider, AntProjectListener {
808         
809         private NbModuleType type;
810         
811         public NbModuleProviderImpl() {
812             getHelper().addAntProjectListener(this);
813         }
814         
815         public NbModuleType getModuleType() {
816             if (type == null) {
817                 type = NbModuleProject.this.getModuleType();
818             }
819             return type;
820         }
821
822         public void configurationXmlChanged(AntProjectEvent ev) {
823             if (ev.getPath().equals(AntProjectHelper.PROJECT_XML_PATH)) {
824                 type = null;
825             }
826         }
827         
828         public void propertiesChanged(AntProjectEvent ev) {
829             // do not need to react here, type is encoded in project.xml
830
}
831         public String JavaDoc getSpecVersion() {
832             return NbModuleProject.this.getSpecVersion();
833         }
834         
835         public String JavaDoc getCodeNameBase() {
836             return NbModuleProject.this.getCodeNameBase();
837         }
838         
839         public String JavaDoc getSourceDirectoryPath() {
840             return NbModuleProject.this.getSourceDirectoryPath();
841         }
842         
843         public FileObject getSourceDirectory() {
844             return NbModuleProject.this.getSourceDirectory();
845         }
846         
847         public FileObject getManifestFile() {
848             return NbModuleProject.this.getManifestFile();
849         }
850         
851         public String JavaDoc getResourceDirectoryPath(boolean inTests) {
852             return evaluator().getProperty(inTests ? "test.unit.src.dir" : "src.dir");
853         }
854         
855         public boolean addDependency(String JavaDoc codeNameBase, String JavaDoc releaseVersion,
856                 SpecificationVersion version,
857                 boolean useInCompiler) throws IOException JavaDoc {
858             return Util.addDependency(NbModuleProject.this, codeNameBase, releaseVersion, version, useInCompiler);
859         }
860         
861         public SpecificationVersion getDependencyVersion(String JavaDoc codenamebase) throws IOException JavaDoc {
862             ModuleList moduleList = getModuleList();
863             ModuleEntry entry = moduleList.getEntry(codenamebase); // NOI18N
864
SpecificationVersion current = new SpecificationVersion(entry.getSpecificationVersion());
865             return current;
866             
867         }
868         
869         public String JavaDoc getProjectFilePath() {
870             return "nbproject/project.xml";
871         }
872         
873         public File JavaDoc getActivePlatformLocation() {
874             return NbModuleProject.this.getPlatformFile(null);
875         }
876
877         
878     }
879     
880     private static final class PrivilegedTemplatesImpl implements PrivilegedTemplates, RecommendedTemplates {
881         
882         private static final String JavaDoc[] PRIVILEGED_NAMES = new String JavaDoc[] {
883             "Templates/Classes/Class.java", // NOI18N
884
"Templates/Classes/Package", // NOI18N
885
"Templates/Classes/Interface.java", // NOI18N
886
//"Templates/GUIForms/JPanel.java", // NOI18N
887
"Templates/JUnit/SimpleJUnitTest.java", // NOI18N
888
"Templates/NetBeansModuleDevelopment/newAction", // NOI18N
889
"Templates/NetBeansModuleDevelopment/emptyLibraryDescriptor", // NOI18N
890
"Templates/NetBeansModuleDevelopment/newLoader", // NOI18N
891
"Templates/NetBeansModuleDevelopment/newProject", // NOI18N
892
"Templates/NetBeansModuleDevelopment/newWindow", // NOI18N
893
"Templates/NetBeansModuleDevelopment/newWizard", // NOI18N
894
//"Templates/Other/properties.properties", // NOI18N
895
};
896         static {
897             assert PRIVILEGED_NAMES.length <= 10 : "Too many privileged templates to fit! extras will be ignored: " +
898                     Arrays.asList(PRIVILEGED_NAMES).subList(10, PRIVILEGED_NAMES.length);
899         }
900         
901         private static final String JavaDoc[] RECOMMENDED_TYPES = new String JavaDoc[] {
902             "java-classes", // NOI18N
903
"java-main-class", // NOI18N
904
"java-forms", // NOI18N
905
"java-beans", // NOI18N
906
"oasis-XML-catalogs", // NOI18N
907
"XML", // NOI18N
908
"ant-script", // NOI18N
909
"ant-task", // NOI18N
910
"junit", // NOI18N
911
"simple-files", // NOI18N
912
"nbm-specific", // NOI18N
913
"nbm-specific2", // NOI18N
914
};
915         
916         public String JavaDoc[] getPrivilegedTemplates() {
917             return PRIVILEGED_NAMES;
918         }
919
920         public String JavaDoc[] getRecommendedTypes() {
921             return RECOMMENDED_TYPES;
922         }
923     }
924
925     private final class LocalizedBundleInfoProvider implements LocalizedBundleInfo.Provider {
926
927         private LocalizedBundleInfo bundleInfo;
928
929         public LocalizedBundleInfo getLocalizedBundleInfo() {
930             if (bundleInfo == null) {
931                 Manifest JavaDoc mf = getManifest();
932                 FileObject srcFO = getSourceDirectory();
933                 if (mf != null && srcFO != null) {
934                     bundleInfo = Util.findLocalizedBundleInfo(srcFO, getManifest());
935                 }
936                 if (bundleInfo != null) {
937                     bundleInfo.addPropertyChangeListener(getLookup().lookup(Info.class));
938                 }
939                 if (mf != null) {
940                     getManifestFile().addFileChangeListener(new FileChangeAdapter() {
941                         public void fileChanged(FileEvent fe) {
942                             // cannot reload manifest-dependent things immediately (see 67961 for more details)
943
bundleInfo = null;
944                         }
945                     });
946                 }
947             }
948             return bundleInfo;
949         }
950     }
951
952 }
953
Popular Tags