1 11 package org.eclipse.jdt.internal.ui.model; 12 13 import org.eclipse.core.runtime.CoreException; 14 import org.eclipse.core.runtime.IAdaptable; 15 import org.eclipse.core.runtime.IProgressMonitor; 16 import org.eclipse.core.runtime.Platform; 17 18 import org.eclipse.core.resources.IResource; 19 import org.eclipse.core.resources.mapping.ModelProvider; 20 import org.eclipse.core.resources.mapping.ResourceMapping; 21 import org.eclipse.core.resources.mapping.ResourceMappingContext; 22 23 import org.eclipse.jdt.core.IJavaElement; 24 import org.eclipse.jdt.core.JavaCore; 25 26 import org.eclipse.jdt.internal.corext.util.JavaElementResourceMapping; 27 28 33 public final class JavaModelProvider extends ModelProvider { 34 35 36 public static final String JAVA_MODEL_PROVIDER_ID= "org.eclipse.jdt.ui.modelProvider"; 38 45 public static IResource getResource(final Object element) { 46 IResource resource= null; 47 if (element instanceof IJavaElement) { 48 resource= ((IJavaElement) element).getResource(); 49 } else if (element instanceof IResource) { 50 resource= (IResource) element; 51 } else if (element instanceof IAdaptable) { 52 final IAdaptable adaptable= (IAdaptable) element; 53 final Object adapted= adaptable.getAdapter(IResource.class); 54 if (adapted instanceof IResource) 55 resource= (IResource) adapted; 56 } else { 57 final Object adapted= Platform.getAdapterManager().getAdapter(element, IResource.class); 58 if (adapted instanceof IResource) 59 resource= (IResource) adapted; 60 } 61 return resource; 62 } 63 64 67 public JavaModelProvider() { 68 } 70 71 74 public ResourceMapping[] getMappings(final IResource resource, final ResourceMappingContext context, final IProgressMonitor monitor) throws CoreException { 75 final IJavaElement element= JavaCore.create(resource); 76 if (element != null) 77 return new ResourceMapping[] { JavaElementResourceMapping.create(element)}; 78 final Object adapted= resource.getAdapter(ResourceMapping.class); 79 if (adapted instanceof ResourceMapping) 80 return new ResourceMapping[] { ((ResourceMapping) adapted)}; 81 return new ResourceMapping[] { new JavaResourceMapping(resource)}; 82 } 83 } 84 | Popular Tags |