private void selectInTree(final VirtualFile[] array, final boolean requestFocus) {
    myTreeIsUpdating = true;
    final List<VirtualFile> fileList = Arrays.asList(array);
    if (!Arrays.asList(myFileSystemTree.getSelectedFiles()).containsAll(fileList)) {
      myFileSystemTree.select(
          array,
          new Runnable() {
            public void run() {
              if (!myFileSystemTree.areHiddensShown()
                  && !Arrays.asList(myFileSystemTree.getSelectedFiles()).containsAll(fileList)) {
                myFileSystemTree.showHiddens(true);
                selectInTree(array, requestFocus);
                return;
              }

              myTreeIsUpdating = false;
              setErrorText(null);
              if (requestFocus) {
                //noinspection SSBasedInspection
                SwingUtilities.invokeLater(
                    new Runnable() {
                      public void run() {
                        myFileSystemTree.getTree().requestFocus();
                      }
                    });
              }
            }
          });
    } else {
      myTreeIsUpdating = false;
      setErrorText(null);
    }
  }
  private void selectInTree(VirtualFile[] array, boolean requestFocus, boolean updatePathNeeded) {
    myTreeIsUpdating = true;
    final List<VirtualFile> fileList = Arrays.asList(array);
    if (!Arrays.asList(myFileSystemTree.getSelectedFiles()).containsAll(fileList)) {
      myFileSystemTree.select(
          array,
          () -> {
            if (!myFileSystemTree.areHiddensShown()
                && !Arrays.asList(myFileSystemTree.getSelectedFiles()).containsAll(fileList)) {
              // try to select files in hidden folders
              myFileSystemTree.showHiddens(true);
              selectInTree(array, requestFocus, updatePathNeeded);
              return;
            }
            if (array.length == 1
                && !Arrays.asList(myFileSystemTree.getSelectedFiles()).containsAll(fileList)) {
              // try to select a parent of a missed file
              VirtualFile parent = array[0].getParent();
              if (parent != null && parent.isValid()) {
                selectInTree(new VirtualFile[] {parent}, requestFocus, updatePathNeeded);
                return;
              }
            }

            reportFileNotFound();
            if (updatePathNeeded) {
              updatePathFromTree(fileList, true);
            }
            if (requestFocus) {
              //noinspection SSBasedInspection
              SwingUtilities.invokeLater(() -> myFileSystemTree.getTree().requestFocus());
            }
          });
    } else {
      reportFileNotFound();
      if (updatePathNeeded) {
        updatePathFromTree(fileList, true);
      }
    }
  }
  protected void restoreSelection(@Nullable VirtualFile toSelect) {
    final VirtualFile lastOpenedFile = FileChooserUtil.getLastOpenedFile(myProject);
    final VirtualFile file =
        FileChooserUtil.getFileToSelect(myChooserDescriptor, myProject, toSelect, lastOpenedFile);

    if (file != null && file.isValid()) {
      myFileSystemTree.select(
          file,
          new Runnable() {
            public void run() {
              if (!file.equals(myFileSystemTree.getSelectedFile())) {
                VirtualFile parent = file.getParent();
                if (parent != null) {
                  myFileSystemTree.select(parent, null);
                }
              } else if (file.isDirectory()) {
                myFileSystemTree.expand(file, null);
              }
            }
          });
    }
  }