예제 #1
0
  /**
   * @return a collection of resources which are associated with this problem. The collection might
   *     be empty.
   */
  public Collection<IResource> getResources() {
    if (resources == null) {
      resources = new LinkedList<IResource>();
      if (file != null) {
        URI fileUri;
        if (!file.isAbsolute()) {
          // make file absolute and convert to URI (does not work the
          // other way round, because File.toURI always returns an
          // absolute URI
          fileUri = new File(new File(project.getLocationURI().getPath()), file.toString()).toURI();
        } else {
          fileUri = file.toURI();
        }

        // find file in workspace (absolute path)
        IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();

        // consider linked paths also
        IFile[] file = root.findFilesForLocationURI(fileUri);
        resources.addAll(Arrays.asList(file));
      }
      if (resources.isEmpty()) {
        // if no resource could be resolved take project
        resources.add(project);
      }
    }
    return resources;
  }
예제 #2
0
  @Override
  public IResource[] getResourcesFromCommand(List<String> cmd, URI buildDirectoryURI) {
    // Start at the back looking for arguments
    List<IResource> resources = new ArrayList<>();
    IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
    for (int i = cmd.size() - 1; i >= 0; --i) {
      String arg = cmd.get(i);
      if (arg.startsWith("-")) { // $NON-NLS-1$
        // ran into an option, we're done.
        break;
      }
      Path srcPath = Paths.get(arg);
      URI uri;
      if (srcPath.isAbsolute()) {
        uri = srcPath.toUri();
      } else {
        try {
          uri = buildDirectoryURI.resolve(arg);
        } catch (IllegalArgumentException e) {
          // Bad URI
          continue;
        }
      }

      for (IFile resource : root.findFilesForLocationURI(uri)) {
        resources.add(resource);
      }
    }

    return resources.toArray(new IResource[resources.size()]);
  }
  protected IFile findFileInWorkspace(IPath path) {
    IFile file = null;
    if (path.isAbsolute()) {
      IWorkspaceRoot root = getProject().getWorkspace().getRoot();

      // construct a URI, based on the project's locationURI, that points
      // to the given path
      URI projectURI = project.getLocationURI();

      URI newURI =
          EFSExtensionManager.getDefault().createNewURIFromPath(projectURI, path.toString());

      IFile[] files = root.findFilesForLocationURI(newURI);

      for (int i = 0; i < files.length; i++) {
        if (files[i].getProject().equals(getProject())) {
          file = files[i];
          break;
        }
      }

    } else {
      file = getProject().getFile(path);
    }
    return file;
  }
예제 #4
0
파일: CommitUI.java 프로젝트: chengn/egit
 /**
  * Retrieves a collection of files that may be committed based on the user's selection when they
  * performed the commit action. That is, even if the user only selected one folder when the action
  * was performed, if the folder contains any files that could be committed, they will be returned.
  *
  * @return a collection of files that is eligible to be committed based on the user's selection
  */
 private Set<String> getSelectedFiles() {
   Set<String> preselectionCandidates = new LinkedHashSet<String>();
   IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
   // iterate through all the files that may be committed
   for (String fileName : files) {
     URI uri = new File(repo.getWorkTree(), fileName).toURI();
     IFile[] workspaceFiles = root.findFilesForLocationURI(uri);
     if (workspaceFiles.length > 0) {
       IFile file = workspaceFiles[0];
       for (IResource resource : selectedResources) {
         // if any selected resource contains the file, add it as a
         // preselection candidate
         if (resource.contains(file)) {
           preselectionCandidates.add(fileName);
           break;
         }
       }
     } else {
       // could be file outside of workspace
       for (IResource resource : selectedResources) {
         if (resource.getFullPath().toFile().equals(new File(uri))) {
           preselectionCandidates.add(fileName);
         }
       }
     }
   }
   return preselectionCandidates;
 }
  /**
   * Return all handles to Resource files for the given URI (in absolute form)
   *
   * @param filePathURI - Absolute URI to the file
   * @return - File handles to resource files within the workspace
   */
  public static IFile[] getWorkSpaceFiles(URI filePathURI) {
    IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();

    IFile[] files = null;
    if (filePathURI != null) {
      files = filterNonExistentFiles(root.findFilesForLocationURI(filePathURI));
    }

    return files;
  }
예제 #6
0
  /**
   * Finds the corresponding project for a file system location, or null if it is outside the
   * workspace.
   *
   * @param fileSystemLocation
   * @return containing project, or null
   */
  public static IProject findContainingProject(String fileSystemLocation) {
    if (fileSystemLocation == null) {
      return null;
    }

    // see if we can convert the fileSystemLocation to an IResource
    URI location = new File(fileSystemLocation).toURI();

    IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
    IResource[] maybes = root.findContainersForLocationURI(location);
    if (maybes.length == 0) {
      maybes = root.findFilesForLocationURI(location);
    }
    if (maybes.length > 0) {
      return maybes[0].getProject();
    } else {
      return null;
    }
  }
  private ISourceContainer getArchiveSourceContainer(String location) throws JavaModelException {
    IWorkspaceRoot root = PDELaunchingPlugin.getWorkspace().getRoot();
    IFile[] containers = root.findFilesForLocationURI(URIUtil.toURI(location));
    for (int i = 0; i < containers.length; i++) {
      IJavaElement element = JavaCore.create(containers[i]);
      if (element instanceof IPackageFragmentRoot) {
        IPackageFragmentRoot archive = (IPackageFragmentRoot) element;
        IPath path = archive.getSourceAttachmentPath();
        if (path == null || path.segmentCount() == 0) continue;

        IPath rootPath = archive.getSourceAttachmentRootPath();
        boolean detectRootPath = rootPath != null && rootPath.segmentCount() > 0;

        IFile archiveFile = root.getFile(path);
        if (archiveFile.exists()) return new ArchiveSourceContainer(archiveFile, detectRootPath);

        File file = path.toFile();
        if (file.exists())
          return new ExternalArchiveSourceContainer(file.getAbsolutePath(), detectRootPath);
      }
    }
    return null;
  }
 /**
  * Returns the resource being edited in the given workbench part.
  *
  * @param part Workbench part to checm.
  * @return Resource being edited.
  */
 protected static IResource getResource(IWorkbenchPart part) {
   IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
   if (part instanceof IEditorPart) {
     IEditorInput editorInput = ((IEditorPart) part).getEditorInput();
     IResource resource = null;
     if (editorInput instanceof IFileEditorInput) {
       resource = ((IFileEditorInput) editorInput).getFile();
     } else if (editorInput instanceof ExternalEditorInput) {
       resource = ((ExternalEditorInput) editorInput).getMarkerResource();
     }
     if (resource != null) return resource;
     /* This file is not in a project, let default case handle it */
     ILocationProvider provider =
         (ILocationProvider) editorInput.getAdapter(ILocationProvider.class);
     if (provider != null) {
       IPath location = provider.getPath(editorInput);
       if (location != null) {
         IFile[] files = root.findFilesForLocationURI(URIUtil.toURI(location));
         if (files.length > 0 && files[0].isAccessible()) return files[0];
       }
     }
   }
   return root;
 }
  @Override
  protected String browse(final SapphireRenderingContext context) {
    final Property property = property();
    final List<Path> roots = getBasePaths();
    String selectedAbsolutePath = null;

    final List<String> extensions;

    if (this.fileExtensionService == null) {
      extensions = this.staticFileExtensionsList;
    } else {
      extensions = this.fileExtensionService.extensions();
    }

    if (enclosed()) {
      final List<IContainer> baseContainers = new ArrayList<IContainer>();

      for (Path path : roots) {
        final IContainer baseContainer = getWorkspaceContainer(path.toFile());

        if (baseContainer != null) {
          baseContainers.add(baseContainer);
        } else {
          break;
        }
      }

      final ITreeContentProvider contentProvider;
      final ILabelProvider labelProvider;
      final ViewerComparator viewerComparator;
      final Object input;

      if (roots.size() == baseContainers.size()) {
        // All paths are in the Eclipse Workspace. Use the available content and label
        // providers.

        contentProvider = new WorkspaceContentProvider(baseContainers);
        labelProvider = new WorkbenchLabelProvider();
        viewerComparator = new ResourceComparator();
        input = ResourcesPlugin.getWorkspace().getRoot();
      } else {
        // At least one of the roots is not in the Eclipse Workspace. Use custom file
        // system content and label providers.

        contentProvider = new FileSystemContentProvider(roots);
        labelProvider = new FileSystemLabelProvider(context);
        viewerComparator = new FileSystemNodeComparator();
        input = new Object();
      }

      final ElementTreeSelectionDialog dialog =
          new ElementTreeSelectionDialog(context.getShell(), labelProvider, contentProvider);

      dialog.setTitle(property.definition().getLabel(false, CapitalizationType.TITLE_STYLE, false));
      dialog.setMessage(
          createBrowseDialogMessage(
              property.definition().getLabel(true, CapitalizationType.NO_CAPS, false)));
      dialog.setAllowMultiple(false);
      dialog.setHelpAvailable(false);
      dialog.setInput(input);
      dialog.setComparator(viewerComparator);

      final Path currentPathAbsolute = convertToAbsolute((Path) ((Value<?>) property).content());

      if (currentPathAbsolute != null) {
        Object initialSelection = null;

        if (contentProvider instanceof WorkspaceContentProvider) {
          final URI uri = currentPathAbsolute.toFile().toURI();
          final IWorkspaceRoot wsroot = ResourcesPlugin.getWorkspace().getRoot();

          final IFile[] files = wsroot.findFilesForLocationURI(uri);

          if (files.length > 0) {
            final IFile file = files[0];

            if (file.exists()) {
              initialSelection = file;
            }
          }

          if (initialSelection == null) {
            final IContainer[] containers = wsroot.findContainersForLocationURI(uri);

            if (containers.length > 0) {
              final IContainer container = containers[0];

              if (container.exists()) {
                initialSelection = container;
              }
            }
          }
        } else {
          initialSelection =
              ((FileSystemContentProvider) contentProvider).find(currentPathAbsolute);
        }

        if (initialSelection != null) {
          dialog.setInitialSelection(initialSelection);
        }
      }

      if (this.type == FileSystemResourceType.FILE) {
        dialog.setValidator(new FileSelectionStatusValidator());
      } else if (this.type == FileSystemResourceType.FOLDER) {
        dialog.addFilter(new ContainersOnlyViewerFilter());
      }

      if (!extensions.isEmpty()) {
        dialog.addFilter(new ExtensionBasedViewerFilter(extensions));
      }

      if (dialog.open() == Window.OK) {
        final Object firstResult = dialog.getFirstResult();

        if (firstResult instanceof IResource) {
          selectedAbsolutePath = ((IResource) firstResult).getLocation().toString();
        } else {
          selectedAbsolutePath = ((FileSystemNode) firstResult).getFile().getPath();
        }
      }
    } else if (this.type == FileSystemResourceType.FOLDER) {
      final DirectoryDialog dialog = new DirectoryDialog(context.getShell());
      dialog.setText(
          property.definition().getLabel(true, CapitalizationType.FIRST_WORD_ONLY, false));
      dialog.setMessage(
          createBrowseDialogMessage(
              property.definition().getLabel(true, CapitalizationType.NO_CAPS, false)));

      final Value<?> value = (Value<?>) property;
      final Path path = (Path) value.content();

      if (path != null) {
        dialog.setFilterPath(path.toOSString());
      } else if (roots.size() > 0) {
        dialog.setFilterPath(roots.get(0).toOSString());
      }

      selectedAbsolutePath = dialog.open();
    } else {
      final FileDialog dialog = new FileDialog(context.getShell());
      dialog.setText(
          property.definition().getLabel(true, CapitalizationType.FIRST_WORD_ONLY, false));

      final Value<?> value = (Value<?>) property;
      final Path path = (Path) value.content();

      if (path != null && path.segmentCount() > 1) {
        dialog.setFilterPath(path.removeLastSegments(1).toOSString());
        dialog.setFileName(path.lastSegment());
      } else if (roots.size() > 0) {
        dialog.setFilterPath(roots.get(0).toOSString());
      }

      if (!extensions.isEmpty()) {
        final StringBuilder buf = new StringBuilder();

        for (String extension : extensions) {
          if (buf.length() > 0) {
            buf.append(';');
          }

          buf.append("*.");
          buf.append(extension);
        }

        dialog.setFilterExtensions(new String[] {buf.toString()});
      }

      selectedAbsolutePath = dialog.open();
    }

    if (selectedAbsolutePath != null) {
      final Path relativePath = convertToRelative(new Path(selectedAbsolutePath));

      if (relativePath != null) {
        String result = relativePath.toPortableString();

        if (this.includeLeadingSlash) {
          result = "/" + result;
        }

        return result;
      }
    }

    return null;
  }