Exemplo n.º 1
0
  /**
   * Return true if the IFile with the given name exists in this project.
   *
   * @param aFileName filename can be relative to one of the input file paths for the
   *     WorkbenchURIConverter.
   * @return <code>true</code> if filename exists in this project
   * @since 1.0.0
   */
  public boolean fileExists(String aFileName) {
    if (aFileName == null) return false;

    IPath path = new Path(aFileName);
    if (path.isAbsolute()) return ResourcesPlugin.getWorkspace().getRoot().getFile(path).exists();
    else return getWorkbenchURIConverter().canGetUnderlyingResource(aFileName);
  }
Exemplo n.º 2
0
  /**
   * Final check before the actual refactoring
   *
   * @return status
   * @throws OperationCanceledException
   */
  public RefactoringStatus checkFinalConditions() throws OperationCanceledException {
    RefactoringStatus status = new RefactoringStatus();
    IContainer destination = fProcessor.getDestination();
    IProject sourceProject = fProcessor.getSourceSelection()[0].getProject();
    IProject destinationProject = destination.getProject();
    if (sourceProject != destinationProject)
      status.merge(MoveUtils.checkMove(phpFiles, sourceProject, destination));

    // Checks if one of the resources already exists with the same name in
    // the destination
    IPath dest = fProcessor.getDestination().getFullPath();
    IResource[] sourceResources = fProcessor.getSourceSelection();

    for (IResource element : sourceResources) {
      String newFilePath = dest.toOSString() + File.separatorChar + element.getName();
      IResource resource =
          ResourcesPlugin.getWorkspace().getRoot().findMember(new Path(newFilePath));
      if (resource != null && resource.exists()) {
        status.merge(
            RefactoringStatus.createFatalErrorStatus(
                NLS.bind(
                    PhpRefactoringCoreMessages.getString("MoveDelegate.6"),
                    element.getName(),
                    dest.toOSString()))); // $NON-NLS-1$
      }
    }
    return status;
  }
 // parameter isFileMandatory is used to determine if at least one file must be selected
 // before being able to proceed to the next page
 public SelectMultiFilePage(
     IWorkbench workbench, IStructuredSelection selection, boolean isFileMandatory) {
   super("SelectMultiFilePage");
   this.workbench = workbench;
   this.selection = selection;
   this.isFileMandatory = isFileMandatory;
   this.workspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
   this.fileNames = null;
 }
 public void setVisible(boolean visible) {
   if (visible == true) {
     if (fFilters != null) {
       sourceFileViewer.resetFilters();
       for (Iterator i = fFilters.iterator(); i.hasNext(); )
         sourceFileViewer.addFilter((ViewerFilter) i.next());
     }
     sourceFileViewer.setInput(ResourcesPlugin.getWorkspace().getRoot());
   }
   super.setVisible(visible);
 }
 /* (non-Javadoc)
  * @see java.lang.Object#equals(java.lang.Object)
  */
 public boolean equals(Object other) {
   if (this == other) return true;
   if (!(other instanceof CVSCompareSubscriber)) return false;
   CVSCompareSubscriber s = (CVSCompareSubscriber) other;
   CVSResourceVariantTree tree1 = (CVSResourceVariantTree) getRemoteTree();
   CVSResourceVariantTree tree2 = (CVSResourceVariantTree) s.getRemoteTree();
   IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
   CVSTag tag1 = tree1.getTag(root);
   CVSTag tag2 = tree2.getTag(root);
   if (tag1 == null || tag2 == null) return false;
   return tag1.equals(tag2) && rootsEqual(s);
 }
 private IResource createSourceResource(IResourceDelta delta) {
   IPath sourcePath = delta.getMovedFromPath();
   IResource resource = delta.getResource();
   IWorkspaceRoot wsRoot = ResourcesPlugin.getWorkspace().getRoot();
   switch (resource.getType()) {
     case IResource.PROJECT:
       return wsRoot.getProject(sourcePath.segment(0));
     case IResource.FOLDER:
       return wsRoot.getFolder(sourcePath);
     case IResource.FILE:
       return wsRoot.getFile(sourcePath);
   }
   return null;
 }
Exemplo n.º 7
0
  /**
   * Returns the charset explicitly set by the user for the given resource, or <code>null</code>. If
   * no setting exists for the given resource and <code>recurse</code> is <code>true</code>, every
   * parent up to the workspace root will be checked until a charset setting can be found.
   *
   * @param resourcePath the path for the resource
   * @param recurse whether the parent should be queried
   * @return the charset setting for the given resource
   */
  public String getCharsetFor(IPath resourcePath, boolean recurse) {
    Assert.isLegal(resourcePath.segmentCount() >= 1);
    IProject project = workspace.getRoot().getProject(resourcePath.segment(0));

    Preferences prefs = getPreferences(project, false, false);
    Preferences derivedPrefs = getPreferences(project, false, true);

    if (prefs == null && derivedPrefs == null)
      // no preferences found - for performance reasons, short-circuit
      // lookup by falling back to workspace's default setting
      return recurse ? ResourcesPlugin.getEncoding() : null;

    return internalGetCharsetFor(prefs, derivedPrefs, resourcePath, recurse);
  }
Exemplo n.º 8
0
  private void createRenameLibraryFolderChange(
      IResource[] sourceResources, CompositeChange rootChange) {
    IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
    LibraryFolderManager lfm = LibraryFolderManager.getInstance();

    for (IResource resource : sourceResources) {
      if (resource.getType() == IResource.FOLDER && lfm.isInLibraryFolder(resource)) {
        IPath newPath = fMainDestinationPath.append(resource.getName());
        IFolder newFolder = root.getFolder(newPath);

        RenameLibraryFolderChange change =
            new RenameLibraryFolderChange((IFolder) resource, newFolder);
        rootChange.add(change);
      }
    }
  }
 private ISchedulingRule getSchedulingRule(IJavaElement element) {
   if (element == null) return null;
   IResource sourceResource = getResource(element);
   IResource destContainer = getResource(getDestinationParent(element));
   if (!(destContainer instanceof IContainer)) {
     return null;
   }
   String newName;
   try {
     newName = getNewNameFor(element);
   } catch (JavaModelException e) {
     return null;
   }
   if (newName == null) newName = element.getElementName();
   IResource destResource;
   String sourceEncoding = null;
   if (sourceResource.getType() == IResource.FILE) {
     destResource = ((IContainer) destContainer).getFile(new Path(newName));
     try {
       sourceEncoding = ((IFile) sourceResource).getCharset(false);
     } catch (CoreException ce) {
       // use default encoding
     }
   } else {
     destResource = ((IContainer) destContainer).getFolder(new Path(newName));
   }
   IResourceRuleFactory factory = ResourcesPlugin.getWorkspace().getRuleFactory();
   ISchedulingRule rule;
   if (isMove()) {
     rule = factory.moveRule(sourceResource, destResource);
   } else {
     rule = factory.copyRule(sourceResource, destResource);
   }
   if (sourceEncoding != null) {
     rule = new MultiRule(new ISchedulingRule[] {rule, factory.charsetRule(destResource)});
   }
   return rule;
 }
Exemplo n.º 10
0
  private String internalGetCharsetFor(
      Preferences prefs, Preferences derivedPrefs, IPath resourcePath, boolean recurse) {
    String charset = null;

    // try to find the encoding in regular and then derived preferences
    if (prefs != null) charset = prefs.get(getKeyFor(resourcePath), null);
    // derivedPrefs may be not null, only if #isDerivedEncodingStoredSeparately returns true
    // so the explicit check against #isDerivedEncodingStoredSeparately is not required
    if (charset == null && derivedPrefs != null)
      charset = derivedPrefs.get(getKeyFor(resourcePath), null);

    if (!recurse) return charset;

    while (charset == null && resourcePath.segmentCount() > 1) {
      resourcePath = resourcePath.removeLastSegments(1);
      // try to find the encoding in regular and then derived preferences
      if (prefs != null) charset = prefs.get(getKeyFor(resourcePath), null);
      if (charset == null && derivedPrefs != null)
        charset = derivedPrefs.get(getKeyFor(resourcePath), null);
    }

    // ensure we default to the workspace encoding if none is found
    return charset == null ? ResourcesPlugin.getEncoding() : charset;
  }
 private IProject getNLProject(final IPluginModelBase name, final Locale locale) {
   return ResourcesPlugin.getWorkspace().getRoot().getProject(pluginName(name, locale));
 }
  /**
   * Copies/moves a package fragment with the name <code>newName</code> to the destination package.
   * <br>
   *
   * @exception JavaModelException if the operation is unable to complete
   */
  private void processPackageFragmentResource(
      PackageFragment source, PackageFragmentRoot root, String newName) throws JavaModelException {
    try {
      String[] newFragName = (newName == null) ? source.names : Util.getTrimmedSimpleNames(newName);
      PackageFragment newFrag = root.getPackageFragment(newFragName);
      IResource[] resources = collectResourcesOfInterest(source);

      // if isMove() can we move the folder itself ? (see
      // http://bugs.eclipse.org/bugs/show_bug.cgi?id=22458)
      boolean shouldMoveFolder =
          isMove() && !newFrag.resource().exists(); // if new pkg fragment exists, it is an override
      IFolder srcFolder = (IFolder) source.resource();
      IPath destPath = newFrag.getPath();
      if (shouldMoveFolder) {
        // check if destination is not included in source
        if (srcFolder.getFullPath().isPrefixOf(destPath)) {
          shouldMoveFolder = false;
        } else {
          // check if there are no sub-packages
          IResource[] members = srcFolder.members();
          for (int i = 0; i < members.length; i++) {
            if (members[i] instanceof IFolder) {
              shouldMoveFolder = false;
              break;
            }
          }
        }
      }
      boolean containsReadOnlySubPackageFragments =
          createNeededPackageFragments(
              (IContainer) source.parent.resource(), root, newFragName, shouldMoveFolder);
      boolean sourceIsReadOnly = Util.isReadOnly(srcFolder);

      // Process resources
      if (shouldMoveFolder) {
        // move underlying resource
        // TODO Revisit once bug 43044 is fixed
        if (sourceIsReadOnly) {
          Util.setReadOnly(srcFolder, false);
        }
        srcFolder.move(destPath, this.force, true /* keep history */, getSubProgressMonitor(1));
        if (sourceIsReadOnly) {
          Util.setReadOnly(srcFolder, true);
        }
        setAttribute(HAS_MODIFIED_RESOURCE_ATTR, TRUE);
      } else {
        // process the leaf resources
        if (resources.length > 0) {
          if (isRename()) {
            if (!destPath.equals(source.getPath())) {
              moveResources(resources, destPath);
            }
          } else if (isMove()) {
            // we need to delete this resource if this operation wants to override existing
            // resources
            for (int i = 0, max = resources.length; i < max; i++) {
              IResource destinationResource =
                  ResourcesPlugin.getWorkspace()
                      .getRoot()
                      .findMember(destPath.append(resources[i].getName()));
              if (destinationResource != null) {
                if (this.force) {
                  deleteResource(destinationResource, IResource.KEEP_HISTORY);
                } else {
                  throw new JavaModelException(
                      new JavaModelStatus(
                          IJavaModelStatusConstants.NAME_COLLISION,
                          Messages.bind(
                              Messages.status_nameCollision,
                              destinationResource.getFullPath().toString())));
                }
              }
            }
            moveResources(resources, destPath);
          } else {
            // we need to delete this resource if this operation wants to override existing
            // resources
            for (int i = 0, max = resources.length; i < max; i++) {
              IResource destinationResource =
                  ResourcesPlugin.getWorkspace()
                      .getRoot()
                      .findMember(destPath.append(resources[i].getName()));
              if (destinationResource != null) {
                if (this.force) {
                  // we need to delete this resource if this operation wants to override existing
                  // resources
                  deleteResource(destinationResource, IResource.KEEP_HISTORY);
                } else {
                  throw new JavaModelException(
                      new JavaModelStatus(
                          IJavaModelStatusConstants.NAME_COLLISION,
                          Messages.bind(
                              Messages.status_nameCollision,
                              destinationResource.getFullPath().toString())));
                }
              }
            }
            copyResources(resources, destPath);
          }
        }
      }

      // Update package statement in compilation unit if needed
      if (!Util.equalArraysOrNull(
          newFragName, source.names)) { // if package has been renamed, update the compilation units
        char[][] inclusionPatterns = root.fullInclusionPatternChars();
        char[][] exclusionPatterns = root.fullExclusionPatternChars();
        for (int i = 0; i < resources.length; i++) {
          String resourceName = resources[i].getName();
          if (Util.isJavaLikeFileName(resourceName)) {
            // we only consider potential compilation units
            ICompilationUnit cu = newFrag.getCompilationUnit(resourceName);
            if (Util.isExcluded(
                cu.getPath(), inclusionPatterns, exclusionPatterns, false /*not a folder*/))
              continue;
            this.parser.setSource(cu);
            CompilationUnit astCU = (CompilationUnit) this.parser.createAST(this.progressMonitor);
            AST ast = astCU.getAST();
            ASTRewrite rewrite = ASTRewrite.create(ast);
            updatePackageStatement(astCU, newFragName, rewrite, cu);
            TextEdit edits = rewrite.rewriteAST();
            applyTextEdit(cu, edits);
            cu.save(null, false);
          }
        }
      }

      // Discard empty old package (if still empty after the rename)
      boolean isEmpty = true;
      if (isMove()) {
        // delete remaining files in this package (.class file in the case where Proj=src=bin)
        // in case of a copy
        updateReadOnlyPackageFragmentsForMove(
            (IContainer) source.parent.resource(), root, newFragName, sourceIsReadOnly);
        if (srcFolder.exists()) {
          IResource[] remaining = srcFolder.members();
          for (int i = 0, length = remaining.length; i < length; i++) {
            IResource file = remaining[i];
            if (file instanceof IFile) {
              if (Util.isReadOnly(file)) {
                Util.setReadOnly(file, false);
              }
              deleteResource(file, IResource.FORCE | IResource.KEEP_HISTORY);
            } else {
              isEmpty = false;
            }
          }
        }
        if (isEmpty) {
          IResource rootResource;
          // check if source is included in destination
          if (destPath.isPrefixOf(srcFolder.getFullPath())) {
            rootResource = newFrag.resource();
          } else {
            rootResource = source.parent.resource();
          }

          // delete recursively empty folders
          deleteEmptyPackageFragment(source, false, rootResource);
        }
      } else if (containsReadOnlySubPackageFragments) {
        // in case of a copy
        updateReadOnlyPackageFragmentsForCopy(
            (IContainer) source.parent.resource(), root, newFragName);
      }
      // workaround for bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=24505
      if (isEmpty && isMove() && !(Util.isExcluded(source) || Util.isExcluded(newFrag))) {
        IJavaProject sourceProject = source.getJavaProject();
        getDeltaFor(sourceProject).movedFrom(source, newFrag);
        IJavaProject destProject = newFrag.getJavaProject();
        getDeltaFor(destProject).movedTo(newFrag, source);
      }
    } catch (JavaModelException e) {
      throw e;
    } catch (CoreException ce) {
      throw new JavaModelException(ce);
    }
  }
Exemplo n.º 13
0
 public static IWorkspaceRoot workspaceRoot() {
   return ResourcesPlugin.getWorkspace().getRoot();
 }
Exemplo n.º 14
0
 public static IWorkspace getWorkspace() {
   return ResourcesPlugin.getWorkspace();
 }
 public CVSTag getTag() {
   return tree.getTag(ResourcesPlugin.getWorkspace().getRoot());
 }