/**
   * @param elementToCopy PsiFile or PsiDirectory
   * @param newName can be not null only if elements.length == 1
   * @param choice a horrible way to pass/keep user preference
   * @return first copied PsiFile (recursively); null if no PsiFiles copied
   */
  @Nullable
  public static PsiFile copyToDirectory(
      @NotNull PsiFileSystemItem elementToCopy,
      @Nullable String newName,
      @NotNull PsiDirectory targetDirectory,
      @Nullable int[] choice)
      throws IncorrectOperationException, IOException {
    if (elementToCopy instanceof PsiFile) {
      PsiFile file = (PsiFile) elementToCopy;
      String name = newName == null ? file.getName() : newName;
      if (checkFileExist(targetDirectory, choice, file, name, "Copy")) return null;
      return targetDirectory.copyFileFrom(name, file);
    } else if (elementToCopy instanceof PsiDirectory) {
      PsiDirectory directory = (PsiDirectory) elementToCopy;
      if (directory.equals(targetDirectory)) {
        return null;
      }
      if (newName == null) newName = directory.getName();
      final PsiDirectory existing = targetDirectory.findSubdirectory(newName);
      final PsiDirectory subdirectory =
          existing == null ? targetDirectory.createSubdirectory(newName) : existing;
      EncodingRegistry.doActionAndRestoreEncoding(
          directory.getVirtualFile(),
          new ThrowableComputable<VirtualFile, IOException>() {
            @Override
            public VirtualFile compute() {
              return subdirectory.getVirtualFile();
            }
          });

      PsiFile firstFile = null;
      PsiElement[] children = directory.getChildren();
      for (PsiElement child : children) {
        PsiFileSystemItem item = (PsiFileSystemItem) child;
        PsiFile f = copyToDirectory(item, item.getName(), subdirectory, choice);
        if (firstFile == null) {
          firstFile = f;
        }
      }
      return firstFile;
    } else {
      throw new IllegalArgumentException("unexpected elementToCopy: " + elementToCopy);
    }
  }
 @Override
 public String getElementText(PsiFileSystemItem element) {
   return element.getName();
 }