KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ccvs > core > util > BuildCleanupListener


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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 package org.eclipse.team.internal.ccvs.core.util;
12
13  
14 import org.eclipse.core.resources.IContainer;
15 import org.eclipse.core.resources.IProject;
16 import org.eclipse.core.resources.IResource;
17 import org.eclipse.core.resources.IResourceChangeEvent;
18 import org.eclipse.core.resources.IResourceChangeListener;
19 import org.eclipse.core.resources.IResourceDelta;
20 import org.eclipse.core.resources.IResourceDeltaVisitor;
21 import org.eclipse.core.resources.ResourcesPlugin;
22 import org.eclipse.core.runtime.CoreException;
23 import org.eclipse.core.runtime.IPath;
24 import org.eclipse.core.runtime.IProgressMonitor;
25 import org.eclipse.team.core.RepositoryProvider;
26 import org.eclipse.team.core.TeamException;
27 import org.eclipse.team.internal.ccvs.core.*;
28 import org.eclipse.team.internal.ccvs.core.CVSException;
29 import org.eclipse.team.internal.ccvs.core.CVSProviderPlugin;
30 import org.eclipse.team.internal.ccvs.core.ICVSFolder;
31 import org.eclipse.team.internal.ccvs.core.ICVSRunnable;
32 import org.eclipse.team.internal.ccvs.core.Policy;
33 import org.eclipse.team.internal.ccvs.core.resources.CVSWorkspaceRoot;
34 import org.eclipse.team.internal.ccvs.core.resources.EclipseSynchronizer;
35
36 /**
37  * Cleanup any CVS folders that were copied by a builder. This will also clean up
38  * CVS folders that were copied by the user since the last auto-build.
39  */

40 public class BuildCleanupListener implements IResourceDeltaVisitor, IResourceChangeListener {
41     
42     public static IResource getResourceFor(IProject container, IResource destination, IPath originating) {
43         switch(destination.getType()) {
44             case IResource.FILE : return container.getFile(originating);
45             case IResource.FOLDER: return container.getFolder(originating);
46             case IResource.PROJECT: return ResourcesPlugin.getWorkspace().getRoot().getProject(originating.toString());
47         }
48         return destination;
49     }
50     
51     /**
52      * @see IResourceDeltaVisitor#visit(IResourceDelta)
53      */

54     public boolean visit(IResourceDelta delta) throws CoreException {
55         IResource resource = delta.getResource();
56         boolean movedFrom = (delta.getFlags() & IResourceDelta.MOVED_FROM) > 0;
57         switch (delta.getKind()) {
58             case IResourceDelta.ADDED :
59                 // make sure the added resource isn't a phantom
60
if (resource.exists()) {
61                     if (EclipseSynchronizer.getInstance().wasPhantom(resource)) {
62                         EclipseSynchronizer.getInstance().resourcesRecreated(new IResource[] { resource }, null);
63                     }
64                     if (resource.getType() == IResource.FOLDER) {
65                         if (resource.getName().equals(SyncFileWriter.CVS_DIRNAME)) {
66                             handleOrphanedSubtree(resource.getParent());
67                         } else {
68                             handleOrphanedSubtree((IContainer)resource);
69                         }
70                     }
71                 }
72                 break;
73             case IResourceDelta.CHANGED :
74                 // This state means there is a resource before and after but changes were made by deleting and moving.
75
// For files, we shouldn'd do anything.
76
// For folders, we should purge the CVS info
77
if (movedFrom && resource.getType() == IResource.FOLDER && resource.exists()) {
78                     // When folders are moved, purge the CVS folders
79
return ! handleOrphanedSubtree((IContainer)resource);
80                 }
81                 break;
82         }
83         return true;
84     }
85     
86     /*
87      * Determine if the container is an orphaned subtree.
88      * If it is, handle it and return true.
89      * Otherwise, return false
90      */

91     private boolean handleOrphanedSubtree(IContainer container) {
92         try {
93             if (CVSWorkspaceRoot.isOrphanedSubtree(container)) {
94                 ICVSFolder mFolder = CVSWorkspaceRoot.getCVSFolderFor(container);
95                 mFolder.unmanage(null);
96                 return true;
97             }
98         } catch (CVSException e) {
99             CVSProviderPlugin.log(e);
100         }
101         return false;
102     }
103     
104     public void resourceChanged(IResourceChangeEvent event) {
105         try {
106             IResourceDelta root = event.getDelta();
107             IResourceDelta[] projectDeltas = root.getAffectedChildren();
108             for (int i = 0; i < projectDeltas.length; i++) {
109                 final IResourceDelta delta = projectDeltas[i];
110                 IResource resource = delta.getResource();
111                 
112                 if (resource.getType() == IResource.PROJECT) {
113                     // If the project is not accessible, don't process it
114
if (!resource.isAccessible()) continue;
115                 }
116                 
117                 RepositoryProvider provider = RepositoryProvider.getProvider(resource.getProject(), CVSProviderPlugin.getTypeId());
118
119                 // Make sure that the project is a CVS folder.
120
ICVSFolder folder = CVSWorkspaceRoot.getCVSFolderFor(resource.getProject());
121                 if (provider != null) {
122                     try {
123                         if (! folder.isCVSFolder()) {
124                             RepositoryProvider.unmap(resource.getProject());
125                             provider = null;
126                         }
127                     } catch (TeamException e) {
128                         CVSProviderPlugin.log(e);
129                     }
130                 }
131                 
132                 // if a project is moved the originating project will not be associated with the CVS provider
133
// however listeners will probably still be interested in the move delta.
134
if ((delta.getFlags() & IResourceDelta.MOVED_TO) > 0) {
135                     IResource destination = getResourceFor(resource.getProject(), resource, delta.getMovedToPath());
136                     provider = RepositoryProvider.getProvider(destination.getProject());
137                 }
138                 
139                 if(provider!=null) {
140                     // Traverse the delta is a runnable so that files are only written at the end
141
folder.run(new ICVSRunnable() {
142                         public void run(IProgressMonitor monitor) throws CVSException {
143                             try {
144                                 delta.accept(BuildCleanupListener.this);
145                             } catch (CoreException e) {
146                                 Util.logError(CVSMessages.ResourceDeltaVisitor_visitError, e);
147                             }
148                         }
149                     }, Policy.monitorFor(null));
150                 }
151             }
152         } catch (CVSException e) {
153             Util.logError(CVSMessages.ResourceDeltaVisitor_visitError, e);
154         }
155     }
156
157 }
158
Popular Tags