Java Code Examples for com.intellij.openapi.roots.ProjectRootManager#getFileIndex()

The following examples show how to use com.intellij.openapi.roots.ProjectRootManager#getFileIndex() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: NewClassAction.java    From json2java4idea with Apache License 2.0 6 votes vote down vote up
@CheckReturnValue
@VisibleForTesting
@SuppressWarnings("WeakerAccess")
static boolean isAvailable(@Nonnull AnActionEvent event) {
    final Project project = event.getProject();
    if (project == null) {
        return false;
    }

    final IdeView view = event.getData(LangDataKeys.IDE_VIEW);
    if (view == null) {
        return false;
    }

    final ProjectRootManager rootManager = ProjectRootManager.getInstance(project);
    final ProjectFileIndex fileIndex = rootManager.getFileIndex();
    final Optional<PsiDirectory> sourceDirectory = Stream.of(view.getDirectories())
            .filter(directory -> {
                final VirtualFile virtualFile = directory.getVirtualFile();
                return fileIndex.isUnderSourceRootOfType(virtualFile, JavaModuleSourceRootTypes.SOURCES);
            })
            .findFirst();
    return sourceDirectory.isPresent();
}
 
Example 2
Source File: BaseProjectViewDirectoryHelper.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static List<VirtualFile> getTopLevelRoots(Project project) {
  List<VirtualFile> topLevelContentRoots = new ArrayList<>();
  ProjectRootManager prm = ProjectRootManager.getInstance(project);
  ProjectFileIndex index = prm.getFileIndex();

  for (VirtualFile root : prm.getContentRoots()) {
    VirtualFile parent = root.getParent();
    if (parent == null || !index.isInContent(parent)) {
      topLevelContentRoots.add(root);
    }
  }
  return topLevelContentRoots;
}
 
Example 3
Source File: ProjectFileIndexFacade.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Inject
public ProjectFileIndexFacade(final Project project, final ProjectRootManager rootManager, final DirectoryIndex directoryIndex) {
  super(project);
  myDirectoryIndex = directoryIndex;
  myFileIndex = rootManager.getFileIndex();
}
 
Example 4
Source File: RecursionDlg.java    From consulo with Apache License 2.0 4 votes vote down vote up
private void setupControls() {
  setTitle("Update Copyright");

  setOKButtonText("Run");

  ButtonGroup group = new ButtonGroup();
  group.add(rbFile);
  group.add(rbAll);

  rbFile.setMnemonic('F');
  rbAll.setMnemonic('A');
  cbSubdirs.setMnemonic('I');

  if (file.isDirectory()) {
    rbFile.setText("File");
    rbFile.setEnabled(false);
    rbAll.setText("All files in " + file.getPresentableUrl());
    rbAll.setSelected(true);
    cbSubdirs.setSelected(true);
    cbSubdirs.setEnabled(true);
  }
  else {
    VirtualFile parent = file.getParent();
    rbFile.setText("File '" + file.getPresentableUrl() + '\'');
    rbFile.setSelected(true);
    if (parent != null) {
      rbAll.setText("All files in " + parent.getPresentableUrl());
      cbSubdirs.setSelected(true);
      cbSubdirs.setEnabled(false);
    }
    else {
      rbAll.setVisible(false);
      cbSubdirs.setVisible(false);
    }
  }

  VirtualFile check = file;
  if (!file.isDirectory()) {
    check = file.getParent();
  }
  ProjectRootManager prm = ProjectRootManager.getInstance(project);
  ProjectFileIndex pfi = prm.getFileIndex();

  VirtualFile[] children = check != null ? check.getChildren() : VirtualFile.EMPTY_ARRAY;
  boolean hasSubdirs = false;
  for (int i = 0; i < children.length && !hasSubdirs; i++) {
    if (children[i].isDirectory() && !pfi.isExcluded(children[i])) {
      hasSubdirs = true;
    }
  }

  cbSubdirs.setVisible(hasSubdirs);
  if (!hasSubdirs) {
    cbSubdirs.setEnabled(false);
    mainPanel.remove(cbSubdirs);
  }

  ActionListener listener = new ActionListener() {
    public void actionPerformed(ActionEvent e) {
      if (cbSubdirs.isVisible()) {
        cbSubdirs.setEnabled(rbAll.isSelected());
      }
    }
  };

  rbFile.addActionListener(listener);
  rbAll.addActionListener(listener);
}
 
Example 5
Source File: DirectoryNode.java    From consulo with Apache License 2.0 4 votes vote down vote up
public DirectoryNode(VirtualFile aDirectory,
                     Project project,
                     boolean compactPackages,
                     boolean showFQName,
                     VirtualFile baseDir, final VirtualFile[] contentRoots) {
  super(project);
  myVDirectory = aDirectory;
  final ProjectRootManager projectRootManager = ProjectRootManager.getInstance(project);
  final ProjectFileIndex index = projectRootManager.getFileIndex();
  String dirName = aDirectory.getName();
  if (showFQName) {
    final VirtualFile contentRoot = index.getContentRootForFile(myVDirectory);
    if (contentRoot != null) {
      if (Comparing.equal(myVDirectory, contentRoot)) {
        myFQName = dirName;
      }
      else {
        final VirtualFile sourceRoot = index.getSourceRootForFile(myVDirectory);
        if (Comparing.equal(myVDirectory, sourceRoot)) {
          myFQName = VfsUtilCore.getRelativePath(myVDirectory, contentRoot, '/');
        }
        else if (sourceRoot != null) {
          myFQName = VfsUtilCore.getRelativePath(myVDirectory, sourceRoot, '/');
        }
        else {
          myFQName = VfsUtilCore.getRelativePath(myVDirectory, contentRoot, '/');
        }
      }

      if (contentRoots.length > 1 && ProjectRootsUtil.isModuleContentRoot(myVDirectory, project)) {
        myFQName = getContentRootName(baseDir, myFQName);
      }
    }
    else {
      myFQName = FilePatternPackageSet.getLibRelativePath(myVDirectory, index);
    }
    dirName = myFQName;
  } else {
    if (contentRoots.length > 1 && ProjectRootsUtil.isModuleContentRoot(myVDirectory, project)) {
      dirName = getContentRootName(baseDir, dirName);
    }
  }
  myDirName = dirName;
  myCompactPackages = compactPackages;
}