add file color for navigation list items which are not PsiElements but could fetch...
[fedora-idea.git] / platform / lang-impl / src / com / intellij / ide / util / DirectoryChooserUtil.java
blob296679097d83a2e3bf2c340cc9c0b1b310c8aab1
1 /*
2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com.intellij.ide.util;
19 import com.intellij.ide.IdeBundle;
20 import com.intellij.ide.IdeView;
21 import com.intellij.openapi.application.ApplicationManager;
22 import com.intellij.openapi.project.Project;
23 import com.intellij.openapi.roots.ProjectFileIndex;
24 import com.intellij.openapi.roots.ProjectRootManager;
25 import com.intellij.psi.PsiDirectory;
26 import com.intellij.refactoring.RefactoringBundle;
27 import org.jetbrains.annotations.Nullable;
29 import java.util.ArrayList;
30 import java.util.Map;
32 public class DirectoryChooserUtil {
33 private DirectoryChooserUtil() {
36 public static PsiDirectory getOrChooseDirectory(IdeView view) {
37 PsiDirectory[] dirs = view.getDirectories();
38 if (dirs.length == 0) return null;
39 if (dirs.length == 1) {
40 return dirs[0];
42 else {
43 Project project = dirs[0].getProject();
44 return selectDirectory(project, dirs, null, "");
48 public static PsiDirectory selectDirectory(Project project,
49 PsiDirectory[] packageDirectories,
50 PsiDirectory defaultDirectory,
51 String postfixToShow) {
52 ProjectFileIndex projectFileIndex = ProjectRootManager.getInstance(project).getFileIndex();
54 ArrayList<PsiDirectory> possibleDirs = new ArrayList<PsiDirectory>();
55 for (PsiDirectory dir : packageDirectories) {
56 if (!dir.isValid()) continue;
57 if (!dir.isWritable()) continue;
58 if (possibleDirs.contains(dir)) continue;
59 if (!projectFileIndex.isInContent(dir.getVirtualFile())) continue;
60 possibleDirs.add(dir);
63 if (possibleDirs.isEmpty()) return null;
64 if (possibleDirs.size() == 1) return possibleDirs.get(0);
66 if (ApplicationManager.getApplication().isUnitTestMode()) return possibleDirs.get(0);
68 DirectoryChooser chooser = new DirectoryChooser(project);
69 chooser.setTitle(IdeBundle.message("title.choose.destination.directory"));
70 chooser.fillList(possibleDirs.toArray(new PsiDirectory[possibleDirs.size()]), defaultDirectory, project, postfixToShow);
71 chooser.show();
72 return chooser.isOK() ? chooser.getSelectedDirectory() : null;
75 @Nullable
76 public static
77 PsiDirectory chooseDirectory(PsiDirectory[] targetDirectories,
78 @Nullable PsiDirectory initialDirectory, Project project,
79 Map<PsiDirectory, String> relativePathsToCreate) {
80 final DirectoryChooser chooser = new DirectoryChooser(project, new DirectoryChooserModuleTreeView(project));
81 chooser.setTitle(RefactoringBundle.message("choose.destination.directory"));
82 chooser.fillList(
83 targetDirectories,
84 initialDirectory,
85 project,
86 relativePathsToCreate
88 chooser.show();
89 if (!chooser.isOK()) return null;
90 return chooser.getSelectedDirectory();