KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > core > hierarchy > RegionBasedHierarchyBuilder


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.core.hierarchy;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.HashMap JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.Map JavaDoc;
17
18 import org.eclipse.core.runtime.IProgressMonitor;
19 import org.eclipse.core.runtime.SubProgressMonitor;
20 import org.eclipse.jdt.core.IClassFile;
21 import org.eclipse.jdt.core.ICompilationUnit;
22 import org.eclipse.jdt.core.IJavaElement;
23 import org.eclipse.jdt.core.IJavaProject;
24 import org.eclipse.jdt.core.IPackageFragment;
25 import org.eclipse.jdt.core.IPackageFragmentRoot;
26 import org.eclipse.jdt.core.IType;
27 import org.eclipse.jdt.core.JavaModelException;
28 import org.eclipse.jdt.internal.core.JavaModelManager;
29 import org.eclipse.jdt.internal.core.JavaProject;
30 import org.eclipse.jdt.internal.core.Openable;
31 import org.eclipse.jdt.internal.core.SearchableEnvironment;
32
33 public class RegionBasedHierarchyBuilder extends HierarchyBuilder {
34     
35     public RegionBasedHierarchyBuilder(TypeHierarchy hierarchy)
36         throws JavaModelException {
37             
38         super(hierarchy);
39     }
40     
41 public void build(boolean computeSubtypes) {
42         
43     JavaModelManager manager = JavaModelManager.getJavaModelManager();
44     try {
45         // optimize access to zip files while building hierarchy
46
manager.cacheZipFiles();
47                 
48         if (this.hierarchy.focusType == null || computeSubtypes) {
49             IProgressMonitor typeInRegionMonitor =
50                 this.hierarchy.progressMonitor == null ?
51                     null :
52                     new SubProgressMonitor(this.hierarchy.progressMonitor, 30);
53             HashMap JavaDoc allOpenablesInRegion = determineOpenablesInRegion(typeInRegionMonitor);
54             this.hierarchy.initialize(allOpenablesInRegion.size());
55             IProgressMonitor buildMonitor =
56                 this.hierarchy.progressMonitor == null ?
57                     null :
58                     new SubProgressMonitor(this.hierarchy.progressMonitor, 70);
59             createTypeHierarchyBasedOnRegion(allOpenablesInRegion, buildMonitor);
60             ((RegionBasedTypeHierarchy)this.hierarchy).pruneDeadBranches();
61         } else {
62             this.hierarchy.initialize(1);
63             this.buildSupertypes();
64         }
65     } finally {
66         manager.flushZipFiles();
67     }
68 }
69 /**
70  * Configure this type hierarchy that is based on a region.
71  */

72 private void createTypeHierarchyBasedOnRegion(HashMap JavaDoc allOpenablesInRegion, IProgressMonitor monitor) {
73     
74     try {
75         int size = allOpenablesInRegion.size();
76         if (monitor != null) monitor.beginTask("", size * 2/* 1 for build binding, 1 for connect hierarchy*/); //$NON-NLS-1$
77
this.infoToHandle = new HashMap JavaDoc(size);
78         Iterator JavaDoc javaProjects = allOpenablesInRegion.entrySet().iterator();
79         while (javaProjects.hasNext()) {
80             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) javaProjects.next();
81             JavaProject project = (JavaProject) entry.getKey();
82             ArrayList JavaDoc allOpenables = (ArrayList JavaDoc) entry.getValue();
83             Openable[] openables = new Openable[allOpenables.size()];
84             allOpenables.toArray(openables);
85     
86             try {
87                 // resolve
88
SearchableEnvironment searchableEnvironment = project.newSearchableNameEnvironment(this.hierarchy.workingCopies);
89                 this.nameLookup = searchableEnvironment.nameLookup;
90                 this.hierarchyResolver.resolve(openables, null, monitor);
91             } catch (JavaModelException e) {
92                 // project doesn't exit: ignore
93
}
94         }
95     } finally {
96         if (monitor != null) monitor.done();
97     }
98 }
99     
100     /**
101      * Returns all of the openables defined in the region of this type hierarchy.
102      * Returns a map from IJavaProject to ArrayList of Openable
103      */

104     private HashMap JavaDoc determineOpenablesInRegion(IProgressMonitor monitor) {
105
106         try {
107             HashMap JavaDoc allOpenables = new HashMap JavaDoc();
108             IJavaElement[] roots =
109                 ((RegionBasedTypeHierarchy) this.hierarchy).region.getElements();
110             int length = roots.length;
111             if (monitor != null) monitor.beginTask("", length); //$NON-NLS-1$
112
for (int i = 0; i <length; i++) {
113                 IJavaElement root = roots[i];
114                 IJavaProject javaProject = root.getJavaProject();
115                 ArrayList JavaDoc openables = (ArrayList JavaDoc) allOpenables.get(javaProject);
116                 if (openables == null) {
117                     openables = new ArrayList JavaDoc();
118                     allOpenables.put(javaProject, openables);
119                 }
120                 switch (root.getElementType()) {
121                     case IJavaElement.JAVA_PROJECT :
122                         injectAllOpenablesForJavaProject((IJavaProject) root, openables);
123                         break;
124                     case IJavaElement.PACKAGE_FRAGMENT_ROOT :
125                         injectAllOpenablesForPackageFragmentRoot((IPackageFragmentRoot) root, openables);
126                         break;
127                     case IJavaElement.PACKAGE_FRAGMENT :
128                         injectAllOpenablesForPackageFragment((IPackageFragment) root, openables);
129                         break;
130                     case IJavaElement.CLASS_FILE :
131                     case IJavaElement.COMPILATION_UNIT :
132                         openables.add(root);
133                         break;
134                     case IJavaElement.TYPE :
135                         IType type = (IType)root;
136                         if (type.isBinary()) {
137                             openables.add(type.getClassFile());
138                         } else {
139                             openables.add(type.getCompilationUnit());
140                         }
141                         break;
142                     default :
143                         break;
144                 }
145                 worked(monitor, 1);
146             }
147             return allOpenables;
148         } finally {
149             if (monitor != null) monitor.done();
150         }
151     }
152     
153     /**
154      * Adds all of the openables defined within this java project to the
155      * list.
156      */

157     private void injectAllOpenablesForJavaProject(
158         IJavaProject project,
159         ArrayList JavaDoc openables) {
160         try {
161             IPackageFragmentRoot[] devPathRoots =
162                 ((JavaProject) project).getPackageFragmentRoots();
163             if (devPathRoots == null) {
164                 return;
165             }
166             for (int j = 0; j < devPathRoots.length; j++) {
167                 IPackageFragmentRoot root = devPathRoots[j];
168                 injectAllOpenablesForPackageFragmentRoot(root, openables);
169             }
170         } catch (JavaModelException e) {
171             // ignore
172
}
173     }
174     
175     /**
176      * Adds all of the openables defined within this package fragment to the
177      * list.
178      */

179     private void injectAllOpenablesForPackageFragment(
180         IPackageFragment packFrag,
181         ArrayList JavaDoc openables) {
182             
183         try {
184             IPackageFragmentRoot root = (IPackageFragmentRoot) packFrag.getParent();
185             int kind = root.getKind();
186             if (kind != 0) {
187                 boolean isSourcePackageFragment = (kind == IPackageFragmentRoot.K_SOURCE);
188                 if (isSourcePackageFragment) {
189                     ICompilationUnit[] cus = packFrag.getCompilationUnits();
190                     for (int i = 0, length = cus.length; i < length; i++) {
191                         openables.add(cus[i]);
192                     }
193                 } else {
194                     IClassFile[] classFiles = packFrag.getClassFiles();
195                     for (int i = 0, length = classFiles.length; i < length; i++) {
196                         openables.add(classFiles[i]);
197                     }
198                 }
199             }
200         } catch (JavaModelException e) {
201             // ignore
202
}
203     }
204     
205     /**
206      * Adds all of the openables defined within this package fragment root to the
207      * list.
208      */

209     private void injectAllOpenablesForPackageFragmentRoot(
210         IPackageFragmentRoot root,
211         ArrayList JavaDoc openables) {
212         try {
213             IJavaElement[] packFrags = root.getChildren();
214             for (int k = 0; k < packFrags.length; k++) {
215                 IPackageFragment packFrag = (IPackageFragment) packFrags[k];
216                 injectAllOpenablesForPackageFragment(packFrag, openables);
217             }
218         } catch (JavaModelException e) {
219             return;
220         }
221     }
222     
223 }
224
Popular Tags