1 12 package org.eclipse.debug.internal.core.sourcelookup.containers; 13 14 import java.io.File ; 15 import java.io.IOException ; 16 import java.util.ArrayList ; 17 import java.util.List ; 18 19 import org.eclipse.core.resources.IContainer; 20 import org.eclipse.core.resources.IFile; 21 import org.eclipse.core.resources.IFolder; 22 import org.eclipse.core.resources.IResource; 23 import org.eclipse.core.resources.IWorkspaceRoot; 24 import org.eclipse.core.resources.ResourcesPlugin; 25 import org.eclipse.core.runtime.CoreException; 26 import org.eclipse.core.runtime.IPath; 27 import org.eclipse.core.runtime.Path; 28 import org.eclipse.debug.core.sourcelookup.ISourceContainer; 29 import org.eclipse.debug.core.sourcelookup.containers.CompositeSourceContainer; 30 import org.eclipse.debug.core.sourcelookup.containers.FolderSourceContainer; 31 32 44 public abstract class ContainerSourceContainer extends CompositeSourceContainer { 45 46 private IContainer fContainer = null; 47 private boolean fSubfolders = false; 48 49 private IPath fRootPath = null; 50 private String [] fRootSegments = null; 51 private File fRootFile = null; 52 private IWorkspaceRoot fRoot = null; 53 54 61 public ContainerSourceContainer(IContainer container, boolean subfolders) { 62 fContainer = container; 63 fSubfolders = subfolders; 64 fRootPath = fContainer.getLocation(); 65 if (fRootPath != null) { 66 fRootSegments = fRootPath.segments(); 67 fRootFile = fRootPath.toFile(); 68 fRoot = ResourcesPlugin.getWorkspace().getRoot(); 69 } 70 } 71 72 79 public IContainer getContainer() { 80 return fContainer; 81 } 82 83 86 public Object [] findSourceElements(String name) throws CoreException { 87 if (fRootPath == null) { 88 return EMPTY; 89 } 90 ArrayList sources = new ArrayList (); 91 92 if ( validateFile(name) ) { 98 IFile file = fContainer.getFile(new Path(name)); 99 if (file.exists()) { 100 sources.add(file); 101 } else { 102 File osFile = new File (fRootFile, name); 103 if (osFile.exists()) { 104 try { 105 Path canonicalPath = new Path(osFile.getCanonicalPath()); 110 String [] canonicalSegments = canonicalPath.segments(); 111 IPath workspacePath = new Path(""); workspacePath = workspacePath.setDevice(canonicalPath.getDevice()); 113 for (int i = 0; i < canonicalSegments.length; i++) { 114 String segment = canonicalSegments[i]; 115 if (i < fRootSegments.length) { 116 if (fRootSegments[i].equalsIgnoreCase(segment)) { 117 workspacePath = workspacePath.append(fRootSegments[i]); 118 } else { 119 workspacePath = workspacePath.append(segment); 120 } 121 } else { 122 workspacePath = workspacePath.append(segment); 123 } 124 } 125 IFile[] files = fRoot.findFilesForLocation(workspacePath); 126 if (isFindDuplicates() && files.length > 1) { 127 for (int i = 0; i < files.length; i++) { 128 sources.add(files[i]); 129 } 130 } else if (files.length > 0) { 131 sources.add(files[0]); 132 } 133 } catch (IOException e) { 134 } 135 } 136 } 137 } 138 139 if ((isFindDuplicates() && fSubfolders) || (sources.isEmpty() && fSubfolders)) { 141 ISourceContainer[] containers = getSourceContainers(); 142 for (int i=0; i < containers.length; i++) { 143 Object [] objects = containers[i].findSourceElements(name); 144 if (objects == null || objects.length == 0) { 145 continue; 146 } 147 if (isFindDuplicates()) { 148 for(int j=0; j < objects.length; j++) 149 sources.add(objects[j]); 150 } else { 151 sources.add(objects[0]); 152 break; 153 } 154 } 155 } 156 157 if(sources.isEmpty()) 158 return EMPTY; 159 return sources.toArray(); 160 } 161 162 165 public String getName() { 166 return getContainer().getName(); 167 } 168 169 172 public boolean equals(Object obj) { 173 if (obj != null && obj instanceof ContainerSourceContainer) { 174 ContainerSourceContainer loc = (ContainerSourceContainer) obj; 175 return loc.getContainer().equals(getContainer()); 176 } 177 return false; 178 } 179 180 183 public int hashCode() { 184 return getContainer().hashCode(); 185 } 186 187 190 public boolean isComposite() { 191 return fSubfolders; 192 } 193 194 197 protected ISourceContainer[] createSourceContainers() throws CoreException { 198 if(fSubfolders) { 199 IResource[] resources = getContainer().members(); 200 List list = new ArrayList (resources.length); 201 for (int i = 0; i < resources.length; i++) { 202 IResource resource = resources[i]; 203 if (resource.getType() == IResource.FOLDER) { 204 list.add(new FolderSourceContainer((IFolder)resource, fSubfolders)); 205 } 206 } 207 ISourceContainer[] containers = (ISourceContainer[]) list.toArray(new ISourceContainer[list.size()]); 208 for (int i = 0; i < containers.length; i++) { 209 ISourceContainer container = containers[i]; 210 container.init(getDirector()); 211 } 212 return containers; 213 } 214 return new ISourceContainer[0]; 215 } 216 217 220 private boolean validateFile(String name) { 221 IContainer container = getContainer(); 222 IPath path = container.getFullPath().append(name); 223 return ResourcesPlugin.getWorkspace().validatePath(path.toOSString(), IResource.FILE).isOK(); 224 } 225 226 } 227 | Popular Tags |