1 11 17 package org.eclipse.team.internal.core; 18 19 import java.util.*; 20 21 import org.eclipse.core.resources.*; 22 import org.eclipse.core.runtime.*; 23 import org.eclipse.team.core.RepositoryProvider; 24 import org.eclipse.team.core.RepositoryProviderType; 25 26 30 public final class TeamResourceChangeListener implements IResourceChangeListener { 31 32 private static final Map metaFilePaths; 34 static { 35 metaFilePaths = new HashMap(); 36 String [] ids = RepositoryProvider.getAllProviderTypeIds(); 37 for (int i = 0; i < ids.length; i++) { 38 String id = ids[i]; 39 IPath[] paths = TeamPlugin.getMetaFilePaths(id); 40 if (paths != null) { 41 metaFilePaths.put(id, paths); 42 } 43 } 44 } 45 46 public void resourceChanged(IResourceChangeEvent event) { 47 IResourceDelta[] projectDeltas = event.getDelta().getAffectedChildren(); 48 for (int i = 0; i < projectDeltas.length; i++) { 49 IResourceDelta delta = projectDeltas[i]; 50 IResource resource = delta.getResource(); 51 IProject project = resource.getProject(); 52 if (!RepositoryProvider.isShared(project)) { 53 handleUnsharedProjectChanges(project, delta); 55 } else { 56 if (delta.getKind() != IResourceDelta.ADDED) continue; 59 if ((delta.getFlags() & IResourceDelta.MOVED_FROM) == 0) continue; 60 RepositoryProvider provider = RepositoryProvider.getProvider(project); 62 if (provider == null) continue; 63 if (provider.getProject().equals(project)) continue; 65 provider.setProject(project); 67 } 68 } 69 } 70 71 private void handleUnsharedProjectChanges(IProject project, IResourceDelta delta) { 72 String repositoryId = null; 73 Set metaFileContainers = new HashSet(); 74 Set badIds = new HashSet(); 75 IFile[] files = getAddedFiles(delta); 76 for (int i = 0; i < files.length; i++) { 77 IFile file = files[i]; 78 String typeId = getMetaFileType(file); 79 if (typeId != null) { 80 if (repositoryId == null) { 82 repositoryId = typeId; 83 } else if (!repositoryId.equals(typeId) && !badIds.contains(typeId)) { 84 TeamPlugin.log(IStatus.WARNING, "Meta files for two repository types (" + repositoryId + " and " + typeId + " was found in project " + project.getName() + ".", null); badIds.add(typeId); 86 } 87 if (typeId.equals(repositoryId)) { 88 IContainer container = getContainer(typeId, file); 89 metaFileContainers.add(container); 90 } 91 } 92 } 93 if (repositoryId != null) { 94 RepositoryProviderType type = RepositoryProviderType.getProviderType(repositoryId); 95 type.metaFilesDetected(project, (IContainer[]) metaFileContainers.toArray(new IContainer[metaFileContainers.size()])); 96 } 97 } 98 99 private IContainer getContainer(String typeId, IFile file) { 100 IPath[] paths = (IPath[])metaFilePaths.get(typeId); 101 IPath foundPath = null; 102 IPath projectRelativePath = file.getProjectRelativePath(); 103 for (int i = 0; i < paths.length; i++) { 104 IPath path = paths[i]; 105 if (isSuffix(projectRelativePath, path)) { 106 foundPath = path; 107 } 108 } 109 IResource resource = file; 110 if (foundPath != null) { 111 for (int i = 0; i < foundPath.segmentCount(); i++) { 112 resource = resource.getParent(); 113 } 114 } 115 if (resource.getType() == IResource.FILE) { 116 return file.getParent(); 117 } 118 return (IContainer)resource; 119 } 120 121 private String getMetaFileType(IFile file) { 122 for (Iterator iter = metaFilePaths.keySet().iterator(); iter.hasNext();) { 123 String id = (String ) iter.next(); 124 IPath[] paths = (IPath[])metaFilePaths.get(id); 125 for (int i = 0; i < paths.length; i++) { 126 IPath path = paths[i]; 127 if (isSuffix(file.getProjectRelativePath(), path)) { 128 return id; 129 } 130 } 131 } 132 return null; 133 } 134 135 private boolean isSuffix(IPath path, IPath suffix) { 136 if (path.segmentCount() < suffix.segmentCount()) 137 return false; 138 for (int i = 0; i < suffix.segmentCount(); i++) { 139 if (!suffix.segment(i).equals(path.segment(path.segmentCount() - suffix.segmentCount() + i))) { 140 return false; 141 } 142 } 143 return true; 144 } 145 146 private IFile[] getAddedFiles(IResourceDelta delta) { 147 final List result = new ArrayList(); 148 try { 149 delta.accept(new IResourceDeltaVisitor() { 150 public boolean visit(IResourceDelta delta) throws CoreException { 151 if ((delta.getKind() & IResourceDelta.ADDED) != 0 152 && delta.getResource().getType() == IResource.FILE) { 153 result.add(delta.getResource()); 154 } 155 return true; 156 } 157 }); 158 } catch (CoreException e) { 159 TeamPlugin.log(IStatus.ERROR, "An error occurred while scanning for meta-file changes", e); } 161 return (IFile[]) result.toArray(new IFile[result.size()]); 162 } 163 } | Popular Tags |