KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > freeform > ui > ProjectModel


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.java.freeform.ui;
21
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Arrays JavaDoc;
26 import java.util.Collection JavaDoc;
27 import java.util.HashSet JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.LinkedHashSet JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.Set JavaDoc;
32 import javax.swing.event.ChangeEvent JavaDoc;
33 import javax.swing.event.ChangeListener JavaDoc;
34 import org.netbeans.api.java.platform.JavaPlatform;
35 import org.netbeans.api.project.ProjectManager;
36 import org.netbeans.modules.ant.freeform.spi.ProjectConstants;
37 import org.netbeans.modules.ant.freeform.spi.support.Util;
38 import org.netbeans.modules.java.freeform.JavaProjectGenerator;
39 import org.netbeans.modules.java.freeform.JavaProjectNature;
40 import org.netbeans.spi.project.AuxiliaryConfiguration;
41 import org.netbeans.spi.project.support.ant.AntProjectHelper;
42 import org.netbeans.spi.project.support.ant.PropertyEvaluator;
43 import org.netbeans.spi.project.support.ant.PropertyUtils;
44 import org.openide.modules.SpecificationVersion;
45 import org.openide.util.Mutex;
46
47 /**
48  * Memory model of project. Used for creation or customization of project.
49  *
50  * @author David Konecny
51  */

52 public class ProjectModel {
53     
54     /** Original project base folder */
55     private File JavaDoc baseFolder;
56     
57     /** Freeform Project base folder */
58     private File JavaDoc nbProjectFolder;
59
60     private PropertyEvaluator evaluator;
61     
62     private String JavaDoc sourceLevel;
63     
64     /** List of JavaProjectGenerator.SourceFolders instances of type "java". */
65     private List JavaDoc<JavaProjectGenerator.SourceFolder> sourceFolders;
66     
67     public List JavaDoc<JavaProjectGenerator.JavaCompilationUnit> javaCompilationUnitsList;
68
69     private Set JavaDoc<String JavaDoc> addedSourceFolders;
70     private Set JavaDoc<String JavaDoc> removedSourceFolders;
71     
72     public static final String JavaDoc TYPE_JAVA = "java"; // NOI18N
73
public static final String JavaDoc CLASSPATH_MODE_COMPILE = "compile"; // NOI18N
74
//Upper bound of sourse level supported by the java freeform project
75
private static final SpecificationVersion JDK_MAX_SUPPORTED_VERSION = new SpecificationVersion ("1.5"); //NOI18N
76

77     private ProjectModel(File JavaDoc baseFolder, File JavaDoc nbProjectFolder, PropertyEvaluator evaluator,
78             List JavaDoc<JavaProjectGenerator.SourceFolder> sourceFolders, List JavaDoc<JavaProjectGenerator.JavaCompilationUnit> compUnits) {
79         this.baseFolder = baseFolder;
80         this.nbProjectFolder = nbProjectFolder;
81         this.evaluator = evaluator;
82         this.sourceFolders = sourceFolders;
83         this.javaCompilationUnitsList = compUnits;
84         if (javaCompilationUnitsList.size() > 0) {
85             sourceLevel = javaCompilationUnitsList.get(0).sourceLevel;
86         }
87         if (sourceLevel == null) {
88             setSourceLevel(getDefaultSourceLevel());
89         }
90         resetState();
91     }
92     
93     private final Set JavaDoc<ChangeListener JavaDoc> listeners = new HashSet JavaDoc<ChangeListener JavaDoc>(1);
94     public final void addChangeListener(ChangeListener JavaDoc l) {
95         synchronized (listeners) {
96             listeners.add(l);
97         }
98     }
99     
100     public final void removeChangeListener(ChangeListener JavaDoc l) {
101         synchronized (listeners) {
102             listeners.remove(l);
103         }
104     }
105
106     /**
107      * Notifies only about change in source folders and compilation units.
108      */

109     protected final void fireChangeEvent() {
110         Collection JavaDoc<ChangeListener JavaDoc> ls;
111         synchronized (listeners) {
112             ls = new HashSet JavaDoc<ChangeListener JavaDoc>(listeners);
113         }
114         ChangeEvent JavaDoc ev = new ChangeEvent JavaDoc(this);
115         for (ChangeListener JavaDoc l : ls) {
116             l.stateChanged(ev);
117         }
118     }
119     
120     private void resetState() {
121         addedSourceFolders = new HashSet JavaDoc<String JavaDoc>();
122         removedSourceFolders = new HashSet JavaDoc<String JavaDoc>();
123     }
124
125     /** Create empty project model. Useful for new project creation. */
126     public static ProjectModel createEmptyModel(File JavaDoc baseFolder, File JavaDoc nbProjectFolder, PropertyEvaluator evaluator) {
127         return new ProjectModel(baseFolder, nbProjectFolder, evaluator,
128                 new ArrayList JavaDoc<JavaProjectGenerator.SourceFolder>(),
129                 new ArrayList JavaDoc<JavaProjectGenerator.JavaCompilationUnit>());
130     }
131
132     /** Create project model of existing project. Useful for project customization. */
133     public static ProjectModel createModel(final File JavaDoc baseFolder, final File JavaDoc nbProjectFolder, final PropertyEvaluator evaluator, final AntProjectHelper helper) {
134         return ProjectManager.mutex().readAccess(new Mutex.Action<ProjectModel>() {
135             public ProjectModel run() {
136                 ProjectModel pm = new ProjectModel(
137                         baseFolder,
138                         nbProjectFolder,
139                         evaluator,
140                         // reads only "java" type because other types are not editable in UI
141
JavaProjectGenerator.getSourceFolders(helper, TYPE_JAVA),
142                         JavaProjectGenerator.getJavaCompilationUnits(helper,
143                             Util.getAuxiliaryConfiguration(helper))
144                     );
145                 // only "java" type of sources was read so fix style to "pacakges" on all
146
updateStyle(pm.sourceFolders);
147                 return pm;
148             }
149         });
150     }
151
152     /** Instantiate project model as new Java project. */
153     public static void instantiateJavaProject(AntProjectHelper helper, ProjectModel model) throws IOException JavaDoc {
154         List JavaDoc<JavaProjectGenerator.SourceFolder> sourceFolders = model.updatePrincipalSourceFolders(model.sourceFolders, true);
155
156         if (sourceFolders.size() > 0) {
157             JavaProjectGenerator.putSourceFolders(helper, sourceFolders, null);
158         }
159         if (sourceFolders.size() > 0) {
160             JavaProjectGenerator.putSourceViews(helper, sourceFolders, null);
161         }
162         JavaProjectGenerator.putJavaCompilationUnits(helper, Util.getAuxiliaryConfiguration(helper), model.javaCompilationUnitsList);
163         List JavaDoc<JavaProjectGenerator.Export> exports = JavaProjectGenerator.guessExports(
164                 model.evaluator, model.baseFolder, JavaProjectGenerator.getTargetMappings(helper), model.javaCompilationUnitsList);
165         if (exports.size() > 0) {
166             JavaProjectGenerator.putExports(helper, exports);
167         }
168         List JavaDoc<String JavaDoc> subprojects = JavaProjectGenerator.guessSubprojects(model.evaluator, model.javaCompilationUnitsList, model.baseFolder, model.nbProjectFolder);
169         if (subprojects.size() > 0) {
170             JavaProjectGenerator.putSubprojects(helper, subprojects);
171         }
172         
173         model.resetState();
174     }
175     
176     /** Persist modifications of project. */
177     public static void saveProject(final AntProjectHelper helper, final ProjectModel model) {
178         ProjectManager.mutex().writeAccess(new Mutex.Action<Void JavaDoc>() {
179             public Void JavaDoc run() {
180                 // stores only "java" type because other types was not read
181
JavaProjectGenerator.putSourceFolders(helper, model.sourceFolders, TYPE_JAVA);
182                 JavaProjectGenerator.putSourceViews(helper, model.sourceFolders, JavaProjectNature.STYLE_PACKAGES);
183
184                 List JavaDoc<JavaProjectGenerator.SourceFolder> sourceFolders = JavaProjectGenerator.getSourceFolders(helper, null);
185                 sourceFolders = model.updatePrincipalSourceFolders(sourceFolders, false);
186                 JavaProjectGenerator.putSourceFolders(helper, sourceFolders, null);
187
188                 AuxiliaryConfiguration aux = Util.getAuxiliaryConfiguration(helper);
189                 JavaProjectGenerator.putJavaCompilationUnits(helper, aux, model.javaCompilationUnitsList);
190                 model.resetState();
191
192                 List JavaDoc<JavaProjectGenerator.Export> exports = JavaProjectGenerator.guessExports(model.getEvaluator(), model.baseFolder,
193                     JavaProjectGenerator.getTargetMappings(helper), model.javaCompilationUnitsList);
194                 JavaProjectGenerator.putExports(helper, exports);
195
196                 List JavaDoc<String JavaDoc> subprojects = JavaProjectGenerator.guessSubprojects(model.getEvaluator(),
197                     model.javaCompilationUnitsList, model.baseFolder, model.nbProjectFolder);
198                 JavaProjectGenerator.putSubprojects(helper, subprojects);
199                 
200                 List JavaDoc<String JavaDoc> buildFolders = JavaProjectGenerator.guessBuildFolders(model.getEvaluator(),
201                     model.javaCompilationUnitsList, model.baseFolder, model.nbProjectFolder);
202                 JavaProjectGenerator.putBuildFolders(helper, buildFolders);
203                 
204                 return null;
205             }
206         });
207     }
208
209     /**
210      * This method according to the state of removed/added source folders
211      * will ensure that all added typed external source roots will have
212      * corresponding principal source folder and that principal source
213      * folders of all removed typed external source roots are removed too.
214      * In addition it can add project base folder as principal root
215      * if it is external.
216      *
217      * It is expected that this method will be called before project instantiation
218      * or before update of project's data.
219      *
220      * @param allSourceFolders list of all source folders, i.e. typed and untyped.
221      * @param checkProjectDir should project base folder be checked
222      * and added as principal source folder if needed or not
223      * @return copy of allSourceFolders items plus added principal source folders
224      */

225     /*private*/ List JavaDoc<JavaProjectGenerator.SourceFolder> updatePrincipalSourceFolders(
226             List JavaDoc<JavaProjectGenerator.SourceFolder> allSourceFolders, boolean checkProjectDir) {
227         List JavaDoc<JavaProjectGenerator.SourceFolder> allSF = new ArrayList JavaDoc<JavaProjectGenerator.SourceFolder>(allSourceFolders);
228         for (String JavaDoc location : addedSourceFolders) {
229             
230             if (!isExternalSourceRoot(location)) {
231                 continue;
232             }
233             
234             boolean exist = false;
235             String JavaDoc label = ""; // NOI18N
236
String JavaDoc includes = null, excludes = null;
237             for (JavaProjectGenerator.SourceFolder _sf : allSF) {
238                 if (_sf.location.equals(location) && _sf.type == null) {
239                     exist = true;
240                     break;
241                 }
242                 if (_sf.location.equals(location) && _sf.type != null) {
243                     // find some label to use
244
label = _sf.label;
245                     includes = _sf.includes;
246                     excludes = _sf.excludes;
247                 }
248             }
249             
250             if (!exist) {
251                 JavaProjectGenerator.SourceFolder _sf = new JavaProjectGenerator.SourceFolder();
252                 _sf.location = location;
253                 _sf.label = label;
254                 _sf.includes = includes;
255                 _sf.excludes = excludes;
256                 allSF.add(_sf);
257             }
258         }
259
260         for (String JavaDoc location : removedSourceFolders) {
261             
262             if (!isExternalSourceRoot(location)) {
263                 continue;
264             }
265             
266             Iterator JavaDoc<JavaProjectGenerator.SourceFolder> it = allSF.iterator();
267             while (it.hasNext()) {
268                 JavaProjectGenerator.SourceFolder _sf = it.next();
269                 if (_sf.location.equals(location) && _sf.type == null) {
270                     it.remove();
271                 }
272             }
273         }
274         
275         if (checkProjectDir && !baseFolder.equals(nbProjectFolder)) {
276             JavaProjectGenerator.SourceFolder gen = new JavaProjectGenerator.SourceFolder();
277             gen.location = "${"+ProjectConstants.PROP_PROJECT_LOCATION+"}"; // NOI18N
278
// XXX: uniquefy label
279
gen.label = baseFolder.getName();
280             allSF.add(gen);
281         }
282         
283         return allSF;
284     }
285
286     private boolean isExternalSourceRoot(String JavaDoc location) {
287         String JavaDoc baseFolder_ = baseFolder.getAbsolutePath();
288         if (!baseFolder_.endsWith(File.separator)) {
289             baseFolder_ += File.separatorChar;
290         }
291         String JavaDoc nbProjectFolder_ = nbProjectFolder.getAbsolutePath();
292         if (!nbProjectFolder_.endsWith(File.separator)) {
293             nbProjectFolder_ += File.separatorChar;
294         }
295         File JavaDoc f = Util.resolveFile(evaluator, baseFolder, location);
296         if (f == null) {
297             return false;
298         }
299         location = f.getAbsolutePath();
300         return (!location.startsWith(baseFolder_) &&
301                     !location.startsWith(nbProjectFolder_));
302     }
303     
304     /** Original project base folder. */
305     public File JavaDoc getBaseFolder() {
306         return baseFolder;
307     }
308     
309     /** NetBeans project folder. */
310     public File JavaDoc getNBProjectFolder() {
311         return nbProjectFolder;
312     }
313     
314     public PropertyEvaluator getEvaluator() {
315         return evaluator;
316     }
317     
318     public int getSourceFoldersCount() {
319         return sourceFolders.size();
320     }
321     
322     public JavaProjectGenerator.SourceFolder getSourceFolder(int index) {
323         return sourceFolders.get(index);
324     }
325     
326     public void moveSourceFolder(int fromIndex, int toIndex) {
327         JavaProjectGenerator.SourceFolder sf = sourceFolders.remove(fromIndex);
328         sourceFolders.add(toIndex, sf);
329     }
330     
331     public void addSourceFolder(JavaProjectGenerator.SourceFolder sf, boolean isTests) {
332         List JavaDoc<CompilationUnitKey> keys = createCompilationUnitKeys();
333         boolean singleCU = isSingleCompilationUnit(keys);
334         if (singleCU) {
335             // Check that source being added is part of the compilation unit.
336
// If it is not then switch to multiple compilation unit mode.
337
JavaProjectGenerator.JavaCompilationUnit cu = javaCompilationUnitsList.get(0);
338             if (cu.isTests != isTests) {
339                 updateCompilationUnits(true);
340                 singleCU = false;
341             }
342         }
343         sourceFolders.add(sf);
344         if (singleCU) {
345             if (TYPE_JAVA.equals(sf.type)) {
346                 // update existing single compilation unit
347
JavaProjectGenerator.JavaCompilationUnit cu = javaCompilationUnitsList.get(0);
348                 cu.packageRoots.add(sf.location);
349             }
350         } else {
351             // make sure new compilation unit is created for the source folder
352
for (CompilationUnitKey key : createCompilationUnitKeys()) {
353                 getCompilationUnit(key, isTests);
354             }
355         }
356         // remember all added locations
357
if (removedSourceFolders.contains(sf.location)) {
358             removedSourceFolders.remove(sf.location);
359         } else {
360             addedSourceFolders.add(sf.location);
361         }
362         fireChangeEvent();
363     }
364
365     public void removeSourceFolder(int index) {
366         JavaProjectGenerator.SourceFolder sf = sourceFolders.get(index);
367         if (TYPE_JAVA.equals(sf.type)) {
368             removeSourceLocation(sf.location);
369         }
370         sourceFolders.remove(index);
371         // remember all removed locations
372
if (addedSourceFolders.contains(sf.location)) {
373             addedSourceFolders.remove(sf.location);
374         } else {
375             removedSourceFolders.add(sf.location);
376         }
377         fireChangeEvent();
378     }
379     
380     public void clearSourceFolders() {
381         sourceFolders.clear();
382         javaCompilationUnitsList.clear();
383         fireChangeEvent();
384     }
385     
386     public String JavaDoc getSourceLevel() {
387         return sourceLevel;
388     }
389
390     public void setSourceLevel(String JavaDoc sourceLevel) {
391         if ((this.sourceLevel == null && sourceLevel == null) ||
392             (this.sourceLevel != null && this.sourceLevel.equals(sourceLevel))) {
393             return;
394         }
395         this.sourceLevel = sourceLevel;
396         for (JavaProjectGenerator.JavaCompilationUnit cu : javaCompilationUnitsList) {
397             cu.sourceLevel = sourceLevel;
398         }
399     }
400     
401     public boolean canHaveSeparateClasspath() {
402         // if there is more than one source root or more than one
403
// compilation unit then enable checkbox "Separate Classpath".
404
return (sourceFolders.size() > 1 || javaCompilationUnitsList.size() > 1);
405     }
406
407     public boolean canCreateSingleCompilationUnit() {
408         // if there are sources and test sources I cannot create
409
// single compilation unit for them:
410
boolean testCU = false;
411         boolean sourceCU = false;
412         for (JavaProjectGenerator.JavaCompilationUnit cu : javaCompilationUnitsList) {
413             if (cu.isTests) {
414                 testCU = true;
415             } else {
416                 sourceCU = true;
417             }
418         }
419         return !(testCU && sourceCU);
420     }
421
422     public static boolean isSingleCompilationUnit(List JavaDoc<ProjectModel.CompilationUnitKey> compilationUnitKeys) {
423         return compilationUnitKeys.size() == 1 && compilationUnitKeys.get(0).label == null;
424     }
425
426     /**
427      * This method checks Java source folders and compilation units and returns
428      * list of CompilationUnitKey which represent them. The problem solved by
429      * this method is that although usually there is 1:1 mapping between
430      * source folders and compilation units, there can be also N:1 mapping when
431      * one classpath is used for all source folders. Also user's customization
432      * of project.xml can result in other combinations and they cannot be
433      * clobbered by opening such a project in UI.
434      */

435     public List JavaDoc<CompilationUnitKey> createCompilationUnitKeys() {
436         // XXX: cache result of this method?
437
List JavaDoc<CompilationUnitKey> l = new ArrayList JavaDoc<CompilationUnitKey>();
438         for (JavaProjectGenerator.JavaCompilationUnit cu : javaCompilationUnitsList) {
439             CompilationUnitKey cul = new CompilationUnitKey();
440             cul.locations = cu.packageRoots;
441             cul.label = null;
442             l.add(cul);
443         }
444         for (JavaProjectGenerator.SourceFolder sf : sourceFolders) {
445             if (!TYPE_JAVA.equals(sf.type)) {
446                 continue;
447             }
448             CompilationUnitKey cul = new CompilationUnitKey();
449             cul.locations = new ArrayList JavaDoc<String JavaDoc>();
450             cul.locations.add(sf.location);
451             cul.label = sf.label;
452             // try to find corresponding JavaCompilationUnit
453
int index = l.indexOf(cul);
454             if (index != -1) {
455                 // use this key intead because it has label
456
CompilationUnitKey cul_ = l.get(index);
457                 cul_.label = sf.label;
458                 continue;
459             }
460             // check whether this SourceFolder.location is not part of an existing JavaCompilationUnit
461
boolean found = false;
462             for (JavaProjectGenerator.JavaCompilationUnit cu_ : javaCompilationUnitsList) {
463                 if (cu_.packageRoots.contains(sf.location)) {
464                     // found: skip it
465
found = true;
466                     break;
467                 }
468             }
469             if (found) {
470                 continue;
471             }
472             // add this source folder then:
473
l.add(cul);
474         }
475         return l;
476     }
477
478     /**
479      * Update compilation units to 1:1 or 1:N mapping to source folders.
480      * The separateClasspath attribute if true says that each source folder
481      * will have its own compilation unit. In opposite case all source
482      * folders will have one compilation unit.
483      */

484     public void updateCompilationUnits(boolean separateClasspath) {
485         if (separateClasspath) {
486             // This means that there was one compilation unit for all sources.
487
// So create compilation unit per source folder.
488
String JavaDoc classpath = null;
489             List JavaDoc<String JavaDoc> output = null;
490             // Copy classpath and output from the first compilation unit
491
// to all compilation units - should be easier to customize for user.
492
if (javaCompilationUnitsList.size() > 0) {
493                 List JavaDoc<JavaProjectGenerator.JavaCompilationUnit.CP> classpaths = javaCompilationUnitsList.get(0).classpath;
494                 if (classpaths != null) {
495                     // find first "compile" mode classpath and use it
496
for (JavaProjectGenerator.JavaCompilationUnit.CP cp : classpaths) {
497                         if (cp.mode.equals(CLASSPATH_MODE_COMPILE)) {
498                             classpath = cp.classpath;
499                             break;
500                         }
501                     }
502                 }
503                 output = javaCompilationUnitsList.get(0).output;
504             }
505             javaCompilationUnitsList.clear();
506             for (JavaProjectGenerator.SourceFolder sf : sourceFolders) {
507                 JavaProjectGenerator.JavaCompilationUnit cu = new JavaProjectGenerator.JavaCompilationUnit();
508                 cu.packageRoots = new ArrayList JavaDoc<String JavaDoc>();
509                 cu.packageRoots.add(sf.location);
510                 if (classpath != null) {
511                     JavaProjectGenerator.JavaCompilationUnit.CP cp = new JavaProjectGenerator.JavaCompilationUnit.CP();
512                     cp.mode = CLASSPATH_MODE_COMPILE;
513                     cp.classpath = classpath;
514                     cu.classpath = new ArrayList JavaDoc<JavaProjectGenerator.JavaCompilationUnit.CP>();
515                     cu.classpath.add(cp);
516                 }
517                 if (output != null) {
518                     cu.output = new ArrayList JavaDoc<String JavaDoc>();
519                     cu.output.addAll(output);
520                 }
521                 cu.sourceLevel = sourceLevel;
522                 javaCompilationUnitsList.add(cu);
523             }
524         } else {
525             // This means that there are some compilation units which should be
526
// merged into one which will be used for all sources.
527
List JavaDoc<String JavaDoc> packageRoots = new ArrayList JavaDoc<String JavaDoc>();
528             // First list of source roots
529
for (JavaProjectGenerator.SourceFolder sf : sourceFolders) {
530                 packageRoots.add(sf.location);
531             }
532             // Now try to merge all classpaths and outputs. Might be easier to customize
533
Set JavaDoc<String JavaDoc> classpath = new LinkedHashSet JavaDoc<String JavaDoc>();
534             Set JavaDoc<String JavaDoc> output = new LinkedHashSet JavaDoc<String JavaDoc>();
535             for (JavaProjectGenerator.JavaCompilationUnit cu : javaCompilationUnitsList) {
536                 if (cu.output != null) {
537                     output.addAll(cu.output);
538                 }
539                 if (cu.classpath != null) {
540                     for (JavaProjectGenerator.JavaCompilationUnit.CP cp : cu.classpath) {
541                         if (cp.mode.equals(CLASSPATH_MODE_COMPILE)) {
542                             classpath.addAll(Arrays.asList(PropertyUtils.tokenizePath(cp.classpath)));
543                         }
544                     }
545                 }
546             }
547             javaCompilationUnitsList.clear();
548             JavaProjectGenerator.JavaCompilationUnit cu = new JavaProjectGenerator.JavaCompilationUnit();
549             cu.packageRoots = packageRoots;
550             JavaProjectGenerator.JavaCompilationUnit.CP cp = new JavaProjectGenerator.JavaCompilationUnit.CP();
551             if (classpath.size() > 0) {
552                 StringBuffer JavaDoc cp_ = new StringBuffer JavaDoc();
553                 Iterator JavaDoc<String JavaDoc> it = classpath.iterator();
554                 while (it.hasNext()) {
555                     cp_.append(it.next());
556                     if (it.hasNext()) {
557                         cp_.append(File.pathSeparatorChar);
558                     }
559                 }
560                 cp.classpath = cp_.toString();
561                 cp.mode = CLASSPATH_MODE_COMPILE;
562                 cu.classpath = new ArrayList JavaDoc<JavaProjectGenerator.JavaCompilationUnit.CP>();
563                 cu.classpath.add(cp);
564             }
565             cu.output = new ArrayList JavaDoc<String JavaDoc>(output);
566             cu.sourceLevel = sourceLevel;
567             javaCompilationUnitsList.add(cu);
568         }
569         fireChangeEvent();
570     }
571
572     /** Retrieve compilation unit or create empty one if it does not exist yet for the given
573      * key which is source package path(s).
574      * The isTests is used only to initialize newly created compilation unit.
575      */

576     public JavaProjectGenerator.JavaCompilationUnit getCompilationUnit(CompilationUnitKey key, boolean isTests) {
577         for (JavaProjectGenerator.JavaCompilationUnit cu : javaCompilationUnitsList) {
578             if (cu.packageRoots.equals(key.locations)) {
579                 return cu;
580             }
581         }
582         JavaProjectGenerator.JavaCompilationUnit cu = new JavaProjectGenerator.JavaCompilationUnit();
583         cu.packageRoots = key.locations;
584         cu.sourceLevel = sourceLevel;
585         cu.isTests = isTests;
586         javaCompilationUnitsList.add(cu);
587         return cu;
588     }
589
590     private void removeSourceLocation(String JavaDoc location) {
591         Iterator JavaDoc<JavaProjectGenerator.JavaCompilationUnit> it = javaCompilationUnitsList.iterator();
592         while (it.hasNext()) {
593             JavaProjectGenerator.JavaCompilationUnit cu = it.next();
594             if (cu.packageRoots.contains(location)) {
595                 cu.packageRoots.remove(location);
596             }
597             if (cu.packageRoots.size() == 0) {
598                 it.remove();
599             }
600         }
601     }
602
603     /** Update style of loaded source folders of type "java" to packages. */
604     private static void updateStyle(List JavaDoc<JavaProjectGenerator.SourceFolder> sources) {
605         for (JavaProjectGenerator.SourceFolder sf : sources) {
606             assert sf.type.equals(TYPE_JAVA);
607             sf.style = JavaProjectNature.STYLE_PACKAGES;
608         }
609     }
610     
611     // only for unit testing
612
void setSourceFolders(List JavaDoc<JavaProjectGenerator.SourceFolder> list) {
613         sourceFolders = list;
614     }
615     
616     // only for unit testing
617
List JavaDoc<JavaProjectGenerator.SourceFolder> getSourceFolders() {
618         return sourceFolders;
619     }
620     
621     // only for unit testing
622
void setJavaCompilationUnits(List JavaDoc<JavaProjectGenerator.JavaCompilationUnit> list) {
623         javaCompilationUnitsList = list;
624     }
625     
626     // only for unit testing
627
List JavaDoc<JavaProjectGenerator.JavaCompilationUnit> getJavaCompilationUnits() {
628         return javaCompilationUnitsList;
629     }
630     
631     
632     /**
633      * Helper method returning source level of the current platform.
634      */

635     public static String JavaDoc getDefaultSourceLevel() {
636         JavaPlatform platform = JavaPlatform.getDefault();
637         SpecificationVersion sv = platform.getSpecification().getVersion();
638         if (sv.compareTo(JDK_MAX_SUPPORTED_VERSION)>0) {
639             sv = JDK_MAX_SUPPORTED_VERSION;
640         }
641         return sv.toString();
642     }
643     
644     public boolean isTestSourceFolder(int index) {
645         return isTestSourceFolder(getSourceFolder(index));
646     }
647     
648     public boolean isTestSourceFolder(JavaProjectGenerator.SourceFolder sf) {
649         for (JavaProjectGenerator.JavaCompilationUnit cu : javaCompilationUnitsList) {
650             if (cu.packageRoots.contains(sf.location)) {
651                 return cu.isTests;
652             }
653         }
654         return false;
655     }
656     
657     public static class CompilationUnitKey {
658         public List JavaDoc<String JavaDoc> locations;
659         public String JavaDoc label;
660         
661         public boolean equals(Object JavaDoc o) {
662             if (o == this) {
663                 return true;
664             }
665             if (!(o instanceof CompilationUnitKey)) {
666                 return false;
667             }
668             CompilationUnitKey cul = (CompilationUnitKey)o;
669             return this.locations.equals(cul.locations);
670         }
671         
672         public int hashCode() {
673             return locations.hashCode()*7;
674         }
675
676         public String JavaDoc toString() {
677             return "PM.CUK:[label="+label+", locations="+locations+", this="+super.toString()+"]"; // NOI18N
678
}
679     }
680     
681 }
682
Popular Tags