1 11 package org.eclipse.team.internal.ccvs.ui.operations; 12 13 import java.lang.reflect.InvocationTargetException ; 14 import java.util.ArrayList ; 15 import java.util.Arrays ; 16 import java.util.HashMap ; 17 import java.util.Iterator ; 18 import java.util.List ; 19 import java.util.Map ; 20 import java.util.Set ; 21 22 import org.eclipse.core.resources.IProject; 23 import org.eclipse.core.resources.IResource; 24 import org.eclipse.core.resources.mapping.*; 25 import org.eclipse.core.runtime.*; 26 import org.eclipse.core.runtime.jobs.ISchedulingRule; 27 import org.eclipse.jface.action.IAction; 28 import org.eclipse.team.core.RepositoryProvider; 29 import org.eclipse.team.core.TeamException; 30 import org.eclipse.team.core.mapping.ISynchronizationScope; 31 import org.eclipse.team.core.mapping.ISynchronizationScopeManager; 32 import org.eclipse.team.core.mapping.provider.SynchronizationScopeManager; 33 import org.eclipse.team.internal.ccvs.core.*; 34 import org.eclipse.team.internal.ccvs.core.client.Command; 35 import org.eclipse.team.internal.ccvs.core.client.Session; 36 import org.eclipse.team.internal.ccvs.core.client.Command.LocalOption; 37 import org.eclipse.team.internal.ccvs.core.resources.CVSWorkspaceRoot; 38 import org.eclipse.team.internal.ccvs.ui.CVSUIPlugin; 39 import org.eclipse.team.internal.ccvs.ui.Policy; 40 import org.eclipse.team.internal.ui.mapping.BuildScopeOperation; 41 import org.eclipse.ui.IWorkbenchPart; 42 43 46 public abstract class RepositoryProviderOperation extends CVSOperation { 47 48 53 public static boolean consultModelsWhenBuildingScope = true; 54 55 private ISynchronizationScopeManager manager; 56 private final ResourceMapping[] selectedMappings; 57 58 63 public interface ICVSTraversal { 64 IResource[] getShallowResources(); 65 IResource[] getDeepResources(); 66 IResource[] getNontraversedFolders(); 67 } 68 69 73 private static class TraversalMapEntry implements ICVSTraversal { 74 RepositoryProvider provider; 76 List files = new ArrayList (); 78 List zeroFolders = new ArrayList (); 80 List shallowFolders = new ArrayList (); 82 List deepFolders = new ArrayList (); 84 public TraversalMapEntry(RepositoryProvider provider) { 85 this.provider = provider; 86 } 87 91 public void add(ResourceTraversal[] traversals) { 92 for (int i = 0; i < traversals.length; i++) { 93 ResourceTraversal traversal = traversals[i]; 94 add(traversal); 95 } 96 } 97 101 public void add(ResourceTraversal traversal) { 102 IResource[] resources = traversal.getResources(); 103 for (int i = 0; i < resources.length; i++) { 104 IResource resource = resources[i]; 105 if (resource.getProject().equals(provider.getProject())) { 106 if (resource.getType() == IResource.FILE) { 107 files.add(resource); 108 } else { 109 switch (traversal.getDepth()) { 110 case IResource.DEPTH_ZERO: 111 zeroFolders.add(resource); 112 break; 113 case IResource.DEPTH_ONE: 114 shallowFolders.add(resource); 115 break; 116 case IResource.DEPTH_INFINITE: 117 deepFolders.add(resource); 118 break; 119 default: 120 deepFolders.add(resource); 121 } 122 } 123 } 124 } 125 } 126 132 public IResource[] getShallowResources() { 133 if (shallowFolders.isEmpty() && deepFolders.isEmpty() && !files.isEmpty()) { 134 return (IResource[]) files.toArray(new IResource[files.size()]); 135 } 136 if (!shallowFolders.isEmpty()) { 137 if (files.isEmpty()) { 138 return (IResource[]) shallowFolders.toArray(new IResource[shallowFolders.size()]); 139 } 140 List result = new ArrayList (); 141 result.addAll(shallowFolders); 142 result.addAll(files); 143 return (IResource[]) result.toArray(new IResource[result.size()]); 144 } 145 return new IResource[0]; 146 } 147 152 public IResource[] getDeepResources() { 153 if (deepFolders.isEmpty()) 154 return new IResource[0]; 155 if (!shallowFolders.isEmpty()) 156 return (IResource[]) deepFolders.toArray(new IResource[deepFolders.size()]); 157 List result = new ArrayList (); 158 result.addAll(deepFolders); 159 result.addAll(files); 160 return (IResource[]) result.toArray(new IResource[result.size()]); 161 } 162 165 public IResource[] getNontraversedFolders() { 166 return (IResource[]) zeroFolders.toArray(new IResource[zeroFolders.size()]); 167 } 168 } 169 170 171 178 public static ResourceMapping[] asResourceMappers(final IResource[] resources) { 179 return asResourceMappers(resources, IResource.DEPTH_INFINITE); 180 } 181 182 189 public static ResourceMapping[] asResourceMappers(final IResource[] resources, int depth) { 190 return WorkspaceResourceMapper.asResourceMappers(resources, depth); 191 } 192 193 public RepositoryProviderOperation(IWorkbenchPart part, final IResource[] resources) { 194 this(part, asResourceMappers(resources)); 195 } 196 197 public RepositoryProviderOperation(IWorkbenchPart part, ResourceMapping[] selectedMappings) { 198 super(part); 199 this.selectedMappings = selectedMappings; 200 } 201 202 205 public void execute(IProgressMonitor monitor) throws CVSException, InterruptedException { 206 try { 207 monitor.beginTask(null, 100); 208 buildScope(monitor); 209 Map table = getProviderTraversalMapping(Policy.subMonitorFor(monitor, 30)); 210 execute(table, Policy.subMonitorFor(monitor, 30)); 211 } catch (CoreException e) { 212 throw CVSException.wrapException(e); 213 } finally { 214 monitor.done(); 215 } 216 } 217 218 protected void endOperation() throws CVSException { 219 if (manager != null) { 220 manager.dispose(); 221 manager = null; 222 } 223 super.endOperation(); 224 } 225 226 public ISynchronizationScope buildScope(IProgressMonitor monitor) throws InterruptedException , CVSException { 227 if (manager == null) { 228 manager = createScopeManager(consultModelsWhenBuildingScope && consultModelsForMappings()); 229 BuildScopeOperation op = new BuildScopeOperation(getPart(), manager); 230 try { 231 op.run(monitor); 232 } catch (InvocationTargetException e) { 233 throw CVSException.wrapException(e); 234 } 235 } 236 return manager.getScope(); 237 } 238 239 244 protected SynchronizationScopeManager createScopeManager(boolean consultModels) { 245 return new SynchronizationScopeManager(getJobName(), getSelectedMappings(), getResourceMappingContext(), consultModels); 246 } 247 248 private void execute(Map providerTraversal, IProgressMonitor monitor) throws CVSException, InterruptedException { 249 Set keySet = providerTraversal.keySet(); 250 monitor.beginTask(null, keySet.size() * 1000); 251 Iterator iterator = keySet.iterator(); 252 while (iterator.hasNext()) { 253 CVSTeamProvider provider = (CVSTeamProvider)iterator.next(); 254 monitor.setTaskName(getTaskName(provider)); 255 TraversalMapEntry entry = (TraversalMapEntry)providerTraversal.get(provider); 256 execute(provider, entry, Policy.subMonitorFor(monitor, 1000)); 257 } 258 } 259 260 268 protected void execute(CVSTeamProvider provider, ICVSTraversal entry, IProgressMonitor monitor) throws CVSException, InterruptedException { 269 IResource[] deepResources = entry.getDeepResources(); 270 IResource[] shallowResources = entry.getShallowResources(); 271 IResource[] nontraversedFolders = entry.getNontraversedFolders(); 272 try { 273 monitor.beginTask(getTaskName(provider), (deepResources.length > 0 ? 100 : 0) + (shallowResources.length > 0 ? 100 : 0) + (nontraversedFolders.length > 0 ? 10 : 0)); 274 if (deepResources.length == 0 && shallowResources.length == 0 && nontraversedFolders.length == 0) 275 return; 276 final ISchedulingRule rule = getSchedulingRule(provider); 277 try { 278 Platform.getJobManager().beginRule(rule, monitor); 279 if (deepResources.length > 0) 280 execute(provider, deepResources, true , Policy.subMonitorFor(monitor, 100)); 281 if (shallowResources.length > 0) 282 execute(provider, shallowResources, false , Policy.subMonitorFor(monitor, 100)); 283 if (nontraversedFolders.length > 0) { 284 handleNontraversedFolders(provider, nontraversedFolders, Policy.subMonitorFor(monitor, 10)); 285 } 286 } finally { 287 Platform.getJobManager().endRule(rule); 288 } 289 } finally { 290 monitor.done(); 291 } 292 } 293 294 300 protected void handleNontraversedFolders(CVSTeamProvider provider, IResource[] nontraversedFolders, IProgressMonitor monitor) throws CVSException { 301 } 303 304 310 protected abstract String getTaskName(CVSTeamProvider provider); 311 312 319 protected ISchedulingRule getSchedulingRule(CVSTeamProvider provider) { 320 return provider.getProject(); 321 } 322 323 327 private Map getProviderTraversalMapping(IProgressMonitor monitor) throws CoreException { 328 Map result = new HashMap (); 329 ResourceMapping[] mappings = getScope().getMappings(); 330 for (int j = 0; j < mappings.length; j++) { 331 ResourceMapping mapping = mappings[j]; 332 IProject[] projects = mapping.getProjects(); 333 ResourceTraversal[] traversals = getScope().getTraversals(mapping); 334 for (int k = 0; k < projects.length; k++) { 335 IProject project = projects[k]; 336 RepositoryProvider provider = RepositoryProvider.getProvider(project, CVSProviderPlugin.getTypeId()); 337 if (provider != null) { 338 TraversalMapEntry entry = (TraversalMapEntry)result.get(provider); 339 if (entry == null) { 340 entry = new TraversalMapEntry(provider); 341 result.put(provider, entry); 342 } 343 entry.add(traversals); 344 } 345 } 346 } 347 return result; 348 } 349 350 356 protected ResourceMappingContext getResourceMappingContext() { 357 return ResourceMappingContext.LOCAL_CONTEXT; 358 } 359 360 369 protected abstract void execute(CVSTeamProvider provider, IResource[] resources, boolean recurse, IProgressMonitor monitor) throws CVSException, InterruptedException ; 370 371 377 protected LocalOption[] getLocalOptions(boolean recurse) { 378 if (!recurse) { 379 return new LocalOption[] { Command.DO_NOT_RECURSE }; 380 } 381 return Command.NO_LOCAL_OPTIONS; 382 } 383 384 protected ICVSResource[] getCVSArguments(IResource[] resources) { 385 ICVSResource[] cvsResources = new ICVSResource[resources.length]; 386 for (int i = 0; i < cvsResources.length; i++) { 387 cvsResources[i] = CVSWorkspaceRoot.getCVSResourceFor(resources[i]); 388 } 389 return cvsResources; 390 } 391 392 395 protected String [] getStringArguments(IResource[] resources) throws CVSException { 396 List arguments = new ArrayList (resources.length); 397 for (int i=0;i<resources.length;i++) { 398 IPath cvsPath = resources[i].getFullPath().removeFirstSegments(1); 399 if (cvsPath.segmentCount() == 0) { 400 arguments.add(Session.CURRENT_LOCAL_FOLDER); 401 } else { 402 arguments.add(cvsPath.toString()); 403 } 404 } 405 return (String [])arguments.toArray(new String [arguments.size()]); 406 } 407 408 protected ICVSRepositoryLocation getRemoteLocation(CVSTeamProvider provider) throws CVSException { 409 CVSWorkspaceRoot workspaceRoot = provider.getCVSWorkspaceRoot(); 410 return workspaceRoot.getRemoteLocation(); 411 } 412 413 protected ICVSFolder getLocalRoot(CVSTeamProvider provider) throws CVSException { 414 CVSWorkspaceRoot workspaceRoot = provider.getCVSWorkspaceRoot(); 415 return workspaceRoot.getLocalRoot(); 416 } 417 418 429 protected void updateWorkspaceSubscriber(CVSTeamProvider provider, ICVSResource[] resources, boolean recurse, IProgressMonitor monitor) { 430 CVSWorkspaceSubscriber s = CVSProviderPlugin.getPlugin().getCVSWorkspaceSubscriber(); 431 monitor.beginTask(null, 100 * resources.length); 432 for (int i = 0; i < resources.length; i++) { 433 ICVSResource resource = resources[i]; 434 if (resource.isFolder()) { 435 try { 436 s.updateRemote(provider, (ICVSFolder)resource, recurse, Policy.subMonitorFor(monitor, 100)); 437 } catch (TeamException e) { 438 CVSUIPlugin.log(e); 440 } 441 } else { 442 monitor.worked(100); 443 } 444 } 445 } 446 447 450 public boolean isKeepOneProgressServiceEntry() { 451 return true; 453 } 454 455 458 protected IAction getGotoAction() { 459 return getShowConsoleAction(); 460 } 461 462 468 protected IResource[] getTraversalRoots() { 469 List result = new ArrayList (); 470 ResourceTraversal[] traversals = getTraversals(); 471 for (int i = 0; i < traversals.length; i++) { 472 ResourceTraversal traversal = traversals[i]; 473 result.addAll(Arrays.asList(traversal.getResources())); 474 } 475 return (IResource[]) result.toArray(new IResource[result.size()]); 476 } 477 478 484 public ResourceTraversal[] getTraversals() { 485 return getScope().getTraversals(); 486 } 487 488 public boolean consultModelsForMappings() { 489 return true; 490 } 491 492 public ResourceMapping[] getSelectedMappings() { 493 return selectedMappings; 494 } 495 496 public ISynchronizationScope getScope() { 497 return manager.getScope(); 498 } 499 500 public ISynchronizationScopeManager getScopeManager() { 501 return manager; 502 } 503 } 504 | Popular Tags |