package com.yoursway.common.resources; import org.eclipse.core.resources.IContainer; import org.eclipse.core.resources.IFolder; import org.eclipse.core.resources.IResourceDelta; import org.eclipse.core.resources.IResourceStatus; import org.eclipse.core.runtime.Assert; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IPath; import org.eclipse.core.runtime.Path; public class ResourceUtils { /** * Returns the given subfolder of the given container, creating it if * necessary. The container must already exist. * * @param container * @param path * @return the given subfolder * @throws CoreException * if the creation failed. */ public static IFolder lookupOrCreateSubfolder(IContainer container, IPath path) throws CoreException { Assert.isNotNull(container); Assert.isNotNull(path); final IFolder controllersFolder; controllersFolder = container.getFolder(path); if (!controllersFolder.exists()) { if (path.segmentCount() > 1) lookupOrCreateSubfolder(container, path.removeLastSegments(1)); controllersFolder.create(true, true, null); } return controllersFolder; } /** * Checks if the given delta contains any changes affecting the given child * (or grandchild) of the resouce represented by the delta. * * @param delta * @param filePath * a path relative to the <code>delta</code> * @return <code>true</code> if the resource at the given path has * changed, or any of its parents were added or removed; * <code>false</code> otherwise. */ public static boolean changedInDelta(IResourceDelta delta, IPath filePath) { Assert.isNotNull(delta); Assert.isNotNull(filePath); final int segmentCount = filePath.segmentCount(); Assert.isTrue(segmentCount > 0); IResourceDelta currentDelta = delta; for (int i = 0; i < segmentCount; i++) { String segment = filePath.segment(i); currentDelta = currentDelta.findMember(new Path(segment)); if (currentDelta == null) return false; int kind = currentDelta.getKind(); if (kind == IResourceDelta.ADDED || kind == IResourceDelta.REMOVED) return true; } Assert.isTrue(currentDelta.getKind() == IResourceDelta.CHANGED); return true; } /** * Checks if the given delta contains any changes affecting the given child * (or grandchild) of the resouce represented by the delta. * * @param delta * @param filePath * a path relative to the <code>delta</code> * @return <code>true</code> if the resource at the given path has * changed, or any of its parents were added or removed; * <code>false</code> otherwise. */ public static void interpretDelta(IResourceDelta delta, IPath filePath, IConcreteResourceChangeVisitor visitor) { Assert.isNotNull(delta); Assert.isNotNull(filePath); Assert.isNotNull(visitor); final int segmentCount = filePath.segmentCount(); IResourceDelta currentDelta = delta; for (int i = 0; i < segmentCount; i++) { String segment = filePath.segment(i); currentDelta = currentDelta.findMember(new Path(segment)); if (currentDelta == null) return; switch(currentDelta.getKind()) { case IResourceDelta.ADDED: visitor.resourceMightHaveBeenAdded(); return; case IResourceDelta.REMOVED: visitor.resourceRemovedIfExisted(); return; } } Assert.isTrue(currentDelta.getKind() == IResourceDelta.CHANGED); visitor.resourceChanged(currentDelta); } public static boolean isNotFoundOrOutOfSync(CoreException e) { return isNotFound(e) || isOutOfSync(e); } public static boolean isNotFound(CoreException e) { return e.getStatus().getCode() == IResourceStatus.RESOURCE_NOT_FOUND; } public static boolean isOutOfSync(CoreException e) { return e.getStatus().getCode() == IResourceStatus.OUT_OF_SYNC_LOCAL; } }