KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ccvs > ui > subscriber > RefreshDirtyStateOperation


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.subscriber;
12
13 import java.lang.reflect.InvocationTargetException JavaDoc;
14 import java.util.ArrayList JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.eclipse.compare.structuremergeviewer.IDiffElement;
18 import org.eclipse.core.resources.*;
19 import org.eclipse.core.resources.mapping.ResourceMapping;
20 import org.eclipse.core.resources.mapping.ResourceMappingContext;
21 import org.eclipse.core.runtime.IProgressMonitor;
22 import org.eclipse.core.runtime.OperationCanceledException;
23 import org.eclipse.team.core.TeamException;
24 import org.eclipse.team.core.diff.IDiff;
25 import org.eclipse.team.core.mapping.provider.ResourceDiffTree;
26 import org.eclipse.team.core.mapping.provider.SynchronizationScopeManager;
27 import org.eclipse.team.core.synchronize.*;
28 import org.eclipse.team.core.synchronize.SyncInfoFilter.ContentComparisonSyncInfoFilter;
29 import org.eclipse.team.internal.ccvs.core.*;
30 import org.eclipse.team.internal.ccvs.core.resources.CVSWorkspaceRoot;
31 import org.eclipse.team.internal.ccvs.ui.CVSUIMessages;
32 import org.eclipse.team.internal.ccvs.ui.Policy;
33 import org.eclipse.team.internal.ccvs.ui.operations.*;
34 import org.eclipse.team.internal.core.mapping.SyncInfoToDiffConverter;
35 import org.eclipse.team.ui.synchronize.ISynchronizePageConfiguration;
36
37 /**
38  * Resets the dirty state of files whose contents match their base.
39  */

40 public class RefreshDirtyStateOperation extends CVSSubscriberOperation {
41     
42     protected RefreshDirtyStateOperation(ISynchronizePageConfiguration configuration, IDiffElement[] elements) {
43         super(configuration, elements);
44     }
45
46     /* (non-Javadoc)
47      * @see org.eclipse.team.internal.ccvs.ui.subscriber.CVSSubscriberOperation#run(org.eclipse.team.core.synchronize.SyncInfoSet, org.eclipse.core.runtime.IProgressMonitor)
48      */

49     protected void runWithProjectRule(IProject project, SyncInfoSet set, IProgressMonitor monitor) throws TeamException {
50         final SyncInfo[] infos = set.getSyncInfos();
51         if (infos.length == 0) return;
52         monitor.beginTask(null, 200);
53         ensureBaseContentsCached(project, infos, Policy.subMonitorFor(monitor, 100));
54         performCleanTimestamps(project, infos, monitor);
55         monitor.done();
56     }
57
58     private void performCleanTimestamps(IProject project, final SyncInfo[] infos, IProgressMonitor monitor) throws CVSException {
59         ICVSFolder folder = CVSWorkspaceRoot.getCVSFolderFor(project);
60         final ContentComparisonSyncInfoFilter comparator = new SyncInfoFilter.ContentComparisonSyncInfoFilter(false);
61         folder.run(new ICVSRunnable() {
62             public void run(IProgressMonitor monitor) throws CVSException {
63                 monitor.beginTask(null, infos.length * 100);
64                 for (int i = 0; i < infos.length; i++) {
65                     SyncInfo info = infos[i];
66                     IResource resource = info.getLocal();
67                     if (resource.getType() == IResource.FILE) {
68                         if (comparator.compareContents((IFile)resource, info.getBase(), Policy.subMonitorFor(monitor, 100))) {
69                             ICVSFile cvsFile = CVSWorkspaceRoot.getCVSFileFor((IFile)resource);
70                             cvsFile.checkedIn(null, false /* not a commit */);
71                         }
72                     }
73                 }
74                 monitor.done();
75             }
76         }, Policy.subMonitorFor(monitor, 100));
77     }
78     
79     private void ensureBaseContentsCached(IProject project, SyncInfo[] infos, IProgressMonitor monitor) throws CVSException {
80         List JavaDoc diffs = new ArrayList JavaDoc();
81         for (int i = 0; i < infos.length; i++) {
82             SyncInfo info = infos[i];
83             IDiff node = getConverter().getDeltaFor(info);
84             diffs.add(node);
85         }
86         ensureBaseContentsCached(project, (IDiff[]) diffs.toArray(new IDiff[diffs.size()]), monitor);
87     }
88
89     private SyncInfoToDiffConverter getConverter() {
90         SyncInfoToDiffConverter converter = (SyncInfoToDiffConverter)CVSProviderPlugin.getPlugin().getCVSWorkspaceSubscriber().getAdapter(SyncInfoToDiffConverter.class);
91         if (converter == null)
92             return SyncInfoToDiffConverter.getDefault();
93         return converter;
94     }
95
96     private void ensureBaseContentsCached(final IProject project, IDiff[] nodes, IProgressMonitor monitor) throws CVSException {
97         try {
98             ResourceDiffTree tree = new ResourceDiffTree();
99             for (int i = 0; i < nodes.length; i++) {
100                 IDiff node = nodes[i];
101                 tree.add(node);
102             }
103             new CacheBaseContentsOperation(getPart(), new ResourceMapping[] { (ResourceMapping)project.getAdapter(ResourceMapping.class) },
104                     tree, true) {
105                 protected ResourceMappingContext getResourceMappingContext() {
106                     return new SingleProjectSubscriberContext(CVSProviderPlugin.getPlugin().getCVSWorkspaceSubscriber(), false, project);
107                 }
108                 protected SynchronizationScopeManager createScopeManager(boolean consultModels) {
109                     return new SingleProjectScopeManager(getJobName(), getSelectedMappings(), getResourceMappingContext(), consultModels, project);
110                 }
111             }.run(monitor);
112         } catch (InvocationTargetException JavaDoc e) {
113             throw CVSException.wrapException(e);
114         } catch (InterruptedException JavaDoc e) {
115             throw new OperationCanceledException();
116         }
117     }
118     
119     protected String JavaDoc getErrorTitle() {
120         return CVSUIMessages.RefreshDirtyStateOperation_0;
121     }
122     
123     /* (non-Javadoc)
124      * @see org.eclipse.team.internal.ccvs.ui.subscriber.CVSSubscriberAction#getJobName(org.eclipse.team.ui.sync.SyncInfoSet)
125      */

126     protected String JavaDoc getJobName() {
127         return CVSUIMessages.RefreshDirtyStateOperation_1;
128     }
129 }
130
Popular Tags