1 11 package org.eclipse.team.ui.synchronize; 12 13 import java.lang.reflect.InvocationTargetException ; 14 import java.util.ArrayList ; 15 import java.util.Arrays ; 16 import java.util.HashSet ; 17 import java.util.List ; 18 import java.util.Set ; 19 20 import org.eclipse.core.resources.IResource; 21 import org.eclipse.core.resources.mapping.*; 22 import org.eclipse.core.runtime.*; 23 import org.eclipse.jface.window.Window; 24 import org.eclipse.swt.widgets.Display; 25 import org.eclipse.team.core.mapping.*; 26 import org.eclipse.team.internal.core.Policy; 27 import org.eclipse.team.internal.core.mapping.CompoundResourceTraversal; 28 import org.eclipse.team.internal.ui.TeamUIMessages; 29 import org.eclipse.team.internal.ui.dialogs.AdditionalMappingsDialog; 30 import org.eclipse.team.ui.TeamOperation; 31 import org.eclipse.ui.IWorkbenchPart; 32 33 42 public abstract class ModelOperation extends TeamOperation { 43 44 private boolean previewRequested; 45 private ISynchronizationScopeManager manager; 46 47 55 public static ModelProvider[] sortByExtension(ModelProvider[] providers) { 56 List result = new ArrayList (); 57 for (int i = 0; i < providers.length; i++) { 58 ModelProvider providerToInsert = providers[i]; 59 int index = result.size(); 60 for (int j = 0; j < result.size(); j++) { 61 ModelProvider provider = (ModelProvider) result.get(j); 62 if (extendsProvider(providerToInsert, provider)) { 63 index = j; 64 break; 65 } 66 } 67 result.add(index, providerToInsert); 68 } 69 return (ModelProvider[]) result.toArray(new ModelProvider[result.size()]); 70 } 71 72 private static boolean extendsProvider(ModelProvider providerToInsert, ModelProvider provider) { 73 String [] extended = providerToInsert.getDescriptor().getExtendedModels(); 74 for (int i = 0; i < extended.length; i++) { 76 String id = extended[i]; 77 if (id.equals(provider.getDescriptor().getId())) { 78 return true; 79 } 80 } 81 return false; 82 } 83 84 89 protected ModelOperation(IWorkbenchPart part, ISynchronizationScopeManager manager) { 90 super(part); 91 this.manager = manager; 92 } 93 94 101 public final void run(IProgressMonitor monitor) throws InvocationTargetException , 102 InterruptedException { 103 try { 104 monitor.beginTask(null, 100); 105 beginOperation(Policy.subMonitorFor(monitor, 5)); 106 execute(Policy.subMonitorFor(monitor, 90)); 107 } finally { 108 endOperation(Policy.subMonitorFor(monitor, 5)); 109 monitor.done(); 110 } 111 } 112 113 123 protected void beginOperation(IProgressMonitor monitor) throws InvocationTargetException { 124 initializeScope(monitor); 125 } 126 127 135 protected void endOperation(IProgressMonitor monitor) throws InvocationTargetException { 136 } 138 139 158 protected final void initializeScope(IProgressMonitor monitor) throws InvocationTargetException { 159 try { 160 if (!manager.isInitialized()) { 161 manager.initialize(monitor); 162 promptIfInputChange(monitor); 163 } 164 } catch (CoreException e) { 165 throw new InvocationTargetException (e); 166 } 167 } 168 169 175 protected void promptIfInputChange(IProgressMonitor monitor) { 176 ISynchronizationScope inputScope = getScope().asInputScope(); 177 if (getScope().hasAdditionalMappings()) { 178 boolean prompt = false; 179 ModelProvider[] inputModelProviders = inputScope.getModelProviders(); 181 if (hasAdditionalMappingsFromIndependantModel(inputModelProviders, getScope().getModelProviders())) { 182 prompt = true; 185 } else if (getScope().hasAdditonalResources()) { 186 prompt = true; 188 } else if (inputModelProviders.length == 1) { 189 String modelProviderId = inputModelProviders[0].getDescriptor().getId(); 193 ResourceMapping[] mappings = getScope().getMappings(); 194 for (int i = 0; i < mappings.length; i++) { 195 ResourceMapping mapping = mappings[i]; 196 if (inputScope.getTraversals(mapping) == null) { 197 String id = mapping.getModelProviderId(); 199 if (id.equals(modelProviderId) && !modelProviderId.equals(ModelProvider.RESOURCE_MODEL_PROVIDER_ID)) { 200 prompt = true; 201 break; 202 } else if (isIndependantModel(modelProviderId, id)) { 203 prompt = true; 204 break; 205 } 206 } 207 } 208 } else { 209 for (int i = 0; i < inputModelProviders.length; i++) { 212 ModelProvider provider = inputModelProviders[i]; 213 String id = provider.getDescriptor().getId(); 214 ResourceMapping[] inputMappings = inputScope.getMappings(id); 215 ResourceMapping[] scopeMappings = getScope().getMappings(id); 216 if (inputMappings.length != scopeMappings.length) { 217 for (int j = 0; j < scopeMappings.length; j++) { 220 ResourceMapping mapping = scopeMappings[j]; 221 ResourceTraversal[] inputTraversals = inputScope.getTraversals(mapping); 222 if (inputTraversals == null) { 223 ResourceTraversal[] scopeTraversals = getScope().getTraversals(mapping); 228 ResourceTraversal[] inputModelTraversals = getTraversals(inputScope, inputMappings); 229 if (overlaps(scopeTraversals, inputModelTraversals)) { 230 prompt = true; 231 break; 232 } 233 } 234 } 235 } 236 } 237 } 238 if (prompt) { 239 String previewMessage = getPreviewRequestMessage(); 240 previewRequested = promptForInputChange(previewMessage, monitor); 241 } 242 } 243 } 244 245 252 protected String getPreviewRequestMessage() { 253 return null; 254 } 255 256 private boolean hasAdditionalMappingsFromIndependantModel(ModelProvider[] inputModelProviders, ModelProvider[] modelProviders) { 257 ModelProvider[] additionalProviders = getAdditionalProviders(inputModelProviders, modelProviders); 258 for (int i = 0; i < additionalProviders.length; i++) { 259 ModelProvider additionalProvider = additionalProviders[i]; 260 boolean independant = true; 261 for (int j = 0; j < inputModelProviders.length; j++) { 263 ModelProvider inputProvider = inputModelProviders[j]; 264 if (!isIndependantModel(additionalProvider.getDescriptor().getId(), inputProvider.getDescriptor().getId())) { 265 independant = false; 266 } 267 } 268 if (independant) 269 return true; 270 } 271 return false; 272 } 273 274 private ModelProvider[] getAdditionalProviders(ModelProvider[] inputModelProviders, ModelProvider[] modelProviders) { 275 Set input = new HashSet (); 276 List result = new ArrayList (); 277 input.addAll(Arrays.asList(inputModelProviders)); 278 for (int i = 0; i < modelProviders.length; i++) { 279 ModelProvider provider = modelProviders[i]; 280 if (!input.contains(provider)) 281 result.add(provider); 282 } 283 return (ModelProvider[]) result.toArray(new ModelProvider[result.size()]); 284 } 285 286 private boolean overlaps(ResourceTraversal[] scopeTraversals, ResourceTraversal[] inputModelTraversals) { 287 for (int i = 0; i < inputModelTraversals.length; i++) { 288 ResourceTraversal inputTraversal = inputModelTraversals[i]; 289 for (int j = 0; j < scopeTraversals.length; j++) { 290 ResourceTraversal scopeTraversal = scopeTraversals[j]; 291 if (overlaps(inputTraversal, scopeTraversal)) { 292 return true; 293 } 294 } 295 } 296 return false; 297 } 298 299 private boolean overlaps(ResourceTraversal inputTraversal, ResourceTraversal scopeTraversal) { 300 IResource[] inputRoots = inputTraversal.getResources(); 301 IResource[] scopeRoots = scopeTraversal.getResources(); 302 for (int i = 0; i < scopeRoots.length; i++) { 303 IResource scopeResource = scopeRoots[i]; 304 for (int j = 0; j < inputRoots.length; j++) { 305 IResource inputResource = inputRoots[j]; 306 if (overlaps(scopeResource, scopeTraversal.getDepth(), inputResource, inputTraversal.getDepth())) 307 return true; 308 } 309 } 310 return false; 311 } 312 313 private boolean overlaps(IResource scopeResource, int scopeDepth, IResource inputResource, int inputDepth) { 314 if (scopeResource.equals(inputResource)) 315 return true; 316 if (scopeDepth == IResource.DEPTH_INFINITE && scopeResource.getFullPath().isPrefixOf(inputResource.getFullPath())) { 317 return true; 318 } 319 if (scopeDepth == IResource.DEPTH_ONE && scopeResource.equals(inputResource.getParent())) { 320 return true; 321 } 322 if (inputDepth == IResource.DEPTH_INFINITE && inputResource.getFullPath().isPrefixOf(scopeResource.getFullPath())) { 323 return true; 324 } 325 if (inputDepth == IResource.DEPTH_ONE && inputResource.equals(scopeResource.getParent())) { 326 return true; 327 } 328 return false; 329 } 330 331 private ResourceTraversal[] getTraversals(ISynchronizationScope inputScope, ResourceMapping[] inputMappings) { 332 CompoundResourceTraversal result = new CompoundResourceTraversal(); 333 for (int i = 0; i < inputMappings.length; i++) { 334 ResourceMapping mapping = inputMappings[i]; 335 result.addTraversals(inputScope.getTraversals(mapping)); 336 } 337 return result.asTraversals(); 338 } 339 340 private boolean isIndependantModel(String modelProviderId, String id) { 341 if (id.equals(modelProviderId)) 342 return false; 343 IModelProviderDescriptor desc1 = ModelProvider.getModelProviderDescriptor(modelProviderId); 344 IModelProviderDescriptor desc2 = ModelProvider.getModelProviderDescriptor(id); 345 return !(isExtension(desc1, desc2) || isExtension(desc2, desc1)); 346 } 347 348 351 private boolean isExtension(IModelProviderDescriptor desc1, IModelProviderDescriptor desc2) { 352 String [] ids = desc1.getExtendedModels(); 353 for (int i = 0; i < ids.length; i++) { 355 String id = ids[i]; 356 if (id.equals(desc2.getId())) { 357 return true; 358 } 359 } 360 for (int i = 0; i < ids.length; i++) { 362 String id = ids[i]; 363 IModelProviderDescriptor desc3 = ModelProvider.getModelProviderDescriptor(id); 364 if (isExtension(desc3, desc2)) { 365 return true; 366 } 367 } 368 return false; 369 } 370 371 380 protected boolean promptForInputChange(String requestPreviewMessage, IProgressMonitor monitor) { 381 return showAllMappings(requestPreviewMessage); 382 } 383 384 private boolean showAllMappings(final String requestPreviewMessage) { 385 final boolean[] canceled = new boolean[] { false }; 386 final boolean[] forcePreview = new boolean[] { false }; 387 Display.getDefault().syncExec(new Runnable () { 388 public void run() { 389 AdditionalMappingsDialog dialog = new AdditionalMappingsDialog(getShell(), TeamUIMessages.ResourceMappingOperation_0, getScope(), getContext()); 390 dialog.setPreviewMessage(requestPreviewMessage); 391 int result = dialog.open(); 392 canceled[0] = result != Window.OK; 393 if (requestPreviewMessage != null) { 394 forcePreview[0] = dialog.isForcePreview(); 395 } 396 } 397 }); 398 399 if (canceled[0]) { 400 throw new OperationCanceledException(); 401 } 402 return forcePreview[0]; 403 } 404 405 411 protected ISynchronizationContext getContext() { 412 return null; 413 } 414 415 422 protected abstract void execute(IProgressMonitor monitor) throws InvocationTargetException , 423 InterruptedException ; 424 425 429 public ISynchronizationScope getScope() { 430 return manager.getScope(); 431 } 432 433 439 public boolean isPreviewRequested() { 440 return previewRequested; 441 } 442 443 447 protected ISynchronizationScopeManager getScopeManager() { 448 return manager; 449 } 450 451 } 452 | Popular Tags |