IDEA-50121
[fedora-idea.git] / platform / lang-impl / src / com / intellij / analysis / BaseAnalysisAction.java
blobed097f0ec906da074f81508e6c170d0962645589
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.analysis;
19 import com.intellij.injected.editor.VirtualFileWindow;
20 import com.intellij.openapi.actionSystem.*;
21 import com.intellij.openapi.diagnostic.Logger;
22 import com.intellij.openapi.fileEditor.FileDocumentManager;
23 import com.intellij.openapi.help.HelpManager;
24 import com.intellij.openapi.module.Module;
25 import com.intellij.openapi.module.ModuleUtil;
26 import com.intellij.openapi.project.Project;
27 import com.intellij.openapi.roots.ProjectFileIndex;
28 import com.intellij.openapi.roots.ProjectRootManager;
29 import com.intellij.openapi.vfs.VirtualFile;
30 import com.intellij.psi.PsiDirectory;
31 import com.intellij.psi.PsiElement;
32 import com.intellij.psi.PsiFile;
33 import org.jetbrains.annotations.NonNls;
34 import org.jetbrains.annotations.NotNull;
35 import org.jetbrains.annotations.Nullable;
37 import javax.swing.*;
38 import java.util.HashSet;
39 import java.util.Set;
41 public abstract class BaseAnalysisAction extends AnAction {
42 private final String myTitle;
43 private final String myAnalysisNoon;
44 private static final Logger LOG = Logger.getInstance("#com.intellij.analysis.BaseAnalysisAction");
46 protected BaseAnalysisAction(String title, String analysisNoon) {
47 myTitle = title;
48 myAnalysisNoon = analysisNoon;
51 public void update(AnActionEvent event) {
52 Presentation presentation = event.getPresentation();
53 presentation.setEnabled(getInspectionScope(event.getDataContext()) != null);
56 public void actionPerformed(AnActionEvent e) {
57 DataContext dataContext = e.getDataContext();
58 final Project project = e.getData(PlatformDataKeys.PROJECT);
59 final Module module = e.getData(LangDataKeys.MODULE);
60 if (project == null) {
61 return;
63 AnalysisScope scope = getInspectionScope(dataContext);
64 LOG.assertTrue(scope != null);
65 /*if (scope.getScopeType() == AnalysisScope.VIRTUAL_FILES){
66 FileDocumentManager.getInstance().saveAllDocuments();
67 analyze(project, scope);
68 return;
69 }*/
70 final boolean rememberScope = e.getPlace().equals(ActionPlaces.MAIN_MENU);
71 final AnalysisUIOptions uiOptions = AnalysisUIOptions.getInstance(project);
72 PsiElement element = LangDataKeys.PSI_ELEMENT.getData(dataContext);
73 BaseAnalysisActionDialog dlg = new BaseAnalysisActionDialog(AnalysisScopeBundle.message("specify.analysis.scope", myTitle),
74 AnalysisScopeBundle.message("analysis.scope.title", myAnalysisNoon),
75 project,
76 scope,
77 module != null && scope.getScopeType() != AnalysisScope.MODULE ? ModuleUtil
78 .getModuleNameInReadAction(module) : null,
79 rememberScope, AnalysisUIOptions.getInstance(project), element){
80 @Nullable
81 protected JComponent getAdditionalActionSettings(final Project project) {
82 return BaseAnalysisAction.this.getAdditionalActionSettings(project, this);
86 protected void doHelpAction() {
87 HelpManager.getInstance().invokeHelp(getHelpTopic());
90 protected Action[] createActions() {
91 return new Action[]{getOKAction(), getCancelAction(), getHelpAction()};
94 dlg.show();
95 if (!dlg.isOK()) {
96 canceled();
97 return;
99 final int oldScopeType = uiOptions.SCOPE_TYPE;
100 scope = dlg.getScope(uiOptions, scope, project, module);
101 if (!rememberScope){
102 uiOptions.SCOPE_TYPE = oldScopeType;
104 uiOptions.ANALYZE_TEST_SOURCES = dlg.isInspectTestSources();
105 FileDocumentManager.getInstance().saveAllDocuments();
107 analyze(project, scope);
110 @NonNls
111 protected String getHelpTopic() {
112 return "reference.dialogs.analyzeDependencies.scope";
115 protected void canceled() {
118 protected abstract void analyze(@NotNull Project project, AnalysisScope scope);
120 @Nullable
121 private static AnalysisScope getInspectionScope(final DataContext dataContext) {
122 if (PlatformDataKeys.PROJECT.getData(dataContext) == null) return null;
124 AnalysisScope scope = getInspectionScopeImpl(dataContext);
126 return scope != null && scope.getScopeType() != AnalysisScope.INVALID ? scope : null;
129 @Nullable
130 private static AnalysisScope getInspectionScopeImpl(DataContext dataContext) {
131 //Possible scopes: file, directory, package, project, module.
132 Project projectContext = PlatformDataKeys.PROJECT_CONTEXT.getData(dataContext);
133 if (projectContext != null) {
134 return new AnalysisScope(projectContext);
137 final AnalysisScope analysisScope = AnalysisScope.KEY.getData(dataContext);
138 if (analysisScope != null) {
139 return analysisScope;
142 Module moduleContext = LangDataKeys.MODULE_CONTEXT.getData(dataContext);
143 if (moduleContext != null) {
144 return new AnalysisScope(moduleContext);
147 Module [] modulesArray = LangDataKeys.MODULE_CONTEXT_ARRAY.getData(dataContext);
148 if (modulesArray != null) {
149 return new AnalysisScope(modulesArray);
151 PsiFile psiFile = LangDataKeys.PSI_FILE.getData(dataContext);
152 if (psiFile != null && psiFile.getManager().isInProject(psiFile)) {
153 return new AnalysisScope(psiFile);
156 PsiElement psiTarget = LangDataKeys.PSI_ELEMENT.getData(dataContext);
157 if (psiTarget instanceof PsiDirectory) {
158 PsiDirectory psiDirectory = (PsiDirectory)psiTarget;
159 if (!psiDirectory.getManager().isInProject(psiDirectory)) return null;
160 return new AnalysisScope(psiDirectory);
162 else if (psiTarget != null) {
163 return null;
166 final VirtualFile[] virtualFiles = PlatformDataKeys.VIRTUAL_FILE_ARRAY.getData(dataContext);
167 if (virtualFiles != null) { //analyze on selection
168 final Project project = PlatformDataKeys.PROJECT.getData(dataContext);
169 final ProjectFileIndex fileIndex = ProjectRootManager.getInstance(project).getFileIndex();
170 Set<VirtualFile> files = new HashSet<VirtualFile>();
171 for (VirtualFile vFile : virtualFiles) {
172 if (fileIndex.isInContent(vFile)) {
173 if (vFile instanceof VirtualFileWindow) {
174 files.add(vFile);
175 vFile = ((VirtualFileWindow)vFile).getDelegate();
177 traverseDirectory(vFile, files);
180 return new AnalysisScope(project, files);
182 return getProjectScope(dataContext);
185 private static AnalysisScope getProjectScope(DataContext dataContext) {
186 return new AnalysisScope(PlatformDataKeys.PROJECT.getData(dataContext));
189 @Nullable
190 protected JComponent getAdditionalActionSettings(Project project, BaseAnalysisActionDialog dialog){
191 return null;
194 private static void traverseDirectory(VirtualFile vFile, Set<VirtualFile> files) {
195 if (vFile.isDirectory()) {
196 final VirtualFile[] virtualFiles = vFile.getChildren();
197 for (VirtualFile virtualFile : virtualFiles) {
198 traverseDirectory(virtualFile, files);
201 else {
202 files.add(vFile);