KickJava   Java API By Example, From Geeks To Geeks.

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


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.refactoring.java.classpath;
21 import java.net.URL JavaDoc;
22 import java.util.*;
23 import org.netbeans.api.java.classpath.ClassPath;
24 import org.netbeans.api.java.project.JavaProjectConstants;
25 import org.netbeans.api.java.queries.SourceForBinaryQuery;
26 import org.netbeans.api.project.FileOwnerQuery;
27 import org.netbeans.api.project.Project;
28 import org.netbeans.api.project.ProjectUtils;
29 import org.netbeans.api.project.SourceGroup;
30 import org.netbeans.api.project.Sources;
31 import org.netbeans.api.project.ui.OpenProjects;
32 import org.openide.filesystems.FileObject;
33 import org.openide.filesystems.FileUtil;
34
35 /**
36  *
37  * @author Jan Becicka
38  */

39 public class Util {
40     
41     private static WeakHashMap superProjectsCache = null;
42     
43     private static WeakHashMap getSuperProjectsCache() {
44         if (superProjectsCache==null)
45             superProjectsCache = new WeakHashMap();
46         return superProjectsCache;
47     }
48     
49     public static Set getSuperprojects(Project project) {
50         Set result = (Set) getSuperProjectsCache().get(project);
51         if (result!=null) {
52             //return cached result
53
return result;
54         }
55         result = new HashSet();
56         Project openedProjects[] = OpenProjects.getDefault().getOpenProjects();
57         HashSet sourcePath = new HashSet();
58         Sources sources = ProjectUtils.getSources(project);
59         SourceGroup[] sourceGroup = sources.getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA);
60         for(int sourceGroupsIndex=0; sourceGroupsIndex<sourceGroup.length; sourceGroupsIndex++) {
61             //add each directory on ClassPath.SOURCE to sourcePath
62
ClassPath cp = ClassPath.getClassPath(sourceGroup[sourceGroupsIndex].getRootFolder(), ClassPath.SOURCE);
63             sourcePath.addAll(Arrays.asList(cp.getRoots()));
64         }
65         
66         for (int i = 0; i<openedProjects.length; i++) {
67             //for all opened projects
68
Project p = openedProjects[i];
69             Sources src = ProjectUtils.getSources(p);
70             SourceGroup[] groups = src.getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA);
71             for(int j=0; j<groups.length; j++) {
72                 //for all groups get COMPILE classpath
73
ClassPath cp = ClassPath.getClassPath(groups[j].getRootFolder(), ClassPath.COMPILE);
74                 List entries = cp.entries();
75                 for (Iterator it = entries.iterator(); it.hasNext();) {
76                     //try to find source for each class path item
77
URL JavaDoc binURL = ((ClassPath.Entry) it.next()).getURL();
78                     FileObject[] roots = SourceForBinaryQuery.findSourceRoots(binURL).getRoots();
79                     for (int z = 0; z<roots.length; z++) {
80                         if (sourcePath.contains(roots[z])) {
81                             //if source of class path item is on sourcePath - this project depend on given project
82
result.add(p);
83                         }
84                     }
85                 }
86             }
87         }
88         getSuperProjectsCache().put(project, result);
89         return result;
90     }
91     
92     static void resetCache() {
93         superProjectsCache = null;
94     }
95     
96     public static boolean isUnderSourceRootOfOpenProjects(FileObject fo) {
97         Project p = FileOwnerQuery.getOwner(fo);
98         if (p==null) return false;
99         Project[] opened = OpenProjects.getDefault().getOpenProjects();
100         for (int i = 0; i<opened.length; i++) {
101             if (p==opened[i]) {
102                 SourceGroup[] gr = ProjectUtils.getSources(p).getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA);
103                 for (int j = 0; j < gr.length; j++) {
104                     if (fo==gr[j].getRootFolder()) return true;
105                     if (FileUtil.isParentOf(gr[j].getRootFolder(), fo))
106                         return true;
107                 }
108                 return false;
109             }
110         }
111         return false;
112     }
113
114 }
115
Popular Tags