1 11 package org.eclipse.team.internal.core.subscribers; 12 13 import org.eclipse.core.resources.*; 14 import org.eclipse.core.runtime.Assert; 15 import org.eclipse.team.core.subscribers.*; 16 17 23 public abstract class SubscriberResourceCollector implements IResourceChangeListener, ISubscriberChangeListener { 24 25 Subscriber subscriber; 26 27 31 public SubscriberResourceCollector(Subscriber subscriber) { 32 Assert.isNotNull(subscriber); 33 this.subscriber = subscriber; 34 ResourcesPlugin.getWorkspace().addResourceChangeListener(this, IResourceChangeEvent.POST_CHANGE); 35 subscriber.addListener(this); 36 } 37 38 43 public Subscriber getSubscriber() { 44 return subscriber; 45 } 46 47 50 public void dispose() { 51 getSubscriber().removeListener(this); 52 ResourcesPlugin.getWorkspace().removeResourceChangeListener(this); 53 } 54 55 58 public void subscriberResourceChanged(ISubscriberChangeEvent[] deltas) { 59 try { 60 beginInput(); 61 IResource[] roots = getRoots(); 62 for (int i = 0; i < deltas.length; i++) { 63 switch (deltas[i].getFlags()) { 64 case ISubscriberChangeEvent.SYNC_CHANGED : 65 if (isAllRootsIncluded() || isDescendantOfRoot(deltas[i].getResource(), roots)) { 66 change(deltas[i].getResource(), IResource.DEPTH_ZERO); 67 } 68 break; 69 case ISubscriberChangeEvent.ROOT_REMOVED : 70 remove(deltas[i].getResource()); 71 break; 72 case ISubscriberChangeEvent.ROOT_ADDED : 73 if (isAllRootsIncluded() || isDescendantOfRoot(deltas[i].getResource(), roots)) { 74 change(deltas[i].getResource(), IResource.DEPTH_INFINITE); 75 } 76 break; 77 } 78 } 79 } finally { 80 endInput(); 81 } 82 } 83 84 90 protected void beginInput() { 91 } 93 94 99 protected void endInput() { 100 } 102 103 104 107 public void resourceChanged(IResourceChangeEvent event) { 108 try { 109 beginInput(); 110 processDelta(event.getDelta(), getRoots()); 111 } finally { 112 endInput(); 113 } 114 } 115 116 122 protected void processDelta(IResourceDelta delta, IResource[] roots) { 123 IResource resource = delta.getResource(); 124 int kind = delta.getKind(); 125 126 if (resource.getType() == IResource.PROJECT) { 127 if (((kind & IResourceDelta.REMOVED) != 0) 129 || (delta.getFlags() & IResourceDelta.OPEN) != 0 && !((IProject) resource).isOpen() 130 || !isAncestorOfRoot(resource, roots)) { 131 if (hasMembers(resource)) { 133 remove(resource); 134 } 135 } 136 } 137 138 boolean visitChildren = false; 139 if (isDescendantOfRoot(resource, roots)) { 140 visitChildren = true; 141 if ((delta.getFlags() & IResourceDelta.TYPE) != 0) { 144 remove(resource); 145 change(resource, IResource.DEPTH_INFINITE); 146 } 147 148 int changeFlags = delta.getFlags(); 151 if ((changeFlags & (IResourceDelta.OPEN | IResourceDelta.CONTENT)) != 0) { 152 change(resource, IResource.DEPTH_ZERO); 153 } 154 155 if ((delta.getKind() & (IResourceDelta.REMOVED | IResourceDelta.ADDED)) != 0) { 157 change(resource, IResource.DEPTH_ZERO); 158 } 159 } 160 161 if (visitChildren || isAncestorOfRoot(resource, roots)) { 163 IResourceDelta[] affectedChildren = delta.getAffectedChildren(IResourceDelta.CHANGED | IResourceDelta.REMOVED | IResourceDelta.ADDED); 164 for (int i = 0; i < affectedChildren.length; i++) { 165 processDelta(affectedChildren[i], roots); 166 } 167 } 168 } 169 170 176 protected IResource[] getRoots() { 177 return getSubscriber().roots(); 178 } 179 180 188 protected abstract boolean hasMembers(IResource resource); 189 190 197 protected abstract void remove(IResource resource); 198 199 204 protected abstract void change(IResource resource, int depth); 205 206 211 protected boolean isAllRootsIncluded() { 212 return true; 213 } 214 215 private boolean isAncestorOfRoot(IResource parent, IResource[] roots) { 216 if (parent.getType() == IResource.ROOT) return true; 218 for (int i = 0; i < roots.length; i++) { 219 IResource resource = roots[i]; 220 if (parent.getFullPath().isPrefixOf(resource.getFullPath())) { 221 return true; 222 } 223 } 224 return false; 225 } 226 227 private boolean isDescendantOfRoot(IResource resource, IResource[] roots) { 228 for (int i = 0; i < roots.length; i++) { 229 IResource root = roots[i]; 230 if (root.getFullPath().isPrefixOf(resource.getFullPath())) { 231 return true; 232 } 233 } 234 return false; 235 } 236 } 237 | Popular Tags |