1 12 package org.eclipse.team.internal.ccvs.ui.operations; 13 14 import java.io.File ; 15 import java.io.IOException ; 16 import java.util.*; 17 18 import org.eclipse.core.resources.*; 19 import org.eclipse.core.runtime.*; 20 import org.eclipse.core.runtime.jobs.*; 21 import org.eclipse.osgi.util.NLS; 22 import org.eclipse.team.core.RepositoryProvider; 23 import org.eclipse.team.core.TeamException; 24 import org.eclipse.team.internal.ccvs.core.*; 25 import org.eclipse.team.internal.ccvs.core.client.*; 26 import org.eclipse.team.internal.ccvs.core.client.Command.LocalOption; 27 import org.eclipse.team.internal.ccvs.core.resources.CVSWorkspaceRoot; 28 import org.eclipse.team.internal.ccvs.core.resources.EclipseSynchronizer; 29 import org.eclipse.team.internal.ccvs.core.syncinfo.FolderSyncInfo; 30 import org.eclipse.team.internal.ccvs.ui.*; 31 import org.eclipse.team.internal.ccvs.ui.Policy; 32 import org.eclipse.team.internal.ui.wizards.WorkingSetsDialog; 33 import org.eclipse.ui.*; 34 38 public abstract class CheckoutProjectOperation extends CheckoutOperation { 39 40 private String targetLocation; 41 42 public CheckoutProjectOperation(IWorkbenchPart part, ICVSRemoteFolder[] remoteFolders, String targetLocation) { 43 super(part, remoteFolders); 44 this.targetLocation = targetLocation; 45 } 46 47 54 protected void createAndOpenProject(IProject project, IProgressMonitor monitor) throws CVSException { 55 try { 56 monitor.beginTask(null, 5); 57 IProjectDescription desc = getDescriptionFor(project); 58 if (project.exists()) { 59 if (desc != null) { 60 project.move(desc, true, Policy.subMonitorFor(monitor, 3)); 61 } 62 } else { 63 if (desc == null) { 64 project.create(Policy.subMonitorFor(monitor, 3)); 66 } else { 67 project.create(desc, Policy.subMonitorFor(monitor, 3)); 69 } 70 } 71 if (!project.isOpen()) { 72 project.open(Policy.subMonitorFor(monitor, 2)); 73 } 74 } catch (CoreException e) { 75 throw CVSException.wrapException(e); 76 } finally { 77 monitor.done(); 78 } 79 } 80 81 protected IProjectDescription getDescriptionFor(IProject project) { 82 if (targetLocation == null) return null; 83 String projectName = project.getName(); 84 IProjectDescription description = ResourcesPlugin.getWorkspace().newProjectDescription(projectName); 85 description.setLocation(getTargetLocationFor(project)); 86 return description; 87 } 88 89 95 protected IPath getTargetLocationFor(IProject project) { 96 if (targetLocation == null) return null; 97 return new Path(targetLocation); 98 } 99 100 protected String getRemoteModuleName(ICVSRemoteFolder resource) { 101 String moduleName; 102 if (resource.isDefinedModule()) { 103 moduleName = resource.getName(); 104 } else { 105 moduleName = resource.getRepositoryRelativePath(); 106 } 107 return moduleName; 108 } 109 110 protected IStatus checkout(final ICVSRemoteFolder resource, IProject project, IProgressMonitor pm) throws CVSException { 111 ICVSFolder root = CVSWorkspaceRoot.getCVSFolderFor(ResourcesPlugin.getWorkspace().getRoot()); 113 ICVSRepositoryLocation repository = resource.getRepository(); 114 final Session session = new Session(repository, root); 116 pm.beginTask(null, 100); 117 Policy.checkCanceled(pm); 118 session.open(Policy.subMonitorFor(pm, 5), false ); 119 try { 120 121 if (project == null && resource.getName().equals(".")) { String name = new Path(null, resource.getRepository().getRootDirectory()).lastSegment(); 125 project = ResourcesPlugin.getWorkspace().getRoot().getProject(name); 126 } 127 128 if (project == null 130 && CVSUIPlugin.getPlugin().isUseProjectNameOnCheckout() 131 && resource instanceof RemoteProjectFolder) { 132 RemoteProjectFolder rpf = (RemoteProjectFolder) resource; 133 if (rpf.hasProjectName()) 134 { 135 project = ResourcesPlugin.getWorkspace().getRoot().getProject(rpf.getProjectName()); 138 } 139 } 140 141 final IProject[] targetProjects = determineProjects(session, resource, project, Policy.subMonitorFor(pm, 5)); 144 if (targetProjects == null) { 145 return getLastError(); 147 } else if (targetProjects.length == 0) { 148 return OK; 149 } 150 151 final boolean sendModuleName = project != null; 152 final IStatus[] result = new IStatus[] { null }; 153 final ISchedulingRule schedulingRule = getSchedulingRule(targetProjects); 154 if (schedulingRule instanceof IResource && ((IResource)schedulingRule).getType() == IResource.ROOT) { 155 try { 158 Job.getJobManager().beginRule(schedulingRule, pm); 159 EclipseSynchronizer.getInstance().run(MultiRule.combine(targetProjects), new ICVSRunnable() { 161 public void run(IProgressMonitor monitor) throws CVSException { 162 result[0] = performCheckout(session, resource, targetProjects, sendModuleName, monitor); 163 } 164 }, Policy.subMonitorFor(pm, 90)); 165 } finally { 166 Job.getJobManager().endRule(schedulingRule); 167 } 168 } else { 169 EclipseSynchronizer.getInstance().run(schedulingRule, new ICVSRunnable() { 170 public void run(IProgressMonitor monitor) throws CVSException { 171 result[0] = performCheckout(session, resource, targetProjects, sendModuleName, monitor); 172 } 173 }, Policy.subMonitorFor(pm, 90)); 174 } 175 String wsName = getWorkingSetName(); 176 if (wsName != null){ 177 createWorkingSet(wsName, targetProjects); 178 } 179 return result[0]; 180 } catch (CVSException e) { 181 return new CVSStatus(e.getStatus().getSeverity(), NLS.bind(CVSUIMessages.CheckoutProjectOperation_1, new String [] {resource.getRepositoryRelativePath(), e.getMessage()}), e); 185 } finally { 186 session.close(); 187 pm.done(); 188 } 189 } 190 191 private ISchedulingRule getSchedulingRule(IProject[] projects) { 192 if (projects.length == 1) { 193 return ResourcesPlugin.getWorkspace().getRuleFactory().modifyRule(projects[0]); 194 } else { 195 Set rules = new HashSet(); 196 for (int i = 0; i < projects.length; i++) { 197 ISchedulingRule modifyRule = ResourcesPlugin.getWorkspace().getRuleFactory().modifyRule(projects[i]); 198 if (modifyRule instanceof IResource && ((IResource)modifyRule).getType() == IResource.ROOT) { 199 return modifyRule; 202 } 203 rules.add(modifyRule); 204 } 205 return new MultiRule((ISchedulingRule[]) rules.toArray(new ISchedulingRule[rules.size()])); 206 } 207 } 208 209 IStatus performCheckout(Session session, ICVSRemoteFolder resource, IProject[] targetProjects, boolean sendModuleName, IProgressMonitor pm) throws CVSException { 210 String taskName; 214 if (targetProjects.length == 1) { 215 taskName = NLS.bind(CVSUIMessages.CheckoutProjectOperation_8, new String [] { resource.getName(), targetProjects[0].getName() }); 216 } else { 217 taskName = NLS.bind(CVSUIMessages.CheckoutProjectOperation_9, new String [] { resource.getName(), String.valueOf(targetProjects.length) }); 218 } 219 pm.beginTask(taskName, 100); 220 pm.setTaskName(taskName); 221 Policy.checkCanceled(pm); 222 try { 223 if (performScrubProjects()) { 225 IStatus result = scrubProjects(resource, targetProjects, Policy.subMonitorFor(pm, 9)); 226 if (!result.isOK()) { 227 return result; 228 } 229 } 230 231 IProject project = null; 235 if (targetProjects.length == 1) { 236 if (sendModuleName) { 237 project = targetProjects[0]; 238 } else if (targetProjects[0].getName().equals(resource.getName())) { 239 String path = resource.getRepositoryRelativePath(); 243 if (!path.equals(FolderSyncInfo.VIRTUAL_DIRECTORY) 244 && new Path(null, path).segmentCount() > 1) { 245 project = targetProjects[0]; 246 } 247 } 248 } 249 250 try { 251 List localOptions = new ArrayList(); 253 if (project != null) { 255 localOptions.add(Checkout.makeDirectoryNameOption(project.getName())); 256 } 257 if (CVSProviderPlugin.getPlugin().getPruneEmptyDirectories()) 259 localOptions.add(Command.PRUNE_EMPTY_DIRECTORIES); 260 CVSTag tag = resource.getTag(); 262 if (tag == null) { 263 tag = CVSTag.DEFAULT; 265 } 266 localOptions.add(Update.makeTagOption(tag)); 267 if (!isRecursive()) 268 localOptions.add(Command.DO_NOT_RECURSE); 269 270 IStatus status = Command.CHECKOUT.execute(session, 272 Command.NO_GLOBAL_OPTIONS, 273 (LocalOption[])localOptions.toArray(new LocalOption[localOptions.size()]), 274 new String []{getRemoteModuleName(resource)}, 275 null, 276 Policy.subMonitorFor(pm, 90)); 277 return status; 278 } finally { 279 refreshProjects(targetProjects, Policy.subMonitorFor(pm, 1)); 281 } 282 } finally { 283 pm.done(); 284 } 285 } 286 287 protected boolean isRecursive() { 288 return true; 289 } 290 291 299 private IProject[] determineProjects(Session session, final ICVSRemoteFolder remoteFolder, IProject project, IProgressMonitor pm) throws CVSException { 300 301 Set targetProjectSet = new HashSet(); 302 String moduleName = getRemoteModuleName(remoteFolder); 303 if (project == null) { 304 305 Policy.checkCanceled(pm); 307 IStatus status = Request.EXPAND_MODULES.execute(session, new String [] {moduleName}, pm); 308 if (status.getCode() == CVSStatus.SERVER_ERROR) { 309 collectStatus(status); 310 return null; 311 } 312 313 String [] expansions = session.getModuleExpansions(); 315 if (expansions.length == 1 && expansions[0].equals(moduleName)) { 316 String lastSegment = new Path(null, expansions[0]).lastSegment(); 318 if (CVSUIPlugin.getPlugin().isUseProjectNameOnCheckout() && remoteFolder instanceof RemoteProjectFolder) { 320 RemoteProjectFolder rpf = (RemoteProjectFolder) remoteFolder; 321 if (rpf.hasProjectName()) { 322 lastSegment = rpf.getProjectName(); 323 } 324 } 325 targetProjectSet.add(ResourcesPlugin.getWorkspace().getRoot().getProject(lastSegment)); 326 } else { 327 for (int j = 0; j < expansions.length; j++) { 328 targetProjectSet.add(ResourcesPlugin.getWorkspace().getRoot().getProject(new Path(null, expansions[j]).segment(0))); 329 } 330 } 331 332 } else { 333 targetProjectSet.add(project); 334 } 335 336 IProject[] targetProjects = (IProject[]) targetProjectSet.toArray(new IProject[targetProjectSet.size()]); 338 return targetProjects; 339 } 340 341 345 protected boolean performScrubProjects() { 346 return true; 347 } 348 349 353 private IStatus scrubProjects(ICVSRemoteFolder remoteFolder, IProject[] projects, IProgressMonitor monitor) throws CVSException { 354 if (projects == null) { 355 monitor.done(); 356 return OK; 357 } 358 if (projects.length > 1) { 360 setInvolvesMultipleResources(true); 361 } 362 for (int i=0;i<projects.length;i++) { 363 IProject project = projects[i]; 364 Policy.checkCanceled(monitor); 365 if (needsPromptForOverwrite(project) && !promptToOverwrite(remoteFolder, project)) { 366 return new CVSStatus(IStatus.INFO, IStatus.CANCEL, NLS.bind(CVSUIMessages.CheckoutProjectOperation_0, new String [] { remoteFolder.getRepositoryRelativePath() }), remoteFolder); 368 } 369 } 370 monitor.beginTask(null, projects.length * 100); 372 for (int i=0;i<projects.length;i++) { 373 IProject project = projects[i]; 374 createAndOpenProject(project, Policy.subMonitorFor(monitor, 10)); 375 scrubProject(project, Policy.subMonitorFor(monitor, 90)); 376 } 377 monitor.done(); 378 return OK; 379 } 380 381 private void scrubProject(IProject project, IProgressMonitor monitor) throws CVSException { 382 try { 383 if (RepositoryProvider.getProvider(project) != null) 385 RepositoryProvider.unmap(project); 386 IResource[] children = project.members(IContainer.INCLUDE_TEAM_PRIVATE_MEMBERS); 389 Policy.checkCanceled(monitor); 390 monitor.beginTask(null, 100 + children.length * 100); 391 monitor.subTask(NLS.bind(CVSUIMessages.CheckoutOperation_scrubbingProject, new String [] { project.getName() })); try { 393 for (int j = 0; j < children.length; j++) { 394 if ( ! children[j].getName().equals(".project")) { children[j].delete(true , Policy.subMonitorFor(monitor, 100)); 396 } 397 } 398 EclipseSynchronizer.getInstance().flush(project, true, Policy.subMonitorFor(monitor, 100)); 401 } finally { 402 monitor.done(); 403 } 404 } catch (TeamException e) { 405 throw CVSException.wrapException(e); 406 } catch (CoreException e) { 407 throw CVSException.wrapException(e); 408 } 409 } 410 411 protected boolean needsPromptForOverwrite(IProject project) { 412 413 IProjectDescription desc = getDescriptionFor(project); 415 if (desc != null) { 416 File localLocation = desc.getLocation().toFile(); 417 if (localLocation.exists()) return true; 418 } 419 420 if (project.exists()) return true; 422 423 File localLocation = getFileLocation(project); 425 if (localLocation.exists()) return true; 426 427 return false; 429 } 430 431 protected File getFileLocation(IProject project) { 432 return new File (project.getParent().getLocation().toFile(), project.getName()); 433 } 434 435 private boolean promptToOverwrite(ICVSRemoteFolder remoteFolder, IProject project) { 436 if (project.exists()) { 438 if (!promptToOverwrite( 439 CVSUIMessages.CheckoutOperation_confirmOverwrite, 440 NLS.bind(CVSUIMessages.CheckoutOperation_thisResourceExists, new String [] { project.getName(), getRemoteModuleName(remoteFolder) }), 441 project)) { 442 return false; 443 } 444 } 445 IPath path = getTargetLocationFor(project); 447 File localLocation = null; 448 if (path == null) { 449 try { 450 if (!project.exists() || !project.isOpen() || project.getDescription().getLocation() != null) { 454 localLocation = getFileLocation(project); 455 } 456 } catch (CoreException e) { 457 CVSUIPlugin.log(e); 458 } 459 } else { 460 localLocation = path.toFile(); 461 } 462 if (localLocation != null && localLocation.exists()) { 463 try { 464 return (promptToOverwrite( 465 CVSUIMessages.CheckoutOperation_confirmOverwrite, 466 NLS.bind(CVSUIMessages.CheckoutOperation_thisExternalFileExists, new String [] { localLocation.getCanonicalPath(), getRemoteModuleName(remoteFolder) }), 467 project)); 468 } catch (IOException e) { 469 CVSUIPlugin.log(CVSException.wrapException(e)); 470 } 471 } 472 return true; 473 } 474 475 478 private void refreshProjects(IProject[] projects, IProgressMonitor monitor) throws CVSException { 479 monitor.beginTask(null, projects.length * 100); 480 try { 481 for (int i = 0; i < projects.length; i++) { 482 IProject project = projects[i]; 483 try { 485 monitor.subTask(NLS.bind(CVSUIMessages.CheckoutOperation_refreshingProject, new String [] { project.getName() })); 486 ICVSFolder folder = CVSWorkspaceRoot.getCVSFolderFor(project); 487 if (folder.isCVSFolder()) { 488 RepositoryProvider.map(project, CVSProviderPlugin.getTypeId()); 489 } 490 } catch (TeamException e) { 491 throw CVSException.wrapException(e); 492 } 493 CVSTeamProvider provider = (CVSTeamProvider)RepositoryProvider.getProvider(project, CVSProviderPlugin.getTypeId()); 494 if (provider != null) { 495 provider.setWatchEditEnabled(CVSProviderPlugin.getPlugin().isWatchEditEnabled()); 496 } 497 } 498 } finally { 499 monitor.done(); 500 } 501 } 502 503 protected String getTaskName() { 504 ICVSRemoteFolder[] remoteFolders = getRemoteFolders(); 505 if (remoteFolders.length == 1) { 506 return NLS.bind(CVSUIMessages.CheckoutSingleProjectOperation_taskname, new String [] { remoteFolders[0].getName() }); 507 } else { 508 return NLS.bind(CVSUIMessages.CheckoutMultipleProjectsOperation_taskName, new String [] { new Integer (remoteFolders.length).toString() }); 509 } 510 } 511 512 void createWorkingSet(String workingSetName, IProject[] projects) { 513 IWorkingSetManager manager = CVSUIPlugin.getPlugin().getWorkbench().getWorkingSetManager(); 514 IWorkingSet oldSet = manager.getWorkingSet(workingSetName); 515 if (oldSet == null) { 516 IWorkingSet newSet = manager.createWorkingSet(workingSetName, projects); 517 newSet.setId(WorkingSetsDialog.resourceWorkingSetId); 518 manager.addWorkingSet(newSet); 519 } else { 520 IAdaptable[] tempElements = oldSet.getElements(); 522 IAdaptable[] adaptedProjects = oldSet.adaptElements(projects); 523 IAdaptable[] finalElementList = new IAdaptable[tempElements.length + adaptedProjects.length]; 524 System.arraycopy(tempElements, 0, finalElementList, 0, tempElements.length); 525 System.arraycopy(adaptedProjects, 0,finalElementList, tempElements.length, adaptedProjects.length); 526 oldSet.setElements(finalElementList); 527 } 528 } 529 530 533 protected String getWorkingSetName(){ 534 return null; 535 } 536 537 } 538 | Popular Tags |