KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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  * Matt McCutchen <hashproduct+eclipse@gmail.com> - Bug 179174 CVS client sets timestamps back when replacing
11  *******************************************************************************/

12 package org.eclipse.team.internal.ccvs.core.util;
13
14 import java.util.*;
15
16 import org.eclipse.core.resources.*;
17 import org.eclipse.core.runtime.*;
18 import org.eclipse.osgi.util.NLS;
19 import org.eclipse.team.internal.ccvs.core.*;
20 import org.eclipse.team.internal.ccvs.core.client.ConsoleListeners;
21 import org.eclipse.team.internal.ccvs.core.client.Session;
22 import org.eclipse.team.internal.ccvs.core.resources.CVSWorkspaceRoot;
23 import org.eclipse.team.internal.ccvs.core.syncinfo.ResourceSyncInfo;
24
25 /**
26  * This class is used to prepare a local CVS workspace for replacement by
27  * the corresponding remote resources. More specifically, this class will
28  * unmanage added and deleted resources so that, after the operation, the
29  * resources in the local workspace will either correspond to the remote
30  * counterparts or be unmanaged.
31  */

32 public class PrepareForReplaceVisitor implements ICVSResourceVisitor {
33
34     private IProgressMonitor monitor;
35     private int depth;
36     private CVSTag tag;
37     private Set/*<ICVSFile>*/ deletedFiles;
38     private Session session;
39
40     public PrepareForReplaceVisitor(Session session, CVSTag tag){
41         this.tag = tag;
42         this.session = session;
43     }
44     
45     /**
46      * @see ICVSResourceVisitor#visitFile(ICVSFile)
47      */

48     public void visitFile(ICVSFile file) throws CVSException {
49         byte[] syncBytes = file.getSyncBytes();
50         if (syncBytes == null) {
51             // Delete unmanaged files if the user wants them deleted
52
if (CVSProviderPlugin.getPlugin().isReplaceUnmanaged()) {
53                 file.delete();
54                 deletedFiles.add(file);
55             }
56         } else if (ResourceSyncInfo.isAddition(syncBytes)) {
57             file.delete();
58             deletedFiles.add(file);
59             file.unmanage(null);
60         } else if (ResourceSyncInfo.isDeletion(syncBytes)) {
61             // If deleted, null the sync info so the file will be refetched.
62
// If we are replacing with the "BASE" tag, the file will not be refetched,
63
// it is necessary to restore it from history (see bug 150158).
64
if (!shouldDeleteModifications()) {
65                 IFile res = (IFile) file.getIResource();
66                 try {
67                     IFileState[] states = res.getHistory(null);
68                     if(states.length > 0){
69                         restoreParentDirectory(file);
70                         // recreate file using the latest state
71
res.create(states[0].getContents(), true, null);
72                     } else {
73                         IStatus status = new Status(Status.ERROR, CVSProviderPlugin.ID,
74                                 CVSMessages.PrepareForReplaceVisitor_DeletedFileWithoutHistoryCannotBeRestoredWhileRevertToBase);
75                         CVSProviderPlugin.log(status);
76                         ConsoleListeners.getInstance().errorLineReceived(session,
77                                 NLS.bind(CVSMessages.PrepareForReplaceVisitor_FileCannotBeReplacedWithBase,
78                                         res.getName()),
79                                 status);
80                     }
81                 } catch (CoreException e) {
82                     CVSProviderPlugin.log(e);
83                 }
84             } else {
85                 file.unmanage(null);
86             }
87         } else if (file.isModified(null) && shouldDeleteModifications()) {
88             // If the file is modified, delete and unmanage it and allow the
89
// replace operation to fetch it again. This is required because "update -C"
90
// will fail for locally modified resources that have been deleted remotely.
91
file.delete();
92             deletedFiles.add(file);
93             // Only unmanage if the delete was successful (bug 76029)
94
file.unmanage(null);
95         }
96         monitor.worked(1);
97     }
98
99     private void restoreParentDirectory(ICVSFile file) throws CVSException {
100         List parents = new ArrayList();
101         ICVSFolder parent = file.getParent();
102         while(!parent.getIResource().exists()){
103             parents.add(parent);
104             parent = parent.getParent();
105         }
106         for(int i = parents.size() - 1; i > -1; i--){
107             ((ICVSFolder)parents.get(i)).mkdir();
108         }
109     }
110
111     /*
112      * see bug 150158
113      */

114     private boolean shouldDeleteModifications() {
115         return tag == null || !tag.getName().equals("BASE"); //$NON-NLS-1$
116
}
117
118     /**
119      * @see ICVSResourceVisitor#visitFolder(ICVSFolder)
120      */

121     public void visitFolder(ICVSFolder folder) throws CVSException {
122         // Delete unmanaged folders if the user wants them deleted
123
if (!folder.isCVSFolder()) {
124             if (CVSProviderPlugin.getPlugin().isReplaceUnmanaged()) {
125                 // Needed to add files inside to deletedFiles set.
126
folder.acceptChildren(this);
127                 folder.delete();
128             }
129         } else {
130             // Visit the children of the folder as appropriate
131
if (depth == IResource.DEPTH_INFINITE) {
132                 folder.acceptChildren(this);
133             } else if (depth == IResource.DEPTH_ONE) {
134                 ICVSResource[] files = folder.members(ICVSFolder.FILE_MEMBERS);
135                 for (int i = 0; i < files.length; i++) {
136                     files[i].accept(this);
137                 }
138             }
139             // Also delete ignored child files that start with .#
140
ICVSResource[] ignoredFiles = folder.members(ICVSFolder.FILE_MEMBERS | ICVSFolder.IGNORED_MEMBERS);
141             for (int i = 0; i < ignoredFiles.length; i++) {
142                 ICVSResource cvsResource = ignoredFiles[i];
143                 if (cvsResource.getName().startsWith(".#")) { //$NON-NLS-1$
144
cvsResource.delete();
145                 }
146             }
147         }
148         monitor.worked(1);
149     }
150     
151     public void visitResources(IProject project, final ICVSResource[] resources, final String JavaDoc oneArgMessage, int depth, IProgressMonitor pm) throws CVSException {
152         this.depth = depth;
153         deletedFiles = new HashSet();
154         CVSWorkspaceRoot.getCVSFolderFor(project).run(new ICVSRunnable() {
155             public void run(IProgressMonitor pm) throws CVSException {
156                 monitor = Policy.infiniteSubMonitorFor(pm, 100);
157                 monitor.beginTask(null, 512);
158                 for (int i = 0; i < resources.length; i++) {
159                     if (oneArgMessage != null) {
160                         monitor.subTask(NLS.bind(oneArgMessage, new String JavaDoc[] { resources[i].getIResource().getFullPath().toString() }));
161                     }
162                     resources[i].accept(PrepareForReplaceVisitor.this);
163                 }
164                 monitor.done();
165             }
166         }, pm);
167     }
168     
169     public Set/*<ICVSFile>*/ getDeletedFiles() {
170         return Collections.unmodifiableSet(deletedFiles);
171     }
172 }
173
Popular Tags