private void loadDirectory(int id, Path directory, @Nullable Path pathToSelect, boolean updatePathBar, boolean updateHistory) {
    var cancelled = new AtomicBoolean(false);

    var vfsDirectory = new PreloadedDirectory(directory);
    update(id, cancelled, () -> {
      myCurrentDirectory = directory;
      updatePathBarAndHistory(directory, updatePathBar, updateHistory);
    });

    var selection = new AtomicReference<FsItem>();
    var error = new AtomicReference<String>();
    try {
      PlatformNioHelper.visitDirectory(directory, null, (file, result) -> {
        BasicFileAttributes attrs;
        try {
          attrs = result.get();
        }
        catch (Exception e) {
          error.set(e.getMessage());
          return true;
        }

        if (attrs.isSymbolicLink()) {
          try {
            attrs = new DelegatingFileAttributes(Files.readAttributes(file, BasicFileAttributes.class));
          }
          catch (IOException e) {
            LOG.debug(e);
          }
        }

        var virtualFile = new LazyDirectoryOrFile(vfsDirectory, file, attrs);
        if (!myDescriptor.isFileVisible(virtualFile, true)) {
          return true;  // not hidden, just ignored
        }
        var visible = myDescriptor.isFileVisible(virtualFile, false);
        var selectable = myDescriptor.isFileSelectable(virtualFile);
        var icon = myDetectProjectDirectories || !virtualFile.isDirectory() ? myDescriptor.getIcon(virtualFile) : AllIcons.Nodes.Folder;
        var item = new FsItem(file, file.getFileName().toString(), attrs, visible, selectable, icon);
        update(id, cancelled, () -> {
          myCurrentContent.add(item);
          if (visible || myShowHiddenFiles) {
            myModel.addRow(item);
          }
        });

        if (pathToSelect != null && file.equals(pathToSelect)) {
          selection.set(item);
        }

        return !cancelled.get();
      });
    }
    catch (IOException | RuntimeException e) {
      LOG.warn(directory.toString(), e);
      error.set(e.getMessage());
    }

    if (!cancelled.get()) {
      WatchKey watchKey = null;
      if (myWatcher != null && isLocalFs(directory)) {
        try {
          watchKey = directory.register(myWatcher, StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_DELETE);
        }
        catch (Exception e) {
          if (LOG.isDebugEnabled()) LOG.debug("cannot watch " + directory, e);
        }
      }

      var _watchKey = watchKey;
      update(
        id,
        () -> {
          myList.setPaintBusy(false);
          updateSelection(selection);
          reportError("file.chooser.cannot.load.dir", error);
          myWatchKey = _watchKey;
        },
        () -> { if (_watchKey != null) _watchKey.cancel(); });
    }
  }
private void loadDirectory(int id, Path directory, @Nullable Path pathToSelect, boolean updatePathBar, boolean updateHistory)