1 12 package org.eclipse.debug.core.sourcelookup.containers; 13 14 import java.net.URI ; 15 import java.util.ArrayList ; 16 import java.util.List ; 17 18 import org.eclipse.core.filesystem.EFS; 19 import org.eclipse.core.filesystem.IFileStore; 20 import org.eclipse.core.resources.IContainer; 21 import org.eclipse.core.resources.IFile; 22 import org.eclipse.core.resources.IFolder; 23 import org.eclipse.core.resources.IResource; 24 import org.eclipse.core.resources.IWorkspaceRoot; 25 import org.eclipse.core.resources.ResourcesPlugin; 26 import org.eclipse.core.runtime.CoreException; 27 import org.eclipse.core.runtime.IPath; 28 import org.eclipse.core.runtime.Path; 29 import org.eclipse.debug.core.sourcelookup.ISourceContainer; 30 31 45 public abstract class ContainerSourceContainer extends CompositeSourceContainer { 46 47 private IContainer fContainer = null; 48 private boolean fSubfolders = false; 49 50 private URI fRootURI = null; 51 private IFileStore fRootFile = null; 52 private IWorkspaceRoot fRoot = null; 53 54 61 public ContainerSourceContainer(IContainer container, boolean subfolders) { 62 fContainer = container; 63 fSubfolders = subfolders; 64 fRootURI = fContainer.getLocationURI(); 65 if (fRootURI != null) { 66 try { 67 fRootFile = EFS.getStore(fRootURI); 68 } catch (CoreException e) { 69 } 70 fRoot = ResourcesPlugin.getWorkspace().getRoot(); 71 } 72 } 73 74 81 public IContainer getContainer() { 82 return fContainer; 83 } 84 85 88 public Object [] findSourceElements(String name) throws CoreException { 89 ArrayList sources = new ArrayList (); 90 91 if ( validateFile(name) ) { 97 IFile file = fContainer.getFile(new Path(name)); 98 if (file.exists()) { 99 sources.add(file); 100 } else { 101 if (fRootURI == null) { 103 return EMPTY; 104 } 105 IFileStore target = fRootFile.getChild(new Path(name)); 107 if (target.fetchInfo().exists()) { 108 IFile[] files = fRoot.findFilesForLocationURI(target.toURI()); 111 if (isFindDuplicates() && files.length > 1) { 112 for (int i = 0; i < files.length; i++) { 113 sources.add(files[i]); 114 } 115 } else if (files.length > 0) { 116 sources.add(files[0]); 117 } 118 } 119 } 120 } 121 122 if ((isFindDuplicates() && fSubfolders) || (sources.isEmpty() && fSubfolders)) { 124 ISourceContainer[] containers = getSourceContainers(); 125 for (int i=0; i < containers.length; i++) { 126 Object [] objects = containers[i].findSourceElements(name); 127 if (objects == null || objects.length == 0) { 128 continue; 129 } 130 if (isFindDuplicates()) { 131 for(int j=0; j < objects.length; j++) 132 sources.add(objects[j]); 133 } else { 134 sources.add(objects[0]); 135 break; 136 } 137 } 138 } 139 140 if(sources.isEmpty()) 141 return EMPTY; 142 return sources.toArray(); 143 } 144 145 148 public String getName() { 149 return getContainer().getName(); 150 } 151 152 155 public boolean equals(Object obj) { 156 if (obj != null && obj instanceof ContainerSourceContainer) { 157 ContainerSourceContainer loc = (ContainerSourceContainer) obj; 158 return loc.getContainer().equals(getContainer()); 159 } 160 return false; 161 } 162 163 166 public int hashCode() { 167 return getContainer().hashCode(); 168 } 169 170 173 public boolean isComposite() { 174 return fSubfolders; 175 } 176 177 180 protected ISourceContainer[] createSourceContainers() throws CoreException { 181 if(fSubfolders) { 182 IResource[] resources = getContainer().members(); 183 List list = new ArrayList (resources.length); 184 for (int i = 0; i < resources.length; i++) { 185 IResource resource = resources[i]; 186 if (resource.getType() == IResource.FOLDER) { 187 list.add(new FolderSourceContainer((IFolder)resource, fSubfolders)); 188 } 189 } 190 ISourceContainer[] containers = (ISourceContainer[]) list.toArray(new ISourceContainer[list.size()]); 191 for (int i = 0; i < containers.length; i++) { 192 ISourceContainer container = containers[i]; 193 container.init(getDirector()); 194 } 195 return containers; 196 } 197 return new ISourceContainer[0]; 198 } 199 200 205 private boolean validateFile(String name) { 206 IContainer container = getContainer(); 207 IPath path = container.getFullPath().append(name); 208 return ResourcesPlugin.getWorkspace().validatePath(path.toOSString(), IResource.FILE).isOK(); 209 } 210 211 } 212 | Popular Tags |