preselect 'Project and Libs' when analyzing library
[fedora-idea.git] / platform / lang-impl / src / com / intellij / analysis / BaseAnalysisActionDialog.java
blob6bc16aca7e3086497a49efc2e45d45f767925ba9
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.text.StringUtil;
29 import com.intellij.openapi.vcs.changes.Change;
30 import com.intellij.openapi.vcs.changes.ChangeList;
31 import com.intellij.openapi.vcs.changes.ChangeListManager;
32 import com.intellij.openapi.vcs.changes.ContentRevision;
33 import com.intellij.openapi.vfs.VirtualFile;
34 import com.intellij.psi.PsiElement;
35 import com.intellij.psi.search.GlobalSearchScope;
36 import com.intellij.psi.search.SearchScope;
37 import com.intellij.psi.util.PsiUtilBase;
38 import com.intellij.ui.TitledSeparator;
39 import org.jetbrains.annotations.NotNull;
40 import org.jetbrains.annotations.Nullable;
42 import javax.swing.*;
43 import java.awt.*;
44 import java.awt.event.ActionEvent;
45 import java.awt.event.ActionListener;
46 import java.util.*;
47 import java.util.List;
49 /**
50 * User: anna
51 * Date: Jul 6, 2005
53 public class BaseAnalysisActionDialog extends DialogWrapper {
54 private JPanel myPanel;
55 private final String myFileName;
56 private final String myModuleName;
57 private JRadioButton myProjectButton;
58 private JRadioButton myModuleButton;
59 private JRadioButton myUncommitedFilesButton;
60 private JRadioButton myCustomScopeButton;
61 private JRadioButton myFileButton;
62 private ScopeChooserCombo myScopeCombo;
63 private JCheckBox myInspectTestSource;
64 private JComboBox myChangeLists;
65 private TitledSeparator myTitledSeparator;
66 private final Project myProject;
67 private final boolean myRememberScope;
68 private final String myAnalysisNoon;
69 private ButtonGroup myGroup;
71 private static final String ALL = AnalysisScopeBundle.message("scope.option.uncommited.files.all.changelists.choice");
72 private final AnalysisUIOptions myAnalysisOptions;
73 @Nullable private final PsiElement myContext;
75 public BaseAnalysisActionDialog(@NotNull String title,
76 @NotNull String analysisNoon,
77 @NotNull Project project,
78 @NotNull final AnalysisScope scope,
79 final String moduleName,
80 final boolean rememberScope,
81 @NotNull AnalysisUIOptions analysisUIOptions,
82 @Nullable PsiElement context) {
83 super(true);
84 myAnalysisOptions = analysisUIOptions;
85 myContext = context;
86 if (!analysisUIOptions.ANALYZE_TEST_SOURCES) {
87 myAnalysisOptions.ANALYZE_TEST_SOURCES = scope.isAnalyzeTestsByDefault();
89 myProject = project;
90 myFileName = scope.getShortenName();
91 myModuleName = moduleName;
92 myRememberScope = rememberScope;
93 myAnalysisNoon = analysisNoon;
94 init();
95 setTitle(title);
96 onScopeRadioButtonPressed();
99 public void setOKActionEnabled(boolean isEnabled) {
100 super.setOKActionEnabled(isEnabled);
103 protected JComponent createCenterPanel() {
104 myTitledSeparator.setText(myAnalysisNoon);
106 //include test option
107 myInspectTestSource.setSelected(myAnalysisOptions.ANALYZE_TEST_SOURCES);
109 //module scope if applicable
110 myModuleButton.setText(AnalysisScopeBundle.message("scope.option.module.with.mnemonic", myModuleName));
111 boolean useModuleScope = false;
112 if (myModuleName != null) {
113 useModuleScope = myAnalysisOptions.SCOPE_TYPE == AnalysisScope.MODULE;
114 myModuleButton.setSelected(myRememberScope && useModuleScope);
117 myModuleButton.setVisible(myModuleName != null && ModuleManager.getInstance(myProject).getModules().length > 1);
119 boolean useUncommitedFiles = false;
120 final ChangeListManager changeListManager = ChangeListManager.getInstance(myProject);
121 final boolean hasVCS = !changeListManager.getAffectedFiles().isEmpty();
122 if (hasVCS){
123 useUncommitedFiles = myAnalysisOptions.SCOPE_TYPE == AnalysisScope.UNCOMMITED_FILES;
124 myUncommitedFilesButton.setSelected(myRememberScope && useUncommitedFiles);
126 myUncommitedFilesButton.setVisible(hasVCS);
128 DefaultComboBoxModel model = new DefaultComboBoxModel();
129 model.addElement(ALL);
130 final List<? extends ChangeList> changeLists = changeListManager.getChangeListsCopy();
131 for (ChangeList changeList : changeLists) {
132 model.addElement(changeList.getName());
134 myChangeLists.setModel(model);
135 myChangeLists.setEnabled(myUncommitedFilesButton.isSelected());
136 myChangeLists.setVisible(hasVCS);
138 //file/package/directory/module scope
139 myFileButton.setText(myFileName);
140 myFileButton.setMnemonic(myFileName.charAt(0));
142 VirtualFile file = PsiUtilBase.getVirtualFile(myContext);
143 ProjectFileIndex fileIndex = ProjectRootManager.getInstance(myProject).getFileIndex();
144 boolean searchInLib = file != null && (fileIndex.isInLibraryClasses(file) || fileIndex.isInLibrarySource(file));
147 String preselect = !StringUtil.isEmptyOrSpaces(myAnalysisOptions.CUSTOM_SCOPE_NAME)
148 ? myAnalysisOptions.CUSTOM_SCOPE_NAME
149 : FindSettings.getInstance().getDefaultScopeName();
150 if (searchInLib && GlobalSearchScope.projectScope(myProject).getDisplayName().equals(preselect)) {
151 myAnalysisOptions.SCOPE_TYPE = AnalysisScope.CUSTOM;
152 myAnalysisOptions.CUSTOM_SCOPE_NAME = preselect = GlobalSearchScope.allScope(myProject).getDisplayName();
155 //custom scope
156 myCustomScopeButton.setSelected(myRememberScope && myAnalysisOptions.SCOPE_TYPE == AnalysisScope.CUSTOM);
158 myScopeCombo.init(myProject, searchInLib, true, preselect);
160 //correct selection
161 myProjectButton.setSelected(myRememberScope && myAnalysisOptions.SCOPE_TYPE == AnalysisScope.PROJECT);
162 myFileButton.setSelected(!myRememberScope ||
163 myAnalysisOptions.SCOPE_TYPE != AnalysisScope.PROJECT && !useModuleScope && myAnalysisOptions.SCOPE_TYPE != AnalysisScope.CUSTOM && !useUncommitedFiles);
165 myScopeCombo.setEnabled(myCustomScopeButton.isSelected());
167 final ActionListener radioButtonPressed = new ActionListener() {
168 public void actionPerformed(ActionEvent e) {
169 onScopeRadioButtonPressed();
172 final Enumeration<AbstractButton> enumeration = myGroup.getElements();
173 while (enumeration.hasMoreElements()) {
174 enumeration.nextElement().addActionListener(radioButtonPressed);
177 //additional panel - inspection profile chooser
178 JPanel wholePanel = new JPanel(new BorderLayout());
179 wholePanel.add(myPanel, BorderLayout.NORTH);
180 final JComponent additionalPanel = getAdditionalActionSettings(myProject);
181 if (additionalPanel!= null){
182 wholePanel.add(additionalPanel, BorderLayout.CENTER);
184 return wholePanel;
187 private void onScopeRadioButtonPressed() {
188 myScopeCombo.setEnabled(myCustomScopeButton.isSelected());
189 myChangeLists.setEnabled(myUncommitedFilesButton.isSelected());
192 @Nullable
193 protected JComponent getAdditionalActionSettings(final Project project) {
194 return null;
197 public boolean isProjectScopeSelected() {
198 return myProjectButton.isSelected();
201 public boolean isModuleScopeSelected() {
202 return myModuleButton != null && myModuleButton.isSelected();
205 public boolean isUncommitedFilesSelected(){
206 return myUncommitedFilesButton != null && myUncommitedFilesButton.isSelected();
209 @Nullable
210 public SearchScope getCustomScope(){
211 if (myCustomScopeButton.isSelected()){
212 return myScopeCombo.getSelectedScope();
214 return null;
217 protected void doOKAction() {
218 myAnalysisOptions.CUSTOM_SCOPE_NAME = myScopeCombo.getSelectedScopeName();
219 super.doOKAction();
222 public boolean isInspectTestSources(){
223 return myInspectTestSource.isSelected();
226 @NotNull
227 public AnalysisScope getScope(@NotNull AnalysisUIOptions uiOptions, @NotNull AnalysisScope defaultScope, @NotNull Project project, Module module) {
228 AnalysisScope scope;
229 if (isProjectScopeSelected()) {
230 scope = new AnalysisScope(project);
231 uiOptions.SCOPE_TYPE = AnalysisScope.PROJECT;
233 else {
234 final SearchScope customScope = getCustomScope();
235 if (customScope != null) {
236 scope = new AnalysisScope(customScope, project);
237 uiOptions.SCOPE_TYPE = AnalysisScope.CUSTOM;
238 uiOptions.CUSTOM_SCOPE_NAME = customScope.getDisplayName();
240 else if (isModuleScopeSelected()) {
241 scope = new AnalysisScope(module);
242 uiOptions.SCOPE_TYPE = AnalysisScope.MODULE;
244 else if (isUncommitedFilesSelected()) {
245 final ChangeListManager changeListManager = ChangeListManager.getInstance(project);
246 List<VirtualFile> files;
247 if (myChangeLists.getSelectedItem() == ALL) {
248 files = changeListManager.getAffectedFiles();
250 else {
251 files = new ArrayList<VirtualFile>();
252 for (ChangeList list : changeListManager.getChangeListsCopy()) {
253 if (!Comparing.strEqual(list.getName(), (String)myChangeLists.getSelectedItem())) continue;
254 final Collection<Change> changes = list.getChanges();
255 for (Change change : changes) {
256 final ContentRevision afterRevision = change.getAfterRevision();
257 if (afterRevision != null) {
258 final VirtualFile vFile = afterRevision.getFile().getVirtualFile();
259 if (vFile != null) {
260 files.add(vFile);
266 scope = new AnalysisScope(project, new HashSet<VirtualFile>(files));
267 uiOptions.SCOPE_TYPE = AnalysisScope.UNCOMMITED_FILES;
269 else {
270 scope = defaultScope;
271 uiOptions.SCOPE_TYPE = defaultScope.getScopeType();//just not project scope
274 uiOptions.ANALYZE_TEST_SOURCES = isInspectTestSources();
275 scope.setIncludeTestSource(isInspectTestSources());
276 return scope;