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.
16 package com
.intellij
.cyclicDependencies
.actions
;
18 import com
.intellij
.analysis
.AnalysisScope
;
19 import com
.intellij
.analysis
.AnalysisScopeBundle
;
20 import com
.intellij
.analysis
.JavaAnalysisScope
;
21 import com
.intellij
.openapi
.actionSystem
.*;
22 import com
.intellij
.openapi
.fileEditor
.FileDocumentManager
;
23 import com
.intellij
.openapi
.module
.Module
;
24 import com
.intellij
.openapi
.module
.ModuleUtil
;
25 import com
.intellij
.openapi
.project
.Project
;
26 import com
.intellij
.openapi
.ui
.DialogWrapper
;
27 import com
.intellij
.psi
.*;
28 import com
.intellij
.psi
.search
.GlobalSearchScope
;
29 import com
.intellij
.ui
.IdeBorderFactory
;
37 public class CyclicDependenciesAction
extends AnAction
{
38 private final String myAnalysisVerb
;
39 private final String myAnalysisNoun
;
40 private final String myTitle
;
42 public CyclicDependenciesAction() {
43 myAnalysisVerb
= AnalysisScopeBundle
.message("action.analyze.verb");
44 myAnalysisNoun
= AnalysisScopeBundle
.message("action.analysis.noun");
45 myTitle
= AnalysisScopeBundle
.message("action.cyclic.dependency.title");
48 public void update(AnActionEvent event
) {
49 Presentation presentation
= event
.getPresentation();
50 presentation
.setEnabled(
51 getInspectionScope(event
.getDataContext()) != null || event
.getData(LangDataKeys
.PSI_FILE
) != null);
54 public void actionPerformed(AnActionEvent e
) {
55 DataContext dataContext
= e
.getDataContext();
56 final Project project
= PlatformDataKeys
.PROJECT
.getData(dataContext
);
57 final Module module
= LangDataKeys
.MODULE
.getData(dataContext
);
58 if (project
!= null) {
59 PsiFile psiFile
= LangDataKeys
.PSI_FILE
.getData(dataContext
);
60 if (psiFile
!= null && !(psiFile
instanceof PsiJavaFile
)) {
63 AnalysisScope scope
= getInspectionScope(dataContext
);
64 if (scope
== null || scope
.getScopeType() != AnalysisScope
.MODULES
){
65 ProjectModuleOrPackageDialog dlg
= new ProjectModuleOrPackageDialog(module
!= null ? ModuleUtil
.getModuleNameInReadAction(module
) : null);
67 if (!dlg
.isOK()) return;
68 if (dlg
.isProjectScopeSelected()) {
69 scope
= getProjectScope(dataContext
);
72 if (dlg
.isModuleScopeSelected()) {
73 scope
= getModuleScope(dataContext
);
78 FileDocumentManager
.getInstance().saveAllDocuments();
80 new CyclicDependenciesHandler(project
, scope
).analyze();
85 private static AnalysisScope
getInspectionScope(final DataContext dataContext
) {
86 final Project project
= PlatformDataKeys
.PROJECT
.getData(dataContext
);
87 if (project
== null) return null;
89 AnalysisScope scope
= getInspectionScopeImpl(dataContext
);
91 return scope
!= null && scope
.getScopeType() != AnalysisScope
.INVALID ? scope
: null;
94 private static AnalysisScope
getInspectionScopeImpl(DataContext dataContext
) {
95 //Possible scopes: package, project, module.
96 Project projectContext
= PlatformDataKeys
.PROJECT_CONTEXT
.getData(dataContext
);
97 if (projectContext
!= null) {
98 return new AnalysisScope(projectContext
);
101 Module moduleContext
= LangDataKeys
.MODULE_CONTEXT
.getData(dataContext
);
102 if (moduleContext
!= null) {
103 return new AnalysisScope(moduleContext
);
106 Module
[] modulesArray
= LangDataKeys
.MODULE_CONTEXT_ARRAY
.getData(dataContext
);
107 if (modulesArray
!= null) {
108 return new AnalysisScope(modulesArray
);
111 PsiElement psiTarget
= LangDataKeys
.PSI_ELEMENT
.getData(dataContext
);
112 if (psiTarget
instanceof PsiDirectory
) {
113 PsiDirectory psiDirectory
= (PsiDirectory
)psiTarget
;
114 if (!psiDirectory
.getManager().isInProject(psiDirectory
)) return null;
115 return new AnalysisScope(psiDirectory
);
117 else if (psiTarget
instanceof PsiPackage
) {
118 PsiPackage pack
= (PsiPackage
)psiTarget
;
119 PsiDirectory
[] dirs
= pack
.getDirectories(GlobalSearchScope
.projectScope(pack
.getProject()));
120 if (dirs
.length
== 0) return null;
121 return new JavaAnalysisScope(pack
, LangDataKeys
.MODULE
.getData(dataContext
));
122 } else if (psiTarget
!= null){
127 return getProjectScope(dataContext
);
130 private static AnalysisScope
getProjectScope(DataContext dataContext
) {
131 final Project data
= PlatformDataKeys
.PROJECT
.getData(dataContext
);
135 return new AnalysisScope(data
);
138 private static AnalysisScope
getModuleScope(DataContext dataContext
) {
139 final Module data
= LangDataKeys
.MODULE
.getData(dataContext
);
143 return new AnalysisScope(data
);
146 private class ProjectModuleOrPackageDialog
extends DialogWrapper
{
147 private final String myModuleName
;
148 private JRadioButton myProjectButton
;
149 private JRadioButton myModuleButton
;
151 private JPanel myScopePanel
;
152 private JPanel myWholePanel
;
155 public ProjectModuleOrPackageDialog(String moduleName
) {
157 myModuleName
= moduleName
;
159 setTitle(AnalysisScopeBundle
.message("cyclic.dependencies.scope.dialog.title", myTitle
));
160 setHorizontalStretch(1.75f
);
161 if (moduleName
== null){
162 myModuleButton
.setVisible(false);
163 myProjectButton
.setSelected(true);
165 myModuleButton
.setSelected(true);
169 protected JComponent
createCenterPanel() {
170 myScopePanel
.setBorder(IdeBorderFactory
.createTitledBorder(AnalysisScopeBundle
.message("analysis.scope.title", myAnalysisNoun
)));
171 myProjectButton
.setText(AnalysisScopeBundle
.message("cyclic.dependencies.scope.dialog.project.button", myAnalysisVerb
));
172 ButtonGroup group
= new ButtonGroup();
173 group
.add(myProjectButton
);
174 if (myModuleName
!= null) {
175 myModuleButton
.setText(AnalysisScopeBundle
.message("cyclic.dependencies.scope.dialog.module.button", myAnalysisVerb
, myModuleName
));
176 group
.add(myModuleButton
);
181 public boolean isProjectScopeSelected() {
182 return myProjectButton
.isSelected();
185 public boolean isModuleScopeSelected() {
186 return myModuleButton
!= null ? myModuleButton
.isSelected() : false;