KickJava   Java API By Example, From Geeks To Geeks.

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


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.ui.operations;
12
13 import java.util.ArrayList JavaDoc;
14
15 import org.eclipse.core.resources.*;
16 import org.eclipse.core.resources.mapping.ResourceMapping;
17 import org.eclipse.core.runtime.IProgressMonitor;
18 import org.eclipse.core.runtime.NullProgressMonitor;
19 import org.eclipse.team.core.TeamException;
20 import org.eclipse.team.core.diff.IDiff;
21 import org.eclipse.team.core.diff.IThreeWayDiff;
22 import org.eclipse.team.core.history.IFileRevision;
23 import org.eclipse.team.core.mapping.IResourceDiff;
24 import org.eclipse.team.core.mapping.IResourceDiffTree;
25 import org.eclipse.team.core.synchronize.SyncInfoFilter;
26 import org.eclipse.team.core.synchronize.SyncInfoFilter.ContentComparisonSyncInfoFilter;
27 import org.eclipse.team.core.variants.IResourceVariant;
28 import org.eclipse.team.internal.ccvs.core.*;
29 import org.eclipse.team.internal.ccvs.core.resources.CVSWorkspaceRoot;
30 import org.eclipse.team.internal.ccvs.ui.Policy;
31 import org.eclipse.ui.IWorkbenchPart;
32
33 /**
34  * Operation that ensures that the contents for base
35  * of each local resource is cached.
36  */

37 public class CacheBaseContentsOperation extends CacheTreeContentsOperation {
38
39
40     public CacheBaseContentsOperation(IWorkbenchPart part, ResourceMapping[] mappings, IResourceDiffTree tree, boolean includeOutgoing) {
41         super(part, mappings, tree);
42     }
43
44     /* (non-Javadoc)
45      * @see org.eclipse.team.internal.ccvs.ui.operations.CacheTreeContentsOperation#getRemoteFileState(org.eclipse.team.core.diff.IThreeWayDiff)
46      */

47     protected IFileRevision getRemoteFileState(IThreeWayDiff twd) {
48         IResourceDiff diff = (IResourceDiff)twd.getRemoteChange();
49         if (diff == null)
50             diff = (IResourceDiff)twd.getLocalChange();
51         IFileRevision base = diff.getBeforeState();
52         return base;
53     }
54
55     /* (non-Javadoc)
56      * @see org.eclipse.team.internal.ccvs.ui.operations.CacheTreeContentsOperation#isEnabledForDirection(int)
57      */

58     protected boolean isEnabledForDirection(int direction) {
59         return true;
60     }
61
62     /* (non-Javadoc)
63      * @see org.eclipse.team.internal.ccvs.ui.operations.CacheTreeContentsOperation#buildTree(org.eclipse.team.internal.ccvs.core.CVSTeamProvider)
64      */

65     protected ICVSRemoteResource buildTree(CVSTeamProvider provider) throws TeamException {
66         return CVSProviderPlugin.getPlugin().getCVSWorkspaceSubscriber().buildBaseTree(provider.getProject(), true, new NullProgressMonitor());
67     }
68     
69     protected void execute(CVSTeamProvider provider, IResource[] resources, boolean recurse, IProgressMonitor monitor) throws CVSException, InterruptedException JavaDoc {
70         IResource[] localChanges = getFilesWithLocalChanges(resources, recurse);
71         super.execute(provider, resources, recurse, monitor);
72         // Now that the contents are cached, reset the timestamps for any false local changes
73
if (localChanges.length > 0) {
74             performCleanTimestamps(localChanges[0].getProject(), localChanges, monitor);
75         }
76     }
77     
78     private IResource[] getFilesWithLocalChanges(IResource[] resources, boolean recurse) {
79         ArrayList JavaDoc result = new ArrayList JavaDoc();
80         for (int i = 0; i < resources.length; i++) {
81             IResource resource = resources[i];
82             IDiff[] nodes = getTree().getDiffs(resource, recurse ? IResource.DEPTH_INFINITE: IResource.DEPTH_ONE);
83             for (int j = 0; j < nodes.length; j++) {
84                 IDiff node = nodes[j];
85                 if (isFileWithLocalChange(node)) {
86                     result.add(getTree().getResource(node));
87                 }
88             }
89         }
90         return (IResource[]) result.toArray(new IResource[result.size()]);
91     }
92
93     private boolean isFileWithLocalChange(IDiff node) {
94         if (node instanceof IThreeWayDiff) {
95             IThreeWayDiff twd = (IThreeWayDiff) node;
96             return (twd.getDirection() == IThreeWayDiff.OUTGOING || twd.getDirection() == IThreeWayDiff.CONFLICTING)
97                 && getTree().getResource(node).getType() == IResource.FILE;
98         }
99         return false;
100     }
101     
102     private void performCleanTimestamps(IProject project, final IResource[] resources, IProgressMonitor monitor) throws CVSException {
103         ICVSFolder folder = CVSWorkspaceRoot.getCVSFolderFor(project);
104         final ContentComparisonSyncInfoFilter comparator = new SyncInfoFilter.ContentComparisonSyncInfoFilter(false);
105         folder.run(new ICVSRunnable() {
106             public void run(IProgressMonitor monitor) throws CVSException {
107                 monitor.beginTask(null, resources.length * 100);
108                 for (int i = 0; i < resources.length; i++) {
109                     IResource resource = resources[i];
110                     if (resource.exists() && resource.getType() == IResource.FILE) {
111                         IResourceVariant remoteResource = (IResourceVariant)CVSWorkspaceRoot.getRemoteResourceFor(resource);
112                         if (remoteResource != null && comparator.compareContents((IFile)resource, remoteResource, Policy.subMonitorFor(monitor, 100))) {
113                             ICVSFile cvsFile = CVSWorkspaceRoot.getCVSFileFor((IFile)resource);
114                             cvsFile.checkedIn(null, false /* not a commit */);
115                         }
116                     }
117                 }
118                 monitor.done();
119             }
120         }, Policy.subMonitorFor(monitor, 100));
121     }
122
123 }
124
Popular Tags