KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > core > TeamResourceChangeListener


1 /*******************************************************************************
2  * Copyright (c) 2004, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 /*
12  * Created on Jan 17, 2005
13  *
14  * TODO To change the template for this generated file go to
15  * Window - Preferences - Java - Code Style - Code Templates
16  */

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 /**
27  * Change listener that detects and handles project moves and
28  * meta-file creation.
29  */

30 public final class TeamResourceChangeListener implements IResourceChangeListener {
31     
32     private static final Map metaFilePaths; // Map of String (repository id) -> IPath[]
33

34     static {
35         metaFilePaths = new HashMap();
36         String JavaDoc[] ids = RepositoryProvider.getAllProviderTypeIds();
37         for (int i = 0; i < ids.length; i++) {
38             String JavaDoc 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                 // Look for meta-file creation in unshared projects
54
handleUnsharedProjectChanges(project, delta);
55             } else {
56                 // Handle project moves
57
// Only consider project additions that are moves
58
if (delta.getKind() != IResourceDelta.ADDED) continue;
59                 if ((delta.getFlags() & IResourceDelta.MOVED_FROM) == 0) continue;
60                 // Only consider projects that have a provider
61
RepositoryProvider provider = RepositoryProvider.getProvider(project);
62                 if (provider == null) continue;
63                 // Only consider providers whose project is not mapped properly already
64
if (provider.getProject().equals(project)) continue;
65                 // Tell the provider about it's new project
66
provider.setProject(project);
67             }
68         }
69     }
70
71     private void handleUnsharedProjectChanges(IProject project, IResourceDelta delta) {
72         String JavaDoc 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 JavaDoc typeId = getMetaFileType(file);
79             if (typeId != null) {
80                 // The file path matches the path for the given type id
81
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); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
85
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 JavaDoc 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 JavaDoc getMetaFileType(IFile file) {
122         for (Iterator iter = metaFilePaths.keySet().iterator(); iter.hasNext();) {
123             String JavaDoc id = (String JavaDoc) 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); //$NON-NLS-1$
160
}
161         return (IFile[]) result.toArray(new IFile[result.size()]);
162     }
163 }
Popular Tags