1 11 package org.eclipse.core.internal.resources; 12 13 import java.net.URI ; 14 import java.util.*; 15 import org.eclipse.core.filesystem.EFS; 16 import org.eclipse.core.filesystem.IFileStore; 17 import org.eclipse.core.internal.events.LifecycleEvent; 18 import org.eclipse.core.internal.utils.*; 19 import org.eclipse.core.resources.*; 20 import org.eclipse.core.resources.team.IMoveDeleteHook; 21 import org.eclipse.core.runtime.*; 22 import org.eclipse.core.runtime.content.IContentTypeMatcher; 23 import org.eclipse.core.runtime.jobs.ISchedulingRule; 24 import org.eclipse.osgi.util.NLS; 25 26 public class Project extends Container implements IProject { 27 28 protected Project(IPath path, Workspace container) { 29 super(path, container); 30 } 31 32 37 protected MultiStatus basicSetDescription(ProjectDescription description, int updateFlags) { 38 String message = Messages.resources_projectDesc; 39 MultiStatus result = new MultiStatus(ResourcesPlugin.PI_RESOURCES, IResourceStatus.FAILED_WRITE_METADATA, message, null); 40 ProjectDescription current = internalGetDescription(); 41 current.setComment(description.getComment()); 42 current.setBuildSpec(description.getBuildSpec(true)); 44 45 boolean flushOrder = false; 47 IProject[] oldReferences = current.getReferencedProjects(); 48 IProject[] newReferences = description.getReferencedProjects(); 49 if (!Arrays.equals(oldReferences, newReferences)) { 50 current.setReferencedProjects(newReferences); 51 flushOrder = true; 52 } 53 oldReferences = current.getDynamicReferences(); 54 newReferences = description.getDynamicReferences(); 55 if (!Arrays.equals(oldReferences, newReferences)) { 56 current.setDynamicReferences(newReferences); 57 flushOrder = true; 58 } 59 60 if (flushOrder) 61 workspace.flushBuildOrder(); 62 63 if ((updateFlags & IResource.AVOID_NATURE_CONFIG) == 0) 65 workspace.getNatureManager().configureNatures(this, current, description, result); 66 else 67 current.setNatureIds(description.getNatureIds(false)); 68 return result; 69 } 70 71 74 public void build(int trigger, IProgressMonitor monitor) throws CoreException { 75 internalBuild(trigger, null, null, monitor); 76 } 77 78 81 public void build(int trigger, String builderName, Map args, IProgressMonitor monitor) throws CoreException { 82 Assert.isNotNull(builderName); 83 internalBuild(trigger, builderName, args, monitor); 84 } 85 86 93 public void checkAccessible(int flags) throws CoreException { 94 super.checkAccessible(flags); 95 if (!isOpen(flags)) { 96 String message = NLS.bind(Messages.resources_mustBeOpen, getFullPath()); 97 throw new ResourceException(IResourceStatus.PROJECT_NOT_OPEN, getFullPath(), message, null); 98 } 99 } 100 101 104 protected void checkDescription(IProject project, IProjectDescription desc, boolean moving) throws CoreException { 105 URI location = desc.getLocationURI(); 106 if (location == null) 107 return; 108 String message = Messages.resources_invalidProjDesc; 109 MultiStatus status = new MultiStatus(ResourcesPlugin.PI_RESOURCES, IResourceStatus.INVALID_VALUE, message, null); 110 status.merge(workspace.validateName(desc.getName(), IResource.PROJECT)); 111 if (moving) { 112 URI sourceLocation = internalGetDescription().getLocationURI(); 118 if (sourceLocation == null || !sourceLocation.equals(location)) 119 status.merge(workspace.validateProjectLocationURI(project, location)); 120 } else 121 status.merge(workspace.validateProjectLocationURI(project, location)); 123 if (!status.isOK()) 124 throw new ResourceException(status); 125 } 126 127 130 public void close(IProgressMonitor monitor) throws CoreException { 131 monitor = Policy.monitorFor(monitor); 132 try { 133 String msg = NLS.bind(Messages.resources_closing_1, getName()); 134 monitor.beginTask(msg, Policy.totalWork); 135 final ISchedulingRule rule = workspace.getRuleFactory().modifyRule(this); 136 try { 137 workspace.broadcastEvent(LifecycleEvent.newEvent(LifecycleEvent.PRE_PROJECT_CLOSE, this)); 139 workspace.prepareOperation(rule, monitor); 140 ResourceInfo info = getResourceInfo(false, false); 141 int flags = getFlags(info); 142 checkExists(flags, true); 143 monitor.subTask(msg); 144 if (!isOpen(flags)) 145 return; 146 workspace.beginOperation(true); 150 workspace.flushBuildOrder(); 152 IProgressMonitor sub = Policy.subMonitorFor(monitor, Policy.opWork / 2, SubProgressMonitor.SUPPRESS_SUBTASK_LABEL); 153 IStatus saveStatus = workspace.getSaveManager().save(ISaveContext.PROJECT_SAVE, this, sub); 154 internalClose(); 155 monitor.worked(Policy.opWork / 2); 156 if (saveStatus != null && !saveStatus.isOK()) 157 throw new ResourceException(saveStatus); 158 } catch (OperationCanceledException e) { 159 workspace.getWorkManager().operationCanceled(); 160 throw e; 161 } finally { 162 workspace.endOperation(rule, true, Policy.subMonitorFor(monitor, Policy.endOpWork)); 163 } 164 } finally { 165 monitor.done(); 166 } 167 } 168 169 172 public void copy(IPath destination, int updateFlags, IProgressMonitor monitor) throws CoreException { 173 monitor = Policy.monitorFor(monitor); 177 if (destination.segmentCount() == 1) { 178 String projectName = destination.segment(0); 180 IProjectDescription desc = getDescription(); 181 desc.setName(projectName); 182 desc.setLocation(null); 183 internalCopy(desc, updateFlags, monitor); 184 } else { 185 checkCopyRequirements(destination, IResource.PROJECT, updateFlags); 187 } 188 } 189 190 193 public void copy(IProjectDescription destination, int updateFlags, IProgressMonitor monitor) throws CoreException { 194 Assert.isNotNull(destination); 198 internalCopy(destination, updateFlags, monitor); 199 } 200 201 protected void copyMetaArea(IProject source, IProject destination, IProgressMonitor monitor) throws CoreException { 202 IFileStore oldMetaArea = EFS.getFileSystem(EFS.SCHEME_FILE).getStore(workspace.getMetaArea().locationFor(source)); 203 IFileStore newMetaArea = EFS.getFileSystem(EFS.SCHEME_FILE).getStore(workspace.getMetaArea().locationFor(destination)); 204 oldMetaArea.copy(newMetaArea, EFS.NONE, monitor); 205 } 206 207 210 public void create(IProgressMonitor monitor) throws CoreException { 211 create(null, monitor); 212 } 213 214 217 public void create(IProjectDescription description, IProgressMonitor monitor) throws CoreException { 218 monitor = Policy.monitorFor(monitor); 219 try { 220 monitor.beginTask(Messages.resources_create, Policy.totalWork); 221 checkValidPath(path, PROJECT, false); 222 final ISchedulingRule rule = workspace.getRuleFactory().createRule(this); 223 try { 224 workspace.prepareOperation(rule, monitor); 225 checkDoesNotExist(); 226 if (description != null) 227 checkDescription(this, description, false); 228 workspace.broadcastEvent(LifecycleEvent.newEvent(LifecycleEvent.PRE_PROJECT_CREATE, this)); 229 workspace.beginOperation(true); 230 workspace.createResource(this, false); 231 workspace.getMetaArea().create(this); 232 ProjectInfo info = (ProjectInfo) getResourceInfo(false, true); 233 234 ProjectDescription desc; 236 if (description == null) { 237 desc = new ProjectDescription(); 238 } else { 239 desc = (ProjectDescription) ((ProjectDescription) description).clone(); 240 desc.setLocationURI(FileUtil.canonicalURI(description.getLocationURI())); 241 } 242 desc.setName(getName()); 243 internalSetDescription(desc, false); 244 final boolean hasSavedDescription = getLocalManager().hasSavedDescription(this); 246 boolean hasContent = hasSavedDescription; 247 if (!hasSavedDescription) 249 hasContent = getLocalManager().hasSavedContent(this); 250 try { 251 if (hasSavedDescription) { 253 updateDescription(); 254 workspace.getMetaArea().writePrivateDescription(this); 256 } else { 257 writeDescription(IResource.FORCE); 259 } 260 } catch (CoreException e) { 261 workspace.deleteResource(this); 262 throw e; 263 } 264 info.clearModificationStamp(); 268 if (hasContent) 270 info.set(ICoreConstants.M_CHILDREN_UNKNOWN); 271 workspace.getSaveManager().requestSnapshot(); 272 } catch (OperationCanceledException e) { 273 workspace.getWorkManager().operationCanceled(); 274 throw e; 275 } finally { 276 workspace.endOperation(rule, true, Policy.subMonitorFor(monitor, Policy.endOpWork)); 277 } 278 } finally { 279 monitor.done(); 280 } 281 } 282 283 286 public void delete(boolean deleteContent, boolean force, IProgressMonitor monitor) throws CoreException { 287 int updateFlags = force ? IResource.FORCE : IResource.NONE; 288 updateFlags |= deleteContent ? IResource.ALWAYS_DELETE_PROJECT_CONTENT : IResource.NEVER_DELETE_PROJECT_CONTENT; 289 delete(updateFlags, monitor); 290 } 291 292 295 public void delete(boolean force, IProgressMonitor monitor) throws CoreException { 296 int updateFlags = force ? IResource.FORCE : IResource.NONE; 297 delete(updateFlags, monitor); 298 } 299 300 public void deleteResource(boolean convertToPhantom, MultiStatus status) throws CoreException { 301 super.deleteResource(convertToPhantom, status); 302 workspace.getMetaArea().delete(this); 304 clearHistory(null); 306 } 307 308 protected void fixupAfterMoveSource() { 309 workspace.deleteResource(this); 310 } 311 312 316 public IContentTypeMatcher getContentTypeMatcher() throws CoreException { 317 return workspace.getContentDescriptionManager().getContentTypeMatcher(this); 318 } 319 320 323 public String getDefaultCharset(boolean checkImplicit) { 324 if (!exists()) 326 return checkImplicit ? ResourcesPlugin.getEncoding() : null; 327 return workspace.getCharsetManager().getCharsetFor(getFullPath(), checkImplicit); 328 } 329 330 333 public IProjectDescription getDescription() throws CoreException { 334 ResourceInfo info = getResourceInfo(false, false); 335 checkAccessible(getFlags(info)); 336 ProjectDescription description = ((ProjectInfo) info).getDescription(); 337 if (description == null) 339 checkAccessible(NULL_FLAG); 340 return (IProjectDescription) description.clone(); 341 } 342 343 346 public IProjectNature getNature(String natureID) throws CoreException { 347 ProjectInfo info = (ProjectInfo) getResourceInfo(false, false); 349 checkAccessible(getFlags(info)); 350 IProjectNature nature = info.getNature(natureID); 351 if (nature == null) { 352 if (!hasNature(natureID)) 354 return null; 355 nature = workspace.getNatureManager().createNature(this, natureID); 356 info.setNature(natureID, nature); 357 } 358 return nature; 359 } 360 361 364 public IContainer getParent() { 365 return workspace.getRoot(); 366 } 367 368 372 public IPath getPluginWorkingLocation(IPluginDescriptor plugin) { 373 if (plugin == null) 374 return null; 375 return getWorkingLocation(plugin.getUniqueIdentifier()); 376 } 377 378 381 public IProject getProject() { 382 return this; 383 } 384 385 388 public IPath getProjectRelativePath() { 389 return Path.EMPTY; 390 } 391 392 395 public IPath getRawLocation() { 396 ProjectDescription description = internalGetDescription(); 397 return description == null ? null : description.getLocation(); 398 } 399 400 403 public URI getRawLocationURI() { 404 ProjectDescription description = internalGetDescription(); 405 return description == null ? null : description.getLocationURI(); 406 } 407 408 411 public IProject[] getReferencedProjects() throws CoreException { 412 ResourceInfo info = getResourceInfo(false, false); 413 checkAccessible(getFlags(info)); 414 ProjectDescription description = ((ProjectInfo) info).getDescription(); 415 if (description == null) 417 checkAccessible(NULL_FLAG); 418 return description.getAllReferences(true); 419 } 420 421 424 public IProject[] getReferencingProjects() { 425 IProject[] projects = workspace.getRoot().getProjects(); 426 List result = new ArrayList(projects.length); 427 for (int i = 0; i < projects.length; i++) { 428 Project project = (Project) projects[i]; 429 if (!project.isAccessible()) 430 continue; 431 ProjectDescription description = project.internalGetDescription(); 432 if (description == null) 433 continue; 434 IProject[] references = description.getAllReferences(false); 435 for (int j = 0; j < references.length; j++) 436 if (references[j].equals(this)) { 437 result.add(projects[i]); 438 break; 439 } 440 } 441 return (IProject[]) result.toArray(new IProject[result.size()]); 442 } 443 444 447 public int getType() { 448 return PROJECT; 449 } 450 451 455 public IPath getWorkingLocation(String id) { 456 if (id == null || !exists()) 457 return null; 458 IPath result = workspace.getMetaArea().getWorkingLocation(this, id); 459 result.toFile().mkdirs(); 460 return result; 461 } 462 463 466 public boolean hasNature(String natureID) throws CoreException { 467 checkAccessible(getFlags(getResourceInfo(false, false))); 468 IProjectDescription desc = internalGetDescription(); 471 if (desc == null) 472 checkAccessible(NULL_FLAG); 473 return desc.hasNature(natureID); 474 } 475 476 479 protected void internalBuild(int trigger, String builderName, Map args, IProgressMonitor monitor) throws CoreException { 480 monitor = Policy.monitorFor(monitor); 481 final ISchedulingRule rule = workspace.getRuleFactory().buildRule(); 482 try { 483 monitor.beginTask("", Policy.opWork); try { 485 workspace.prepareOperation(rule, monitor); 486 ResourceInfo info = getResourceInfo(false, false); 487 int flags = getFlags(info); 488 if (!exists(flags, true) || !isOpen(flags)) 489 return; 490 workspace.beginOperation(true); 491 workspace.aboutToBuild(this, trigger); 492 IStatus result; 493 try { 494 result = workspace.getBuildManager().build(this, trigger, builderName, args, Policy.subMonitorFor(monitor, Policy.opWork)); 495 } finally { 496 workspace.broadcastBuildEvent(this, IResourceChangeEvent.POST_BUILD, trigger); 498 } 499 if (!result.isOK()) 500 throw new ResourceException(result); 501 } finally { 502 if (workspace.getElementTree().isImmutable()) 504 workspace.newWorkingTree(); 505 workspace.endOperation(rule, false, Policy.subMonitorFor(monitor, Policy.endOpWork)); 506 } 507 } finally { 508 monitor.done(); 509 } 510 } 511 512 517 protected void internalClose() throws CoreException { 518 workspace.flushBuildOrder(); 519 getMarkerManager().removeMarkers(this, IResource.DEPTH_INFINITE); 520 IResource[] members = members(IContainer.INCLUDE_PHANTOMS | IContainer.INCLUDE_TEAM_PRIVATE_MEMBERS); 523 for (int i = 0; i < members.length; i++) { 524 Resource member = (Resource) members[i]; 525 workspace.deleteResource(member); 526 } 527 ResourceInfo info = getResourceInfo(false, true); 529 info.clear(M_OPEN); 530 info.clearSessionProperties(); 531 info.clearModificationStamp(); 532 info.setSyncInfo(null); 533 } 534 535 protected void internalCopy(IProjectDescription destDesc, int updateFlags, IProgressMonitor monitor) throws CoreException { 536 monitor = Policy.monitorFor(monitor); 537 try { 538 String message = NLS.bind(Messages.resources_copying, getFullPath()); 539 monitor.beginTask(message, Policy.totalWork); 540 String destName = destDesc.getName(); 541 IPath destPath = new Path(destName).makeAbsolute(); 542 Project destination = (Project) workspace.getRoot().getProject(destName); 543 final ISchedulingRule rule = workspace.getRuleFactory().copyRule(this, destination); 544 try { 545 workspace.prepareOperation(rule, monitor); 546 assertCopyRequirements(destPath, IResource.PROJECT, updateFlags); 549 checkDescription(destination, destDesc, false); 550 workspace.broadcastEvent(LifecycleEvent.newEvent(LifecycleEvent.PRE_PROJECT_COPY, this, destination, updateFlags)); 551 552 workspace.beginOperation(true); 553 getLocalManager().refresh(this, DEPTH_INFINITE, true, Policy.subMonitorFor(monitor, Policy.opWork * 20 / 100)); 554 555 getPropertyManager().closePropertyStore(this); 557 getLocalManager().getHistoryStore().closeHistoryStore(this); 558 559 copyMetaArea(this, destination, Policy.subMonitorFor(monitor, Policy.opWork * 5 / 100)); 561 562 internalCopyProjectOnly(destination, Policy.subMonitorFor(monitor, Policy.opWork * 5 / 100)); 564 565 destination.internalSetDescription(destDesc, false); 567 568 destination.getStore().mkdir(EFS.NONE, Policy.subMonitorFor(monitor, Policy.opWork * 5 / 100)); 570 571 message = Messages.resources_copyProblem; 574 MultiStatus problems = new MultiStatus(ResourcesPlugin.PI_RESOURCES, IResourceStatus.INTERNAL_ERROR, message, null); 575 576 IResource[] children = members(IContainer.INCLUDE_TEAM_PRIVATE_MEMBERS); 577 final int childCount = children.length; 578 final int childWork = childCount > 1 ? Policy.opWork * 50 / 100 / (childCount - 1) : 0; 579 for (int i = 0; i < childCount; i++) { 580 IResource child = children[i]; 581 if (!isProjectDescriptionFile(child)) { 582 try { 583 child.copy(destPath.append(child.getName()), updateFlags, Policy.subMonitorFor(monitor, childWork)); 584 } catch (CoreException e) { 585 problems.merge(e.getStatus()); 586 } 587 } 588 } 589 590 try { 592 destination.writeDescription(IResource.FORCE); 593 } catch (CoreException e) { 594 try { 595 destination.delete((updateFlags & IResource.FORCE) != 0, null); 596 } catch (CoreException e2) { 597 } 599 throw e; 600 } 601 monitor.worked(Policy.opWork * 5 / 100); 602 603 monitor.subTask(Messages.resources_updating); 605 getLocalManager().refresh(destination, DEPTH_INFINITE, true, Policy.subMonitorFor(monitor, Policy.opWork * 10 / 100)); 606 if (!problems.isOK()) 607 throw new ResourceException(problems); 608 } catch (OperationCanceledException e) { 609 workspace.getWorkManager().operationCanceled(); 610 throw e; 611 } finally { 612 workspace.endOperation(rule, true, Policy.subMonitorFor(monitor, Policy.endOpWork)); 613 } 614 } finally { 615 monitor.done(); 616 } 617 } 618 619 622 protected void internalCopyProjectOnly(IResource destination, IProgressMonitor monitor) throws CoreException { 623 getPropertyManager().closePropertyStore(this); 625 getLocalManager().getHistoryStore().closeHistoryStore(this); 626 workspace.copyTree(this, destination.getFullPath(), IResource.DEPTH_ZERO, IResource.NONE, false); 628 getPropertyManager().copy(this, destination, IResource.DEPTH_ZERO); 629 630 ProjectInfo info = (ProjectInfo) ((Resource) destination).getResourceInfo(false, true); 631 632 info.description = null; 634 info.natures = null; 635 info.setMarkers(null); 636 info.clearSessionProperties(); 637 } 638 639 645 public ProjectDescription internalGetDescription() { 646 ProjectInfo info = (ProjectInfo) getResourceInfo(false, false); 647 if (info == null) 648 return null; 649 return info.getDescription(); 650 } 651 652 657 void internalSetDescription(IProjectDescription value, boolean incrementContentId) { 658 ProjectInfo info = (ProjectInfo) getResourceInfo(false, true); 659 info.setDescription((ProjectDescription) value); 660 getLocalManager().setLocation(this, info, value.getLocationURI()); 661 if (incrementContentId) { 662 info.incrementContentId(); 663 if (info.getModificationStamp() != NULL_STAMP) 665 workspace.updateModificationStamp(info); 666 } 667 } 668 669 public void internalSetLocal(boolean flag, int depth) throws CoreException { 670 if (depth == IResource.DEPTH_ZERO) 672 return; 673 if (depth == IResource.DEPTH_ONE) 674 depth = IResource.DEPTH_ZERO; 675 IResource[] children = getChildren(IResource.NONE); 678 for (int i = 0; i < children.length; i++) 679 ((Resource) children[i]).internalSetLocal(flag, depth); 680 } 681 682 685 public boolean isAccessible() { 686 return isOpen(); 687 } 688 689 public boolean isLinked(int options) { 690 return false; } 692 693 697 public boolean isLocal(int depth) { 698 return isLocal(-1, depth); 700 } 701 702 706 public boolean isLocal(int flags, int depth) { 707 if (depth == DEPTH_ZERO) 709 return true; 710 if (depth == DEPTH_ONE) 711 depth = DEPTH_ZERO; 712 IResource[] children = getChildren(IResource.NONE); 715 for (int i = 0; i < children.length; i++) 716 if (!children[i].isLocal(depth)) 717 return false; 718 return true; 719 } 720 721 724 public boolean isNatureEnabled(String natureId) throws CoreException { 725 checkAccessible(getFlags(getResourceInfo(false, false))); 726 return workspace.getNatureManager().isNatureEnabled(this, natureId); 727 } 728 729 732 public boolean isOpen() { 733 ResourceInfo info = getResourceInfo(false, false); 734 return isOpen(getFlags(info)); 735 } 736 737 740 public boolean isOpen(int flags) { 741 return flags != NULL_FLAG && ResourceInfo.isSet(flags, M_OPEN); 742 } 743 744 748 protected boolean isProjectDescriptionFile(IResource resource) { 749 return resource.getType() == IResource.FILE && resource.getFullPath().segmentCount() == 2 && resource.getName().equals(IProjectDescription.DESCRIPTION_FILE_NAME); 750 } 751 752 755 public void move(IProjectDescription destination, boolean force, IProgressMonitor monitor) throws CoreException { 756 Assert.isNotNull(destination); 757 move(destination, force ? IResource.FORCE : IResource.NONE, monitor); 758 } 759 760 763 public void move(IProjectDescription description, int updateFlags, IProgressMonitor monitor) throws CoreException { 764 Assert.isNotNull(description); 765 monitor = Policy.monitorFor(monitor); 766 try { 767 String message = NLS.bind(Messages.resources_moving, getFullPath()); 768 monitor.beginTask(message, Policy.totalWork); 769 IProject destination = workspace.getRoot().getProject(description.getName()); 770 final ISchedulingRule rule = workspace.getRuleFactory().moveRule(this, destination); 771 try { 772 workspace.prepareOperation(rule, monitor); 773 if (!getName().equals(description.getName())) { 776 IPath destPath = Path.ROOT.append(description.getName()); 777 assertMoveRequirements(destPath, IResource.PROJECT, updateFlags); 778 } 779 checkDescription(destination, description, true); 780 workspace.beginOperation(true); 781 message = Messages.resources_moveProblem; 782 MultiStatus status = new MultiStatus(ResourcesPlugin.PI_RESOURCES, IStatus.ERROR, message, null); 783 WorkManager workManager = workspace.getWorkManager(); 784 ResourceTree tree = new ResourceTree(getLocalManager(), workManager.getLock(), status, updateFlags); 785 IMoveDeleteHook hook = workspace.getMoveDeleteHook(); 786 workspace.broadcastEvent(LifecycleEvent.newEvent(LifecycleEvent.PRE_PROJECT_MOVE, this, destination, updateFlags)); 787 int depth = 0; 788 try { 789 depth = workManager.beginUnprotected(); 790 if (!hook.moveProject(tree, this, description, updateFlags, Policy.subMonitorFor(monitor, Policy.opWork / 2))) 791 tree.standardMoveProject(this, description, updateFlags, Policy.subMonitorFor(monitor, Policy.opWork / 2)); 792 } finally { 793 workManager.endUnprotected(depth); 794 } 795 tree.makeInvalid(); 797 if (!tree.getStatus().isOK()) 798 throw new ResourceException(tree.getStatus()); 799 } catch (OperationCanceledException e) { 800 workspace.getWorkManager().operationCanceled(); 801 throw e; 802 } finally { 803 workspace.endOperation(rule, true, Policy.subMonitorFor(monitor, Policy.endOpWork)); 804 } 805 } finally { 806 monitor.done(); 807 } 808 } 809 810 813 public void open(int updateFlags, IProgressMonitor monitor) throws CoreException { 814 monitor = Policy.monitorFor(monitor); 815 try { 816 String msg = NLS.bind(Messages.resources_opening_1, getName()); 817 monitor.beginTask(msg, Policy.totalWork); 818 monitor.subTask(msg); 819 final ISchedulingRule rule = workspace.getRuleFactory().modifyRule(this); 820 try { 821 workspace.prepareOperation(rule, monitor); 822 ProjectInfo info = (ProjectInfo) getResourceInfo(false, false); 823 int flags = getFlags(info); 824 checkExists(flags, true); 825 if (isOpen(flags)) 826 return; 827 828 workspace.beginOperation(true); 829 workspace.flushBuildOrder(); 831 info = (ProjectInfo) getResourceInfo(false, true); 832 info.set(M_OPEN); 833 boolean unknownChildren = info.isSet(M_CHILDREN_UNKNOWN); 835 if (unknownChildren) 836 info.clear(M_CHILDREN_UNKNOWN); 837 boolean used = info.isSet(M_USED); 840 if (used) { 841 workspace.getSaveManager().restore(this, Policy.subMonitorFor(monitor, Policy.opWork * 20 / 100)); 842 } else { 843 info.set(M_USED); 844 IStatus result = reconcileLinks(info.getDescription()); 846 if (!result.isOK()) 847 throw new CoreException(result); 848 workspace.updateModificationStamp(info); 849 monitor.worked(Policy.opWork * 20 / 100); 850 } 851 startup(); 852 if (!used && unknownChildren) { 854 if ((updateFlags & IResource.BACKGROUND_REFRESH) != 0) { 856 workspace.refreshManager.refresh(this); 857 monitor.worked(Policy.opWork * 80 / 100); 858 } else { 859 refreshLocal(IResource.DEPTH_INFINITE, Policy.subMonitorFor(monitor, Policy.opWork * 80 / 100)); 860 } 861 } 862 workspace.getAliasManager().updateAliases(this, getStore(), IResource.DEPTH_INFINITE, monitor); 864 } catch (OperationCanceledException e) { 865 workspace.getWorkManager().operationCanceled(); 866 throw e; 867 } finally { 868 workspace.endOperation(rule, true, Policy.subMonitorFor(monitor, Policy.endOpWork)); 869 } 870 } finally { 871 monitor.done(); 872 } 873 } 874 875 878 public void open(IProgressMonitor monitor) throws CoreException { 879 open(IResource.NONE, monitor); 880 } 881 882 891 public IStatus reconcileLinks(ProjectDescription newDescription) { 892 HashMap newLinks = newDescription.getLinks(); 893 String msg = Messages.links_errorLinkReconcile; 894 MultiStatus status = new MultiStatus(ResourcesPlugin.PI_RESOURCES, IResourceStatus.OPERATION_FAILED, msg, null); 895 ProjectDescription oldDescription = internalGetDescription(); 897 if (oldDescription != null) { 898 HashMap oldLinks = oldDescription.getLinks(); 899 if (oldLinks != null) { 900 for (Iterator it = oldLinks.values().iterator(); it.hasNext();) { 901 LinkDescription oldLink = (LinkDescription) it.next(); 902 Resource oldLinkResource = (Resource) findMember(oldLink.getProjectRelativePath()); 903 if (oldLinkResource == null || !oldLinkResource.isLinked()) 904 continue; 905 LinkDescription newLink = null; 906 if (newLinks != null) 907 newLink = (LinkDescription) newLinks.get(oldLink.getProjectRelativePath()); 908 if (newLink == null || !newLink.getLocationURI().equals(oldLinkResource.getLocationURI()) || newLink.getType() != oldLinkResource.getType()) { 910 try { 911 oldLinkResource.delete(IResource.NONE, null); 912 oldLinkResource.refreshLocal(IResource.DEPTH_INFINITE, null); 914 } catch (CoreException e) { 915 status.merge(e.getStatus()); 916 } 917 } 918 } 919 } 920 } 921 if (newLinks == null) 923 return status; 924 List sortedLinks = new ArrayList(newLinks.values()); 926 Collections.sort(sortedLinks); 927 for (Iterator it = sortedLinks.iterator(); it.hasNext();) { 928 LinkDescription newLink = (LinkDescription) it.next(); 929 try { 930 Resource toLink = workspace.newResource(getFullPath().append(newLink.getProjectRelativePath()), newLink.getType()); 931 IContainer parent = toLink.getParent(); 932 if (parent != null && !parent.exists() && parent.getType() == FOLDER) 933 ((Folder) parent).ensureExists(Policy.monitorFor(null)); 934 toLink.createLink(newLink.getLocationURI(), IResource.REPLACE | IResource.ALLOW_MISSING_LOCAL, null); 935 } catch (CoreException e) { 936 status.merge(e.getStatus()); 937 } 938 } 939 return status; 940 } 941 942 945 public void setDescription(IProjectDescription description, int updateFlags, IProgressMonitor monitor) throws CoreException { 946 monitor = Policy.monitorFor(monitor); 950 try { 951 monitor.beginTask(Messages.resources_setDesc, Policy.totalWork); 952 final ISchedulingRule rule = workspace.getRoot(); 953 try { 954 workspace.prepareOperation(rule, monitor); 956 ResourceInfo info = getResourceInfo(false, false); 957 checkAccessible(getFlags(info)); 958 ProjectDescription oldDescription = internalGetDescription(); 960 ProjectDescription newDescription = (ProjectDescription) description; 961 boolean hasPublicChanges = oldDescription.hasPublicChanges(newDescription); 962 boolean hasPrivateChanges = oldDescription.hasPrivateChanges(newDescription); 963 if (!hasPublicChanges && !hasPrivateChanges) 964 return; 965 checkDescription(this, newDescription, false); 966 boolean hadSavedDescription = true; 969 if (((updateFlags & IResource.FORCE) == 0)) { 970 hadSavedDescription = getLocalManager().hasSavedDescription(this); 971 if (hadSavedDescription && !getLocalManager().isDescriptionSynchronized(this)) { 972 String message = NLS.bind(Messages.resources_projectDescSync, getName()); 973 throw new ResourceException(IResourceStatus.OUT_OF_SYNC_LOCAL, getFullPath(), message, null); 974 } 975 } 976 if (!hadSavedDescription) 978 hadSavedDescription = workspace.getMetaArea().hasSavedProject(this); 979 workspace.broadcastEvent(LifecycleEvent.newEvent(LifecycleEvent.PRE_PROJECT_CHANGE, this)); 980 workspace.beginOperation(true); 981 MultiStatus status = basicSetDescription(newDescription, updateFlags); 982 if (hadSavedDescription && !status.isOK()) 983 throw new CoreException(status); 984 writeDescription(oldDescription, updateFlags, hasPublicChanges, hasPrivateChanges); 986 info = getResourceInfo(false, true); 988 info.incrementContentId(); 989 workspace.updateModificationStamp(info); 990 if (!hadSavedDescription) { 991 String msg = NLS.bind(Messages.resources_missingProjectMetaRepaired, getName()); 992 status.merge(new ResourceStatus(IResourceStatus.MISSING_DESCRIPTION_REPAIRED, getFullPath(), msg)); 993 } 994 if (!status.isOK()) 995 throw new CoreException(status); 996 } finally { 997 workspace.endOperation(rule, true, Policy.subMonitorFor(monitor, Policy.endOpWork)); 998 } 999 } finally { 1000 monitor.done(); 1001 } 1002 } 1003 1004 1007 public void setDescription(IProjectDescription description, IProgressMonitor monitor) throws CoreException { 1008 setDescription(description, IResource.KEEP_HISTORY, monitor); 1010 } 1011 1012 1018 protected void startup() throws CoreException { 1019 if (!isOpen()) 1020 return; 1021 workspace.broadcastEvent(LifecycleEvent.newEvent(LifecycleEvent.PRE_PROJECT_OPEN, this)); 1022 } 1023 1024 1027 public void touch(IProgressMonitor monitor) throws CoreException { 1028 monitor = Policy.monitorFor(monitor); 1029 try { 1030 String message = NLS.bind(Messages.resources_touch, getFullPath()); 1031 monitor.beginTask(message, Policy.totalWork); 1032 final ISchedulingRule rule = workspace.getRuleFactory().modifyRule(this); 1033 try { 1034 workspace.prepareOperation(rule, monitor); 1035 workspace.broadcastEvent(LifecycleEvent.newEvent(LifecycleEvent.PRE_PROJECT_CHANGE, this)); 1036 workspace.beginOperation(true); 1037 super.touch(Policy.subMonitorFor(monitor, Policy.opWork)); 1038 } catch (OperationCanceledException e) { 1039 workspace.getWorkManager().operationCanceled(); 1040 throw e; 1041 } finally { 1042 workspace.endOperation(rule, true, Policy.subMonitorFor(monitor, Policy.endOpWork)); 1043 } 1044 } finally { 1045 monitor.done(); 1046 } 1047 } 1048 1049 1054 protected void updateDescription() throws CoreException { 1055 if (ProjectDescription.isWriting) 1056 return; 1057 ProjectDescription.isReading = true; 1058 try { 1059 workspace.broadcastEvent(LifecycleEvent.newEvent(LifecycleEvent.PRE_PROJECT_CHANGE, this)); 1060 ProjectDescription description = getLocalManager().read(this, false); 1061 IStatus result = null; 1063 if (isOpen()) 1064 result = reconcileLinks(description); 1065 internalSetDescription(description, true); 1066 if (result != null && !result.isOK()) 1067 throw new CoreException(result); 1068 } finally { 1069 ProjectDescription.isReading = false; 1070 } 1071 } 1072 1073 1076 public void writeDescription(int updateFlags) throws CoreException { 1077 writeDescription(internalGetDescription(), updateFlags, true, true); 1078 } 1079 1080 1091 public void writeDescription(IProjectDescription description, int updateFlags, boolean hasPublicChanges, boolean hasPrivateChanges) throws CoreException { 1092 if (ProjectDescription.isReading) 1093 return; 1094 ProjectDescription.isWriting = true; 1095 try { 1096 getLocalManager().internalWrite(this, description, updateFlags, hasPublicChanges, hasPrivateChanges); 1097 } finally { 1098 ProjectDescription.isWriting = false; 1099 } 1100 } 1101} 1102 | Popular Tags |