KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ccvs > ui > operations > ReplaceOperation


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.ui.operations;
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.List JavaDoc;
16 import java.util.Set JavaDoc;
17
18 import org.eclipse.core.resources.*;
19 import org.eclipse.core.resources.mapping.ResourceMapping;
20 import org.eclipse.core.runtime.*;
21 import org.eclipse.core.runtime.Status;
22 import org.eclipse.osgi.util.NLS;
23 import org.eclipse.team.internal.ccvs.core.*;
24 import org.eclipse.team.internal.ccvs.core.client.*;
25 import org.eclipse.team.internal.ccvs.core.client.Command.LocalOption;
26 import org.eclipse.team.internal.ccvs.core.syncinfo.ResourceSyncInfo;
27 import org.eclipse.team.internal.ccvs.core.util.PrepareForReplaceVisitor;
28 import org.eclipse.team.internal.ccvs.ui.CVSUIMessages;
29 import org.eclipse.team.internal.ccvs.ui.Policy;
30 import org.eclipse.ui.IWorkbenchPart;
31
32 /**
33  * This operation replaces the local resources with their remote contents
34  */

35 public class ReplaceOperation extends UpdateOperation {
36
37     public ReplaceOperation(IWorkbenchPart part, IResource[] resources, CVSTag tag, boolean recurse) {
38         super(part, asResourceMappers(resources, recurse ? IResource.DEPTH_INFINITE : IResource.DEPTH_ONE), new LocalOption[] { Update.IGNORE_LOCAL_CHANGES }, tag);
39     }
40
41     public ReplaceOperation(IWorkbenchPart part, ResourceMapping[] mappings, CVSTag tag) {
42         super(part, mappings, new LocalOption[] { Update.IGNORE_LOCAL_CHANGES }, tag);
43     }
44
45     /* (non-Javadoc)
46      * @see org.eclipse.team.internal.ccvs.ui.operations.CVSOperation#getTaskName()
47      */

48     protected String JavaDoc getTaskName() {
49         return CVSUIMessages.ReplaceOperation_taskName;
50     }
51
52     /* (non-Javadoc)
53      * @see org.eclipse.team.internal.ccvs.ui.operations.SingleCommandOperation#executeCommand(org.eclipse.team.internal.ccvs.core.client.Session, org.eclipse.team.internal.ccvs.core.CVSTeamProvider, org.eclipse.core.resources.IResource[], org.eclipse.core.runtime.IProgressMonitor)
54      */

55     protected IStatus executeCommand(
56         final Session session,
57         final CVSTeamProvider provider,
58         final ICVSResource[] resources,
59         final boolean recurse, IProgressMonitor monitor)
60         throws CVSException, InterruptedException JavaDoc {
61         
62         final IStatus[] status = new IStatus[] { Status.OK_STATUS };
63         try {
64             ResourcesPlugin.getWorkspace().run(new IWorkspaceRunnable() {
65                 public void run(IProgressMonitor monitor) throws CoreException {
66                     try {
67                         status[0] = internalExecuteCommand(session, provider, resources, recurse, monitor);
68                     } catch (InterruptedException JavaDoc e) {
69                         throw new OperationCanceledException();
70                     }
71                 }
72             
73             }, null, IWorkspace.AVOID_UPDATE, monitor);
74         } catch (CoreException e) {
75             throw CVSException.wrapException(e);
76         }
77         return status[0];
78     }
79
80     // Files deleted by the PrepareForReplaceVisitor.
81
private Set JavaDoc/*<ICVSFile>*/ prepDeletedFiles;
82
83     private IStatus internalExecuteCommand(Session session, CVSTeamProvider provider, ICVSResource[] resources, boolean recurse, IProgressMonitor monitor) throws CVSException, InterruptedException JavaDoc {
84         monitor.beginTask(null, 100);
85         ICVSResource[] managedResources = getResourcesToUpdate(resources);
86         try {
87             // Purge any unmanaged or added files
88
PrepareForReplaceVisitor pfrv = new PrepareForReplaceVisitor(session, getTag());
89             pfrv.visitResources(
90                 provider.getProject(),
91                 resources,
92                 CVSUIMessages.ReplaceOperation_1,
93                 recurse ? IResource.DEPTH_INFINITE : IResource.DEPTH_ONE,
94                 Policy.subMonitorFor(monitor, 30));
95             prepDeletedFiles = pfrv.getDeletedFiles();
96             
97             // Only perform the remote command if some of the resources being replaced were managed
98
IStatus status = OK;
99             if (managedResources.length > 0) {
100                 // Perform an update, ignoring any local file modifications
101
status = super.executeCommand(session, provider, managedResources, recurse, Policy.subMonitorFor(monitor, 70));
102             }
103             
104             // Prune any empty folders left after the resources were purged.
105
// This is done to prune any empty folders that contained only unmanaged resources
106
if (status.isOK() && CVSProviderPlugin.getPlugin().getPruneEmptyDirectories()) {
107                 new PruneFolderVisitor().visit(session, resources);
108             }
109             
110             return status;
111         } finally {
112             monitor.done();
113         }
114     }
115
116     /**
117      * Return the resources that need to be updated from the server.
118      * By default, this is all resources that are managed.
119      * @param resources all resources being replaced
120      * @return resources that are to be updated from the server
121      * @throws CVSException
122      */

123     protected ICVSResource[] getResourcesToUpdate(ICVSResource[] resources) throws CVSException {
124         // Accumulate the managed resources from the list of provided resources
125
List JavaDoc managedResources = new ArrayList JavaDoc();
126         for (int i = 0; i < resources.length; i++) {
127             ICVSResource resource = resources[i];
128             if ((resource.isFolder() && ((ICVSFolder)resource).isCVSFolder())) {
129                 managedResources.add(resource);
130             } else if (!resource.isFolder()){
131                 byte[] syncBytes = ((ICVSFile)resource).getSyncBytes();
132                 if (syncBytes != null && !ResourceSyncInfo.isAddition(syncBytes)) {
133                     managedResources.add(resource);
134                 }
135             }
136         }
137         return (ICVSResource[]) managedResources.toArray(new ICVSResource[managedResources.size()]);
138     }
139     
140     /* (non-Javadoc)
141      * @see org.eclipse.team.internal.ccvs.ui.operations.UpdateOperation#getUpdateCommand()
142      */

143     protected Update getUpdateCommand() {
144         // Use a special replace command that doesn't set back the timestamps
145
// of files in the passed set if it recreates them.
146
return new Replace(prepDeletedFiles);
147     }
148
149     /* (non-Javadoc)
150      * @see org.eclipse.team.internal.ccvs.ui.operations.RepositoryProviderOperation#getTaskName(org.eclipse.team.internal.ccvs.core.CVSTeamProvider)
151      */

152     protected String JavaDoc getTaskName(CVSTeamProvider provider) {
153         return NLS.bind(CVSUIMessages.ReplaceOperation_0, new String JavaDoc[] { provider.getProject().getName() });
154     }
155 }
156
Popular Tags