1 11 package org.eclipse.team.internal.ccvs.ui.actions; 12 13 import java.lang.reflect.InvocationTargetException ; 14 import java.util.ArrayList ; 15 import java.util.HashSet ; 16 import java.util.Hashtable ; 17 import java.util.Iterator ; 18 import java.util.List ; 19 import java.util.Set ; 20 21 import org.eclipse.core.resources.*; 22 import org.eclipse.core.runtime.*; 23 import org.eclipse.jface.action.IAction; 24 import org.eclipse.jface.dialogs.MessageDialog; 25 import org.eclipse.jface.operation.IRunnableWithProgress; 26 import org.eclipse.osgi.util.NLS; 27 import org.eclipse.swt.widgets.Display; 28 import org.eclipse.swt.widgets.Shell; 29 import org.eclipse.team.core.RepositoryProvider; 30 import org.eclipse.team.core.TeamException; 31 import org.eclipse.team.internal.ccvs.core.*; 32 import org.eclipse.team.internal.ccvs.core.resources.CVSWorkspaceRoot; 33 import org.eclipse.team.internal.ccvs.core.resources.EclipseSynchronizer; 34 import org.eclipse.team.internal.ccvs.core.syncinfo.FolderSyncInfo; 35 import org.eclipse.team.internal.ccvs.core.syncinfo.ResourceSyncInfo; 36 import org.eclipse.team.internal.ccvs.core.util.Util; 37 import org.eclipse.team.internal.ccvs.ui.CVSUIMessages; 38 import org.eclipse.team.internal.ccvs.ui.Policy; 39 import org.eclipse.team.internal.ui.actions.TeamAction; 40 import org.eclipse.team.internal.ui.dialogs.IPromptCondition; 41 import org.eclipse.team.internal.ui.dialogs.PromptingDialog; 42 43 46 public abstract class WorkspaceAction extends CVSAction { 47 48 public interface IProviderAction { 49 public IStatus execute(CVSTeamProvider provider, IResource[] resources, IProgressMonitor monitor) throws CVSException; 50 } 51 52 55 protected boolean beginExecution(IAction action) throws TeamException { 56 if (super.beginExecution(action)) { 57 if (requiresLocalSyncInfo()) { 59 handleOrphanedSubtrees(); 63 if (!isEnabled()) { 65 MessageDialog.openInformation(getShell(), CVSUIMessages.CVSAction_disabledTitle, CVSUIMessages.CVSAction_disabledMessage); return false; 67 } 68 } 69 return true; 70 } else { 71 return false; 72 } 73 } 74 75 79 private boolean handleOrphanedSubtrees() { 80 IResource[] resources = getSelectedResources(); 82 for (int i = 0; i < resources.length; i++) { 83 IResource resource = resources[i]; 84 handleOrphanedSubtree(resource); 85 } 86 return false; 87 } 88 89 93 private void handleOrphanedSubtree(IResource resource) { 94 try { 95 if (!CVSWorkspaceRoot.isSharedWithCVS(resource)) return ; 96 ICVSFolder folder; 97 if (resource.getType() == IResource.FILE) { 98 folder = CVSWorkspaceRoot.getCVSFolderFor(resource.getParent()); 99 } else { 100 folder = CVSWorkspaceRoot.getCVSFolderFor((IContainer)resource); 101 } 102 handleOrphanedSubtree(folder); 103 } catch (CVSException e) { 104 CVSProviderPlugin.log(e); 105 } 106 } 107 108 111 private void handleOrphanedSubtree(final ICVSFolder folder) throws CVSException { 112 if (folder.getIResource().getType() == IResource.PROJECT) return; 113 if (CVSWorkspaceRoot.isOrphanedSubtree((IContainer)folder.getIResource())) { 114 try { 115 run(new IRunnableWithProgress() { 116 public void run(IProgressMonitor monitor) throws InvocationTargetException , InterruptedException { 117 try { 118 folder.unmanage(null); 119 } catch (CVSException e) { 120 CVSProviderPlugin.log(e); 121 } 122 } 123 }, true, PROGRESS_BUSYCURSOR); 124 } catch (InvocationTargetException e) { 125 } catch (InterruptedException e) { 127 throw new OperationCanceledException(); 128 } 129 } 130 handleOrphanedSubtree(folder.getParent()); 131 } 132 133 140 protected boolean isSyncInfoLoaded(IResource[] resources) throws CVSException { 141 return EclipseSynchronizer.getInstance().isSyncInfoLoaded(resources, getEnablementDepth()); 142 } 143 144 149 protected int getActionDepth() { 150 return IResource.DEPTH_INFINITE; 151 } 152 153 158 protected int getEnablementDepth() { 159 return IResource.DEPTH_ZERO; 160 } 161 162 166 protected boolean ensureSyncInfoLoaded(IResource[] resources) throws CVSException { 167 boolean keepTrying = true; 168 while (keepTrying) { 169 try { 170 EclipseSynchronizer.getInstance().ensureSyncInfoLoaded(resources, getActionDepth()); 171 keepTrying = false; 172 } catch (CVSException e) { 173 if (e.getStatus().getCode() == IResourceStatus.OUT_OF_SYNC_LOCAL) { 174 Set projects = new HashSet (); 176 for (int i = 0; i < resources.length; i++) { 177 IResource resource = resources[i]; 178 projects.add(resource.getProject()); 179 } 180 if (promptToRefresh(getShell(), (IResource[]) projects.toArray(new IResource[projects.size()]), e.getStatus())) { 182 for (Iterator iter = projects.iterator();iter.hasNext();) { 183 IProject project = (IProject) iter.next(); 184 try { 185 project.refreshLocal(IResource.DEPTH_INFINITE, null); 186 } catch (CoreException coreException) { 187 throw CVSException.wrapException(coreException); 188 } 189 } 190 } else { 191 return false; 192 } 193 } else { 194 throw e; 195 } 196 } 197 } 198 return true; 199 } 200 201 207 protected void setActionEnablement(IAction action) { 208 try { 209 boolean requires = requiresLocalSyncInfo(); 210 if (!requires || (requires && isSyncInfoLoaded(getSelectedResources()))) { 211 super.setActionEnablement(action); 212 } else { 213 action.setEnabled(true); 217 } 218 } catch (CVSException e) { 219 action.setEnabled(true); 223 } 224 } 225 226 238 protected boolean requiresLocalSyncInfo() { 239 return true; 240 } 241 242 protected boolean promptToRefresh(final Shell shell, final IResource[] resources, final IStatus status) { 243 final boolean[] result = new boolean[] { false}; 244 Runnable runnable = new Runnable () { 245 public void run() { 246 Shell shellToUse = shell; 247 if (shell == null) { 248 shellToUse = new Shell(Display.getCurrent()); 249 } 250 String question; 251 if (resources.length == 1) { 252 question = NLS.bind(CVSUIMessages.CVSAction_refreshQuestion, new String [] { status.getMessage(), resources[0].getFullPath().toString() }); 253 } else { 254 question = NLS.bind(CVSUIMessages.CVSAction_refreshMultipleQuestion, new String [] { status.getMessage() }); 255 } 256 result[0] = MessageDialog.openQuestion(shellToUse, CVSUIMessages.CVSAction_refreshTitle, question); 257 } 258 }; 259 Display.getDefault().syncExec(runnable); 260 return result[0]; 261 } 262 263 268 protected boolean needsToSaveDirtyEditors() { 269 return true; 270 } 271 272 288 public boolean isEnabled() { 289 290 boolean enabled = super.isEnabled(); 292 if(enabled) return true; 293 294 IResource[] resources = getSelectedResourcesWithOverlap(); 296 297 if(resources.length==0) return false; 299 300 if (!isEnabledForMultipleResources() && resources.length != 1) return false; 302 303 List folderPaths = new ArrayList (); 305 List filePaths = new ArrayList (); 306 for (int i = 0; i < resources.length; i++) { 307 IResource resource = resources[i]; 308 309 if(resource.getType() == IResource.PROJECT) { 311 if (! resource.isAccessible()) return false; 312 } 313 314 if (CVSWorkspaceRoot.isLinkedResource(resource)) return false; 316 317 if(RepositoryProvider.getProvider(resource.getProject(), CVSProviderPlugin.getTypeId()) == null) { 319 return false; 320 } 321 322 IPath resourceFullPath = resource.getFullPath(); 324 if(resource.getType() == IResource.FILE) { 325 filePaths.add(resourceFullPath); 326 } else { 327 folderPaths.add(resourceFullPath); 328 } 329 330 ICVSResource cvsResource = getCVSResourceFor(resource); 332 try { 333 if (!isEnabledForCVSResource(cvsResource)) { 334 return false; 335 } 336 } catch (CVSException e) { 337 if (!isEnabledForException(e)) 338 return false; 339 } 340 } 341 if(!folderPaths.isEmpty()) { 344 for (Iterator fileIter = filePaths.iterator(); fileIter.hasNext();) { 345 IPath resourcePath = (IPath) fileIter.next(); 346 for (Iterator it = folderPaths.iterator(); it.hasNext();) { 347 IPath folderPath = (IPath) it.next(); 348 if (folderPath.isPrefixOf(resourcePath)) { 349 return false; 350 } 351 } 352 } 353 } 354 return true; 355 } 356 357 362 protected boolean isEnabledForCVSResource(ICVSResource cvsResource) throws CVSException { 363 boolean managed = false; 364 boolean ignored = false; 365 boolean added = false; 366 if (cvsResource.isIgnored()) { 367 ignored = true; 368 } else if (cvsResource.isFolder()) { 369 managed = ((ICVSFolder)cvsResource).isCVSFolder(); 370 } else { 371 ResourceSyncInfo info = cvsResource.getSyncInfo(); 372 managed = info != null; 373 if (managed) added = info.isAdded(); 374 } 375 if (managed && ! isEnabledForManagedResources()) return false; 376 if ( ! managed && ! isEnabledForUnmanagedResources()) return false; 377 if ( ignored && ! isEnabledForIgnoredResources()) return false; 378 if (added && ! isEnabledForAddedResources()) return false; 379 if ( ! cvsResource.exists() && ! isEnabledForNonExistantResources()) return false; 380 return true; 381 } 382 383 387 protected boolean isEnabledForIgnoredResources() { 388 return false; 389 } 390 391 395 protected boolean isEnabledForUnmanagedResources() { 396 return false; 397 } 398 399 403 protected boolean isEnabledForManagedResources() { 404 return true; 405 } 406 407 411 protected boolean isEnabledForAddedResources() { 412 return true; 413 } 414 415 419 protected boolean isEnabledForMultipleResources() { 420 return true; 421 } 422 423 427 protected boolean isEnabledForNonExistantResources() { 428 return false; 429 } 430 431 protected void executeProviderAction(IProviderAction action, IResource[] resources, IProgressMonitor monitor) throws InvocationTargetException { 432 Hashtable table = getProviderMapping(resources); 433 Set keySet = table.keySet(); 434 monitor.beginTask(null, keySet.size() * 1000); 435 Iterator iterator = keySet.iterator(); 436 437 while (iterator.hasNext()) { 438 IProgressMonitor subMonitor = new SubProgressMonitor(monitor, 1000); 439 CVSTeamProvider provider = (CVSTeamProvider)iterator.next(); 440 List list = (List )table.get(provider); 441 IResource[] providerResources = (IResource[])list.toArray(new IResource[list.size()]); 442 try { 443 addStatus(action.execute(provider, providerResources, subMonitor)); 444 } catch (CVSException e) { 445 throw new InvocationTargetException (e); 446 } 447 448 } 449 } 450 451 protected void executeProviderAction(IProviderAction action, IProgressMonitor monitor) throws InvocationTargetException { 452 executeProviderAction(action, getSelectedResources(), monitor); 453 } 454 455 460 protected String calculateActionTagValue() { 461 try { 462 IResource[] resources = getSelectedResources(); 463 CVSTag commonTag = null; 464 boolean sameTagType = true; 465 boolean multipleSameNames = true; 466 467 for (int i = 0; i < resources.length; i++) { 468 ICVSResource cvsResource = getCVSResourceFor(resources[i]); 469 CVSTag tag = null; 470 if(cvsResource.isFolder()) { 471 FolderSyncInfo info = ((ICVSFolder)cvsResource).getFolderSyncInfo(); 472 if(info != null) { 473 tag = info.getTag(); 474 } 475 if (tag != null && tag.getType() == CVSTag.BRANCH) { 476 tag = Util.getAccurateFolderTag(resources[i], tag); 477 } 478 } else { 479 tag = Util.getAccurateFileTag(cvsResource); 480 } 481 if(tag == null) { 482 tag = new CVSTag(); 483 } 484 if(commonTag == null) { 485 commonTag = tag; 486 } else if(!commonTag.equals(tag)) { 487 if(commonTag.getType() != tag.getType()) { 488 sameTagType = false; 489 } 490 if(!commonTag.getName().equals(tag.getName())) { 491 multipleSameNames = false; 492 } 493 } 494 } 495 496 String actionText = CVSUIMessages.ReplaceWithLatestAction_multipleTags; 498 if(commonTag != null) { 499 int tagType = commonTag.getType(); 500 String tagName = commonTag.getName(); 501 if(sameTagType && !multipleSameNames) { 503 if(tagType == CVSTag.BRANCH) { 504 actionText = CVSUIMessages.ReplaceWithLatestAction_multipleBranches; } else { 506 actionText = CVSUIMessages.ReplaceWithLatestAction_multipleVersions; 507 } 508 } else if(sameTagType && multipleSameNames) { 510 if(tagType == CVSTag.BRANCH) { 511 actionText = NLS.bind(CVSUIMessages.ReplaceWithLatestAction_singleBranch, new String [] { tagName }); } else if(tagType == CVSTag.VERSION){ 513 actionText = NLS.bind(CVSUIMessages.ReplaceWithLatestAction_singleVersion, new String [] { tagName }); 514 } else if(tagType == CVSTag.HEAD) { 515 actionText = NLS.bind(CVSUIMessages.ReplaceWithLatestAction_singleHEAD, new String [] { tagName }); 516 } 517 } 518 } 519 520 return actionText; 521 } catch (CVSException e) { 522 return CVSUIMessages.ReplaceWithLatestAction_multipleTags; } 525 } 526 527 protected IResource[] checkOverwriteOfDirtyResources(IResource[] resources, IProgressMonitor monitor) throws CVSException, InterruptedException { 528 List dirtyResources = new ArrayList (); 529 IResource[] selectedResources = getSelectedResources(); 530 531 try { 532 monitor = Policy.monitorFor(monitor); 533 monitor.beginTask(null, selectedResources.length * 100); 534 monitor.setTaskName(CVSUIMessages.ReplaceWithAction_calculatingDirtyResources); 535 for (int i = 0; i < selectedResources.length; i++) { 536 IResource resource = selectedResources[i]; 537 ICVSResource cvsResource = getCVSResourceFor(resource); 538 if(cvsResource.isModified(Policy.subMonitorFor(monitor, 100))) { 539 dirtyResources.add(resource); 540 } 541 } 542 } finally { 543 monitor.done(); 544 } 545 546 PromptingDialog dialog = new PromptingDialog(getShell(), selectedResources, 547 getPromptCondition((IResource[]) dirtyResources.toArray(new IResource[dirtyResources.size()])), CVSUIMessages.ReplaceWithAction_confirmOverwrite); 548 return dialog.promptForMultiple(); 549 } 550 551 555 protected IPromptCondition getPromptCondition(IResource[] resources) { 556 return getOverwriteLocalChangesPrompt(resources); 557 } 558 } 559 | Popular Tags |