update copyright
[fedora-idea.git] / java / java-impl / src / com / intellij / cyclicDependencies / actions / CyclicDependenciesAction.java
blob880e2f57027ab9c0249d992d8256005afc277d5f
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.
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;
31 import javax.swing.*;
33 /**
34 * User: anna
35 * Date: Jan 31, 2005
37 public class CyclicDependenciesAction extends AnAction{
38 private final String myAnalysisVerb;
39 private final String myAnalysisNoon;
40 private final String myTitle;
42 public CyclicDependenciesAction() {
43 myAnalysisVerb = AnalysisScopeBundle.message("action.analyze.verb");
44 myAnalysisNoon = 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.getDataContext().getData(DataConstants.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)) {
61 return;
63 AnalysisScope scope = getInspectionScope(dataContext);
64 if (scope == null || scope.getScopeType() != AnalysisScope.MODULES){
65 ProjectModuleOrPackageDialog dlg = new ProjectModuleOrPackageDialog(module != null ? ModuleUtil.getModuleNameInReadAction(module) : null);
66 dlg.show();
67 if (!dlg.isOK()) return;
68 if (dlg.isProjectScopeSelected()) {
69 scope = getProjectScope(dataContext);
71 else {
72 if (dlg.isModuleScopeSelected()) {
73 scope = getModuleScope(dataContext);
78 FileDocumentManager.getInstance().saveAllDocuments();
80 new CyclicDependenciesHandler(project, scope).analyze();
85 private 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 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 = (Module)dataContext.getData(DataConstants.MODULE_CONTEXT);
102 if (moduleContext != null) {
103 return new AnalysisScope(moduleContext);
106 Module [] modulesArray = (Module[])dataContext.getData(DataConstants.MODULE_CONTEXT_ARRAY);
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 == null || dirs.length == 0) return null;
121 return new JavaAnalysisScope(pack, LangDataKeys.MODULE.getData(dataContext));
122 } else if (psiTarget != null){
123 return null;
127 return getProjectScope(dataContext);
130 private AnalysisScope getProjectScope(DataContext dataContext) {
131 return new AnalysisScope(PlatformDataKeys.PROJECT.getData(dataContext));
134 private AnalysisScope getModuleScope(DataContext dataContext) {
135 return new AnalysisScope(LangDataKeys.MODULE.getData(dataContext));
138 private class ProjectModuleOrPackageDialog extends DialogWrapper {
139 private final String myModuleName;
140 private JRadioButton myProjectButton;
141 private JRadioButton myModuleButton;
143 private JPanel myScopePanel;
144 private JPanel myWholePanel;
147 public ProjectModuleOrPackageDialog(String moduleName) {
148 super(true);
149 myModuleName = moduleName;
150 init();
151 setTitle(AnalysisScopeBundle.message("cyclic.dependencies.scope.dialog.title", myTitle));
152 setHorizontalStretch(1.75f);
153 if (moduleName == null){
154 myModuleButton.setVisible(false);
155 myProjectButton.setSelected(true);
156 } else {
157 myModuleButton.setSelected(true);
161 protected JComponent createCenterPanel() {
162 myScopePanel.setBorder(IdeBorderFactory.createTitledBorder(AnalysisScopeBundle.message("analysis.scope.title", myAnalysisNoon)));
163 myProjectButton.setText(AnalysisScopeBundle.message("cyclic.dependencies.scope.dialog.project.button", myAnalysisVerb));
164 ButtonGroup group = new ButtonGroup();
165 group.add(myProjectButton);
166 if (myModuleName != null) {
167 myModuleButton.setText(AnalysisScopeBundle.message("cyclic.dependencies.scope.dialog.module.button", myAnalysisVerb, myModuleName));
168 group.add(myModuleButton);
170 return myWholePanel;
173 public boolean isProjectScopeSelected() {
174 return myProjectButton.isSelected();
177 public boolean isModuleScopeSelected() {
178 return myModuleButton != null ? myModuleButton.isSelected() : false;