KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > apisupport > project > ui > customizer > SingleModuleProperties


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.customizer;
21
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.text.Collator JavaDoc;
25 import java.util.Arrays JavaDoc;
26 import java.util.Collection JavaDoc;
27 import java.util.Collections JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.HashSet JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.Map JavaDoc;
32 import java.util.Set JavaDoc;
33 import java.util.SortedSet JavaDoc;
34 import java.util.TreeMap JavaDoc;
35 import java.util.TreeSet JavaDoc;
36 import javax.swing.SwingUtilities JavaDoc;
37 import javax.swing.event.ListDataEvent JavaDoc;
38 import javax.swing.event.ListDataListener JavaDoc;
39 import org.netbeans.api.java.platform.JavaPlatform;
40 import org.netbeans.api.project.Project;
41 import org.netbeans.api.project.ProjectManager;
42 import org.netbeans.api.project.ProjectUtils;
43 import org.netbeans.modules.apisupport.project.EditableManifest;
44 import org.netbeans.modules.apisupport.project.ManifestManager;
45 import org.netbeans.modules.apisupport.project.NbModuleProject;
46 import org.netbeans.modules.apisupport.project.spi.NbModuleProvider;
47 import org.netbeans.modules.apisupport.project.ProjectXMLManager;
48 import org.netbeans.modules.apisupport.project.SuiteProvider;
49 import org.netbeans.modules.apisupport.project.Util;
50 import org.netbeans.modules.apisupport.project.suite.SuiteProject;
51 import org.netbeans.modules.apisupport.project.ui.customizer.CustomizerComponentFactory.DependencyListModel;
52 import org.netbeans.modules.apisupport.project.ui.customizer.CustomizerComponentFactory.FriendListModel;
53 import org.netbeans.modules.apisupport.project.ui.customizer.CustomizerComponentFactory.PublicPackagesTableModel;
54 import org.netbeans.modules.apisupport.project.ui.customizer.CustomizerComponentFactory.RequiredTokenListModel;
55 import org.netbeans.modules.apisupport.project.universe.LocalizedBundleInfo;
56 import org.netbeans.modules.apisupport.project.universe.ModuleEntry;
57 import org.netbeans.modules.apisupport.project.universe.ModuleList;
58 import org.netbeans.modules.apisupport.project.universe.NbPlatform;
59 import org.netbeans.spi.project.support.ant.AntProjectHelper;
60 import org.netbeans.spi.project.support.ant.PropertyEvaluator;
61 import org.openide.ErrorManager;
62 import org.openide.filesystems.FileObject;
63 import org.openide.filesystems.FileUtil;
64 import org.openide.util.Utilities;
65
66 /**
67  * Provides convenient access to a lot of NetBeans Module's properties.
68  *
69  * @author Martin Krauskopf
70  */

71 public final class SingleModuleProperties extends ModuleProperties {
72     
73     private static final String JavaDoc[] IDE_TOKENS = new String JavaDoc[] {
74         "org.openide.modules.os.Windows", // NOI18N
75
"org.openide.modules.os.Unix", // NOI18N
76
"org.openide.modules.os.MacOSX", // NOI18N
77
"org.openide.modules.os.PlainUnix", // NOI18N
78
"org.openide.modules.os.OS2" // NOI18N
79
};
80     
81     // property keys for project.properties
82
public static final String JavaDoc BUILD_COMPILER_DEBUG = "build.compiler.debug"; // NOI18N
83
public static final String JavaDoc BUILD_COMPILER_DEPRECATION = "build.compiler.deprecation"; // NOI18N
84
public static final String JavaDoc CLUSTER_DIR = "cluster.dir"; // NOI18N
85
public static final String JavaDoc IS_AUTOLOAD = "is.autoload"; // NOI18N
86
public static final String JavaDoc IS_EAGER = "is.eager"; // NOI18N
87
public static final String JavaDoc JAVAC_SOURCES = "javac.source"; // NOI18N
88
public static final String JavaDoc JAVADOC_TITLE = "javadoc.title"; // NOI18N
89
public static final String JavaDoc LICENSE_FILE = "license.file"; // NOI18N
90
public static final String JavaDoc NBM_HOMEPAGE = "nbm.homepage"; // NOI18N
91
public static final String JavaDoc NBM_MODULE_AUTHOR = "nbm.module.author"; // NOI18N
92
public static final String JavaDoc NBM_NEEDS_RESTART = "nbm.needs.restart"; // NOI18N
93
public static final String JavaDoc SPEC_VERSION_BASE = "spec.version.base"; // NOI18N
94
/** @see "#66278" */
95     public static final String JavaDoc JAVAC_COMPILERARGS = "javac.compilerargs"; // NOI18N
96

97     static final String JavaDoc[] SOURCE_LEVELS = {"1.4", "1.5"}; // NOI18N
98

99     private final static Map JavaDoc<String JavaDoc, String JavaDoc> DEFAULTS;
100     
101     private boolean majorReleaseVersionChanged;
102     private boolean specificationVersionChanged;
103     private boolean implementationVersionChange;
104     private boolean providedTokensChanged;
105
106     private boolean moduleListRefreshNeeded;
107     
108     static {
109         // setup defaults
110
Map JavaDoc<String JavaDoc, String JavaDoc> map = new HashMap JavaDoc<String JavaDoc, String JavaDoc>();
111         map.put(BUILD_COMPILER_DEBUG, "true"); // NOI18N
112
map.put(BUILD_COMPILER_DEPRECATION, "true"); // NOI18N
113
map.put(IS_AUTOLOAD, "false"); // NOI18N
114
map.put(IS_EAGER, "false"); // NOI18N
115
map.put(JAVAC_SOURCES, "1.4"); // NOI18N
116
map.put(NBM_NEEDS_RESTART, "false"); // NOI18N
117
DEFAULTS = Collections.unmodifiableMap(map);
118     }
119     
120     // helpers for storing and retrieving real values currently stored on the disk
121
private NbModuleProvider.NbModuleType moduleType;
122     private SuiteProvider suiteProvider;
123     private ProjectXMLManager projectXMLManager;
124     private final LocalizedBundleInfo.Provider bundleInfoProvider;
125     private LocalizedBundleInfo bundleInfo;
126     
127     // keeps current state of the user changes
128
private String JavaDoc majorReleaseVersion;
129     private String JavaDoc specificationVersion;
130     private String JavaDoc implementationVersion;
131     private String JavaDoc provTokensString;
132     private SortedSet JavaDoc<String JavaDoc> requiredTokens;
133     private NbPlatform activePlatform;
134     private NbPlatform originalPlatform;
135     private JavaPlatform activeJavaPlatform;
136     
137     /** package name / selected */
138     private SortedSet JavaDoc<String JavaDoc> availablePublicPackages;
139     
140     private String JavaDoc[] allTokens;
141     
142     /** Unmodifiable sorted set of all categories in the module's universe. */
143     private SortedSet JavaDoc<String JavaDoc> modCategories;
144     
145     /** Unmodifiable sorted set of all dependencies in the module's universe. */
146     private Set JavaDoc<ModuleDependency> universeDependencies;
147     
148     // models
149
private PublicPackagesTableModel publicPackagesModel;
150     private DependencyListModel dependencyListModel;
151     private FriendListModel friendListModel;
152     private RequiredTokenListModel requiredTokensListModel;
153     
154     public static final String JavaDoc NB_PLATFORM_PROPERTY = "nbPlatform"; // NOI18N
155
public static final String JavaDoc JAVA_PLATFORM_PROPERTY = "nbjdk.active"; // NOI18N
156
public static final String JavaDoc DEPENDENCIES_PROPERTY = "moduleDependencies"; // NOI18N
157

158     /**
159      * Returns an instance of SingleModuleProperties for the given project.
160      */

161     public static SingleModuleProperties getInstance(final NbModuleProject project) {
162         SuiteProvider sp = project.getLookup().lookup(SuiteProvider.class);
163         return new SingleModuleProperties(project.getHelper(), project.evaluator(), sp, Util.getModuleType(project),
164                 project.getLookup().lookup(LocalizedBundleInfo.Provider.class));
165     }
166     
167     /**
168      * Creates a new instance of SingleModuleProperties
169      */

170     SingleModuleProperties(AntProjectHelper helper, PropertyEvaluator evaluator,
171             SuiteProvider sp, NbModuleProvider.NbModuleType moduleType,
172             LocalizedBundleInfo.Provider bundleInfoProvider) {
173         // XXX consider SingleModuleProperties(NbModuleProject) constructor. Life would be easier.
174
super(helper, evaluator);
175         this.bundleInfoProvider = bundleInfoProvider;
176         refresh(moduleType, sp);
177     }
178     
179     protected void refresh(NbModuleProvider.NbModuleType moduleType,
180             SuiteProvider suiteProvider) {
181         reloadProperties();
182         // reset
183
this.suiteProvider = suiteProvider;
184         this.moduleType = moduleType;
185         universeDependencies = null;
186         modCategories = null;
187         availablePublicPackages = null;
188         dependencyListModel = null;
189         friendListModel = null;
190         requiredTokensListModel = null;
191         projectXMLManager = null;
192         if (isSuiteComponent()) {
193             assert getSuiteDirectory() != null;
194             ModuleList.refreshSuiteModuleList(getSuiteDirectory());
195         }
196         ManifestManager manifestManager = ManifestManager.getInstance(getManifestFile(), false);
197         majorReleaseVersion = manifestManager.getReleaseVersion();
198         specificationVersion = manifestManager.getSpecificationVersion();
199         implementationVersion = manifestManager.getImplementationVersion();
200         provTokensString = manifestManager.getProvidedTokensString();
201         originalPlatform = activePlatform = NbPlatform.getPlatformByID(
202                 getEvaluator().getProperty("nbplatform.active")); // NOI18N
203
String JavaDoc activeJdk = getEvaluator().getProperty("nbjdk.active"); // NOI18N
204
if (activeJdk != null) {
205             activeJavaPlatform = ModuleProperties.findJavaPlatformByID(activeJdk); // NOI18N
206
} else {
207             String JavaDoc activeJdkHome = getEvaluator().getProperty("nbjdk.home"); // NOI18N
208
activeJavaPlatform = ModuleProperties.findJavaPlatformByLocation(activeJdkHome);
209         }
210         getPublicPackagesModel().reloadData(loadPublicPackages());
211         requiredTokens = Collections.unmodifiableSortedSet(
212                 new TreeSet JavaDoc<String JavaDoc>(Arrays.asList(manifestManager.getRequiredTokens())));
213         bundleInfo = bundleInfoProvider.getLocalizedBundleInfo();
214         if (bundleInfo != null) {
215             try {
216                 bundleInfo.reload();
217             } catch (IOException JavaDoc ioe) {
218                 ErrorManager.getDefault().notify(ioe);
219             }
220         }
221         firePropertiesRefreshed();
222     }
223     
224     void libraryWrapperAdded() {
225         // presuambly we do not need to reset anything else
226
universeDependencies = null;
227     }
228     
229     Map JavaDoc<String JavaDoc, String JavaDoc> getDefaultValues() {
230         return DEFAULTS;
231     }
232     
233     LocalizedBundleInfo getBundleInfo() {
234         return bundleInfo;
235     }
236     
237     // ---- READ ONLY start
238

239     /** Returns code name base of the module this instance managing. */
240     String JavaDoc getCodeNameBase() {
241         return getProjectXMLManager().getCodeNameBase();
242     }
243     
244     String JavaDoc getJarFile() {
245         return getHelper().resolveFile(getEvaluator().evaluate("${cluster}/${module.jar}")).getAbsolutePath(); // NOI18N
246
}
247     
248     String JavaDoc getSuiteDirectoryPath() {
249         return getSuiteDirectory() != null ? getSuiteDirectory().getPath() : null;
250     }
251     
252     File JavaDoc getSuiteDirectory() {
253         return suiteProvider != null ? suiteProvider.getSuiteDirectory() : null;
254     }
255     
256     /** Call only for suite component modules. */
257     SuiteProject getSuite() {
258         assert isSuiteComponent();
259         SuiteProject suite = null;
260         try {
261             suite = (SuiteProject) ProjectManager.getDefault().findProject(FileUtil.toFileObject(getSuiteDirectory()));
262         } catch (IOException JavaDoc e) {
263             Util.err.notify(ErrorManager.INFORMATIONAL, e);
264         }
265         return suite;
266     }
267     
268     // ---- READ ONLY end
269

270     /** Check whether the active platform is valid. */
271     boolean isActivePlatformValid() {
272         NbPlatform plaf = getActivePlatform();
273         return plaf == null || plaf.isValid();
274     }
275     
276     /**
277      * Returns currently set platform. i.e. platform set in the
278      * <em>Libraries</em> panel. Note that it could be <code>null</code> for
279      * NetBeans.org modules.
280      */

281     NbPlatform getActivePlatform() {
282         if (moduleType != NbModuleProvider.NETBEANS_ORG
283                 && (activePlatform == null || !NbPlatform.getPlatforms().contains(activePlatform))) {
284             ModuleProperties.reportLostPlatform(activePlatform);
285             activePlatform = NbPlatform.getDefaultPlatform();
286         }
287         return activePlatform;
288     }
289     
290     void setActivePlatform(NbPlatform newPlaf) {
291         if (this.activePlatform != newPlaf) {
292             NbPlatform oldPlaf = this.activePlatform;
293             this.activePlatform = newPlaf;
294             this.dependencyListModel = null;
295             this.universeDependencies = null;
296             this.modCategories = null;
297             firePropertyChange(NB_PLATFORM_PROPERTY, oldPlaf, newPlaf);
298         }
299     }
300     
301     JavaPlatform getActiveJavaPlatform() {
302         return activeJavaPlatform;
303     }
304     
305     void setActiveJavaPlatform(JavaPlatform nue) {
306         JavaPlatform old = activeJavaPlatform;
307         if (nue != old) {
308             activeJavaPlatform = nue;
309             firePropertyChange(JAVA_PLATFORM_PROPERTY, old, nue);
310         }
311     }
312     
313     String JavaDoc getMajorReleaseVersion() {
314         return majorReleaseVersion;
315     }
316     
317     void setMajorReleaseVersion(String JavaDoc ver) {
318         if (!Utilities.compareObjects(majorReleaseVersion, ver)) {
319             majorReleaseVersion = ver;
320             majorReleaseVersionChanged = true;
321         }
322     }
323     
324     String JavaDoc getSpecificationVersion() {
325         return specificationVersion;
326     }
327     
328     void setSpecificationVersion(String JavaDoc ver) {
329         if (!Utilities.compareObjects(specificationVersion, ver)) {
330             specificationVersion = ver;
331             specificationVersionChanged = true;
332         }
333     }
334     
335     String JavaDoc getImplementationVersion() {
336         return implementationVersion;
337     }
338     
339     void setImplementationVersion(String JavaDoc ver) {
340         if (!Utilities.compareObjects(implementationVersion, ver)) {
341             implementationVersion = ver;
342             implementationVersionChange = true;
343         }
344     }
345     
346     String JavaDoc getProvidedTokens() {
347         return provTokensString;
348     }
349     
350     void setProvidedTokens(String JavaDoc tokens) {
351         if (!Utilities.compareObjects(provTokensString, tokens)) {
352             provTokensString = tokens;
353             providedTokensChanged = true;
354         }
355     }
356     
357     boolean isStandalone() {
358         return moduleType == NbModuleProvider.STANDALONE;
359     }
360     
361     boolean isNetBeansOrg() {
362         return moduleType == NbModuleProvider.NETBEANS_ORG;
363     }
364     
365     boolean isSuiteComponent() {
366         return moduleType == NbModuleProvider.SUITE_COMPONENT;
367     }
368
369     public void setModuleListRefreshNeeded(boolean moduleListRefreshNeeded) {
370         this.moduleListRefreshNeeded = moduleListRefreshNeeded;
371     }
372     
373     boolean isModuleListRefreshNeeded() {
374         return moduleListRefreshNeeded;
375     }
376     
377     boolean dependingOnImplDependency() {
378         DependencyListModel depsModel = getDependenciesListModel();
379         if (depsModel == CustomizerComponentFactory.INVALID_DEP_LIST_MODEL) {
380             return false;
381         }
382         Set JavaDoc<ModuleDependency> deps = depsModel.getDependencies();
383         for (Iterator JavaDoc it = deps.iterator(); it.hasNext(); ) {
384             ModuleDependency dep = (ModuleDependency) it.next();
385             if (dep.hasImplementationDepedendency()) {
386                 return true;
387             }
388         }
389         return false;
390     }
391     
392     private ProjectXMLManager getProjectXMLManager() {
393         if (projectXMLManager == null) {
394             try {
395                 projectXMLManager = ProjectXMLManager.getInstance(getProjectDirectoryFile());
396             } catch (IOException JavaDoc e) {
397                 assert false : e;
398             }
399         }
400         return projectXMLManager;
401     }
402     
403     /**
404      * Returns list model of module's dependencies regarding the currently
405      * selected platform.
406      */

407     DependencyListModel getDependenciesListModel() {
408         if (dependencyListModel == null) {
409             if (isActivePlatformValid()) {
410                 try {
411                     dependencyListModel = new DependencyListModel(
412                             getProjectXMLManager().getDirectDependencies(getActivePlatform()), true);
413                     // add listener and fire DEPENDENCIES_PROPERTY when deps are changed
414
dependencyListModel.addListDataListener(new ListDataListener JavaDoc() {
415                         public void contentsChanged(ListDataEvent JavaDoc e) {
416                             firePropertyChange(DEPENDENCIES_PROPERTY, null,
417                                     getDependenciesListModel());
418                         }
419                         public void intervalAdded(ListDataEvent JavaDoc e) {
420                             contentsChanged(null);
421                         }
422                         public void intervalRemoved(ListDataEvent JavaDoc e) {
423                             contentsChanged(null);
424                         }
425                     });
426                 } catch (IOException JavaDoc ioe) {
427                     ErrorManager.getDefault().notify(ioe);
428                     dependencyListModel = CustomizerComponentFactory.getInvalidDependencyListModel();
429                 }
430             } else {
431                 dependencyListModel = CustomizerComponentFactory.getInvalidDependencyListModel();
432             }
433         }
434         return dependencyListModel;
435     }
436     
437     /**
438      * Returns a set of available {@link ModuleDependency modules dependencies}
439      * in the module's universe according to the currently selected {@link
440      * #getActivePlatform() platform}.<p>
441      *
442      * <strong>Note:</strong> Don't call this method from EDT, since it may be
443      * really slow. The {@link AssertionError} will be thrown if you try to do
444      * so.
445      *
446      * @param filterExcludedModules if <code>true</code> and this module is a
447      * suite component, modules excluded from the suite's module list
448      * will be excluded from the returned set.
449      * @param apiProvidersOnly if <code>true</code> only modules which provide
450      * public packages and have friendly relationship with this module
451      * will be included in the returned set
452      */

453     Set JavaDoc<ModuleDependency> getUniverseDependencies(
454             final boolean filterExcludedModules, final boolean apiProvidersOnly) {
455         assert !SwingUtilities.isEventDispatchThread() :
456             "SingleModuleProperties.getUniverseDependencies() cannot be called from EDT"; // NOI18N
457
if (universeDependencies == null) {
458             reloadModuleListInfo();
459         }
460         Set JavaDoc<ModuleDependency> result = new HashSet JavaDoc<ModuleDependency>(universeDependencies);
461         if (filterExcludedModules && isSuiteComponent()) {
462             SuiteProject suite = getSuite();
463             String JavaDoc[] disableModules = SuiteProperties.getArrayProperty(
464                     suite.getEvaluator(), SuiteProperties.DISABLED_MODULES_PROPERTY);
465             String JavaDoc[] enableClusters = SuiteProperties.getArrayProperty(
466                     suite.getEvaluator(), SuiteProperties.ENABLED_CLUSTERS_PROPERTY);
467             String JavaDoc[] disableClusters = SuiteProperties.getArrayProperty(
468                     suite.getEvaluator(), SuiteProperties.DISABLED_CLUSTERS_PROPERTY);
469             String JavaDoc suiteClusterProp = getEvaluator().getProperty("cluster"); // NOI18N
470
File JavaDoc suiteClusterDir = suiteClusterProp != null ? getHelper().resolveFile(suiteClusterProp) : null;
471             for (Iterator JavaDoc<ModuleDependency> it = result.iterator(); it.hasNext();) {
472                 ModuleDependency dep = it.next();
473                 ModuleEntry me = dep.getModuleEntry();
474                 if (me.getClusterDirectory().equals(suiteClusterDir)) {
475                     // #72124: do not filter other modules in the same suite.
476
continue;
477                 }
478                 if (isExcluded(me, disableModules, enableClusters, disableClusters)) {
479                     it.remove();
480                 }
481             }
482         }
483         if (apiProvidersOnly) { // remove module without public/friend API
484
for (Iterator JavaDoc<ModuleDependency> it = result.iterator(); it.hasNext();) {
485                 ModuleDependency dep = it.next();
486                 ModuleEntry me = dep.getModuleEntry();
487                 if (me.getPublicPackages().length == 0 || !me.isDeclaredAsFriend(getCodeNameBase())) {
488                     it.remove();
489                 }
490             }
491         }
492         return Collections.unmodifiableSet(result);
493     }
494     
495     /**
496      * Delegates to {@link #getUniverseDependencies(boolean, boolean)} with
497      * <code>false</code> as a second parameter.
498      */

499     Set JavaDoc<ModuleDependency> getUniverseDependencies(final boolean filterExcludedModules) {
500         return getUniverseDependencies(filterExcludedModules, false);
501     }
502     
503     private static boolean isExcluded(final ModuleEntry me,
504             final String JavaDoc[] disableModules, final String JavaDoc[] enableClusters, final String JavaDoc[] disableClusters) {
505         if (Arrays.binarySearch(disableModules, me.getCodeNameBase()) >= 0) {
506             return true;
507         }
508         if (enableClusters.length != 0 && Arrays.binarySearch(enableClusters, me.getClusterDirectory().getName()) < 0) {
509             return true;
510         }
511         if (enableClusters.length == 0 && Arrays.binarySearch(disableClusters, me.getClusterDirectory().getName()) >= 0) {
512             return true;
513         }
514         return false;
515     }
516     
517     /**
518      * Returns sorted arrays of CNBs of available friends for this module.
519      */

520     String JavaDoc[] getAvailableFriends() {
521         SortedSet JavaDoc<String JavaDoc> set = new TreeSet JavaDoc<String JavaDoc>();
522         if (isSuiteComponent()) {
523             for (Iterator JavaDoc it = SuiteUtils.getSubProjects(getSuite()).iterator(); it.hasNext();) {
524                 Project prj = (Project) it.next();
525                 String JavaDoc cnb = ProjectUtils.getInformation(prj).getName();
526                 if (!getCodeNameBase().equals(cnb)) {
527                     set.add(cnb);
528                 }
529             }
530         } else if (isNetBeansOrg()) {
531             Set JavaDoc<ModuleDependency> deps = getUniverseDependencies(false);
532             for (Iterator JavaDoc it = deps.iterator(); it.hasNext();) {
533                 ModuleDependency dep = (ModuleDependency) it.next();
534                 set.add(dep.getModuleEntry().getCodeNameBase());
535             }
536         } // else standalone module - leave empty (see the UI spec)
537
return set.toArray(new String JavaDoc[set.size()]);
538     }
539     
540     FriendListModel getFriendListModel() {
541         if (friendListModel == null) {
542             friendListModel = new FriendListModel(getProjectXMLManager().getFriends());
543         }
544         return friendListModel;
545     }
546     
547     RequiredTokenListModel getRequiredTokenListModel() {
548         if (requiredTokensListModel == null) {
549             requiredTokensListModel = new RequiredTokenListModel(requiredTokens);
550         }
551         return requiredTokensListModel;
552     }
553     
554     // XXX should be probably moved into ModuleList
555
String JavaDoc[] getAllTokens() {
556         if (allTokens == null) {
557             try {
558                 SortedSet JavaDoc<String JavaDoc> provTokens = new TreeSet JavaDoc<String JavaDoc>();
559                 provTokens.addAll(Arrays.asList(IDE_TOKENS));
560                 for (Iterator JavaDoc it = getModuleList().getAllEntriesSoft().iterator(); it.hasNext(); ) {
561                     ModuleEntry me = (ModuleEntry) it.next();
562                     provTokens.addAll(Arrays.asList(me.getProvidedTokens()));
563                 }
564                 String JavaDoc[] result = new String JavaDoc[provTokens.size()];
565                 return provTokens.toArray(result);
566             } catch (IOException JavaDoc e) {
567                 allTokens = new String JavaDoc[0];
568                 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
569             }
570         }
571         return allTokens;
572     }
573     
574     PublicPackagesTableModel getPublicPackagesModel() {
575         if (publicPackagesModel == null) {
576             publicPackagesModel = new PublicPackagesTableModel(loadPublicPackages());
577         }
578         return publicPackagesModel;
579     }
580     
581     /** Loads a map of package-isSelected entries. */
582     private Map JavaDoc<String JavaDoc, Boolean JavaDoc> loadPublicPackages() {
583         Collection JavaDoc<String JavaDoc> selectedPackages = getSelectedPackages();
584         Map JavaDoc<String JavaDoc, Boolean JavaDoc> publicPackages = new TreeMap JavaDoc<String JavaDoc, Boolean JavaDoc>();
585         for (Iterator JavaDoc<String JavaDoc> it = getAvailablePublicPackages().iterator(); it.hasNext(); ) {
586             String JavaDoc pkg = it.next();
587             publicPackages.put(pkg, Boolean.valueOf(selectedPackages.contains(pkg)));
588         }
589         return publicPackages;
590     }
591     
592     private Collection JavaDoc<String JavaDoc> getSelectedPackages() {
593         Collection JavaDoc<String JavaDoc> sPackages = new HashSet JavaDoc<String JavaDoc>();
594         ManifestManager.PackageExport[] pexports = getProjectXMLManager().getPublicPackages();
595         for (int i = 0; i < pexports.length; i++) {
596             ManifestManager.PackageExport pexport = pexports[i];
597             if (pexport.isRecursive()) {
598                 for (Iterator JavaDoc it = getAvailablePublicPackages().iterator(); it.hasNext(); ) {
599                     String JavaDoc p = (String JavaDoc) it.next();
600                     if (p.startsWith(pexport.getPackage())) {
601                         sPackages.add(p);
602                     }
603                 }
604             } else {
605                 sPackages.add(pexport.getPackage());
606             }
607         }
608         return sPackages;
609     }
610     
611     /**
612      * Returns set of all available public packages for the project.
613      */

614     Set JavaDoc<String JavaDoc> getAvailablePublicPackages() {
615         if (availablePublicPackages == null) {
616             availablePublicPackages = Util.scanProjectForPackageNames(getProjectDirectoryFile());
617         }
618         return availablePublicPackages;
619     }
620     
621     void storeProperties() throws IOException JavaDoc {
622         super.storeProperties();
623         
624         // Store chnages in manifest
625
storeManifestChanges();
626         
627         // store localized info
628
if (bundleInfo != null && bundleInfo.isModified()) {
629             bundleInfo.store();
630         } // XXX else ignore for now but we could save into some default location
631

632         // Store project.xml changes
633
// store module dependencies
634
DependencyListModel dependencyListModel = getDependenciesListModel();
635         if (dependencyListModel.isChanged()) {
636             Set JavaDoc<ModuleDependency> depsToSave = new TreeSet JavaDoc<ModuleDependency>(dependencyListModel.getDependencies());
637             
638             // process removed modules
639
depsToSave.removeAll(dependencyListModel.getRemovedDependencies());
640             
641             // process added modules
642
depsToSave.addAll(dependencyListModel.getAddedDependencies());
643             
644             // process edited modules
645
for (Map.Entry JavaDoc<ModuleDependency,ModuleDependency> entry : dependencyListModel.getEditedDependencies().entrySet()) {
646                 depsToSave.remove(entry.getKey());
647                 depsToSave.add(entry.getValue());
648             }
649             getProjectXMLManager().replaceDependencies(depsToSave);
650         }
651         String JavaDoc[] friends = getFriendListModel().getFriends();
652         String JavaDoc[] publicPkgs = getPublicPackagesModel().getSelectedPackages();
653         if (getPublicPackagesModel().isChanged() || getFriendListModel().isChanged()) {
654             if (friends.length > 0) { // store friends packages
655
getProjectXMLManager().replaceFriends(friends, publicPkgs);
656             } else { // store public packages
657
getProjectXMLManager().replacePublicPackages(publicPkgs);
658             }
659             setModuleListRefreshNeeded(true);
660         }
661         
662         if (isStandalone()) {
663             ModuleProperties.storePlatform(getHelper(), getActivePlatform());
664             ModuleProperties.storeJavaPlatform(getHelper(), getEvaluator(), getActiveJavaPlatform(), false);
665         } else if (isNetBeansOrg()) {
666             ModuleProperties.storeJavaPlatform(getHelper(), getEvaluator(), getActiveJavaPlatform(), true);
667         }
668     }
669     
670     /**
671      * Store appropriately properties regarding the manifest file.
672      */

673     private void storeManifestChanges() throws IOException JavaDoc {
674         FileObject manifestFO = FileUtil.toFileObject(getManifestFile());
675         EditableManifest em;
676         if (manifestFO != null) {
677             em = Util.loadManifest(manifestFO);
678         } else { // manifest doesn't exist yet
679
em = new EditableManifest();
680             manifestFO = FileUtil.createData(
681                     getHelper().getProjectDirectory(), "manifest.mf"); // NOI18N
682
}
683         boolean changed = false;
684         if (majorReleaseVersionChanged) {
685             String JavaDoc module = "".equals(getMajorReleaseVersion()) ?
686                 getCodeNameBase() :
687                 getCodeNameBase() + '/' + getMajorReleaseVersion();
688             setManifestAttribute(em, ManifestManager.OPENIDE_MODULE, module);
689             changed = true;
690         }
691         if (specificationVersionChanged) {
692             setManifestAttribute(em, ManifestManager.OPENIDE_MODULE_SPECIFICATION_VERSION,
693                     getSpecificationVersion());
694             changed = true;
695         }
696         if (implementationVersionChange) {
697             setManifestAttribute(em, ManifestManager.OPENIDE_MODULE_IMPLEMENTATION_VERSION,
698                     getImplementationVersion());
699             changed = true;
700         }
701         if (providedTokensChanged) {
702             setManifestAttribute(em, ManifestManager.OPENIDE_MODULE_PROVIDES,
703                     getProvidedTokens());
704             changed = true;
705         }
706         if (getRequiredTokenListModel().isChanged()) {
707             String JavaDoc[] reqTokens = getRequiredTokenListModel().getTokens();
708             StringBuffer JavaDoc result = new StringBuffer JavaDoc(reqTokens.length > 1 ? "\n " : ""); // NOI18N
709
for (int i = 0; i < reqTokens.length; i++) {
710                 if (i != 0) {
711                     result.append(",\n "); // NOI18N
712
}
713                 result.append(reqTokens[i]);
714             }
715             setManifestAttribute(em, ManifestManager.OPENIDE_MODULE_REQUIRES, result.toString());
716             changed = true;
717         }
718         if (changed) {
719             Util.storeManifest(manifestFO, em);
720         }
721     }
722     
723     // XXX should be something similar provided be EditableManifest?
724
private void setManifestAttribute(EditableManifest em, String JavaDoc key, String JavaDoc value) {
725         assert value != null;
726         if ("".equals(value)) {
727             if (em.getAttribute(key, null) != null) {
728                 em.removeAttribute(key, null);
729             }
730         } else {
731             em.setAttribute(key, value, null);
732         }
733     }
734     
735     // package provide for unit test
736
File JavaDoc getManifestFile() {
737         return getHelper().resolveFile(getEvaluator().getProperty("manifest.mf")); // NOI18N
738
}
739     
740     /**
741      * Returns a set of all available categories in the module's universe
742      * according to the currently selected platform ({@link
743      * #getActivePlatform()})<p>
744      * <strong>Note:</strong> Don't call this method from EDT, since it may be
745      * really slow. The {@link AssertionError} will be thrown if you try to do
746      * so.
747      */

748     SortedSet JavaDoc getModuleCategories() {
749         assert !SwingUtilities.isEventDispatchThread() :
750             "SingleModuleProperties.getModuleCategories() cannot be called from EDT"; // NOI18N
751
if (modCategories == null && !reloadModuleListInfo()) {
752             return new TreeSet JavaDoc();
753         }
754         return modCategories;
755     }
756     
757     /**
758      * Prepare all ModuleDependencies from this module's universe. Also prepare
759      * all categories. <strong>Package-private only for unit tests.</strong>
760      */

761     boolean reloadModuleListInfo() {
762         assert !SwingUtilities.isEventDispatchThread() :
763             "SingleModuleProperties.reloadModuleListInfo() cannot be called from EDT"; // NOI18N
764
if (isActivePlatformValid()) {
765             try {
766                 SortedSet JavaDoc<String JavaDoc> allCategories = new TreeSet JavaDoc<String JavaDoc>(Collator.getInstance());
767                 Set JavaDoc<ModuleDependency> allDependencies = new HashSet JavaDoc<ModuleDependency>();
768                 for (Iterator JavaDoc it = getModuleList().getAllEntriesSoft().iterator(); it.hasNext(); ) {
769                     ModuleEntry me = (ModuleEntry) it.next();
770                     if (!me.getCodeNameBase().equals(getCodeNameBase())) {
771                         allDependencies.add(new ModuleDependency(me));
772                     }
773                     String JavaDoc cat = me.getCategory();
774                     if (cat != null) {
775                         allCategories.add(cat);
776                     }
777                 }
778                 modCategories = Collections.unmodifiableSortedSet(allCategories);
779                 universeDependencies = Collections.unmodifiableSet(allDependencies);
780                 return true;
781             } catch (IOException JavaDoc ioe) {
782                 ErrorManager.getDefault().notify(ioe);
783             }
784         }
785         return false;
786     }
787     
788     /**
789      * Helper method to get the <code>ModuleList</code> for the project this
790      * instance manage. <strong>Package-private only for unit tests.</strong>
791      */

792     ModuleList getModuleList() throws IOException JavaDoc {
793         if (getActivePlatform() != this.originalPlatform) {
794             return ModuleList.getModuleList(
795                     getProjectDirectoryFile(), getActivePlatform().getDestDir());
796         } else {
797             return ModuleList.getModuleList(getProjectDirectoryFile());
798         }
799     }
800     
801     /**
802      * Just use a combination of evaluator and resolver. May return
803      * <code>null</code> if evaluating fails.
804      */

805     File JavaDoc evaluateFile(final String JavaDoc currentLicence) {
806         String JavaDoc evaluated = getEvaluator().evaluate(currentLicence);
807         return evaluated == null ? null : getHelper().resolveFile(evaluated);
808     }
809     
810     Project getProject() {
811         Project p = null;
812         try {
813             p = ProjectManager.getDefault().findProject(getHelper().getProjectDirectory());
814         } catch (IOException JavaDoc e) {
815             assert false : e;
816         }
817         return p;
818     }
819     
820 }
821
Popular Tags