1 11 package org.eclipse.core.internal.resources; 12 13 import java.util.*; 14 import org.eclipse.core.internal.localstore.IHistoryStore; 15 import org.eclipse.core.internal.utils.*; 16 import org.eclipse.core.internal.watson.*; 17 import org.eclipse.core.resources.*; 18 import org.eclipse.core.runtime.*; 19 import org.eclipse.core.runtime.jobs.ISchedulingRule; 20 import org.eclipse.osgi.util.NLS; 21 22 public abstract class Container extends Resource implements IContainer { 23 protected Container(IPath path, Workspace container) { 24 super(path, container); 25 } 26 27 31 public void convertToPhantom() throws CoreException { 32 if (isPhantom()) 33 return; 34 super.convertToPhantom(); 35 IResource[] members = members(IContainer.INCLUDE_PHANTOMS | IContainer.INCLUDE_TEAM_PRIVATE_MEMBERS); 36 for (int i = 0; i < members.length; i++) 37 ((Resource) members[i]).convertToPhantom(); 38 } 39 40 43 public boolean exists(IPath childPath) { 44 return workspace.getResourceInfo(getFullPath().append(childPath), false, false) != null; 45 } 46 47 50 public IResource findMember(String name) { 51 return findMember(name, false); 52 } 53 54 57 public IResource findMember(String name, boolean phantom) { 58 IPath childPath = getFullPath().append(name); 59 ResourceInfo info = workspace.getResourceInfo(childPath, phantom, false); 60 return info == null ? null : workspace.newResource(childPath, info.getType()); 61 } 62 63 66 public IResource findMember(IPath childPath) { 67 return findMember(childPath, false); 68 } 69 70 73 public IResource findMember(IPath childPath, boolean phantom) { 74 childPath = getFullPath().append(childPath); 75 ResourceInfo info = workspace.getResourceInfo(childPath, phantom, false); 76 return (info == null) ? null : workspace.newResource(childPath, info.getType()); 77 } 78 79 protected void fixupAfterMoveSource() throws CoreException { 80 super.fixupAfterMoveSource(); 81 if (!synchronizing(getResourceInfo(true, false))) 82 return; 83 IResource[] members = members(IContainer.INCLUDE_PHANTOMS | IContainer.INCLUDE_TEAM_PRIVATE_MEMBERS); 84 for (int i = 0; i < members.length; i++) 85 ((Resource) members[i]).fixupAfterMoveSource(); 86 } 87 88 protected IResource[] getChildren(int memberFlags) { 89 IPath[] children = null; 90 try { 91 children = workspace.tree.getChildren(path); 92 } catch (IllegalArgumentException e) { 93 } 96 if (children == null || children.length == 0) 97 return ICoreConstants.EMPTY_RESOURCE_ARRAY; 98 Resource[] result = new Resource[children.length]; 99 int found = 0; 100 for (int i = 0; i < children.length; i++) { 101 ResourceInfo info = workspace.getResourceInfo(children[i], true, false); 102 if (info != null && isMember(info.getFlags(), memberFlags)) 103 result[found++] = workspace.newResource(children[i], info.getType()); 104 } 105 if (found == result.length) 106 return result; 107 Resource[] trimmedResult = new Resource[found]; 108 System.arraycopy(result, 0, trimmedResult, 0, found); 109 return trimmedResult; 110 } 111 112 115 public IFile getFile(String name) { 116 return (IFile) workspace.newResource(getFullPath().append(name), FILE); 117 } 118 119 122 public IFile getFile(IPath childPath) { 123 return (IFile) workspace.newResource(getFullPath().append(childPath), FILE); 124 } 125 126 129 public IFolder getFolder(String name) { 130 return (IFolder) workspace.newResource(getFullPath().append(name), FOLDER); 131 } 132 133 136 public IFolder getFolder(IPath childPath) { 137 return (IFolder) workspace.newResource(getFullPath().append(childPath), FOLDER); 138 } 139 140 143 public boolean isLocal(int flags, int depth) { 144 if (!super.isLocal(flags, depth)) 145 return false; 146 if (depth == DEPTH_ZERO) 147 return true; 148 if (depth == DEPTH_ONE) 149 depth = DEPTH_ZERO; 150 IResource[] children = getChildren(IResource.NONE); 153 for (int i = 0; i < children.length; i++) 154 if (!children[i].isLocal(depth)) 155 return false; 156 return true; 157 } 158 159 162 public IResource[] members() throws CoreException { 163 return members(IResource.NONE); 165 } 166 167 170 public IResource[] members(boolean phantom) throws CoreException { 171 return members(phantom ? INCLUDE_PHANTOMS : IResource.NONE); 173 } 174 175 178 public IResource[] members(int memberFlags) throws CoreException { 179 final boolean phantom = (memberFlags & INCLUDE_PHANTOMS) != 0; 180 ResourceInfo info = getResourceInfo(phantom, false); 181 checkAccessible(getFlags(info)); 182 if (info.isSet(ICoreConstants.M_CHILDREN_UNKNOWN)) 184 workspace.refreshManager.refresh(this); 185 return getChildren(memberFlags); 186 } 187 188 191 public String getDefaultCharset() throws CoreException { 192 return getDefaultCharset(true); 193 } 194 195 198 public IFile[] findDeletedMembersWithHistory(int depth, IProgressMonitor monitor) { 199 IHistoryStore historyStore = getLocalManager().getHistoryStore(); 200 IPath basePath = getFullPath(); 201 IWorkspaceRoot root = getWorkspace().getRoot(); 202 Set deletedFiles = new HashSet(); 203 204 if (depth == IResource.DEPTH_ZERO) { 205 if (historyStore.getStates(basePath, monitor).length > 0) { 207 IFile file = root.getFile(basePath); 208 if (!file.exists()) { 209 deletedFiles.add(file); 210 } 211 } 212 } else { 213 Set allFilePaths = historyStore.allFiles(basePath, depth, monitor); 214 for (Iterator it = allFilePaths.iterator(); it.hasNext();) { 216 IPath filePath = (IPath) it.next(); 217 IFile file = root.getFile(filePath); 218 if (!file.exists()) { 219 deletedFiles.add(file); 220 } 221 } 222 } 223 return (IFile[]) deletedFiles.toArray(new IFile[deletedFiles.size()]); 224 } 225 226 231 public void setDefaultCharset(String charset) throws CoreException { 232 ResourceInfo info = getResourceInfo(false, false); 233 checkAccessible(getFlags(info)); 234 workspace.getCharsetManager().setCharsetFor(getFullPath(), charset); 235 } 236 237 240 public void setDefaultCharset(String newCharset, IProgressMonitor monitor) throws CoreException { 241 monitor = Policy.monitorFor(monitor); 242 try { 243 String message = NLS.bind(Messages.resources_settingDefaultCharsetContainer, getFullPath()); 244 monitor.beginTask(message, Policy.totalWork); 245 final ISchedulingRule rule = workspace.getRuleFactory().charsetRule(this); 248 try { 249 workspace.prepareOperation(rule, monitor); 250 checkAccessible(getFlags(getResourceInfo(false, false))); 251 workspace.beginOperation(true); 252 workspace.getCharsetManager().setCharsetFor(getFullPath(), newCharset); 253 IElementContentVisitor visitor = new IElementContentVisitor() { 255 boolean visitedRoot = false; 256 257 public boolean visitElement(ElementTree tree, IPathRequestor requestor, Object elementContents) { 258 if (elementContents == null) 259 return false; 260 IPath nodePath = requestor.requestPath(); 261 if (!visitedRoot) { 265 visitedRoot = true; 266 ResourceInfo info = workspace.getResourceInfo(nodePath, false, true); 267 if (info == null) 268 return false; 269 info.incrementCharsetGenerationCount(); 270 return true; 271 } 272 if (workspace.getCharsetManager().getCharsetFor(nodePath, false) != null) 274 return false; 275 ResourceInfo info = workspace.getResourceInfo(nodePath, false, true); 276 if (info == null) 277 return false; 278 info.incrementCharsetGenerationCount(); 279 return true; 280 } 281 }; 282 try { 283 new ElementTreeIterator(workspace.getElementTree(), getFullPath()).iterate(visitor); 284 } catch (WrappedRuntimeException e) { 285 throw (CoreException) e.getTargetException(); 286 } 287 monitor.worked(Policy.opWork); 288 } catch (OperationCanceledException e) { 289 workspace.getWorkManager().operationCanceled(); 290 throw e; 291 } finally { 292 workspace.endOperation(rule, true, Policy.subMonitorFor(monitor, Policy.endOpWork)); 293 } 294 } finally { 295 monitor.done(); 296 } 297 } 298 } 299 | Popular Tags |