Custom child for java file node in project tree

I'm trying to add custom children to java files in project tree. I created implemetion of TreeStructureProvider, but node isn't added to tree.


public class AndroidViewTreeStructureProvider implements TreeStructureProvider {


    @Override
    public Collection<AbstractTreeNode> modify(AbstractTreeNode abstractTreeNode, Collection<AbstractTreeNode> abstractTreeNodes, ViewSettings viewSettings) {

        if (abstractTreeNode.getValue() instanceof PsiJavaDirectoryImpl) {

            for (AbstractTreeNode child : abstractTreeNodes) {

                if (child instanceof JavaSourceNode) {

                    return abstractTreeNodes;

                }

                if(child.getValue() instanceof PsiJavaFileImpl) {

                    PsiJavaFileImpl javaFile = (PsiJavaFileImpl) child.getValue();

                    child.getChildren().add(new JavaSourceNode(javaFile, abstractTreeNode.getProject()));

                }

            }

        }

        return abstractTreeNodes;

    }

    @Nullable

    @Override

    public Object getData(Collection<AbstractTreeNode> abstractTreeNodes, String s) {

        return null;

    }

    private class JavaSourceNode extends ProjectViewNode<String> {

        protected JavaSourceNode(PsiJavaFileImpl src, Project projects) {

            super(projects, src.getName(), ViewSettings.DEFAULT);

        }

        @Override

        public boolean contains(@NotNull VirtualFile virtualFile) {

            return false;  //To change body of implemented methods use File | Settings | File Templates.

        }

        @NotNull

        @Override

        public Collection<? extends AbstractTreeNode> getChildren() {

            Collection<PsiFileNode> children = new ArrayList<PsiFileNode>();

            return children;

        }

        protected void update(PresentationData presentation) {

            presentation.setPresentableText(getValue());

            presentation.setIcon(PlatformIcons.SYMLINK_ICON);

        }

    }

}

2 comments
Comment actions Permalink

The TreeStructureProvider allows you to modify the list of children of specifically the node that is passed to the modify() method, and you do it by returning a new list of children from the method. Trying to modify the list of children of some other nodes directly is not going to work.

0
Comment actions Permalink

So is there any other way to add custom node to PsiJavaFile node?

0

Please sign in to leave a comment.