/** @deprecated */ @Nullable public static PsiDirectory findOrCreateDirectoryForPackage( Project project, String packageName, PsiDirectory baseDir, boolean askUserToCreate, boolean filterSourceDirsForTestBaseDir) throws IncorrectOperationException { PsiDirectory psiDirectory = null; if (!"".equals(packageName)) { 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.length() > 0) { postfixToShow = File.separatorChar + postfixToShow; } PsiDirectory[] directories = rootPackage.getDirectories(); if (filterSourceDirsForTestBaseDir) { directories = filterSourceDirectories(baseDir, project, directories); } 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.length() > 0) { final String name = getLeftPart(restOfName); PsiDirectory foundExistingDirectory = psiDirectory.findSubdirectory(name); if (foundExistingDirectory == null) { if (!askedToCreate && askUserToCreate) { int toCreate = Messages.showYesNoDialog( project, IdeBundle.message("prompt.create.non.existing.package", packageName), IdeBundle.message("title.package.not.found"), Messages.getQuestionIcon()); if (toCreate != 0) { return null; } askedToCreate = true; } psiDirectory = createSubdirectory(psiDirectory, name, project); } else { psiDirectory = foundExistingDirectory; } restOfName = cutLeftPart(restOfName); } return psiDirectory; }
@Nullable public static PsiDirectory findOrCreateDirectoryForPackage( @NotNull Module module, String packageName, PsiDirectory baseDir, boolean askUserToCreate, boolean filterSourceDirsForBaseTestDirectory) throws IncorrectOperationException { final Project project = module.getProject(); PsiDirectory psiDirectory = null; if (!packageName.isEmpty()) { PsiPackage rootPackage = findLongestExistingPackage(module, 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.length() > 0) { postfixToShow = File.separatorChar + postfixToShow; } PsiDirectory[] moduleDirectories = getPackageDirectoriesInModule(rootPackage, module); if (filterSourceDirsForBaseTestDirectory) { moduleDirectories = filterSourceDirectories(baseDir, project, moduleDirectories); } psiDirectory = DirectoryChooserUtil.selectDirectory( project, moduleDirectories, baseDir, postfixToShow); if (psiDirectory == null) return null; } } if (psiDirectory == null) { if (!checkSourceRootsConfigured(module, askUserToCreate)) return null; final List<VirtualFile> sourceRoots = ModuleRootManager.getInstance(module).getSourceRoots(JavaModuleSourceRootTypes.SOURCES); List<PsiDirectory> directoryList = new ArrayList<PsiDirectory>(); for (VirtualFile sourceRoot : sourceRoots) { final PsiDirectory directory = PsiManager.getInstance(project).findDirectory(sourceRoot); if (directory != null) { directoryList.add(directory); } } PsiDirectory[] sourceDirectories = directoryList.toArray(new PsiDirectory[directoryList.size()]); 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.length() > 0) { final String name = getLeftPart(restOfName); PsiDirectory foundExistingDirectory = psiDirectory.findSubdirectory(name); if (foundExistingDirectory == null) { if (!askedToCreate && askUserToCreate) { if (!ApplicationManager.getApplication().isUnitTestMode()) { int toCreate = Messages.showYesNoDialog( project, IdeBundle.message("prompt.create.non.existing.package", packageName), IdeBundle.message("title.package.not.found"), Messages.getQuestionIcon()); if (toCreate != 0) { return null; } } askedToCreate = true; } final PsiDirectory psiDirectory1 = psiDirectory; try { psiDirectory = ActionRunner.runInsideWriteAction( new ActionRunner.InterruptibleRunnableWithResult<PsiDirectory>() { public PsiDirectory run() throws Exception { return psiDirectory1.createSubdirectory(name); } }); } catch (IncorrectOperationException e) { throw e; } catch (IOException e) { throw new IncorrectOperationException(e.toString(), e); } catch (Exception e) { LOG.error(e); } } else { psiDirectory = foundExistingDirectory; } restOfName = cutLeftPart(restOfName); } return psiDirectory; }