Expand node in project view

I've created a directory and a file in it like this:

PsiFile classFile = PsiFileFactory.getInstance(project).createFileFromText("Test.java", "class Test{}");

PsiDirectory directory = PsiManager.getInstance(project).findDirectory(newModuleRoot); directory.add(classFile);


But the directory in the project view is collapsed.

Can I expand the directory programmatically?
7 comments
Comment actions Permalink

Try following:

ApplicationManager.getApplication().invokeLater(new Runnable()
{
    public void run()
    {
        ProjectView.getInstance(project).selectPsiElement(classFile, false);
    }
});
0
Comment actions Permalink

For some reason I just don't have access to ProjectView. When I type "ProjectView.getInstance(project)" I get "Can't resolve symbol 'ProjectView'".

0
Comment actions Permalink

It is located in idea.jar. Just add it to your SDK classpath.

0
Comment actions Permalink

Yeah, thanks. I wonder why IDEA didn't include it in Plugin SDK when I created Plugin SDK from IDEA installation home dir.

0
Comment actions Permalink

I tried

ProjectView.getInstance(project).selectPsiElement(classFile, false);


It didn't work.

My guess is it's because UI doesn't know yet that tree structure changed, cause I've just added new module. I tried doing

ProjectView.getInstance(project).refresh();


It didn't help either.

0
Comment actions Permalink

Thank you. The problem indeed was that I was using created PsiFIle while I should have used the one returned by the add() method. That was a tricky one, I must say.
selectPsiElement() serves fine for my purpose, so my code became something like this:

PsiFile classFile = PsiFileFactory.getInstance(project).createFileFromText("Test.java", "class Test{}");
PsiDirectory directory = PsiManager.getInstance(project).findDirectory(newModuleRoot);
classFile = directory.add(classFile);
ProjectView.getInstance(project).selectPsiElement(classFile, false);

0

Please sign in to leave a comment.