remove Profile
[idea/community.git] / platform / lang-impl / src / com / intellij / codeInspection / actions / CodeInspectionAction.java
1 /*
2  * Copyright 2000-2016 JetBrains s.r.o.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 package com.intellij.codeInspection.actions;
17
18 import com.intellij.analysis.AnalysisScope;
19 import com.intellij.analysis.BaseAnalysisAction;
20 import com.intellij.analysis.BaseAnalysisActionDialog;
21 import com.intellij.codeInspection.InspectionManager;
22 import com.intellij.codeInspection.InspectionProfile;
23 import com.intellij.codeInspection.InspectionsBundle;
24 import com.intellij.codeInspection.ex.GlobalInspectionContextImpl;
25 import com.intellij.codeInspection.ex.InspectionManagerEx;
26 import com.intellij.codeInspection.ex.InspectionProfileImpl;
27 import com.intellij.openapi.fileEditor.FileDocumentManager;
28 import com.intellij.openapi.options.ex.SingleConfigurableEditor;
29 import com.intellij.openapi.project.Project;
30 import com.intellij.profile.codeInspection.InspectionProfileManager;
31 import com.intellij.profile.codeInspection.InspectionProjectProfileManager;
32 import com.intellij.profile.codeInspection.ProjectInspectionProfileManager;
33 import com.intellij.profile.codeInspection.ui.ErrorsConfigurable;
34 import com.intellij.profile.codeInspection.ui.header.InspectionToolsConfigurable;
35 import com.intellij.profile.codeInspection.ui.header.ProfilesComboBox;
36 import com.intellij.ui.ComboboxWithBrowseButton;
37 import org.jetbrains.annotations.NonNls;
38 import org.jetbrains.annotations.NotNull;
39
40 import javax.swing.*;
41 import java.awt.event.ActionEvent;
42 import java.awt.event.ActionListener;
43 import java.util.ArrayList;
44 import java.util.List;
45
46 public class CodeInspectionAction extends BaseAnalysisAction {
47   private GlobalInspectionContextImpl myGlobalInspectionContext;
48   protected InspectionProfileImpl myExternalProfile;
49
50   public CodeInspectionAction() {
51     super(InspectionsBundle.message("inspection.action.title"), InspectionsBundle.message("inspection.action.noun"));
52   }
53
54   public CodeInspectionAction(String title, String analysisNoon) {
55     super(title, analysisNoon);
56   }
57
58   @Override
59   protected void analyze(@NotNull Project project, @NotNull AnalysisScope scope) {
60     try {
61       runInspections(project, scope);
62     }
63     finally {
64       myGlobalInspectionContext = null;
65       myExternalProfile = null;
66     }
67   }
68
69   protected void runInspections(Project project, AnalysisScope scope) {
70     scope.setSearchInLibraries(false);
71     FileDocumentManager.getInstance().saveAllDocuments();
72     final GlobalInspectionContextImpl inspectionContext = getGlobalInspectionContext(project);
73     inspectionContext.setExternalProfile(myExternalProfile);
74     inspectionContext.setCurrentScope(scope);
75     inspectionContext.doInspections(scope);
76   }
77
78
79   private GlobalInspectionContextImpl getGlobalInspectionContext(Project project) {
80     if (myGlobalInspectionContext == null) {
81       myGlobalInspectionContext = ((InspectionManagerEx)InspectionManager.getInstance(project)).createNewGlobalContext(false);
82     }
83     return myGlobalInspectionContext;
84   }
85
86   @Override
87   @NonNls
88   protected String getHelpTopic() {
89     return "reference.dialogs.inspection.scope";
90   }
91
92   @Override
93   protected void canceled() {
94     super.canceled();
95     myGlobalInspectionContext = null;
96   }
97
98   @Override
99   protected JComponent getAdditionalActionSettings(@NotNull final Project project, final BaseAnalysisActionDialog dialog) {
100     final AdditionalPanel panel = new AdditionalPanel();
101     final InspectionManagerEx manager = (InspectionManagerEx)InspectionManager.getInstance(project);
102     final ProfilesComboBox profiles = (ProfilesComboBox)panel.myBrowseProfilesCombo.getComboBox();
103     final InspectionProfileManager profileManager = InspectionProfileManager.getInstance();
104     final ProjectInspectionProfileManager projectProfileManager = ProjectInspectionProfileManager.getInstanceImpl(project);
105     reloadProfiles(profiles, profileManager, projectProfileManager, manager);
106     panel.myBrowseProfilesCombo.addActionListener(new ActionListener() {
107       @Override
108       public void actionPerformed(ActionEvent e) {
109         final InspectionToolsConfigurable errorConfigurable = createConfigurable(projectProfileManager, profileManager, profiles);
110         final MySingleConfigurableEditor editor = new MySingleConfigurableEditor(project, errorConfigurable, manager);
111         if (editor.showAndGet()) {
112           reloadProfiles(profiles, profileManager, projectProfileManager, manager);
113         }
114         else {
115           //if profile was disabled and cancel after apply was pressed
116           final InspectionProfile profile = (InspectionProfile)profiles.getSelectedItem();
117           final boolean canExecute = profile != null && profile.isExecutable(project);
118           dialog.setOKActionEnabled(canExecute);
119         }
120       }
121     });
122     profiles.addActionListener(new ActionListener() {
123       @Override
124       public void actionPerformed(ActionEvent e) {
125         myExternalProfile = (InspectionProfileImpl)profiles.getSelectedItem();
126         final boolean canExecute = myExternalProfile != null && myExternalProfile.isExecutable(project);
127         dialog.setOKActionEnabled(canExecute);
128         if (canExecute) {
129           manager.setProfile(myExternalProfile.getName());
130         }
131       }
132     });
133     final InspectionProfile profile = (InspectionProfile)profiles.getSelectedItem();
134     dialog.setOKActionEnabled(profile != null && profile.isExecutable(project));
135     return panel.myAdditionalPanel;
136   }
137
138   protected InspectionToolsConfigurable createConfigurable(ProjectInspectionProfileManager projectProfileManager,
139                                                            InspectionProfileManager profileManager,
140                                                            final ProfilesComboBox profilesCombo) {
141     return new ExternalProfilesComboboxAwareInspectionToolsConfigurable(projectProfileManager, profilesCombo);
142   }
143
144   protected static class ExternalProfilesComboboxAwareInspectionToolsConfigurable extends InspectionToolsConfigurable {
145     private final ProfilesComboBox myProfilesCombo;
146
147     public ExternalProfilesComboboxAwareInspectionToolsConfigurable(@NotNull ProjectInspectionProfileManager projectProfileManager, ProfilesComboBox profilesCombo) {
148       super(projectProfileManager);
149       myProfilesCombo = profilesCombo;
150     }
151
152     @Override
153     protected InspectionProfileImpl getCurrentProfile() {
154       return (InspectionProfileImpl)myProfilesCombo.getSelectedItem();
155     }
156
157     @Override
158     protected void addProfile(InspectionProfileImpl model) {
159       super.addProfile(model);
160       myProfilesCombo.addProfile(model.getParentProfile());
161     }
162
163     @Override
164     protected void applyRootProfile(@NotNull String name, boolean isProjectLevel) {
165       for (int i = 0; i < myProfilesCombo.getItemCount(); i++) {
166         final InspectionProfileImpl profile = myProfilesCombo.getItemAt(i);
167         if (name.equals(profile.getName())) {
168           myProfilesCombo.setSelectedIndex(i);
169           break;
170         }
171       }
172     }
173   }
174
175
176   private void reloadProfiles(ProfilesComboBox profilesCombo,
177                               InspectionProfileManager inspectionProfileManager,
178                               InspectionProjectProfileManager inspectionProjectProfileManager,
179                               InspectionManagerEx inspectionManager) {
180     InspectionProfileImpl selectedProfile = getGlobalInspectionContext(inspectionManager.getProject()).getCurrentProfile();
181     List<InspectionProfileImpl> profiles = new ArrayList<>();
182     profiles.addAll(inspectionProfileManager.getProfiles());
183     profiles.addAll(inspectionProjectProfileManager.getProfiles());
184     profilesCombo.reset(profiles);
185     profilesCombo.selectProfile(selectedProfile);
186   }
187
188   private static class AdditionalPanel {
189     public ComboboxWithBrowseButton myBrowseProfilesCombo;
190     public JPanel myAdditionalPanel;
191
192     private void createUIComponents() {
193       myBrowseProfilesCombo = new ComboboxWithBrowseButton(new ProfilesComboBox() {
194         @Override
195         protected void onProfileChosen(InspectionProfileImpl inspectionProfile) {
196           //do nothing here
197         }
198       });
199     }
200   }
201
202   private static class MySingleConfigurableEditor extends SingleConfigurableEditor {
203     private final InspectionManagerEx myManager;
204
205     public MySingleConfigurableEditor(final Project project, final ErrorsConfigurable configurable, InspectionManagerEx manager) {
206       super(project, configurable, createDimensionKey(configurable));
207       myManager = manager;
208     }
209
210
211     @Override
212     protected void doOKAction() {
213       final Object o = ((ErrorsConfigurable)getConfigurable()).getSelectedObject();
214       if (o instanceof InspectionProfile) {
215         myManager.setProfile(((InspectionProfile)o).getName());
216       }
217       super.doOKAction();
218     }
219   }
220 }