1 11 package org.eclipse.jdt.launching.sourcelookup.containers; 12 13 import java.util.ArrayList ; 14 import java.util.List ; 15 16 import org.eclipse.core.resources.IContainer; 17 import org.eclipse.core.resources.IResource; 18 import org.eclipse.core.resources.IWorkspaceRoot; 19 import org.eclipse.core.resources.ResourcesPlugin; 20 import org.eclipse.core.runtime.CoreException; 21 import org.eclipse.core.runtime.IPath; 22 import org.eclipse.debug.core.sourcelookup.ISourceContainer; 23 import org.eclipse.debug.core.sourcelookup.ISourceContainerType; 24 import org.eclipse.debug.core.sourcelookup.containers.CompositeSourceContainer; 25 import org.eclipse.debug.core.sourcelookup.containers.FolderSourceContainer; 26 import org.eclipse.debug.core.sourcelookup.containers.ProjectSourceContainer; 27 import org.eclipse.jdt.core.IClasspathEntry; 28 import org.eclipse.jdt.core.IJavaProject; 29 import org.eclipse.jdt.core.JavaCore; 30 import org.eclipse.jdt.internal.launching.LaunchingPlugin; 31 32 42 public class JavaProjectSourceContainer extends CompositeSourceContainer { 43 44 private IJavaProject fProject; 46 private ISourceContainer[] fSourceFolders; 48 private ISourceContainer[] fOthers; 50 51 private static String [] fgJavaExtensions = null; 52 53 56 static { 57 String [] extensions = JavaCore.getJavaLikeExtensions(); 58 fgJavaExtensions = new String [extensions.length]; 59 for (int i = 0; i < extensions.length; i++) { 60 String ext = extensions[i]; 61 fgJavaExtensions[i] = '.' + ext; 62 } 63 } 64 65 69 public static final String TYPE_ID = LaunchingPlugin.getUniqueIdentifier() + ".sourceContainer.javaProject"; 71 76 public JavaProjectSourceContainer(IJavaProject project) { 77 fProject = project; 78 } 79 82 public String getName() { 83 return fProject.getElementName(); 84 } 85 86 89 public ISourceContainerType getType() { 90 return getSourceContainerType(TYPE_ID); 91 } 92 93 98 public IJavaProject getJavaProject() { 99 return fProject; 100 } 101 104 protected ISourceContainer[] createSourceContainers() throws CoreException { 105 List containers = new ArrayList (); 106 IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot(); 107 if (fProject.getProject().isOpen()) { 108 IClasspathEntry[] entries = fProject.getRawClasspath(); 109 for (int i = 0; i < entries.length; i++) { 110 IClasspathEntry entry = entries[i]; 111 switch (entry.getEntryKind()) { 112 case IClasspathEntry.CPE_SOURCE: 113 IPath path = entry.getPath(); 114 IResource resource = root.findMember(path); 115 if (resource instanceof IContainer) { 116 containers.add(new FolderSourceContainer((IContainer)resource, false)); 117 } 118 break; 119 } 120 } 121 } 122 fSourceFolders = (ISourceContainer[]) containers.toArray(new ISourceContainer[containers.size()]); 124 ISourceContainer theProject = new ProjectSourceContainer(fProject.getProject(), false); 125 fOthers = new ISourceContainer[] {theProject}; 126 containers.add(theProject); 127 return (ISourceContainer[]) containers.toArray(new ISourceContainer[containers.size()]); 128 } 129 132 public boolean equals(Object obj) { 133 if (obj instanceof JavaProjectSourceContainer) { 134 return getJavaProject().equals(((JavaProjectSourceContainer)obj).getJavaProject()); 135 } 136 return super.equals(obj); 137 } 138 141 public int hashCode() { 142 return getJavaProject().hashCode(); 143 } 144 147 public Object [] findSourceElements(String name) throws CoreException { 148 getSourceContainers(); 150 151 if (isJavaLikeFileName(name)) { 152 Object [] objects = findSourceElements(name, fSourceFolders); 154 List filtered = null; 155 for (int i = 0; i < objects.length; i++) { 156 Object object = objects[i]; 157 if (object instanceof IResource) { 158 if (!getJavaProject().isOnClasspath((IResource)object)) { 159 if (filtered == null) { 160 filtered = new ArrayList (objects.length); 161 for (int j = 0; j < objects.length; j++) { 162 filtered.add(objects[j]); 163 } 164 } 165 filtered.remove(object); 166 } 167 } 168 } 169 if (filtered == null) { 170 return objects; 171 } 172 return filtered.toArray(); 173 } 174 return findSourceElements(name, fOthers); 176 } 177 public void dispose() { 178 fSourceFolders = null; 179 fOthers = null; 180 super.dispose(); 181 } 182 183 private boolean isJavaLikeFileName(String name) { 184 for (int i = 0; i < fgJavaExtensions.length; i++) { 185 String ext = fgJavaExtensions[i]; 186 if (name.endsWith(ext)) { 187 return true; 188 } 189 } 190 return false; 191 } 192 } 193 | Popular Tags |