1 11 package org.eclipse.team.internal.ccvs.core; 12 13 import java.util.*; 14 15 import org.eclipse.core.resources.*; 16 import org.eclipse.core.runtime.QualifiedName; 17 import org.eclipse.osgi.util.NLS; 18 import org.eclipse.team.core.TeamException; 19 import org.eclipse.team.core.subscribers.*; 20 import org.eclipse.team.core.variants.IResourceVariantTree; 21 import org.eclipse.team.core.variants.SessionResourceVariantByteStore; 22 import org.eclipse.team.internal.ccvs.core.resources.CVSWorkspaceRoot; 23 import org.eclipse.team.internal.ccvs.core.syncinfo.CVSResourceVariantTree; 24 import org.eclipse.team.internal.ccvs.core.syncinfo.MultiTagResourceVariantTree; 25 26 30 public class CVSCompareSubscriber extends CVSSyncTreeSubscriber implements ISubscriberChangeListener { 31 32 public static final String ID = "org.eclipse.team.cvs.ui.compare-participant"; public static final String ID_MODAL = "org.eclipse.team.cvs.ui.compare-participant-modal"; 35 public static final String QUALIFIED_NAME = CVSProviderPlugin.ID + ".compare"; private static final String UNIQUE_ID_PREFIX = "compare-"; 38 private IResource[] resources; 39 private CVSResourceVariantTree tree; 40 41 public CVSCompareSubscriber(IResource[] resources, CVSTag tag) { 42 super(getUniqueId(), NLS.bind(CVSMessages.CVSCompareSubscriber_2, new String [] { tag.getName() })); this.resources = resources; 44 tree = new CVSResourceVariantTree(new SessionResourceVariantByteStore(), tag, getCacheFileContentsHint()); 45 initialize(); 46 } 47 48 public CVSCompareSubscriber(IResource[] resources, CVSTag[] tags, String name) { 49 super(getUniqueId(), NLS.bind(CVSMessages.CVSCompareSubscriber_2, new String [] { name })); resetRoots(resources, tags); 51 initialize(); 52 } 53 54 58 public void resetRoots(IResource[] resources, CVSTag[] tags) { 59 if (this.resources != null) { 60 List removed = new ArrayList(); 61 for (int i = 0; i < this.resources.length; i++) { 62 IResource resource = this.resources[i]; 63 removed.add(new SubscriberChangeEvent(this, ISubscriberChangeEvent.ROOT_REMOVED, resource)); 64 } 65 this.resources = new IResource[0]; 66 fireTeamResourceChange((ISubscriberChangeEvent[]) removed.toArray(new ISubscriberChangeEvent[removed.size()])); 67 if (tree != null) { 68 tree.dispose(); 69 tree = null; 70 } 71 } 72 this.resources = resources; 73 MultiTagResourceVariantTree multiTree = new MultiTagResourceVariantTree(new SessionResourceVariantByteStore(), getCacheFileContentsHint()); 74 for (int i = 0; i < tags.length; i++) { 75 multiTree.addResource(resources[i], tags[i]); 76 } 77 tree = multiTree; 78 } 79 80 private void initialize() { 81 CVSProviderPlugin.getPlugin().getCVSWorkspaceSubscriber().addListener(this); 82 } 83 84 public void dispose() { 85 CVSProviderPlugin.getPlugin().getCVSWorkspaceSubscriber().removeListener(this); 86 tree.dispose(); 87 } 88 89 private static QualifiedName getUniqueId() { 90 String uniqueId = Long.toString(System.currentTimeMillis()); 91 return new QualifiedName(QUALIFIED_NAME, UNIQUE_ID_PREFIX + uniqueId); 92 } 93 94 97 protected IResourceVariantTree getBaseTree() { 98 return null; 100 } 101 102 105 protected IResourceVariantTree getRemoteTree() { 106 return tree; 107 } 108 109 112 public boolean isThreeWay() { 113 return false; 114 } 115 116 119 public IResource[] roots() { 120 return resources; 121 } 122 123 126 public void subscriberResourceChanged(ISubscriberChangeEvent[] deltas) { 127 List outgoingDeltas = new ArrayList(deltas.length); 128 for (int i = 0; i < deltas.length; i++) { 129 ISubscriberChangeEvent delta = deltas[i]; 130 if ((delta.getFlags() & ISubscriberChangeEvent.ROOT_REMOVED) != 0) { 131 IResource resource = delta.getResource(); 132 outgoingDeltas.addAll(Arrays.asList(handleRemovedRoot(resource))); 133 } else if ((delta.getFlags() & ISubscriberChangeEvent.SYNC_CHANGED) != 0) { 134 IResource resource = delta.getResource(); 135 try { 136 if (isSupervised(resource)) { 137 outgoingDeltas.add(new SubscriberChangeEvent(this, delta.getFlags(), resource)); 138 } 139 } catch (TeamException e) { 140 CVSProviderPlugin.log(e); 142 } 143 } 144 } 145 146 fireTeamResourceChange((SubscriberChangeEvent[]) outgoingDeltas.toArray(new SubscriberChangeEvent[outgoingDeltas.size()])); 147 } 148 149 private SubscriberChangeEvent[] handleRemovedRoot(IResource removedRoot) { 150 List removals = new ArrayList(resources.length); 152 for (int j = 0; j < resources.length; j++) { 153 IResource root = resources[j]; 154 if (removedRoot.getFullPath().isPrefixOf(root.getFullPath())) { 155 removals.add(root); 157 try { 158 tree.flushVariants(root, IResource.DEPTH_INFINITE); 159 } catch (TeamException e) { 160 CVSProviderPlugin.log(e); 161 } 162 } 163 } 164 if (removals.isEmpty()) { 165 return new SubscriberChangeEvent[0]; 166 } 167 168 List newRoots = new ArrayList(resources.length); 170 newRoots.addAll(Arrays.asList(resources)); 171 newRoots.removeAll(removals); 172 resources = (IResource[]) newRoots.toArray(new IResource[newRoots.size()]); 173 174 SubscriberChangeEvent[] deltas = new SubscriberChangeEvent[removals.size()]; 176 for (int i = 0; i < deltas.length; i++) { 177 deltas[i] = new SubscriberChangeEvent(this, ISubscriberChangeEvent.ROOT_REMOVED, (IResource)removals.get(i)); 178 } 179 return deltas; 180 } 181 182 185 public boolean isSupervised(IResource resource) throws TeamException { 186 if (super.isSupervised(resource)) { 187 if (!resource.exists() && !getRemoteTree().hasResourceVariant(resource)) { 188 return false; 190 } 191 for (int i = 0; i < resources.length; i++) { 192 IResource root = resources[i]; 193 if (root.getFullPath().isPrefixOf(resource.getFullPath())) { 194 return true; 195 } 196 } 197 } 198 return false; 199 } 200 201 204 protected boolean getCacheFileContentsHint() { 205 return true; 206 } 207 208 public CVSTag getTag() { 209 return tree.getTag(ResourcesPlugin.getWorkspace().getRoot()); 210 } 211 212 215 public boolean equals(Object other) { 216 if(this == other) return true; 217 if(! (other instanceof CVSCompareSubscriber)) return false; 218 CVSCompareSubscriber s = (CVSCompareSubscriber)other; 219 CVSResourceVariantTree tree1 = (CVSResourceVariantTree)getRemoteTree(); 220 CVSResourceVariantTree tree2 = (CVSResourceVariantTree)s.getRemoteTree(); 221 IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot(); 222 CVSTag tag1 = tree1.getTag(root); 223 CVSTag tag2 = tree2.getTag(root); 224 if (tag1 == null || tag2 == null) return false; 225 return tag1.equals(tag2) && rootsEqual(s); 226 } 227 228 234 public void primeRemoteTree() throws CVSException { 235 for (int i = 0; i < resources.length; i++) { 236 IResource resource = resources[i]; 237 ICVSResource cvsResource = CVSWorkspaceRoot.getCVSResourceFor(resource); 238 cvsResource.accept(new ICVSResourceVisitor() { 239 public void visitFile(ICVSFile file) throws CVSException { 240 byte[] bytes = file.getSyncBytes(); 241 if (bytes != null) { 242 try { 243 tree.getByteStore().setBytes(file.getIResource(), bytes); 244 } catch (TeamException e) { 245 throw CVSException.wrapException(e); 246 } 247 } 248 } 249 public void visitFolder(ICVSFolder folder) throws CVSException { 250 folder.acceptChildren(this); 254 } 255 }); 256 } 257 } 258 259 266 public CVSTag getTag(IResource root) { 267 return tree.getTag(root); 268 } 269 270 275 public boolean isMultipleTagComparison() { 276 return getTag() == null; 277 } 278 279 } 280 | Popular Tags |