Build CheckboxTree from the selected project files


First of all I want to say that I'm not from the Java world =)

The task that I want to solve: select the selected files / folders in the dialog in CheckboxTree with the structure preserved and filtering "images only". I was faced with a problem in choosing the approach to solving the problem.

Here is the test structure:

Initially, I selected the top level of the folder and then it's enough to build a model for CheckboxTree using recursion. But there are problems there is a choice like this: 

Or even more complicated, like this

There are a lot of problems: for each received VirtualFile [] it is necessary to receive parents and descendants, and also duplication.

So I decided to rely on the file paths in VFS:

VirtualFile[] files = e.getData(CommonDataKeys.VIRTUAL_FILE_ARRAY);

List<VirtualFile> imageFiles = new ArrayList<>();
List<VirtualFile> uniqueImageFiles = new ArrayList<>();

// Filtering
for (VirtualFile file : files) {
    VfsUtil.visitChildrenRecursively(file, new VirtualFileVisitor() {
        public boolean visitFile(@NotNull VirtualFile file) {
            if (file.isInLocalFileSystem() && allowedExtensions.contains(file.getExtension())) {

            return file.isInLocalFileSystem() && file.isDirectory();


// Clear duplicate
for (VirtualFile file : imageFiles) {
    if (!uniqueImageFiles.contains(file)) { 

So in uniqueImageFiles, I have everything I need (including children as image files). For each VirtualFile, I execute:

for (VirtualFile file : uniqueImageFiles) {
    // Getting a relative path without the file itself
    String relativePath = file.getParent().getPath().replace(project.getBasePath(), "");
    relativePath = StringUtils.strip(relativePath, "/");

    // Explode path-string to segments
    String[] pathSegments = relativePath.split("\\/", -1);

    buildTreeBranch(pathSegments, file, parentNode);

Thus, I build a model for CheckboxTree easily and simply, keeping the whole structure completely.

But I'm worried about some questions:
1. How many safe pieces of code (file always image):

Project project = ProjectManager.getInstance().GetOpenProjects()[0];
String relativePath = file.getParent().GetPath ().Replace(project.getBasePath (), "");

2. Is there a simple way to get a relative path for VirtualFile?

3. I'm not sure about the correct approach


Every row in a Swing tree is represented by TreePath

TreePath contains all ancestors of the selected nodes

It should not be hard to create a custom tree model from array of paths.



Also you can create a custom tree renderer, which may gray out unselected nodes (a and b in example above)


The way you're getting a project is far from being a recommended one. Use `ActionEvent.getProject()`.

For relative paths, there is `VfsUtilCore.getRelativePath(VirtualFile, VirtualFile)`.


Please sign in to leave a comment.