KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > projectimport > j2seimport > AbstractProject


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.projectimport.j2seimport;
21
22 import java.io.File JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Collection JavaDoc;
25 import java.util.HashSet JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.LinkedHashSet JavaDoc;
28 import java.util.Set JavaDoc;
29 import java.util.logging.Logger JavaDoc;
30 import org.openide.filesystems.FileObject;
31 import org.openide.filesystems.FileUtil;
32 import org.openide.util.NbBundle;
33
34 /**
35  *
36  * @author Radek Matous
37  */

38 public final class AbstractProject implements ProjectModel {
39     private final Collection JavaDoc libraries;
40     private final Collection JavaDoc userLibraries;
41     private final Collection JavaDoc sourceRoots;
42     private final Set JavaDoc dependencies;
43     private File JavaDoc jdkDirectory;
44     private String JavaDoc jdkId;
45     private final FileObject projectDir;
46     private final String JavaDoc name;
47     private WarningContainer warnings;
48     
49     private boolean isAlreadyImported = false;
50     
51     public static final Logger JavaDoc logger =
52             LoggerFactory.getDefault().createLogger(AbstractProject.class);
53     
54     
55     /** Creates a new instance of AbstractProjectDefinition */
56     public AbstractProject(String JavaDoc name, FileObject projectDir) {
57         this.projectDir = projectDir;
58         this.name = name;
59         if (name.indexOf('/') != -1) {
60             throw new IllegalArgumentException JavaDoc();
61         }
62         
63         libraries = new LinkedHashSet JavaDoc();
64         userLibraries = new LinkedHashSet JavaDoc();
65         sourceRoots = new LinkedHashSet JavaDoc();
66         dependencies = new LinkedHashSet JavaDoc();
67         warnings = new WarningContainer();
68         
69         logger.finest("created project: " + "\""+ name + "\" (" + //NOI18N
70
projectDir.getPath() + ")");//NOI18N
71

72     }
73     
74     public String JavaDoc getName() {
75         return name;
76     }
77     
78     public FileObject getProjectDir() {
79         return projectDir;
80     }
81     
82     
83     public boolean isAlreadyImported() {
84         return isAlreadyImported;
85     }
86     
87     public void setAsImported() {
88         isAlreadyImported = true;
89     }
90     
91     public Collection JavaDoc/*<AbstractProjectDefinition.AbstractLibraryEntry>*/ getLibraries() {
92         return libraries;
93     }
94     
95     private void addWarning(final String JavaDoc warning, boolean userNotification) {
96         StringBuffer JavaDoc sbuf = new StringBuffer JavaDoc(NbBundle.getMessage(AbstractProject.class, "MSG_ProjectDefinitionWarning"));//NOI18N
97
sbuf.append(" ").append(warning);//NOI18N
98

99         String JavaDoc warningPlusPrefix = sbuf.toString();
100         warnings.add(warningPlusPrefix, userNotification);
101         AbstractProject.logger.warning(warningPlusPrefix);
102     }
103     
104     
105     public boolean addLibrary(final AbstractProject.Library lEntry) {
106         if (isAlreadyImported()) {
107             throw new IllegalStateException JavaDoc("Unexpected usage: project was already imported");//NOI18N
108
}
109         
110         if (!lEntry.isValid()) {
111             addWarning(NbBundle.getMessage(AbstractProject.class, "MSG_InvalidLibrary",
112                     lEntry.getArchiv().getAbsolutePath(),this.getName()), false);//NOI18N
113
}
114         
115         boolean retVal = libraries.add(lEntry);
116         if (retVal == false) {
117             addWarning(NbBundle.getMessage(AbstractProject.class, "MSG_AlreadyExistsLibrary",
118                     lEntry.getArchiv().getAbsolutePath(),this.getName()), false); //NOI18N
119
}
120         
121         logger.finest("added library: " + "\"" + lEntry.getArchiv().getAbsolutePath() + "\""); //NOI18N
122

123         return retVal;
124     }
125     
126     
127     public Collection JavaDoc/*<AbstractProjectDefinition.AbstractUserLibrary>*/ getUserLibraries() {
128         return userLibraries;
129     }
130     
131     public boolean addUserLibrary(final AbstractProject.UserLibrary uEntry) {
132         if (isAlreadyImported()) {
133             throw new IllegalStateException JavaDoc("Unexpected usage: project was already imported");//NOI18N
134
}
135         
136         if (!uEntry.isValid()) {
137             addWarning(NbBundle.getMessage(AbstractProject.class, "MSG_InvalidUserLibrary",
138                     uEntry.getName(),this.getName()), true); //NOI18N
139
}
140         
141         checkUserLibrary(uEntry);
142         boolean retVal = userLibraries.add(uEntry);
143         
144         if (retVal == false) {
145             addWarning(NbBundle.getMessage(AbstractProject.class, "MSG_AlreadyExistsUserLibrary",
146                     uEntry.getName(),this.getName()), false); //NOI18N
147
}
148         
149         logger.finest("added user library: " + "\"" + uEntry.getName() + "\""); //NOI18N
150

151         return retVal;
152     }
153     
154     private void checkUserLibrary(AbstractProject.UserLibrary uLibrary) {
155         for (Iterator JavaDoc it = uLibrary.getLibraries().iterator(); it.hasNext();) {
156             AbstractProject.Library lEntry = (AbstractProject.Library)it.next();
157             if (getLibraries().contains(lEntry)) {
158                 addWarning(NbBundle.getMessage(AbstractProject.class, "MSG_AlreadyExistsLibrary",
159                         lEntry.getArchiv().getAbsolutePath(),this.getName()), false); //NOI18N
160
}
161         }
162     }
163     
164     public Collection JavaDoc/*<AbstractProjectDefinition.AbstractSourceRootEntry>*/ getSourceRoots() {
165         return sourceRoots;
166     }
167     
168     public boolean addSourceRoot(final AbstractProject.SourceRoot srcEntry) {
169         if (isAlreadyImported()) {
170             throw new IllegalStateException JavaDoc("Unexpected usage: project was already imported");//NOI18N
171
}
172         
173         if (!srcEntry.isValid()) {
174             addWarning(NbBundle.getMessage(AbstractProject.class, "MSG_InvalidSourceRoot",
175                     srcEntry.getDirectory().getAbsolutePath(),this.getName()), false); //NOI18N
176
return false;
177         }
178         
179         boolean retVal = sourceRoots.add(srcEntry);
180         if (retVal == false) {
181             addWarning(NbBundle.getMessage(AbstractProject.class, "MSG_AlreadyExistsSourceRoot",
182                     srcEntry.getDirectory().getAbsolutePath(),this.getName()), false); //NOI18N
183
}
184         
185         logger.finest("added source root: " + "\"" + srcEntry.getLabel()+" ("+srcEntry.getDirectory().getAbsolutePath()+ ")\"" ); //NOI18N
186

187         return retVal;
188     }
189     
190     public java.util.Set JavaDoc/*<AbstractProjectDefinition>*/ getDependencies() {
191         return dependencies;
192     }
193     
194     public boolean addDependency(final AbstractProject projectDefinition) {
195         if (isAlreadyImported()) {
196             throw new IllegalStateException JavaDoc("Unexpected usage: project was already imported");//NOI18N
197
}
198         
199         logger.finest("added dependency: " + "\""+ projectDefinition.getName() + "\" (" + //NOI18N
200
projectDefinition.getProjectDir().getPath() + ")");//NOI18N
201

202         return dependencies.add(projectDefinition);
203     }
204     
205     
206     public File JavaDoc getJDKDirectory() {
207         return jdkDirectory;
208     }
209     
210     public void setJDKDirectory(File JavaDoc jdkDirectory) {
211         this.jdkDirectory = jdkDirectory;
212     }
213     
214     public void setInvalidJDK(String JavaDoc expectedJDKId) {
215         addWarning(NbBundle.getMessage(AbstractProject.class, "MSG_JDKDoesnExistUseDefault",
216                 this.getName(), expectedJDKId),true); //NOI18N
217
}
218     
219     
220     public WarningContainer getWarnings() {
221         return warnings;
222     }
223     
224     public Collection JavaDoc/**<String>*/ getErrors() {
225         Collection JavaDoc errors = new HashSet JavaDoc();
226         DependencyValidator instance = DependencyValidator.checkProject(this);
227         if (!instance.isValid()) {
228             errors.add(instance.getErrorMessage());
229         }
230         if (getSourceRoots().size() == 0) {
231             errors.add(NbBundle.getMessage(
232                     AbstractProject.class,"ERR_NoSourceRoot",this.getName()));//NOI18N
233

234         }
235         return errors;
236     }
237     
238     
239     public static final class SourceRoot implements ProjectModel.SourceRoot {
240         private final String JavaDoc label;
241         private final File JavaDoc sourceFolder;
242         
243         public SourceRoot(String JavaDoc label, File JavaDoc sourceFolder) {
244             this.label = label;
245             this.sourceFolder = sourceFolder;
246         }
247         public final String JavaDoc getLabel() {
248             return label;
249         }
250         
251         public final File JavaDoc getDirectory() {
252             return sourceFolder;
253         }
254         
255         
256         public final boolean isValid() {
257             return AbstractProject.isValid(getDirectory());
258         }
259         
260         public boolean equals(Object JavaDoc obj) {
261             return (obj instanceof SourceRoot) ?
262                 ((SourceRoot)obj).getDirectory().equals(getDirectory()) : false;
263         }
264         
265         public int hashCode() {
266             return getLabel().hashCode();
267         }
268         
269         public String JavaDoc toString() {
270             StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
271             sb.append(NbBundle.getMessage(AbstractProject.class,"TXT_SourceRoot"));//NOI18N
272
sb.append((isValid()) ? "" : "!");//NOI18N
273
sb.append((sourceFolder != null) ? sourceFolder.getAbsolutePath() : "");//NOI18N
274

275             return sb.toString();
276         }
277         
278     }
279     
280     public static final class Library implements ProjectModel.Library {
281         private File JavaDoc archiv;
282         public Library(File JavaDoc archiv) {
283             this.archiv = archiv;
284         }
285         
286         public java.io.File JavaDoc getArchiv() {
287             return archiv;
288         }
289         
290         public final boolean isValid() {
291             return AbstractProject.isValidArchiv(getArchiv());
292         }
293         
294         public boolean equals(Object JavaDoc obj) {
295             return (obj instanceof Library ) ?
296                 ((Library )obj).getArchiv().equals(getArchiv()) : false;
297         }
298         
299         public String JavaDoc toString() {
300             StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
301             sb.append(NbBundle.getMessage(AbstractProject.class,"TXT_Library"));//NOI18N
302
sb.append((isValid()) ? "" : "!");//NOI18N
303
sb.append((archiv != null) ? archiv.getAbsolutePath() : "");//NOI18N
304

305             return sb.toString();
306         }
307     }
308     
309     public static final class UserLibrary implements ProjectModel.UserLibrary {
310         private final String JavaDoc name;
311         private Collection JavaDoc/*<AbstractProject.Library>*/ libraries;
312         private boolean fileNotFound = false;
313         private Collection JavaDoc/*<AbstractProject.UserLibrary>*/ dependencies;
314         
315
316         public UserLibrary(String JavaDoc name, boolean fileNotFound) {
317             this(name);
318             this.fileNotFound = fileNotFound;
319         }
320         
321         public UserLibrary(String JavaDoc name) {
322             this.name = name;
323             libraries = new HashSet JavaDoc();
324             dependencies = new HashSet JavaDoc();
325         }
326         
327         public UserLibrary(String JavaDoc name, Collection JavaDoc/*<AbstractLibraryEntry>*/ libraries) {
328             this(name);
329             libraries.addAll(libraries);
330         }
331         
332         
333         public boolean addLibrary(ProjectModel.Library lEntry) {
334             logger.finest("added library: " + "\"" + lEntry.getArchiv().getAbsolutePath() + "\""); //NOI18N
335

336             return libraries.add(lEntry);
337         }
338         
339         public boolean addDependency(ProjectModel.UserLibrary uLibrary) {
340             logger.finest("added library dependency: " + "\"" + uLibrary.getName() + "\""); //NOI18N
341

342             return dependencies.add(uLibrary);
343         }
344         
345         
346         
347         public String JavaDoc getName() {
348             return name;
349         }
350         
351         public Collection JavaDoc/*<AbstractProjectDefinition.AbstractLibraryEntry>*/ getLibraries() {
352             return libraries;
353         }
354
355         public final boolean fileNotFound() {
356             return fileNotFound;
357         }
358         
359         public final boolean isValid() {
360             boolean isValid = false;//no included library means invalid state
361
for (Iterator JavaDoc it = getLibraries().iterator(); it.hasNext();) {
362                 AbstractProject.Library lEntry = (AbstractProject.Library)it.next();
363                 isValid = lEntry.isValid();
364                 if (!isValid) {break;}
365                 
366             }
367             return isValid;
368         }
369         
370         public String JavaDoc toString() {
371             StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
372             sb.append(NbBundle.getMessage(AbstractProject.class,"TXT_UserLibrary"));//NOI18N
373
sb.append((isValid()) ? "" : "!");//NOI18N
374
sb.append(getName());
375             
376             return sb.toString();
377         }
378
379         public Collection JavaDoc getDependencies() {
380             return dependencies;
381         }
382         
383         
384     }
385     
386     private static boolean isValid(File JavaDoc f) {
387         File JavaDoc srcFolder = FileUtil.normalizeFile(f);
388         return (srcFolder.isDirectory() && FileUtil.toFileObject(srcFolder) != null);
389     }
390     
391     private static boolean isValidArchiv(File JavaDoc f) {
392         File JavaDoc srcFolder = FileUtil.normalizeFile(f);
393         FileObject srcFileObject = FileUtil.toFileObject(srcFolder);
394         return (srcFolder.exists() && srcFileObject != null) && FileUtil.isArchiveFile(srcFileObject);
395     }
396     
397     public String JavaDoc toString() {
398         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
399         
400         sb.append(NbBundle.getMessage(AbstractProject.class,"TXT_Project"));//NOI18N
401
sb.append(this.getName()).append("\n");//NOI18N
402

403         Collection JavaDoc all = new ArrayList JavaDoc();
404         all.addAll(getSourceRoots());
405         all.addAll(getLibraries());
406         all.addAll(getUserLibraries());
407         
408         for (Iterator JavaDoc it = all.iterator(); it.hasNext();) {
409             sb.append(it.next().toString()).append("\n");//NOI18N
410
}
411         
412         for (Iterator JavaDoc it = getDependencies().iterator(); it.hasNext();) {
413             sb.append(NbBundle.getMessage(AbstractProject.class,"TXT_Deps"));//NOI18N
414
sb.append(((AbstractProject)it.next()).getProjectDir().getPath()).append("\n");
415         }
416         
417         
418         
419         sb.append(NbBundle.getMessage(AbstractProject.class,"TXT_JDK"));//NOI18N
420
sb.append((getJDKDirectory() != null) ? getJDKDirectory().getAbsolutePath() : "!");//NOI18N
421

422         return sb.toString();
423     }
424
425     public String JavaDoc getJdkId() {
426         return jdkId;
427     }
428
429     public void setJdkId(String JavaDoc jdkId) {
430         this.jdkId = jdkId;
431     }
432     
433 }
434
Popular Tags