update copyright
[fedora-idea.git] / java / java-impl / src / com / intellij / codeInspection / dependencyViolation / DependencyInspection.java
blobcd13d258affdd24ab4a83a12e8c5451e8b684494
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.codeInspection.dependencyViolation;
18 import com.intellij.analysis.AnalysisScope;
19 import com.intellij.codeHighlighting.HighlightDisplayLevel;
20 import com.intellij.codeInspection.*;
21 import com.intellij.codeInspection.ex.BaseLocalInspectionTool;
22 import com.intellij.ide.DataManager;
23 import com.intellij.lang.StdLanguages;
24 import com.intellij.openapi.actionSystem.PlatformDataKeys;
25 import com.intellij.openapi.options.ShowSettingsUtil;
26 import com.intellij.openapi.project.Project;
27 import com.intellij.openapi.project.ProjectManager;
28 import com.intellij.packageDependencies.DependenciesBuilder;
29 import com.intellij.packageDependencies.DependencyRule;
30 import com.intellij.packageDependencies.DependencyValidationManager;
31 import com.intellij.packageDependencies.ForwardDependenciesBuilder;
32 import com.intellij.packageDependencies.ui.DependencyConfigurable;
33 import com.intellij.psi.PsiElement;
34 import com.intellij.psi.PsiFile;
35 import org.jetbrains.annotations.NonNls;
36 import org.jetbrains.annotations.NotNull;
37 import org.jetbrains.annotations.Nullable;
39 import javax.swing.*;
40 import java.awt.*;
41 import java.awt.event.ActionEvent;
42 import java.awt.event.ActionListener;
43 import java.text.MessageFormat;
44 import java.util.ArrayList;
46 /**
47 * User: anna
48 * Date: Feb 6, 2005
50 public class DependencyInspection extends BaseLocalInspectionTool {
52 public static final String GROUP_DISPLAY_NAME = "";
53 public static final String DISPLAY_NAME = InspectionsBundle.message("illegal.package.dependencies");
54 @NonNls public static final String SHORT_NAME = "Dependency";
56 @NotNull
57 public String getGroupDisplayName() {
58 return DependencyInspection.GROUP_DISPLAY_NAME;
61 @NotNull
62 public String getDisplayName() {
63 return DependencyInspection.DISPLAY_NAME;
66 @NotNull
67 public String getShortName() {
68 return DependencyInspection.SHORT_NAME;
71 public JComponent createOptionsPanel() {
72 final JButton editDependencies = new JButton(InspectionsBundle.message("inspection.dependency.configure.button.text"));
73 editDependencies.addActionListener(new ActionListener() {
74 public void actionPerformed(ActionEvent e) {
75 Project project = PlatformDataKeys.PROJECT.getData(DataManager.getInstance().getDataContext(editDependencies));
76 if (project == null) project = ProjectManager.getInstance().getDefaultProject();
77 ShowSettingsUtil.getInstance().editConfigurable(editDependencies, new DependencyConfigurable(project));
79 });
81 JPanel depPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
82 depPanel.add(editDependencies);
83 return depPanel;
86 @Nullable
87 public ProblemDescriptor[] checkFile(@NotNull final PsiFile file, @NotNull final InspectionManager manager, boolean isOnTheFly) {
88 if (file == null) return null;
89 if (file.getViewProvider().getPsi(StdLanguages.JAVA) == null) return null;
90 final DependencyValidationManager validationManager = DependencyValidationManager.getInstance(file.getProject());
91 if (!validationManager.hasRules()) return null;
92 if (validationManager.getApplicableRules(file).length == 0) return null;
93 final ArrayList<ProblemDescriptor> problems = new ArrayList<ProblemDescriptor>();
94 ForwardDependenciesBuilder builder = new ForwardDependenciesBuilder(file.getProject(), new AnalysisScope(file));
95 builder.analyzeFileDependencies(file, new DependenciesBuilder.DependencyProcessor() {
96 public void process(PsiElement place, PsiElement dependency) {
97 PsiFile dependencyFile = dependency.getContainingFile();
98 if (dependencyFile != null && dependencyFile.isPhysical() && dependencyFile.getVirtualFile() != null) {
99 final DependencyRule[] rule = validationManager.getViolatorDependencyRules(file, dependencyFile);
100 for (DependencyRule dependencyRule : rule) {
101 StringBuffer message = new StringBuffer();
102 message.append(MessageFormat.format(InspectionsBundle.message("inspection.dependency.violator.problem.descriptor"), dependencyRule.getDisplayText()));
103 problems.add(manager.createProblemDescriptor(place, message.toString(), new LocalQuickFix[]{new EditDependencyRulesAction(dependencyRule)}, ProblemHighlightType.GENERIC_ERROR_OR_WARNING));
108 return problems.isEmpty() ? null : problems.toArray(new ProblemDescriptor[problems.size()]);
111 @NotNull
112 public HighlightDisplayLevel getDefaultLevel() {
113 return HighlightDisplayLevel.ERROR;
116 public boolean isEnabledByDefault() {
117 return true;
120 private static class EditDependencyRulesAction implements LocalQuickFix {
121 private final DependencyRule myRule;
122 public EditDependencyRulesAction(DependencyRule rule) {
123 myRule = rule;
126 @NotNull
127 public String getName() {
128 return InspectionsBundle.message("edit.dependency.rules.text", myRule.getDisplayText());
131 @NotNull
132 public String getFamilyName() {
133 return InspectionsBundle.message("edit.dependency.rules.family");
136 public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
137 ShowSettingsUtil.getInstance().editConfigurable(project, new DependencyConfigurable(project));