com.intellij.ide.projectView.impl.nodes.PsiDirectoryNode Java Examples

The following examples show how to use com.intellij.ide.projectView.impl.nodes.PsiDirectoryNode. 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: SpeedSearch.java    From netbeans-mmd-plugin with Apache License 2.0 6 votes vote down vote up
@Override
protected boolean isMatchingElement(Object element, String pattern) {
  Object userObject = ((DefaultMutableTreeNode) ((TreePath) element).getLastPathComponent()).getUserObject();
  if (userObject instanceof PsiDirectoryNode) {
    String str = getElementText(element);
    if (str == null) {
      return false;
    }
    str = str.toLowerCase();
    if (pattern.indexOf('.') >= 0) {
      return compare(str, pattern);
    }
    StringTokenizer tokenizer = new StringTokenizer(str, ".");
    while (tokenizer.hasMoreTokens()) {
      String token = tokenizer.nextToken();
      if (compare(token, pattern)) {
        return true;
      }
    }
    return false;
  } else {
    return super.isMatchingElement(element, pattern);
  }
}
 
Example #2
Source File: VirtualFileTreeNode.java    From intellij-pants-plugin with Apache License 2.0 6 votes vote down vote up
@Override
protected void update(@NotNull PresentationData presentation) {
  final PsiManager psiManager = PsiManager.getInstance(myProject);
  final VirtualFile virtualFile = getValue();
  final PsiFile psiElement = virtualFile.isValid() ? psiManager.findFile(virtualFile) : null;
  if (psiElement instanceof PsiDirectory) {
    new PsiDirectoryNode(myProject, (PsiDirectory)psiElement, getSettings()).update(presentation);
  }
  else if (psiElement != null) {
    new PsiFileNode(myProject, psiElement, getSettings()).update(presentation);
  }
  else {
    presentation.setPresentableText(virtualFile.getName());
    presentation.setIcon(virtualFile.isDirectory() ? AllIcons.Nodes.Folder : virtualFile.getFileType().getIcon());
  }
}
 
Example #3
Source File: VirtualFileTreeNode.java    From intellij-pants-plugin with Apache License 2.0 6 votes vote down vote up
@NotNull
@Override
public Collection<? extends AbstractTreeNode<?>> getChildren() {
  final PsiManager psiManager = PsiManager.getInstance(myProject);
  final VirtualFile virtualFile = getValue();
  return ContainerUtil.mapNotNull(
    virtualFile.isValid() && virtualFile.isDirectory() ? virtualFile.getChildren() : VirtualFile.EMPTY_ARRAY,
    new Function<VirtualFile, AbstractTreeNode<?>>() {
      @Override
      public AbstractTreeNode<?> fun(VirtualFile file) {
        final PsiElement psiElement = file.isDirectory() ? psiManager.findDirectory(file) : psiManager.findFile(file);
        if (psiElement instanceof PsiDirectory && ModuleUtil.findModuleForPsiElement(psiElement) != null) {
          // PsiDirectoryNode doesn't render files outside of a project
          // let's use PsiDirectoryNode only for folders in a modules
          return new PsiDirectoryNode(myProject, (PsiDirectory)psiElement, getSettings());
        }
        else if (psiElement instanceof PsiFile) {
          return new PsiFileNode(myProject, (PsiFile)psiElement, getSettings());
        }
        else {
          return shouldShow(file) ? new VirtualFileTreeNode(myProject, file, getSettings()) : null;
        }
      }
    }
  );
}
 
Example #4
Source File: PantsTreeStructureProvider.java    From intellij-pants-plugin with Apache License 2.0 6 votes vote down vote up
@NotNull
@Override
public Collection<AbstractTreeNode<?>> modify(
  @NotNull final AbstractTreeNode<?> node,
  @NotNull Collection<AbstractTreeNode<?>> collection,
  ViewSettings settings
) {
  Project project = node.getProject();
  if (project == null || !(node instanceof PsiDirectoryNode)) return collection;
  PsiDirectoryNode directory = (PsiDirectoryNode) node;

  List<PsiFileNode> newNodes =
    Optional.ofNullable(getModuleOf(directory))
      .filter(module -> isModuleRoot(directory, module))
      .flatMap(PantsUtil::findModuleAddress)
      .flatMap(buildPAth -> PantsUtil.findFileRelativeToBuildRoot(project, buildPAth))
      .filter(buildFile -> !alreadyExists(collection, buildFile))
      .map(buildFile -> createNode(settings, project, buildFile))
      .orElseGet(Collections::emptyList);

  if (newNodes.isEmpty()) return collection;

  return Stream.concat(collection.stream(), newNodes.stream()).collect(Collectors.toList());
}
 
Example #5
Source File: AbstractProjectViewPSIPane.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Override
protected boolean isMatchingElement(Object element, String pattern) {
  Object userObject = ((DefaultMutableTreeNode)((TreePath)element).getLastPathComponent()).getUserObject();
  if (userObject instanceof PsiDirectoryNode) {
    String str = getElementText(element);
    if (str == null) return false;
    str = str.toLowerCase();
    if (pattern.indexOf('.') >= 0) {
      return compare(str, pattern);
    }
    StringTokenizer tokenizer = new StringTokenizer(str, ".");
    while (tokenizer.hasMoreTokens()) {
      String token = tokenizer.nextToken();
      if (compare(token, pattern)) {
        return true;
      }
    }
    return false;
  }
  else {
    return super.isMatchingElement(element, pattern);
  }
}
 
Example #6
Source File: BlazePsiDirectoryNode.java    From intellij with Apache License 2.0 5 votes vote down vote up
private static AbstractTreeNode<?> wrap(AbstractTreeNode<?> node) {
  if (!(node instanceof PsiDirectoryNode)) {
    return node;
  }
  PsiDirectoryNode dir = (PsiDirectoryNode) node;
  return dir instanceof BlazePsiDirectoryNode
      ? dir
      : new BlazePsiDirectoryNode(dir.getProject(), dir.getValue(), dir.getSettings());
}
 
Example #7
Source File: PackageDecoration.java    From SVNToolBox with Apache License 2.0 5 votes vote down vote up
@Override
public boolean isForMe(ProjectViewNode node) {
    if (node instanceof PsiDirectoryNode) {
        PsiDirectoryNode dirNode = (PsiDirectoryNode) node;
        final Project project = dirNode.getProject();
        final PsiDirectory psiDirectory = dirNode.getValue();
        if (project != null && psiDirectory != null) {
            final VirtualFile directoryFile = psiDirectory.getVirtualFile();
            return ProjectRootsUtil.isInSource(directoryFile, project) ||
                    ProjectRootsUtil.isInTestSource(directoryFile, project);
        }
    }
    return false;
}
 
Example #8
Source File: ModuleDecoration.java    From SVNToolBox with Apache License 2.0 5 votes vote down vote up
@Override
public boolean isForMe(ProjectViewNode node) {
    if (node instanceof PsiDirectoryNode) {
        PsiDirectoryNode dirNode = (PsiDirectoryNode) node;
        final Object parentValue = dirNode.getParent().getValue();
        return parentValue instanceof Project || parentValue instanceof ModuleGroup;
    }
    return false;
}
 
Example #9
Source File: ContentRootDecoration.java    From SVNToolBox with Apache License 2.0 5 votes vote down vote up
@Override
public boolean isForMe(ProjectViewNode node) {
    if (node instanceof PsiDirectoryNode) {
        PsiDirectoryNode dirNode = (PsiDirectoryNode) node;            
        final PsiDirectory psiDirectory = dirNode.getValue();
        if (psiDirectory != null) {
            final Project project = dirNode.getProject();
            final VirtualFile directoryFile = psiDirectory.getVirtualFile();
            return ProjectRootsUtil.isModuleContentRoot(directoryFile, project)
                    || ProjectRootsUtil.isInSource(directoryFile, project);
        }
    }
    return false;
}
 
Example #10
Source File: ScratchTreeStructureProvider.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
static Collection<AbstractTreeNode> getDirectoryChildrenImpl(@Nonnull Project project, @Nullable PsiDirectory directory, @Nonnull ViewSettings settings, @Nonnull PsiFileSystemItemFilter filter) {
  final List<AbstractTreeNode> result = new ArrayList<>();
  PsiElementProcessor<PsiFileSystemItem> processor = new PsiElementProcessor<PsiFileSystemItem>() {
    @Override
    public boolean execute(@Nonnull PsiFileSystemItem element) {
      if (!filter.shouldShow(element)) {
        // skip
      }
      else if (element instanceof PsiDirectory) {
        result.add(new PsiDirectoryNode(project, (PsiDirectory)element, settings, filter) {
          @Override
          public Collection<AbstractTreeNode> getChildrenImpl() {
            //noinspection ConstantConditions
            return getDirectoryChildrenImpl(getProject(), getValue(), getSettings(), getFilter());
          }
        });
      }
      else if (element instanceof PsiFile) {
        result.add(new PsiFileNode(project, (PsiFile)element, settings) {
          @Override
          public Comparable<ExtensionSortKey> getTypeSortKey() {
            PsiFile value = getValue();
            Language language = value == null ? null : value.getLanguage();
            LanguageFileType fileType = language == null ? null : language.getAssociatedFileType();
            return fileType == null ? null : new ExtensionSortKey(fileType.getDefaultExtension());
          }
        });
      }
      return true;
    }
  };

  return AbstractTreeUi.calculateYieldingToWriteAction(() -> {
    if (directory == null || !directory.isValid()) return Collections.emptyList();
    directory.processChildren(processor);
    return result;
  });
}
 
Example #11
Source File: AbstractProjectViewPSIPane.java    From consulo with Apache License 2.0 5 votes vote down vote up
/**
 * @param object   an object that represents a node in the project tree
 * @param expanded {@code true} if the corresponding node is expanded,
 *                 {@code false} if it is collapsed
 * @return a non-null value if the corresponding node should be , or {@code null}
 */
protected ErrorStripe getStripe(Object object, boolean expanded) {
  if (expanded && object instanceof PsiDirectoryNode) return null;
  if (object instanceof PresentableNodeDescriptor) {
    PresentableNodeDescriptor node = (PresentableNodeDescriptor)object;
    TextAttributesKey key = node.getPresentation().getTextAttributesKey();
    TextAttributes attributes = key == null ? null : EditorColorsManager.getInstance().getSchemeForCurrentUITheme().getAttributes(key);
    Color color = attributes == null ? null : attributes.getErrorStripeColor();
    if (color != null) return ErrorStripe.create(color, 1);
  }
  return null;
}
 
Example #12
Source File: SmartElementDescriptor.java    From consulo with Apache License 2.0 4 votes vote down vote up
protected boolean isMarkModified() {
  return getParentDescriptor() instanceof PsiDirectoryNode;
}
 
Example #13
Source File: SmartElementDescriptor.java    From consulo with Apache License 2.0 4 votes vote down vote up
protected boolean isMarkReadOnly() {
  return getParentDescriptor() instanceof PsiDirectoryNode;
}
 
Example #14
Source File: AbstractProjectViewPane.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Nonnull
public PsiDirectory[] getSelectedDirectories() {
  List<PsiDirectory> directories = ContainerUtil.newArrayList();
  for (PsiDirectoryNode node : getSelectedNodes(PsiDirectoryNode.class)) {
    PsiDirectory directory = node.getValue();
    if (directory != null) {
      directories.add(directory);
      Object parentValue = node.getParent().getValue();
      if (parentValue instanceof PsiDirectory && Registry.is("projectView.choose.directory.on.compacted.middle.packages")) {
        while (true) {
          directory = directory.getParentDirectory();
          if (directory == null || directory.equals(parentValue)) {
            break;
          }
          directories.add(directory);
        }
      }
    }
  }
  if (!directories.isEmpty()) {
    return directories.toArray(PsiDirectory.EMPTY_ARRAY);
  }

  final PsiElement[] elements = getSelectedPSIElements();
  if (elements.length == 1) {
    final PsiElement element = elements[0];
    if (element instanceof PsiDirectory) {
      return new PsiDirectory[]{(PsiDirectory)element};
    }
    else if (element instanceof PsiDirectoryContainer) {
      return ((PsiDirectoryContainer)element).getDirectories();
    }
    else {
      final PsiFile containingFile = element.getContainingFile();
      if (containingFile != null) {
        final PsiDirectory psiDirectory = containingFile.getContainingDirectory();
        if (psiDirectory != null) {
          return new PsiDirectory[]{psiDirectory};
        }
        final VirtualFile file = containingFile.getVirtualFile();
        if (file instanceof VirtualFileWindow) {
          final VirtualFile delegate = ((VirtualFileWindow)file).getDelegate();
          final PsiFile delegatePsiFile = containingFile.getManager().findFile(delegate);
          if (delegatePsiFile != null && delegatePsiFile.getContainingDirectory() != null) {
            return new PsiDirectory[]{delegatePsiFile.getContainingDirectory()};
          }
        }
        return PsiDirectory.EMPTY_ARRAY;
      }
    }
  }
  else {
    TreePath path = getSelectedPath();
    if (path != null) {
      Object component = path.getLastPathComponent();
      if (component instanceof DefaultMutableTreeNode) {
        return getSelectedDirectoriesInAmbiguousCase(((DefaultMutableTreeNode)component).getUserObject());
      }
      return getSelectedDirectoriesInAmbiguousCase(component);
    }
  }
  return PsiDirectory.EMPTY_ARRAY;
}
 
Example #15
Source File: PantsTreeStructureProvider.java    From intellij-pants-plugin with Apache License 2.0 4 votes vote down vote up
private boolean isModuleRoot(@NotNull PsiDirectoryNode node, Module module) {
  return ArrayUtil.indexOf(ModuleRootManager.getInstance(module).getContentRoots(), node.getVirtualFile()) >= 0;
}
 
Example #16
Source File: PantsTreeStructureProvider.java    From intellij-pants-plugin with Apache License 2.0 4 votes vote down vote up
private Module getModuleOf(@NotNull PsiDirectoryNode node) {
  return ModuleUtil.findModuleForPsiElement(node.getValue());
}
 
Example #17
Source File: KnowledgeViewProjectNode.java    From netbeans-mmd-plugin with Apache License 2.0 4 votes vote down vote up
@Nonnull
@Override
public Collection<? extends AbstractTreeNode> getChildren() {
  final ArrayList<AbstractTreeNode> result = new ArrayList<AbstractTreeNode>();
  final PsiManager psiManager = PsiManager.getInstance(getProject());

  for (final Module m : ModuleManager.getInstance(myProject).getModules()) {
    final VirtualFile knowledgeFolder = IdeaUtils.findKnowledgeFolderForModule(m, false);
    if (knowledgeFolder != null) {

      final String moduleName = m.getName();

      final PsiDirectory dir = psiManager.findDirectory(knowledgeFolder);
      final PsiDirectoryNode node = new PsiDirectoryNode(myProject, dir, getSettings()) {

        protected Icon patchIcon(final Icon original, final VirtualFile file) {
          return AllIcons.File.FOLDER;
        }

        @Override
        public String getTitle() {
          return moduleName;
        }

        @Override
        public String toString() {
          return moduleName;
        }

        @Override
        public boolean isFQNameShown() {
          return false;
        }

        @Override
        public VirtualFile getVirtualFile() {
          return knowledgeFolder;
        }

        @Nullable
        @Override
        protected String calcTooltip() {
          return "The Knowledge folder for " + m.getName();
        }

        @Override
        protected boolean shouldShowModuleName() {
          return false;
        }

      };
      result.add(node);
    }
  }
  return result;
}