KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > refactoring > java > classpath > RefactoringClassPathImplementation


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 package org.netbeans.modules.refactoring.java.classpath;
20 import java.beans.PropertyChangeEvent JavaDoc;
21 import java.beans.PropertyChangeListener JavaDoc;
22 import java.beans.PropertyChangeSupport JavaDoc;
23 import java.net.URL JavaDoc;
24 import java.util.Arrays JavaDoc;
25 import java.util.Collection JavaDoc;
26 import java.util.Set JavaDoc;
27 import java.util.HashSet JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.ArrayList JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.Collections JavaDoc;
32 import org.netbeans.api.java.project.JavaProjectConstants;
33 import org.netbeans.api.project.FileOwnerQuery;
34 import org.netbeans.api.project.Project;
35 import org.netbeans.api.project.ProjectUtils;
36 import org.netbeans.api.project.SourceGroup;
37 import org.netbeans.api.project.Sources;
38 import org.netbeans.api.project.ui.OpenProjects;
39 import org.netbeans.spi.java.classpath.ClassPathFactory;
40 import org.openide.ErrorManager;
41 import org.openide.filesystems.FileObject;
42 import org.openide.filesystems.FileStateInvalidException;
43 import org.netbeans.api.java.classpath.ClassPath;
44 import org.netbeans.api.java.classpath.GlobalPathRegistry;
45 import org.netbeans.api.java.classpath.GlobalPathRegistryEvent;
46
47 import org.netbeans.api.java.classpath.GlobalPathRegistryListener;
48
49 import org.netbeans.api.java.queries.SourceForBinaryQuery;
50 import org.netbeans.spi.java.classpath.ClassPathImplementation;
51 import org.netbeans.spi.java.classpath.support.ClassPathSupport;
52
53 import org.openide.util.WeakListeners;
54
55
56
57
58 public class RefactoringClassPathImplementation implements ClassPathImplementation, GlobalPathRegistryListener, PropertyChangeListener JavaDoc {
59
60     private List JavaDoc resourceCache;
61     private PropertyChangeSupport JavaDoc support;
62     private Collection JavaDoc fileSet;
63
64     private RefactoringClassPathImplementation(Collection JavaDoc set) {
65         this.support = new PropertyChangeSupport JavaDoc(this);
66         this.fileSet = set;
67     }
68
69     public synchronized List JavaDoc getResources() {
70         if (this.resourceCache == null) {
71             this.resourceCache = Collections.unmodifiableList(this.createResources());
72         }
73         return this.resourceCache;
74     }
75
76     public void addPropertyChangeListener(PropertyChangeListener JavaDoc listener) {
77         this.support.addPropertyChangeListener (listener);
78     }
79
80     public void removePropertyChangeListener(PropertyChangeListener JavaDoc listener) {
81         this.support.removePropertyChangeListener(listener);
82     }
83     
84     public void pathsAdded(GlobalPathRegistryEvent event) {
85         resetCache();
86     }
87
88     
89     public void pathsRemoved(GlobalPathRegistryEvent event) {
90         resetCache();
91     }
92
93     public void propertyChange (PropertyChangeEvent JavaDoc event) {
94         resetCache();
95     }
96
97     private List JavaDoc createResources () {
98         List JavaDoc result = new ArrayList JavaDoc ();
99         Set JavaDoc covered = new HashSet JavaDoc ();
100         GlobalPathRegistry regs = GlobalPathRegistry.getDefault();
101         regs.addGlobalPathRegistryListener((GlobalPathRegistryListener)WeakListeners.create(GlobalPathRegistryListener.class, this, regs));
102         assert regs != null : "GlobalPathRegistry.getDefault() returned null"; //NOI18N
103

104         computeDependencies();
105
106         for (Iterator JavaDoc it = sources.iterator(); it.hasNext();) {
107             ClassPath cp = (ClassPath) it.next ();
108             for (Iterator JavaDoc et = cp.entries().iterator(); et.hasNext();) {
109                 ClassPath.Entry entry = (ClassPath.Entry) et.next();
110                 URL JavaDoc url = entry.getURL();
111                 assert url != null : "ClassPath.Entry.getURL() returned null"; //NOI18N
112
if (covered.add (url))
113                     result.add (ClassPathSupport.createResource(url));
114             }
115             cp.addPropertyChangeListener((PropertyChangeListener JavaDoc)WeakListeners.create(PropertyChangeListener JavaDoc.class,this,cp));
116         }
117         addResources(boot,covered,result);
118         addResources(compile,covered,result);
119         return result;
120     }
121     
122     private Set JavaDoc sources;
123     private Set JavaDoc compile;
124     private Set JavaDoc boot;
125     
126     private void computeDependencies() {
127         if (fileSet.isEmpty()) {
128             GlobalPathRegistry regs = GlobalPathRegistry.getDefault();
129             sources = regs.getPaths(ClassPath.SOURCE);
130             assert sources != null : "GlobalPathRegistry.getPath() for SOURCES returned null"; //NOI18N
131
compile = regs.getPaths(ClassPath.COMPILE);
132             assert compile != null : "GlobalPathRegistry.getPath() for COMPILE returned null"; //NOI18N
133
boot = regs.getPaths(ClassPath.BOOT);
134             assert boot != null : "GlobalPathRegistry.getPath() for BOOT returned null"; //NOI18N
135
} else {
136             OpenProjects.getDefault().addPropertyChangeListener((PropertyChangeListener JavaDoc)WeakListeners.create(PropertyChangeListener JavaDoc.class,this,OpenProjects.getDefault()));
137             
138             sources = new HashSet JavaDoc();
139             compile = new HashSet JavaDoc();
140             boot = new HashSet JavaDoc();
141             for (Iterator JavaDoc it = getRelevantProjects().iterator();it.hasNext();) {
142                 Project p = (Project) it.next();
143                 Sources src = ProjectUtils.getSources(p);
144                 SourceGroup[] groups = src.getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA);
145                 for(int i=0; i<groups.length; i++) {
146                     ClassPath cp = ClassPath.getClassPath(groups[i].getRootFolder(), ClassPath.SOURCE);
147                     sources.add(cp);
148                     cp = ClassPath.getClassPath(groups[i].getRootFolder(), ClassPath.COMPILE);
149                     compile.add(cp);
150                     cp = ClassPath.getClassPath(groups[i].getRootFolder(), ClassPath.BOOT);
151                     boot.add(cp);
152                 }
153             }
154         }
155     }
156     
157     private Collection JavaDoc getRelevantProjects() {
158         Collection JavaDoc relevantProjects = new HashSet JavaDoc();
159         for (Iterator JavaDoc i = fileSet.iterator(); i.hasNext();) {
160             FileObject fo = (FileObject) i.next();
161             Project p = FileOwnerQuery.getOwner(fo);
162             if (p == null) {
163                 return Arrays.asList(OpenProjects.getDefault().getOpenProjects());
164             }
165             relevantProjects.addAll(Util.getSuperprojects(p));
166             relevantProjects.add(p);
167         }
168         return relevantProjects;
169     }
170
171     private void addResources (Set JavaDoc classPaths, Set JavaDoc coveredResources, List JavaDoc addTo) {
172         for (Iterator JavaDoc it = classPaths.iterator(); it.hasNext();) {
173             ClassPath cp = (ClassPath) it.next ();
174             for (Iterator JavaDoc et = cp.entries().iterator(); et.hasNext();) {
175                 ClassPath.Entry entry = (ClassPath.Entry) et.next();
176                 URL JavaDoc url = entry.getURL();
177                 assert url != null : "ClassPath.Entry.getURL() returned null"; //NOI18N
178
if (!isCovered (coveredResources, url)) {
179                     addTo.add (ClassPathSupport.createResource(url));
180                     coveredResources.add(url);
181                 }
182             }
183             cp.addPropertyChangeListener((PropertyChangeListener JavaDoc)WeakListeners.create(PropertyChangeListener JavaDoc.class,this,cp));
184         }
185     }
186
187     private static boolean isCovered (Set JavaDoc coveredResources, URL JavaDoc url) {
188         if (coveredResources.contains(url))
189             return true;
190         FileObject[] fos = SourceForBinaryQuery.findSourceRoots(url).getRoots();
191         assert fos != null : "SourceForBinaryQuery.findSourceRoot() returned null."; // NOI18N
192
for (int i=0; i< fos.length; i++) {
193             try {
194                 if (coveredResources.contains(fos[i].getURL())) {
195                     return true;
196                 }
197             } catch (FileStateInvalidException e) {
198                 ErrorManager.getDefault().notify(e);
199             }
200         }
201         return false;
202     }
203     
204     private void firePropertyChange () {
205         this.support.firePropertyChange(PROP_RESOURCES,null,null);
206     }
207     
208     
209     public synchronized static ClassPath getDefault () {
210         if (defaultInstance == null) {
211             defaultInstance = ClassPathFactory.createClassPath(
212                 new RefactoringClassPathImplementation(Collections.EMPTY_SET));
213         }
214         return defaultInstance;
215     }
216     
217     public synchronized static ClassPath getCustom(Collection JavaDoc<FileObject> set) {
218         assert set != null;
219         if (customInstance == null || !theSameProjects(set, customInstanceSPI.fileSet)) {
220             customInstanceSPI = new RefactoringClassPathImplementation (set);
221             customInstance = ClassPathFactory.createClassPath (customInstanceSPI);
222         }
223         return customInstance;
224     }
225     
226     private static boolean theSameProjects(Collection JavaDoc<FileObject> set1, Collection JavaDoc<FileObject> set2) {
227         if (set1.equals(set2))
228             return true;
229         HashSet JavaDoc projects1 = getProjects(set1);
230         HashSet JavaDoc projects2 = getProjects(set2);
231         
232         return projects1.equals(projects2);
233     }
234     
235     private static HashSet JavaDoc getProjects(Collection JavaDoc<FileObject> files) {
236         HashSet JavaDoc projects = new HashSet JavaDoc(2);
237         for (FileObject fo:files) {
238             if (fo!=null) {
239                 Project p = FileOwnerQuery.getOwner(fo);
240                 projects.add(p);
241             }
242         }
243         return projects;
244     }
245     
246     private synchronized static void resetCache () {
247         defaultInstance = null;
248         customInstance = null;
249         Util.resetCache();
250     }
251     
252     private static ClassPath defaultInstance;
253     private static ClassPath customInstance;
254     private static RefactoringClassPathImplementation customInstanceSPI;
255
256 }
257
Popular Tags