1 11 package org.eclipse.team.internal.core; 12 13 import java.util.*; 14 15 import org.eclipse.core.resources.*; 16 import org.eclipse.core.runtime.CoreException; 17 import org.eclipse.core.runtime.IAdaptable; 18 import org.eclipse.team.core.RepositoryProvider; 19 import org.eclipse.team.core.RepositoryProviderType; 20 import org.eclipse.team.internal.core.subscribers.ActiveChangeSet; 21 import org.eclipse.team.internal.core.subscribers.ActiveChangeSetManager; 22 23 29 public abstract class ChangeTracker implements IResourceChangeListener, IRepositoryProviderListener { 30 31 private Map trackedProjects = new HashMap(); private boolean disposed; 33 34 37 public ChangeTracker() { 38 super(); 39 } 40 41 45 public void start() { 46 ResourcesPlugin.getWorkspace().addResourceChangeListener(this, IResourceChangeEvent.POST_CHANGE); 47 RepositoryProviderManager.getInstance().addListener(this); 48 IProject[] allProjects = ResourcesPlugin.getWorkspace().getRoot().getProjects(); 49 for (int i = 0; i < allProjects.length; i++) { 50 IProject project = allProjects[i]; 51 if (isProjectOfInterest(project)) 52 trackProject(project); 53 } 54 } 55 56 60 public void dispose() { 61 disposed = true; 62 ResourcesPlugin.getWorkspace().removeResourceChangeListener(this); 63 RepositoryProviderManager.getInstance().removeListener(this); 64 } 65 66 72 public void resourceChanged(IResourceChangeEvent event) { 73 if (disposed) return; 74 IResourceDelta delta = event.getDelta(); 75 IResourceDelta[] projectDeltas = delta.getAffectedChildren(IResourceDelta.ADDED | IResourceDelta.CHANGED | IResourceDelta.REMOVED); 76 for (int i = 0; i < projectDeltas.length; i++) { 77 IResourceDelta projectDelta = projectDeltas[i]; 78 IResource resource = projectDelta.getResource(); 79 if (resource.getType() == IResource.PROJECT) { 80 IProject project = (IProject)resource; 81 if (isProjectOfInterest(project)) { 82 if (isProjectTracked(project)) { 83 IResource[] resources = getProjectChanges(project, projectDelta); 84 if (resources.length > 0) 85 handleChanges(project, resources); 86 } else { 87 trackProject(project); 88 } 89 } else { 90 stopTrackingProject(project); 91 } 92 } 93 } 94 } 95 96 private IResource[] getProjectChanges(IProject project, IResourceDelta projectDelta) { 97 final List result = new ArrayList(); 98 try { 99 projectDelta.accept(new IResourceDeltaVisitor() { 100 public boolean visit(IResourceDelta delta) throws CoreException { 101 if (isResourceOfInterest(delta.getResource()) & isChangeOfInterest(delta)) { 102 result.add(delta.getResource()); 103 } 104 return true; 105 } 106 }); 107 } catch (CoreException e) { 108 TeamPlugin.log(e); 109 } 110 return (IResource[]) result.toArray(new IResource[result.size()]); 111 } 112 113 118 protected boolean isChangeOfInterest(IResourceDelta delta) { 119 return (delta.getKind() & (IResourceDelta.ADDED | IResourceDelta.REMOVED | IResourceDelta.CHANGED)) > 0; 120 } 121 122 127 protected void stopTrackingProject(IProject project) { 128 trackedProjects.remove(project); 129 } 130 131 136 protected final boolean isProjectTracked(IProject project) { 137 return trackedProjects.containsKey(project); 138 } 139 140 151 protected boolean isProjectOfInterest(IProject project) { 152 return project.isAccessible(); 153 } 154 155 159 public void providerMapped(RepositoryProvider provider) { 160 if (disposed) return; 161 if (isProjectOfInterest(provider.getProject())) { 162 trackProject(provider.getProject()); 163 } 164 } 165 166 170 public void providerUnmapped(IProject project) { 171 if (disposed) return; 172 stopTrackingProject(project); 173 } 174 175 180 protected abstract boolean isResourceOfInterest(IResource resource); 181 182 187 protected abstract void handleChanges(IProject project, IResource[] resources); 188 189 195 protected abstract void handleProjectChange(IProject project); 196 197 203 protected final boolean trackProject(IProject project) { 204 if (RepositoryProvider.isShared(project)) { 205 try { 206 String currentId = project.getPersistentProperty(TeamPlugin.PROVIDER_PROP_KEY); 207 if (currentId != null) { 208 RepositoryProviderType type = RepositoryProviderType.getProviderType(currentId); 209 if (type != null) { 210 ActiveChangeSetManager collector = getCollector(type); 211 if (collector != null) { 212 trackedProjects.put(project, collector); 213 projectTracked(project); 217 return true; 218 } 219 } 220 } 221 } catch (CoreException e) { 222 TeamPlugin.log(e); 223 } 224 } 225 return false; 226 } 227 228 private ActiveChangeSetManager getCollector(RepositoryProviderType type) { 229 if (type instanceof IAdaptable) { 230 IAdaptable adaptable = (IAdaptable) type; 231 Object o = adaptable.getAdapter(ActiveChangeSetManager.class); 232 if (o instanceof ActiveChangeSetManager) { 233 return (ActiveChangeSetManager) o; 234 } 235 } 236 return null; 237 } 238 239 244 protected void projectTracked(IProject project) { 245 handleProjectChange(project); 246 } 247 248 255 protected void groupAsSet(IProject project, String name, IFile[] files) throws CoreException { 256 ActiveChangeSetManager collector = getCollector(project); 257 if (collector != null) { 258 ActiveChangeSet set = collector.getSet(name); 259 if (set == null) { 260 set = collector.createSet(name, files); 261 set.setUserCreated(false); 262 collector.add(set); 263 } else { 264 set.setUserCreated(false); 265 set.add(files); 266 } 267 } 268 } 269 270 private ActiveChangeSetManager getCollector(IProject project) { 271 return (ActiveChangeSetManager)trackedProjects.get(project); 272 } 273 274 281 protected boolean isModified(IFile file) throws CoreException { 282 ActiveChangeSetManager collector = getCollector(file.getProject()); 283 if (collector != null) 284 return collector.isModified(file); 285 return false; 286 } 287 288 } 289 | Popular Tags |