KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ccvs > core > CVSSyncTreeSubscriber


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.core;
12
13 import java.util.Arrays JavaDoc;
14 import java.util.HashSet JavaDoc;
15 import java.util.Set JavaDoc;
16
17 import org.eclipse.core.resources.IResource;
18 import org.eclipse.core.resources.IResourceStatus;
19 import org.eclipse.core.runtime.*;
20 import org.eclipse.team.core.RepositoryProvider;
21 import org.eclipse.team.core.TeamException;
22 import org.eclipse.team.core.diff.IDiff;
23 import org.eclipse.team.core.subscribers.Subscriber;
24 import org.eclipse.team.core.synchronize.SyncInfo;
25 import org.eclipse.team.core.variants.*;
26 import org.eclipse.team.internal.ccvs.core.filehistory.CVSResourceVariantFileRevision;
27 import org.eclipse.team.internal.ccvs.core.resources.CVSWorkspaceRoot;
28 import org.eclipse.team.internal.core.mapping.ResourceVariantFileRevision;
29 import org.eclipse.team.internal.core.mapping.SyncInfoToDiffConverter;
30
31 /**
32  * This class provides common functionality for three way synchronizing
33  * for CVS.
34  */

35 public abstract class CVSSyncTreeSubscriber extends ResourceVariantTreeSubscriber implements IAdaptable {
36     
37     public static final String JavaDoc SYNC_KEY_QUALIFIER = "org.eclipse.team.cvs"; //$NON-NLS-1$
38

39     private IResourceVariantComparator comparisonCriteria;
40     
41     private QualifiedName id;
42     private String JavaDoc name;
43     private SyncInfoToDiffConverter converter = new CVSSyncInfoToDiffConverter();
44     
45     public class CVSSyncInfoToDiffConverter extends SyncInfoToDiffConverter {
46         protected ResourceVariantFileRevision asFileRevision(IResourceVariant variant) {
47             return new CVSResourceVariantFileRevision(variant);
48         }
49     }
50     
51     CVSSyncTreeSubscriber(QualifiedName id, String JavaDoc name) {
52         this.id = id;
53         this.name = name;
54         this.comparisonCriteria = new CVSRevisionNumberCompareCriteria(isThreeWay());
55     }
56
57     /* (non-Javadoc)
58      * @see org.eclipse.team.core.sync.ISyncTreeSubscriber#getId()
59      */

60     public QualifiedName getId() {
61         return id;
62     }
63
64     /* (non-Javadoc)
65      * @see org.eclipse.team.core.sync.ISyncTreeSubscriber#getName()
66      */

67     public String JavaDoc getName() {
68         return name;
69     }
70
71     /* (non-Javadoc)
72      * @see org.eclipse.team.core.sync.ISyncTreeSubscriber#getSyncInfo(org.eclipse.core.resources.IResource)
73      */

74     public SyncInfo getSyncInfo(IResource resource) throws TeamException {
75         if (!isSupervised(resource)) return null;
76         if(resource.getType() == IResource.FILE || !isThreeWay()) {
77             return super.getSyncInfo(resource);
78         } else {
79             // In CVS, folders do not have a base. Hence, the remote is used as the base.
80
IResourceVariant remoteResource = getRemoteTree().getResourceVariant(resource);
81             return getSyncInfo(resource, remoteResource, remoteResource);
82         }
83     }
84     
85     /* (non-Javadoc)
86      * @see org.eclipse.team.core.sync.ISyncTreeSubscriber#isSupervised(org.eclipse.core.resources.IResource)
87      */

88     public boolean isSupervised(IResource resource) throws TeamException {
89         try {
90             RepositoryProvider provider = RepositoryProvider.getProvider(resource.getProject(), CVSProviderPlugin.getTypeId());
91             if (provider == null) return false;
92             // TODO: what happens for resources that don't exist?
93
// TODO: is it proper to use ignored here?
94
ICVSResource cvsThing = CVSWorkspaceRoot.getCVSResourceFor(resource);
95             if (cvsThing.isIgnored()) {
96                 // An ignored resource could have an incoming addition (conflict)
97
return getRemoteTree().hasResourceVariant(resource);
98             }
99             return true;
100         } catch (TeamException e) {
101             // If there is no resource in core this means there is no local and no remote
102
// so the resource is not supervised.
103
if (e.getStatus().getCode() == IResourceStatus.RESOURCE_NOT_FOUND || !resource.getProject().isAccessible()) {
104                 return false;
105             }
106             throw e;
107         }
108     }
109
110     /* (non-Javadoc)
111      * @see org.eclipse.team.core.subscribers.TeamSubscriber#getDefaultComparisonCriteria()
112      */

113     public IResourceVariantComparator getResourceComparator() {
114         return comparisonCriteria;
115     }
116
117     /* (non-Javadoc)
118      * @see org.eclipse.team.internal.core.subscribers.caches.ResourceVariantTreeSubscriber#getSyncInfo(org.eclipse.core.resources.IResource, org.eclipse.team.core.synchronize.IResourceVariant, org.eclipse.team.core.synchronize.IResourceVariant)
119      */

120     protected SyncInfo getSyncInfo(IResource local, IResourceVariant base, IResourceVariant remote) throws TeamException {
121         CVSSyncInfo info = new CVSSyncInfo(local, base, remote, this);
122         info.init();
123         return info;
124     }
125     
126     /*
127      * Indicate whether file contents should be cached on a refresh
128      */

129     protected boolean getCacheFileContentsHint() {
130         return false;
131     }
132     
133     /*
134      * Indicate whether the subscriber is two-way or three-way
135      */

136     protected boolean isThreeWay() {
137         return true;
138     }
139     
140     protected boolean rootsEqual(Subscriber other) {
141         Set JavaDoc roots1 = new HashSet JavaDoc(Arrays.asList(other.roots()));
142         Set JavaDoc roots2 = new HashSet JavaDoc(Arrays.asList(roots()));
143         if(roots1.size() != roots2.size()) return false;
144         return roots2.containsAll(roots1);
145     }
146     
147     
148     public IDiff getDiff(IResource resource) throws CoreException {
149         SyncInfo info = getSyncInfo(resource);
150         if (info == null || info.getKind() == SyncInfo.IN_SYNC)
151             return null;
152         return converter.getDeltaFor(info);
153     }
154     public Object JavaDoc getAdapter(Class JavaDoc adapter) {
155         if (adapter == SyncInfoToDiffConverter.class) {
156             return converter;
157         }
158         return Platform.getAdapterManager().getAdapter(this, adapter);
159     }
160
161 }
162
Popular Tags