/*
  * Determine if the resource is a descendant of an orphaned subtree.
  * If it is, purge the CVS folders of the subtree.
  */
 private void handleOrphanedSubtree(IResource resource) {
   try {
     if (!CVSWorkspaceRoot.isSharedWithCVS(resource)) return;
     ICVSFolder folder;
     if (resource.getType() == IResource.FILE) {
       folder = CVSWorkspaceRoot.getCVSFolderFor(resource.getParent());
     } else {
       folder = CVSWorkspaceRoot.getCVSFolderFor((IContainer) resource);
     }
     handleOrphanedSubtree(folder);
   } catch (CVSException e) {
     CVSProviderPlugin.log(e);
   }
 }
 /*
  * Recursively check for and handle orphaned CVS folders
  */
 private void handleOrphanedSubtree(final ICVSFolder folder) throws CVSException {
   if (folder.getIResource().getType() == IResource.PROJECT) return;
   if (CVSWorkspaceRoot.isOrphanedSubtree((IContainer) folder.getIResource())) {
     try {
       run(
           new IRunnableWithProgress() {
             public void run(IProgressMonitor monitor)
                 throws InvocationTargetException, InterruptedException {
               try {
                 folder.unmanage(null);
               } catch (CVSException e) {
                 CVSProviderPlugin.log(e);
               }
             }
           },
           true,
           PROGRESS_BUSYCURSOR);
     } catch (InvocationTargetException e) {
       // Ignore this since we logged the one we care about above
     } catch (InterruptedException e) {
       throw new OperationCanceledException();
     }
   }
   handleOrphanedSubtree(folder.getParent());
 }
  /**
   * Prime the remote tree with the sync info from the local workspace. This is done to ensure that
   * we don't get a huge number of outgoing changes before the first refresh.
   */
  public void primeRemoteTree() throws CVSException {
    for (int i = 0; i < resources.length; i++) {
      IResource resource = resources[i];
      ICVSResource cvsResource = CVSWorkspaceRoot.getCVSResourceFor(resource);
      cvsResource.accept(
          new ICVSResourceVisitor() {
            public void visitFile(ICVSFile file) throws CVSException {
              byte[] bytes = file.getSyncBytes();
              if (bytes != null) {
                try {
                  tree.getByteStore().setBytes(file.getIResource(), bytes);
                } catch (TeamException e) {
                  throw CVSException.wrapException(e);
                }
              }
            }

            public void visitFolder(ICVSFolder folder) throws CVSException {
              // No need to copy sync info for folders since
              // CVS resource variant tree will get missing
              // folder info from the local resources
              folder.acceptChildren(this);
            }
          });
    }
  }
  /**
   * The action is enabled for the appropriate resources. This method checks that:
   *
   * <ol>
   *   <li>there is no overlap between a selected file and folder (overlapping folders is allowed
   *       because of logical vs. physical mapping problem in views)
   *   <li>the state of the resources match the conditions provided by:
   *       <ul>
   *         <li>isEnabledForIgnoredResources()
   *         <li>isEnabledForManagedResources()
   *         <li>isEnabledForUnManagedResources() (i.e. not ignored and not managed)
   *       </ul>
   * </ol>
   *
   * @see TeamAction#isEnabled()
   */
  public boolean isEnabled() {

    // allow the super to decide enablement. if the super doesn't know it will return false.
    boolean enabled = super.isEnabled();
    if (enabled) return true;

    // invoke the inherited method so that overlaps are maintained
    IResource[] resources = getSelectedResourcesWithOverlap();

    // disable if no resources are selected
    if (resources.length == 0) return false;

    // disable properly for single resource enablement
    if (!isEnabledForMultipleResources() && resources.length != 1) return false;

    // validate enabled for each resource in the selection
    List folderPaths = new ArrayList();
    List filePaths = new ArrayList();
    for (int i = 0; i < resources.length; i++) {
      IResource resource = resources[i];

      // only enable for accessible resources
      if (resource.getType() == IResource.PROJECT) {
        if (!resource.isAccessible()) return false;
      }

      // no CVS actions are enabled if the selection contains a linked resource
      if (CVSWorkspaceRoot.isLinkedResource(resource)) return false;

      // only enable for resources in a project shared with CVS
      if (RepositoryProvider.getProvider(resource.getProject(), CVSProviderPlugin.getTypeId())
          == null) {
        return false;
      }

      // collect files and folders separately to check for overlap later
      IPath resourceFullPath = resource.getFullPath();
      if (resource.getType() == IResource.FILE) {
        filePaths.add(resourceFullPath);
      } else {
        folderPaths.add(resourceFullPath);
      }

      // ensure that resource management state matches what the action requires
      ICVSResource cvsResource = getCVSResourceFor(resource);
      try {
        if (!isEnabledForCVSResource(cvsResource)) {
          return false;
        }
      } catch (CVSException e) {
        if (!isEnabledForException(e)) return false;
      }
    }
    // Ensure that there is no overlap between files and folders
    // NOTE: folder overlap must be allowed because of logical vs. physical
    if (!folderPaths.isEmpty()) {
      for (Iterator fileIter = filePaths.iterator(); fileIter.hasNext(); ) {
        IPath resourcePath = (IPath) fileIter.next();
        for (Iterator it = folderPaths.iterator(); it.hasNext(); ) {
          IPath folderPath = (IPath) it.next();
          if (folderPath.isPrefixOf(resourcePath)) {
            return false;
          }
        }
      }
    }
    return true;
  }