inspect code: up/down listeners (IDEA-15209)
[fedora-idea.git] / platform / lang-impl / src / com / intellij / analysis / BaseAnalysisActionDialog.java
bloba96103a924a9575b801e911040282a41c0fc3f24
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.find.FindSettings;
20 import com.intellij.ide.util.scopeChooser.ScopeChooserCombo;
21 import com.intellij.openapi.module.Module;
22 import com.intellij.openapi.module.ModuleManager;
23 import com.intellij.openapi.project.Project;
24 import com.intellij.openapi.roots.ProjectFileIndex;
25 import com.intellij.openapi.roots.ProjectRootManager;
26 import com.intellij.openapi.ui.DialogWrapper;
27 import com.intellij.openapi.util.Comparing;
28 import com.intellij.openapi.util.Disposer;
29 import com.intellij.openapi.util.text.StringUtil;
30 import com.intellij.openapi.vcs.changes.Change;
31 import com.intellij.openapi.vcs.changes.ChangeList;
32 import com.intellij.openapi.vcs.changes.ChangeListManager;
33 import com.intellij.openapi.vcs.changes.ContentRevision;
34 import com.intellij.openapi.vfs.VirtualFile;
35 import com.intellij.psi.PsiElement;
36 import com.intellij.psi.search.GlobalSearchScope;
37 import com.intellij.psi.search.SearchScope;
38 import com.intellij.psi.util.PsiUtilBase;
39 import com.intellij.refactoring.util.RadioUpDownListener;
40 import com.intellij.ui.TitledSeparator;
41 import org.jetbrains.annotations.NotNull;
42 import org.jetbrains.annotations.Nullable;
44 import javax.swing.*;
45 import java.awt.*;
46 import java.awt.event.ActionEvent;
47 import java.awt.event.ActionListener;
48 import java.util.*;
49 import java.util.List;
51 /**
52 * User: anna
53 * Date: Jul 6, 2005
55 public class BaseAnalysisActionDialog extends DialogWrapper {
56 private JPanel myPanel;
57 private final String myFileName;
58 private final String myModuleName;
59 private JRadioButton myProjectButton;
60 private JRadioButton myModuleButton;
61 private JRadioButton myUncommitedFilesButton;
62 private JRadioButton myCustomScopeButton;
63 private JRadioButton myFileButton;
64 private ScopeChooserCombo myScopeCombo;
65 private JCheckBox myInspectTestSource;
66 private JComboBox myChangeLists;
67 private TitledSeparator myTitledSeparator;
68 private final Project myProject;
69 private final boolean myRememberScope;
70 private final String myAnalysisNoon;
71 private ButtonGroup myGroup;
73 private static final String ALL = AnalysisScopeBundle.message("scope.option.uncommited.files.all.changelists.choice");
74 private final AnalysisUIOptions myAnalysisOptions;
75 @Nullable private final PsiElement myContext;
77 public BaseAnalysisActionDialog(@NotNull String title,
78 @NotNull String analysisNoon,
79 @NotNull Project project,
80 @NotNull final AnalysisScope scope,
81 final String moduleName,
82 final boolean rememberScope,
83 @NotNull AnalysisUIOptions analysisUIOptions,
84 @Nullable PsiElement context) {
85 super(true);
86 Disposer.register(myDisposable, myScopeCombo);
87 myAnalysisOptions = analysisUIOptions;
88 myContext = context;
89 if (!analysisUIOptions.ANALYZE_TEST_SOURCES) {
90 myAnalysisOptions.ANALYZE_TEST_SOURCES = scope.isAnalyzeTestsByDefault();
92 myProject = project;
93 myFileName = scope.getShortenName();
94 myModuleName = moduleName;
95 myRememberScope = rememberScope;
96 myAnalysisNoon = analysisNoon;
97 init();
98 setTitle(title);
99 onScopeRadioButtonPressed();
102 public void setOKActionEnabled(boolean isEnabled) {
103 super.setOKActionEnabled(isEnabled);
106 protected JComponent createCenterPanel() {
107 myTitledSeparator.setText(myAnalysisNoon);
109 //include test option
110 myInspectTestSource.setSelected(myAnalysisOptions.ANALYZE_TEST_SOURCES);
112 //module scope if applicable
113 myModuleButton.setText(AnalysisScopeBundle.message("scope.option.module.with.mnemonic", myModuleName));
114 boolean useModuleScope = false;
115 if (myModuleName != null) {
116 useModuleScope = myAnalysisOptions.SCOPE_TYPE == AnalysisScope.MODULE;
117 myModuleButton.setSelected(myRememberScope && useModuleScope);
120 myModuleButton.setVisible(myModuleName != null && ModuleManager.getInstance(myProject).getModules().length > 1);
122 boolean useUncommitedFiles = false;
123 final ChangeListManager changeListManager = ChangeListManager.getInstance(myProject);
124 final boolean hasVCS = !changeListManager.getAffectedFiles().isEmpty();
125 if (hasVCS){
126 useUncommitedFiles = myAnalysisOptions.SCOPE_TYPE == AnalysisScope.UNCOMMITED_FILES;
127 myUncommitedFilesButton.setSelected(myRememberScope && useUncommitedFiles);
129 myUncommitedFilesButton.setVisible(hasVCS);
131 DefaultComboBoxModel model = new DefaultComboBoxModel();
132 model.addElement(ALL);
133 final List<? extends ChangeList> changeLists = changeListManager.getChangeListsCopy();
134 for (ChangeList changeList : changeLists) {
135 model.addElement(changeList.getName());
137 myChangeLists.setModel(model);
138 myChangeLists.setEnabled(myUncommitedFilesButton.isSelected());
139 myChangeLists.setVisible(hasVCS);
141 //file/package/directory/module scope
142 myFileButton.setText(myFileName);
143 myFileButton.setMnemonic(myFileName.charAt(0));
145 VirtualFile file = PsiUtilBase.getVirtualFile(myContext);
146 ProjectFileIndex fileIndex = ProjectRootManager.getInstance(myProject).getFileIndex();
147 boolean searchInLib = file != null && (fileIndex.isInLibraryClasses(file) || fileIndex.isInLibrarySource(file));
149 String preselect = StringUtil.isEmptyOrSpaces(myAnalysisOptions.CUSTOM_SCOPE_NAME)
150 ? FindSettings.getInstance().getDefaultScopeName()
151 : myAnalysisOptions.CUSTOM_SCOPE_NAME;
152 if (searchInLib && GlobalSearchScope.projectScope(myProject).getDisplayName().equals(preselect)) {
153 preselect = GlobalSearchScope.allScope(myProject).getDisplayName();
155 if (GlobalSearchScope.allScope(myProject).getDisplayName().equals(preselect)) {
156 myAnalysisOptions.SCOPE_TYPE = AnalysisScope.CUSTOM;
157 myAnalysisOptions.CUSTOM_SCOPE_NAME = preselect;
158 searchInLib = true;
161 //custom scope
162 myCustomScopeButton.setSelected(myRememberScope && myAnalysisOptions.SCOPE_TYPE == AnalysisScope.CUSTOM);
164 myScopeCombo.init(myProject, searchInLib, true, preselect);
166 //correct selection
167 myProjectButton.setSelected(myRememberScope && myAnalysisOptions.SCOPE_TYPE == AnalysisScope.PROJECT);
168 myFileButton.setSelected(!myRememberScope ||
169 myAnalysisOptions.SCOPE_TYPE != AnalysisScope.PROJECT && !useModuleScope && myAnalysisOptions.SCOPE_TYPE != AnalysisScope.CUSTOM && !useUncommitedFiles);
171 myScopeCombo.setEnabled(myCustomScopeButton.isSelected());
173 final ActionListener radioButtonPressed = new ActionListener() {
174 public void actionPerformed(ActionEvent e) {
175 onScopeRadioButtonPressed();
178 final Enumeration<AbstractButton> enumeration = myGroup.getElements();
179 while (enumeration.hasMoreElements()) {
180 enumeration.nextElement().addActionListener(radioButtonPressed);
183 //additional panel - inspection profile chooser
184 JPanel wholePanel = new JPanel(new BorderLayout());
185 wholePanel.add(myPanel, BorderLayout.NORTH);
186 final JComponent additionalPanel = getAdditionalActionSettings(myProject);
187 if (additionalPanel!= null){
188 wholePanel.add(additionalPanel, BorderLayout.CENTER);
190 new RadioUpDownListener(myProjectButton, myModuleButton, myUncommitedFilesButton, myFileButton, myCustomScopeButton);
191 return wholePanel;
194 private void onScopeRadioButtonPressed() {
195 myScopeCombo.setEnabled(myCustomScopeButton.isSelected());
196 myChangeLists.setEnabled(myUncommitedFilesButton.isSelected());
199 @Override
200 public JComponent getPreferredFocusedComponent() {
201 final Enumeration<AbstractButton> enumeration = myGroup.getElements();
202 while (enumeration.hasMoreElements()) {
203 final AbstractButton button = enumeration.nextElement();
204 if (button.isSelected()) {
205 return button;
208 return myPanel;
211 @Nullable
212 protected JComponent getAdditionalActionSettings(final Project project) {
213 return null;
216 public boolean isProjectScopeSelected() {
217 return myProjectButton.isSelected();
220 public boolean isModuleScopeSelected() {
221 return myModuleButton != null && myModuleButton.isSelected();
224 public boolean isUncommitedFilesSelected(){
225 return myUncommitedFilesButton != null && myUncommitedFilesButton.isSelected();
228 @Nullable
229 public SearchScope getCustomScope(){
230 if (myCustomScopeButton.isSelected()){
231 return myScopeCombo.getSelectedScope();
233 return null;
236 public boolean isInspectTestSources(){
237 return myInspectTestSource.isSelected();
240 @NotNull
241 public AnalysisScope getScope(@NotNull AnalysisUIOptions uiOptions, @NotNull AnalysisScope defaultScope, @NotNull Project project, Module module) {
242 AnalysisScope scope;
243 if (isProjectScopeSelected()) {
244 scope = new AnalysisScope(project);
245 uiOptions.SCOPE_TYPE = AnalysisScope.PROJECT;
247 else {
248 final SearchScope customScope = getCustomScope();
249 if (customScope != null) {
250 scope = new AnalysisScope(customScope, project);
251 uiOptions.SCOPE_TYPE = AnalysisScope.CUSTOM;
252 uiOptions.CUSTOM_SCOPE_NAME = customScope.getDisplayName();
254 else if (isModuleScopeSelected()) {
255 scope = new AnalysisScope(module);
256 uiOptions.SCOPE_TYPE = AnalysisScope.MODULE;
258 else if (isUncommitedFilesSelected()) {
259 final ChangeListManager changeListManager = ChangeListManager.getInstance(project);
260 List<VirtualFile> files;
261 if (myChangeLists.getSelectedItem() == ALL) {
262 files = changeListManager.getAffectedFiles();
264 else {
265 files = new ArrayList<VirtualFile>();
266 for (ChangeList list : changeListManager.getChangeListsCopy()) {
267 if (!Comparing.strEqual(list.getName(), (String)myChangeLists.getSelectedItem())) continue;
268 final Collection<Change> changes = list.getChanges();
269 for (Change change : changes) {
270 final ContentRevision afterRevision = change.getAfterRevision();
271 if (afterRevision != null) {
272 final VirtualFile vFile = afterRevision.getFile().getVirtualFile();
273 if (vFile != null) {
274 files.add(vFile);
280 scope = new AnalysisScope(project, new HashSet<VirtualFile>(files));
281 uiOptions.SCOPE_TYPE = AnalysisScope.UNCOMMITED_FILES;
283 else {
284 scope = defaultScope;
285 uiOptions.SCOPE_TYPE = defaultScope.getScopeType();//just not project scope
288 uiOptions.ANALYZE_TEST_SOURCES = isInspectTestSources();
289 scope.setIncludeTestSource(isInspectTestSources());
291 FindSettings.getInstance().setDefaultScopeName(scope.getDisplayName());
292 return scope;