michaelsnowden
michaelsnowden

Reputation: 6202

FileChooser only for single directories in current project

I'm developing an IntelliJ-IDEA plugin.

I am trying to select single directories that are within the current project using their FileChooser classes.

Here's what I have so far:

public class MyAction extends AnAction {
    public void actionPerformed(AnActionEvent actionEvent) {
        com.intellij.openapi.project.Project project = actionEvent.getProject();
        final DataContext dataContext = actionEvent.getDataContext();
        assert project != null;
        final PsiFile currentFile = DataKeys.PSI_FILE.getData(dataContext);
        VirtualFile chooseFile = project.getBaseDir();
        if (currentFile != null) {
            chooseFile = currentFile.getVirtualFile();
        }
        FileChooserDescriptor descriptor = FileChooserDescriptorFactory.createSingleFolderDescriptor();
        chooseFile = FileChooser.chooseFile(descriptor, project, chooseFile);
        if (chooseFile == null) {
            return;
        }
        ... do stuff with the file
    }
}

I want to restrict the FileChooser to only display files in the current IDEA project. The FileChooser should either make it impossible to select anything else, or display an error if a user does select something else. Is there a way to do this?

Upvotes: 1

Views: 270

Answers (1)

Bas Leijdekkers
Bas Leijdekkers

Reputation: 26532

You will probably want to do something like this:

descriptor.setRoots(ProjectRootManager.getInstance(project).getContentRoots());

Upvotes: 1

Related Questions