KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ccvs > ui > mappings > MergeSubscriberContext


1 /*******************************************************************************
2  * Copyright (c) 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.mappings;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.List 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.team.core.diff.IDiff;
20 import org.eclipse.team.core.diff.IThreeWayDiff;
21 import org.eclipse.team.core.history.IFileRevision;
22 import org.eclipse.team.core.mapping.ISynchronizationScopeManager;
23 import org.eclipse.team.core.mapping.provider.ResourceDiffTree;
24 import org.eclipse.team.core.subscribers.Subscriber;
25 import org.eclipse.team.core.variants.IResourceVariant;
26 import org.eclipse.team.internal.ccvs.core.*;
27 import org.eclipse.team.internal.ccvs.core.resources.CVSWorkspaceRoot;
28 import org.eclipse.team.internal.ccvs.core.syncinfo.*;
29 import org.eclipse.team.internal.ccvs.ui.CVSUIMessages;
30 import org.eclipse.team.internal.ui.Utils;
31
32 public class MergeSubscriberContext extends CVSSubscriberMergeContext {
33
34     public static MergeSubscriberContext createContext(ISynchronizationScopeManager manager, Subscriber subscriber) {
35         MergeSubscriberContext mergeContext = new MergeSubscriberContext(subscriber, manager);
36         mergeContext.initialize();
37         return mergeContext;
38     }
39
40     private boolean cancel = true;
41     
42     public MergeSubscriberContext(Subscriber subscriber, ISynchronizationScopeManager manager) {
43         super(subscriber, manager);
44     }
45
46     /* (non-Javadoc)
47      * @see org.eclipse.team.core.mapping.IMergeContext#markAsMerged(org.eclipse.team.core.diff.IDiffNode, boolean, org.eclipse.core.runtime.IProgressMonitor)
48      */

49     public void markAsMerged(final IDiff diff, boolean inSyncHint, IProgressMonitor monitor) throws CoreException {
50         run(new IWorkspaceRunnable() {
51             public void run(IProgressMonitor monitor) throws CoreException {
52                 ((CVSMergeSubscriber)getSubscriber()).merged(new IResource[] { getDiffTree().getResource(diff)});
53             }
54         }, getMergeRule(diff), IResource.NONE, monitor);
55     }
56     
57     public void markAsMerged(final IDiff[] diffs, boolean inSyncHint, IProgressMonitor monitor) throws CoreException {
58         run(new IWorkspaceRunnable() {
59             public void run(IProgressMonitor monitor) throws CoreException {
60                 List JavaDoc result = new ArrayList JavaDoc();
61                 for (int i = 0; i < diffs.length; i++) {
62                     IDiff diff = diffs[i];
63                     result.add(getDiffTree().getResource(diff));
64                 }
65                 ((CVSMergeSubscriber)getSubscriber()).merged((IResource[]) result.toArray(new IResource[result.size()]));
66             }
67         }, getMergeRule(diffs), IResource.NONE, monitor);
68     }
69     
70     public void dispose() {
71         if (cancel)
72             ((CVSMergeSubscriber)getSubscriber()).cancel();
73         super.dispose();
74     }
75
76     public void setCancelSubscriber(boolean b) {
77         cancel = b;
78     }
79     
80     public IStatus merge(final IDiff diff, final boolean ignoreLocalChanges, IProgressMonitor monitor) throws CoreException {
81         final IStatus[] status = new IStatus[] { Status.OK_STATUS };
82         run(new IWorkspaceRunnable() {
83             public void run(IProgressMonitor monitor) throws CoreException {
84                 IThreeWayDiff currentDiff = (IThreeWayDiff)getSubscriber().getDiff(getDiffTree().getResource(diff));
85                 if (!MergeSubscriberContext.this.equals(currentDiff, (IThreeWayDiff)diff)) {
86                     throw new CVSException(NLS.bind(CVSUIMessages.CVSMergeContext_1, diff.getPath()));
87                 }
88                 status[0] = MergeSubscriberContext.super.merge(diff, ignoreLocalChanges, monitor);
89                 if (status[0].isOK()) {
90                     IResource resource = ResourceDiffTree.getResourceFor(diff);
91                     if (resource.getType() == IResource.FILE && resource.exists() && diff instanceof IThreeWayDiff) {
92                         IThreeWayDiff twd = (IThreeWayDiff) diff;
93                         if (twd.getKind() == IDiff.ADD && twd.getDirection() == IThreeWayDiff.INCOMING) {
94                             IFileRevision remote = Utils.getRemote(diff);
95                             IResourceVariant variant = (IResourceVariant)Utils.getAdapter(remote, IResourceVariant.class);
96                             byte[] syncBytes = variant.asBytes();
97                             MutableResourceSyncInfo info = new MutableResourceSyncInfo(resource.getName(), ResourceSyncInfo.ADDED_REVISION);
98                             info.setKeywordMode(ResourceSyncInfo.getKeywordMode(syncBytes));
99                             info.setTag(getTag(resource.getParent()));
100                             CVSWorkspaceRoot.getCVSFileFor((IFile)resource).setSyncInfo(info, ICVSFile.DIRTY);
101                         }
102                     }
103                 }
104             }
105         
106         }, getMergeRule(diff), IWorkspace.AVOID_UPDATE, monitor);
107         return status[0];
108     }
109
110     CVSTag getTag(IContainer parent) throws CVSException {
111         ICVSFolder folder = CVSWorkspaceRoot.getCVSFolderFor(parent);
112         FolderSyncInfo info = folder.getFolderSyncInfo();
113         if (info != null)
114             return info.getTag();
115         return null;
116     }
117     
118     boolean equals(IThreeWayDiff currentDiff, IThreeWayDiff diffTreeDiff) {
119         return currentDiff != null
120             && currentDiff.getKind() == diffTreeDiff.getKind()
121             && currentDiff.getDirection() == diffTreeDiff.getDirection();
122     }
123 }
124
Popular Tags