1 11 package org.eclipse.jdt.internal.corext.util; 12 13 import java.util.ArrayList ; 14 import java.util.HashSet ; 15 import java.util.List ; 16 import java.util.Set ; 17 18 import org.eclipse.core.resources.mapping.RemoteResourceMappingContext; 19 import org.eclipse.core.resources.mapping.ResourceMapping; 20 import org.eclipse.core.resources.mapping.ResourceMappingContext; 21 import org.eclipse.core.resources.mapping.ResourceTraversal; 22 23 import org.eclipse.core.runtime.Assert; 24 import org.eclipse.core.runtime.CoreException; 25 import org.eclipse.core.runtime.IProgressMonitor; 26 import org.eclipse.core.runtime.NullProgressMonitor; 27 28 import org.eclipse.core.resources.IContainer; 29 import org.eclipse.core.resources.IFile; 30 import org.eclipse.core.resources.IProject; 31 import org.eclipse.core.resources.IResource; 32 import org.eclipse.core.resources.IResourceVisitor; 33 34 import org.eclipse.jdt.core.IClassFile; 35 import org.eclipse.jdt.core.ICompilationUnit; 36 import org.eclipse.jdt.core.IJavaElement; 37 import org.eclipse.jdt.core.IJavaModel; 38 import org.eclipse.jdt.core.IJavaProject; 39 import org.eclipse.jdt.core.IPackageFragment; 40 import org.eclipse.jdt.core.IPackageFragmentRoot; 41 import org.eclipse.jdt.core.IType; 42 import org.eclipse.jdt.core.JavaModelException; 43 44 45 import org.eclipse.jdt.internal.ui.JavaPlugin; 46 import org.eclipse.jdt.internal.ui.browsing.LogicalPackage; 47 import org.eclipse.jdt.internal.ui.model.JavaModelProvider; 48 49 56 public abstract class JavaElementResourceMapping extends ResourceMapping { 57 58 protected JavaElementResourceMapping() { 59 } 60 61 public IJavaElement getJavaElement() { 62 Object o= getModelObject(); 63 if (o instanceof IJavaElement) 64 return (IJavaElement)o; 65 return null; 66 } 67 68 public boolean equals(Object obj) { 69 if (!(obj instanceof JavaElementResourceMapping)) 70 return false; 71 return getJavaElement().equals(((JavaElementResourceMapping)obj).getJavaElement()); 72 } 73 74 public int hashCode() { 75 IJavaElement javaElement= getJavaElement(); 76 if (javaElement == null) 77 return super.hashCode(); 78 79 return javaElement.hashCode(); 80 } 81 82 public String getModelProviderId() { 83 return JavaModelProvider.JAVA_MODEL_PROVIDER_ID; 84 } 85 86 public boolean contains(ResourceMapping mapping) { 87 if (mapping instanceof JavaElementResourceMapping) { 88 JavaElementResourceMapping javaMapping = (JavaElementResourceMapping) mapping; 89 IJavaElement element = getJavaElement(); 90 IJavaElement other = javaMapping.getJavaElement(); 91 if (other != null && element != null) 92 return element.getPath().isPrefixOf(other.getPath()); 93 } 94 return false; 95 } 96 97 99 private static final class JavaModelResourceMapping extends JavaElementResourceMapping { 100 private final IJavaModel fJavaModel; 101 private JavaModelResourceMapping(IJavaModel model) { 102 Assert.isNotNull(model); 103 fJavaModel= model; 104 } 105 public Object getModelObject() { 106 return fJavaModel; 107 } 108 public IProject[] getProjects() { 109 IJavaProject[] projects= null; 110 try { 111 projects= fJavaModel.getJavaProjects(); 112 } catch (JavaModelException e) { 113 JavaPlugin.log(e); 114 return new IProject[0]; 115 } 116 IProject[] result= new IProject[projects.length]; 117 for (int i= 0; i < projects.length; i++) { 118 result[i]= projects[i].getProject(); 119 } 120 return result; 121 } 122 public ResourceTraversal[] getTraversals(ResourceMappingContext context, IProgressMonitor monitor) throws CoreException { 123 IJavaProject[] projects= fJavaModel.getJavaProjects(); 124 ResourceTraversal[] result= new ResourceTraversal[projects.length]; 125 for (int i= 0; i < projects.length; i++) { 126 result[i]= new ResourceTraversal(new IResource[] {projects[i].getProject()}, IResource.DEPTH_INFINITE, 0); 127 } 128 return result; 129 } 130 } 131 132 private static final class JavaProjectResourceMapping extends JavaElementResourceMapping { 133 private final IJavaProject fProject; 134 private JavaProjectResourceMapping(IJavaProject project) { 135 Assert.isNotNull(project); 136 fProject= project; 137 } 138 public Object getModelObject() { 139 return fProject; 140 } 141 public IProject[] getProjects() { 142 return new IProject[] {fProject.getProject() }; 143 } 144 public ResourceTraversal[] getTraversals(ResourceMappingContext context, IProgressMonitor monitor) throws CoreException { 145 return new ResourceTraversal[] { 146 new ResourceTraversal(new IResource[] {fProject.getProject()}, IResource.DEPTH_INFINITE, 0) 147 }; 148 } 149 } 150 151 private static final class PackageFragementRootResourceMapping extends JavaElementResourceMapping { 152 private final IPackageFragmentRoot fRoot; 153 private PackageFragementRootResourceMapping(IPackageFragmentRoot root) { 154 Assert.isNotNull(root); 155 fRoot= root; 156 } 157 public Object getModelObject() { 158 return fRoot; 159 } 160 public IProject[] getProjects() { 161 return new IProject[] {fRoot.getJavaProject().getProject() }; 162 } 163 public ResourceTraversal[] getTraversals(ResourceMappingContext context, IProgressMonitor monitor) throws CoreException { 164 return new ResourceTraversal[] { 165 new ResourceTraversal(new IResource[] {fRoot.getResource()}, IResource.DEPTH_INFINITE, 0) 166 }; 167 } 168 } 169 170 private static final class LocalPackageFragementTraversal extends ResourceTraversal { 171 private final IPackageFragment fPack; 172 public LocalPackageFragementTraversal(IPackageFragment pack) throws CoreException { 173 super(new IResource[] {pack.getResource()}, IResource.DEPTH_ONE, 0); 174 fPack= pack; 175 } 176 public void accept(IResourceVisitor visitor) throws CoreException { 177 IFile[] files= getPackageContent(fPack); 178 final IResource resource= fPack.getResource(); 179 if (resource != null) 180 visitor.visit(resource); 181 for (int i= 0; i < files.length; i++) { 182 visitor.visit(files[i]); 183 } 184 } 185 } 186 187 private static final class PackageFragmentResourceMapping extends JavaElementResourceMapping { 188 private final IPackageFragment fPack; 189 private PackageFragmentResourceMapping(IPackageFragment pack) { 190 Assert.isNotNull(pack); 191 fPack= pack; 192 } 193 public Object getModelObject() { 194 return fPack; 195 } 196 public IProject[] getProjects() { 197 return new IProject[] { fPack.getJavaProject().getProject() }; 198 } 199 public ResourceTraversal[] getTraversals(ResourceMappingContext context, IProgressMonitor monitor) throws CoreException { 200 if (context instanceof RemoteResourceMappingContext) { 201 return new ResourceTraversal[] { 202 new ResourceTraversal(new IResource[] {fPack.getResource()}, IResource.DEPTH_ONE, 0) 203 }; 204 } else { 205 return new ResourceTraversal[] { new LocalPackageFragementTraversal(fPack) }; 206 } 207 } 208 public void accept(ResourceMappingContext context, IResourceVisitor visitor, IProgressMonitor monitor) throws CoreException { 209 if (context instanceof RemoteResourceMappingContext) { 210 super.accept(context, visitor, monitor); 211 } else { 212 IFile[] files= getPackageContent(fPack); 214 if (monitor == null) 215 monitor= new NullProgressMonitor(); 216 monitor.beginTask("", files.length + 1); final IResource resource= fPack.getResource(); 218 if (resource != null) 219 visitor.visit(resource); 220 monitor.worked(1); 221 for (int i= 0; i < files.length; i++) { 222 visitor.visit(files[i]); 223 monitor.worked(1); 224 } 225 } 226 } 227 } 228 229 private static IFile[] getPackageContent(IPackageFragment pack) throws CoreException { 230 List result= new ArrayList (); 231 IContainer container= (IContainer)pack.getResource(); 232 if (container != null) { 233 IResource[] members= container.members(); 234 for (int m= 0; m < members.length; m++) { 235 IResource member= members[m]; 236 if (member instanceof IFile) { 237 IFile file= (IFile)member; 238 if ("class".equals(file.getFileExtension()) && file.isDerived()) continue; 240 result.add(member); 241 } 242 } 243 } 244 return (IFile[])result.toArray(new IFile[result.size()]); 245 } 246 247 248 private static final class CompilationUnitResourceMapping extends JavaElementResourceMapping { 249 private final ICompilationUnit fUnit; 250 private CompilationUnitResourceMapping(ICompilationUnit unit) { 251 Assert.isNotNull(unit); 252 fUnit= unit; 253 } 254 public Object getModelObject() { 255 return fUnit; 256 } 257 public IProject[] getProjects() { 258 return new IProject[] {fUnit.getJavaProject().getProject() }; 259 } 260 public ResourceTraversal[] getTraversals(ResourceMappingContext context, IProgressMonitor monitor) throws CoreException { 261 return new ResourceTraversal[] { 262 new ResourceTraversal(new IResource[] {fUnit.getResource()}, IResource.DEPTH_ONE, 0) 263 }; 264 } 265 } 266 267 private static final class ClassFileResourceMapping extends JavaElementResourceMapping { 268 private final IClassFile fClassFile; 269 private ClassFileResourceMapping(IClassFile classFile) { 270 fClassFile= classFile; 271 } 272 public Object getModelObject() { 273 return fClassFile; 274 } 275 public IProject[] getProjects() { 276 return new IProject[] { fClassFile.getJavaProject().getProject() }; 277 } 278 public ResourceTraversal[] getTraversals(ResourceMappingContext context, IProgressMonitor monitor) throws CoreException { 279 return new ResourceTraversal[] { 280 new ResourceTraversal(new IResource[] {fClassFile.getResource()}, IResource.DEPTH_ONE, 0) 281 }; 282 } 283 } 284 285 private static final class LogicalPackageResourceMapping extends ResourceMapping { 286 private final IPackageFragment[] fFragments; 287 private LogicalPackageResourceMapping(IPackageFragment[] fragments) { 288 fFragments= fragments; 289 } 290 public Object getModelObject() { 291 return fFragments; 292 } 293 public IProject[] getProjects() { 294 Set result= new HashSet (); 295 for (int i= 0; i < fFragments.length; i++) { 296 result.add(fFragments[i].getJavaProject().getProject()); 297 } 298 return (IProject[])result.toArray(new IProject[result.size()]); 299 } 300 public ResourceTraversal[] getTraversals(ResourceMappingContext context, IProgressMonitor monitor) throws CoreException { 301 List result= new ArrayList (); 302 if (context instanceof RemoteResourceMappingContext) { 303 for (int i= 0; i < fFragments.length; i++) { 304 result.add(new ResourceTraversal( 305 new IResource[] {fFragments[i].getResource()}, IResource.DEPTH_ONE, 0)); 306 } 307 } else { 308 for (int i= 0; i < fFragments.length; i++) { 309 result.add(new LocalPackageFragementTraversal(fFragments[i])); 310 } 311 } 312 return (ResourceTraversal[])result.toArray(new ResourceTraversal[result.size()]); 313 } 314 315 public String getModelProviderId() { 316 return JavaModelProvider.JAVA_MODEL_PROVIDER_ID; 317 } 318 } 319 320 public static ResourceMapping create(IJavaElement element) { 321 switch (element.getElementType()) { 322 case IJavaElement.TYPE: 323 return create((IType)element); 324 case IJavaElement.COMPILATION_UNIT: 325 return create((ICompilationUnit)element); 326 case IJavaElement.CLASS_FILE: 327 return create((IClassFile)element); 328 case IJavaElement.PACKAGE_FRAGMENT: 329 return create((IPackageFragment)element); 330 case IJavaElement.PACKAGE_FRAGMENT_ROOT: 331 return create((IPackageFragmentRoot)element); 332 case IJavaElement.JAVA_PROJECT: 333 return create((IJavaProject)element); 334 case IJavaElement.JAVA_MODEL: 335 return create((IJavaModel)element); 336 default: 337 return null; 338 } 339 340 } 341 342 public static ResourceMapping create(final IJavaModel model) { 343 return new JavaModelResourceMapping(model); 344 } 345 346 public static ResourceMapping create(final IJavaProject project) { 347 return new JavaProjectResourceMapping(project); 348 } 349 350 public static ResourceMapping create(final IPackageFragmentRoot root) { 351 if (root.isExternal()) 352 return null; 353 return new PackageFragementRootResourceMapping(root); 354 } 355 356 public static ResourceMapping create(final IPackageFragment pack) { 357 IPackageFragmentRoot root= (IPackageFragmentRoot)pack.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT); 359 if (!root.isArchive()) { 360 return new PackageFragmentResourceMapping(pack); 361 } 362 return null; 363 } 364 365 public static ResourceMapping create(ICompilationUnit unit) { 366 if (unit == null) 367 return null; 368 return new CompilationUnitResourceMapping(unit.getPrimary()); 369 } 370 371 public static ResourceMapping create(IClassFile classFile) { 372 IPackageFragmentRoot root= (IPackageFragmentRoot)classFile.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT); 374 if (!root.isArchive()) { 375 return new ClassFileResourceMapping(classFile); 376 } 377 return null; 378 } 379 380 public static ResourceMapping create(IType type) { 381 IJavaElement parent= type.getParent(); 383 if (parent instanceof ICompilationUnit) { 384 return create((ICompilationUnit)parent); 385 } 386 return null; 387 } 388 389 public static ResourceMapping create(LogicalPackage logicalPackage) { 390 IPackageFragment[] fragments= logicalPackage.getFragments(); 391 List toProcess= new ArrayList (fragments.length); 392 for (int i= 0; i < fragments.length; i++) { 393 IPackageFragmentRoot root= (IPackageFragmentRoot)fragments[i].getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT); 395 if (!root.isArchive()) { 396 toProcess.add(fragments[i]); 397 } 398 } 399 if (toProcess.size() == 0) 400 return null; 401 return new LogicalPackageResourceMapping((IPackageFragment[])toProcess.toArray(new IPackageFragment[toProcess.size()])); 402 } 403 } 404 | Popular Tags |