KickJava   Java API By Example, From Geeks To Geeks.

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


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.lang.reflect.InvocationTargetException JavaDoc;
14 import java.util.Date JavaDoc;
15
16 import org.eclipse.core.resources.*;
17 import org.eclipse.core.runtime.*;
18 import org.eclipse.osgi.util.NLS;
19 import org.eclipse.swt.widgets.Shell;
20 import org.eclipse.team.core.TeamException;
21 import org.eclipse.team.core.synchronize.SyncInfoFilter.ContentComparisonSyncInfoFilter;
22 import org.eclipse.team.core.variants.IResourceVariant;
23 import org.eclipse.team.internal.ccvs.core.*;
24 import org.eclipse.team.internal.ccvs.core.resources.CVSWorkspaceRoot;
25 import org.eclipse.team.internal.ccvs.core.syncinfo.MutableResourceSyncInfo;
26 import org.eclipse.team.internal.ccvs.core.syncinfo.ResourceSyncInfo;
27 import org.eclipse.team.internal.ccvs.ui.*;
28 import org.eclipse.team.internal.ccvs.ui.CVSUIPlugin;
29 import org.eclipse.team.internal.ccvs.ui.Policy;
30
31 /**
32  * Reconcile an existing unshared local project with an existing remote folder.
33  */

34 public class ReconcileProjectOperation extends ShareProjectOperation {
35
36     private ICVSRemoteFolder folder;
37     private ContentComparisonSyncInfoFilter contentCompare = new ContentComparisonSyncInfoFilter(false);
38
39     public ReconcileProjectOperation(Shell shell, IProject project, ICVSRemoteFolder folder) {
40         super(shell, folder.getRepository(), project, folder.getRepositoryRelativePath());
41         this.folder = folder;
42     }
43
44     /* (non-Javadoc)
45      * @see org.eclipse.team.internal.ccvs.ui.operations.CVSOperation#getTaskName()
46      */

47     protected String JavaDoc getTaskName() {
48         return NLS.bind(CVSUIMessages.ReconcileProjectOperation_0, new String JavaDoc[] { getProject().getName(), folder.getRepositoryRelativePath() });
49     }
50     
51     /* (non-Javadoc)
52      * @see org.eclipse.team.internal.ccvs.ui.operations.ShareProjectOperation#createRemoteFolder(org.eclipse.core.runtime.IProgressMonitor)
53      */

54     protected ICVSRemoteFolder createRemoteFolder(IProgressMonitor monitor) throws CVSException {
55         // The folder already exists so just return the handle
56
return folder;
57     }
58     
59     /* (non-Javadoc)
60      * @see org.eclipse.team.internal.ccvs.ui.operations.ShareProjectOperation#mapProjectToRemoteFolder(org.eclipse.team.internal.ccvs.core.ICVSRemoteFolder, org.eclipse.core.runtime.IProgressMonitor)
61      */

62     protected void mapProjectToRemoteFolder(ICVSRemoteFolder remote, IProgressMonitor monitor) throws TeamException {
63         // Map the project
64
monitor.beginTask(null, 100);
65         super.mapProjectToRemoteFolder(remote, Policy.subMonitorFor(monitor, 10));
66         // Reconcile the sync info
67
reconcileSyncInfo(Policy.subMonitorFor(monitor, 90));
68         monitor.done();
69     }
70     
71     /* (non-Javadoc)
72      * @see org.eclipse.team.internal.ccvs.ui.operations.CVSOperation#execute(org.eclipse.core.runtime.IProgressMonitor)
73      */

74     protected void reconcileSyncInfo(IProgressMonitor monitor) throws CVSException {
75         try {
76             monitor.beginTask(null, 100);
77             // Fetch the entire remote tree
78
ICVSRemoteFolder remote = CheckoutToRemoteFolderOperation.checkoutRemoteFolder(getPart(), folder, Policy.subMonitorFor(monitor, 80));
79             // Traverse the tree and populate the workspace base or remote
80
// with the sync info depending on file contents
81
populateWorkspace(remote, Policy.subMonitorFor(monitor, 20));
82         } catch (InvocationTargetException JavaDoc e) {
83             throw CVSException.wrapException(e);
84         } catch (TeamException e) {
85             throw CVSException.wrapException(e);
86         } catch (InterruptedException JavaDoc e) {
87             throw new OperationCanceledException();
88         } finally {
89             monitor.done();
90         }
91
92     }
93
94     private void populateWorkspace(final ICVSRemoteFolder remote, IProgressMonitor monitor) throws CVSException {
95         CVSWorkspaceRoot.getCVSFolderFor(getProject()).run(new ICVSRunnable() {
96             public void run(IProgressMonitor monitor) throws CVSException {
97                 populateWorkspace(getProject(), remote, monitor);
98             }
99         }, monitor);
100         
101     }
102
103     /* private */ void populateWorkspace(IResource resource, ICVSRemoteResource remote, IProgressMonitor monitor) throws CVSException {
104         try {
105             if (resource.getType() == IResource.FILE) {
106                 if (remote.isContainer()) {
107                     CVSUIPlugin.log(IStatus.ERROR, NLS.bind(CVSUIMessages.ReconcileProjectOperation_1, new String JavaDoc[] { resource.getFullPath().toString(), remote.getRepositoryRelativePath() }), null);
108                 } else {
109                     IFile file = (IFile)resource;
110                     IResourceVariant variant = (IResourceVariant)remote;
111                     if (file.exists()
112                             && variant != null
113                             && contentCompare.compareContents(file, variant, monitor)) {
114                         // The contents are the same so populate the local workspace
115
// with the remote sync info and make the file in-sync
116
makeInSync(file, remote, monitor);
117                     } else {
118                         // Would like to put the bytes in the remote but this
119
// is complicated due to subscriber events.
120
// We'll refresh the subcriber at the end.
121
}
122                 }
123             } else {
124                 if (!remote.isContainer()) {
125                     CVSUIPlugin.log(IStatus.ERROR, NLS.bind(CVSUIMessages.ReconcileProjectOperation_2, new String JavaDoc[] { resource.getFullPath().toString(), remote.getRepositoryRelativePath() }), null);
126                 } else {
127                     // Map the local folder to the remote folder.
128
// (Note that this will make phantoms for non-exisiting local folders)
129
ICVSFolder folder = (ICVSFolder)CVSWorkspaceRoot.getCVSResourceFor(resource);
130                     folder.setFolderSyncInfo(((ICVSFolder)remote).getFolderSyncInfo());
131                     // Traverse the children of the remote
132
// (The members were prefetched).
133
ICVSRemoteResource[] members = remote.members(monitor);
134                     for (int i = 0; i < members.length; i++) {
135                         ICVSRemoteResource member = members[i];
136                         populateWorkspace(getLocalChild((IContainer)resource, member), member, monitor);
137                     }
138                 }
139             }
140         } catch (TeamException e) {
141             throw CVSException.wrapException(e);
142         }
143     }
144
145     /*
146      * Get the local resource handle for corresponding to the remote resource
147      */

148     private IResource getLocalChild(IContainer parent, ICVSRemoteResource member) {
149         IResource resource = parent.findMember(member.getName());
150         if (resource == null) {
151             if (member.isContainer()) {
152                 resource = parent.getFolder(new Path(null, member.getName()));
153             } else {
154                 resource = parent.getFile(new Path(null, member.getName()));
155             }
156         }
157         return resource;
158     }
159
160     /*
161      * Make the file in-sync with its corresponding remote.
162      */

163     private void makeInSync(IFile file, ICVSRemoteResource remote, IProgressMonitor monitor) throws CVSException {
164         ICVSFile cvsFile = CVSWorkspaceRoot.getCVSFileFor(file);
165         ResourceSyncInfo info = remote.getSyncInfo();
166         Date JavaDoc modTime = info.getTimeStamp();
167         if (modTime != null) {
168             cvsFile.setTimeStamp(modTime);
169         }
170         modTime = cvsFile.getTimeStamp();
171         MutableResourceSyncInfo newInfoWithTimestamp = info.cloneMutable();
172         newInfoWithTimestamp.setTimeStamp(modTime);
173         cvsFile.setSyncInfo(newInfoWithTimestamp, ICVSFile.CLEAN);
174     }
175
176 }
177
Popular Tags