public static @Nullable PsiDirectory findOrCreateDirectoryForPackage(@NotNull Project project,
                                                                       @NotNull String packageName,
                                                                       @Nullable PsiDirectory baseDir,
                                                                       boolean askUserToCreate) throws IncorrectOperationException {
    PsiDirectory psiDirectory = null;

    if (!packageName.isEmpty()) {
      PsiPackage rootPackage = findLongestExistingPackage(project, packageName);
      if (rootPackage != null) {
        int beginIndex = rootPackage.getQualifiedName().length() + 1;
        packageName = beginIndex < packageName.length() ? packageName.substring(beginIndex) : "";
        String postfixToShow = packageName.replace('.', File.separatorChar);
        if (!packageName.isEmpty()) {
          postfixToShow = File.separatorChar + postfixToShow;
        }
        PsiDirectory[] directories = rootPackage.getDirectories();
        psiDirectory = DirectoryChooserUtil.selectDirectory(project, directories, baseDir, postfixToShow);
        if (psiDirectory == null) return null;
      }
    }

    if (psiDirectory == null) {
      PsiDirectory[] sourceDirectories = ProjectRootUtil.getSourceRootDirectories(project);
      psiDirectory = DirectoryChooserUtil.selectDirectory(project, sourceDirectories, baseDir,
                                     File.separatorChar + packageName.replace('.', File.separatorChar));
      if (psiDirectory == null) return null;
    }

    String restOfName = packageName;
    boolean askedToCreate = false;
    while (!restOfName.isEmpty()) {
      final String name = getLeftPart(restOfName);
      PsiDirectory foundExistingDirectory = psiDirectory.findSubdirectory(name);
      if (foundExistingDirectory == null) {
        if (!askedToCreate && askUserToCreate) {
          int toCreate = Messages.showYesNoDialog(project,
                                                  JavaBundle.message("prompt.create.non.existing.package", packageName),
                                                  JavaBundle.message("title.package.not.found"),
                                                  Messages.getQuestionIcon());
          if (toCreate != Messages.YES) {
            return null;
          }
          askedToCreate = true;
        }
        psiDirectory = createSubdirectory(project, psiDirectory, name);
      }
      else {
        psiDirectory = foundExistingDirectory;
      }
      restOfName = cutLeftPart(restOfName);
    }
    return psiDirectory;
  }
public static @Nullable PsiDirectory findOrCreateDirectoryForPackage(@NotNull Project project,
                                                                       @NotNull String packageName,
                                                                       @Nullable PsiDirectory baseDir,
                                                                       boolean askUserToCreate)