1 package com
.intellij
.ide
.util
;
3 import com
.intellij
.ide
.IdeBundle
;
4 import com
.intellij
.ide
.IdeView
;
5 import com
.intellij
.openapi
.application
.ApplicationManager
;
6 import com
.intellij
.openapi
.project
.Project
;
7 import com
.intellij
.openapi
.roots
.ProjectFileIndex
;
8 import com
.intellij
.openapi
.roots
.ProjectRootManager
;
9 import com
.intellij
.psi
.PsiDirectory
;
10 import com
.intellij
.refactoring
.RefactoringBundle
;
11 import org
.jetbrains
.annotations
.Nullable
;
13 import java
.util
.ArrayList
;
16 public class DirectoryChooserUtil
{
17 private DirectoryChooserUtil() {
20 public static PsiDirectory
getOrChooseDirectory(IdeView view
) {
21 PsiDirectory
[] dirs
= view
.getDirectories();
22 if (dirs
.length
== 0) return null;
23 if (dirs
.length
== 1) {
27 Project project
= dirs
[0].getProject();
28 return selectDirectory(project
, dirs
, null, "");
32 public static PsiDirectory
selectDirectory(Project project
,
33 PsiDirectory
[] packageDirectories
,
34 PsiDirectory defaultDirectory
,
35 String postfixToShow
) {
36 ProjectFileIndex projectFileIndex
= ProjectRootManager
.getInstance(project
).getFileIndex();
38 ArrayList
<PsiDirectory
> possibleDirs
= new ArrayList
<PsiDirectory
>();
39 for (PsiDirectory dir
: packageDirectories
) {
40 if (!dir
.isValid()) continue;
41 if (!dir
.isWritable()) continue;
42 if (possibleDirs
.contains(dir
)) continue;
43 if (!projectFileIndex
.isInContent(dir
.getVirtualFile())) continue;
44 possibleDirs
.add(dir
);
47 if (possibleDirs
.isEmpty()) return null;
48 if (possibleDirs
.size() == 1) return possibleDirs
.get(0);
50 if (ApplicationManager
.getApplication().isUnitTestMode()) return possibleDirs
.get(0);
52 DirectoryChooser chooser
= new DirectoryChooser(project
);
53 chooser
.setTitle(IdeBundle
.message("title.choose.destination.directory"));
54 chooser
.fillList(possibleDirs
.toArray(new PsiDirectory
[possibleDirs
.size()]), defaultDirectory
, project
, postfixToShow
);
56 return chooser
.isOK() ? chooser
.getSelectedDirectory() : null;
61 PsiDirectory
chooseDirectory(PsiDirectory
[] targetDirectories
,
62 @Nullable PsiDirectory initialDirectory
, Project project
,
63 Map
<PsiDirectory
, String
> relativePathsToCreate
) {
64 final DirectoryChooser chooser
= new DirectoryChooser(project
, new DirectoryChooserModuleTreeView(project
));
65 chooser
.setTitle(RefactoringBundle
.message("choose.destination.directory"));
73 if (!chooser
.isOK()) return null;
74 return chooser
.getSelectedDirectory();