1 11 package org.eclipse.team.core.mapping; 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.*; 21 22 30 public abstract class ChangeTracker { 31 32 private Map trackedProjects = new HashMap(); private boolean disposed; 34 private ChangeListener changeListener = new ChangeListener(); 35 36 private class ChangeListener implements IResourceChangeListener, IRepositoryProviderListener { 37 43 public void resourceChanged(IResourceChangeEvent event) { 44 if (disposed) return; 45 IResourceDelta delta = event.getDelta(); 46 IResourceDelta[] projectDeltas = delta.getAffectedChildren(IResourceDelta.ADDED | IResourceDelta.CHANGED | IResourceDelta.REMOVED); 47 for (int i = 0; i < projectDeltas.length; i++) { 48 IResourceDelta projectDelta = projectDeltas[i]; 49 IResource resource = projectDelta.getResource(); 50 if (resource.getType() == IResource.PROJECT) { 51 IProject project = (IProject)resource; 52 if (isProjectOfInterest(project)) { 53 if (isProjectTracked(project)) { 54 IResource[] resources = getProjectChanges(project, projectDelta); 55 if (resources.length > 0) 56 handleChanges(project, resources); 57 } else { 58 trackProject(project); 59 } 60 } else { 61 stopTrackingProject(project); 62 } 63 } 64 } 65 } 66 67 71 public void providerMapped(RepositoryProvider provider) { 72 if (disposed) return; 73 if (isProjectOfInterest(provider.getProject())) { 74 trackProject(provider.getProject()); 75 } 76 } 77 78 82 public void providerUnmapped(IProject project) { 83 if (disposed) return; 84 stopTrackingProject(project); 85 } 86 } 87 88 91 public ChangeTracker() { 92 super(); 93 } 94 95 99 public void start() { 100 ResourcesPlugin.getWorkspace().addResourceChangeListener(changeListener, IResourceChangeEvent.POST_CHANGE); 101 RepositoryProviderManager.getInstance().addListener(changeListener); 102 IProject[] allProjects = ResourcesPlugin.getWorkspace().getRoot().getProjects(); 103 for (int i = 0; i < allProjects.length; i++) { 104 IProject project = allProjects[i]; 105 if (isProjectOfInterest(project)) 106 trackProject(project); 107 } 108 } 109 110 114 public void dispose() { 115 disposed = true; 116 ResourcesPlugin.getWorkspace().removeResourceChangeListener(changeListener); 117 RepositoryProviderManager.getInstance().removeListener(changeListener); 118 } 119 120 private IResource[] getProjectChanges(IProject project, IResourceDelta projectDelta) { 121 final List result = new ArrayList(); 122 try { 123 projectDelta.accept(new IResourceDeltaVisitor() { 124 public boolean visit(IResourceDelta delta) throws CoreException { 125 if (isResourceOfInterest(delta.getResource()) & isChangeOfInterest(delta)) { 126 result.add(delta.getResource()); 127 } 128 return true; 129 } 130 }); 131 } catch (CoreException e) { 132 TeamPlugin.log(e); 133 } 134 return (IResource[]) result.toArray(new IResource[result.size()]); 135 } 136 137 142 protected boolean isChangeOfInterest(IResourceDelta delta) { 143 return (delta.getKind() & (IResourceDelta.ADDED | IResourceDelta.REMOVED | IResourceDelta.CHANGED)) > 0; 144 } 145 146 151 protected void stopTrackingProject(IProject project) { 152 trackedProjects.remove(project); 153 } 154 155 160 protected final boolean isProjectTracked(IProject project) { 161 return trackedProjects.containsKey(project); 162 } 163 164 175 protected boolean isProjectOfInterest(IProject project) { 176 return project.isAccessible(); 177 } 178 179 184 protected abstract boolean isResourceOfInterest(IResource resource); 185 186 191 protected abstract void handleChanges(IProject project, IResource[] resources); 192 193 199 protected abstract void handleProjectChange(IProject project); 200 201 207 protected final boolean trackProject(IProject project) { 208 if (RepositoryProvider.isShared(project)) { 209 try { 210 String currentId = project.getPersistentProperty(TeamPlugin.PROVIDER_PROP_KEY); 211 if (currentId != null) { 212 RepositoryProviderType type = RepositoryProviderType.getProviderType(currentId); 213 if (type != null) { 214 IChangeGroupingRequestor collector = getCollector(type); 215 if (collector != null) { 216 trackedProjects.put(project, collector); 217 projectTracked(project); 221 return true; 222 } 223 } 224 } 225 } catch (CoreException e) { 226 TeamPlugin.log(e); 227 } 228 } 229 return false; 230 } 231 232 private IChangeGroupingRequestor getCollector(RepositoryProviderType type) { 233 if (type instanceof IAdaptable) { 234 IAdaptable adaptable = (IAdaptable) type; 235 Object o = adaptable.getAdapter(IChangeGroupingRequestor.class); 236 if (o instanceof IChangeGroupingRequestor) { 237 return (IChangeGroupingRequestor) o; 238 } 239 } 240 return null; 241 } 242 243 248 protected void projectTracked(IProject project) { 249 handleProjectChange(project); 250 } 251 252 259 protected void ensureGrouped(IProject project, String name, IFile[] files) throws CoreException { 260 IChangeGroupingRequestor collector = getCollector(project); 261 if (collector != null) { 262 collector.ensureChangesGrouped(project, files, name); 263 } 264 } 265 266 private IChangeGroupingRequestor getCollector(IProject project) { 267 return (IChangeGroupingRequestor)trackedProjects.get(project); 268 } 269 270 277 protected boolean isModified(IFile file) throws CoreException { 278 IChangeGroupingRequestor collector = getCollector(file.getProject()); 279 if (collector != null) 280 return collector.isModified(file); 281 return false; 282 } 283 284 } 285 | Popular Tags |