reload scopes if they were changed (IDEA-24326 ); merge events on apply
[fedora-idea.git] / platform / lang-impl / src / com / intellij / analysis / BaseAnalysisActionDialog.java
blob4efae3ccd126817f262b5c1e899f9f495fdb162e
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.ui.TitledSeparator;
40 import org.jetbrains.annotations.NotNull;
41 import org.jetbrains.annotations.Nullable;
43 import javax.swing.*;
44 import java.awt.*;
45 import java.awt.event.ActionEvent;
46 import java.awt.event.ActionListener;
47 import java.util.*;
48 import java.util.List;
50 /**
51 * User: anna
52 * Date: Jul 6, 2005
54 public class BaseAnalysisActionDialog extends DialogWrapper {
55 private JPanel myPanel;
56 private final String myFileName;
57 private final String myModuleName;
58 private JRadioButton myProjectButton;
59 private JRadioButton myModuleButton;
60 private JRadioButton myUncommitedFilesButton;
61 private JRadioButton myCustomScopeButton;
62 private JRadioButton myFileButton;
63 private ScopeChooserCombo myScopeCombo;
64 private JCheckBox myInspectTestSource;
65 private JComboBox myChangeLists;
66 private TitledSeparator myTitledSeparator;
67 private final Project myProject;
68 private final boolean myRememberScope;
69 private final String myAnalysisNoon;
70 private ButtonGroup myGroup;
72 private static final String ALL = AnalysisScopeBundle.message("scope.option.uncommited.files.all.changelists.choice");
73 private final AnalysisUIOptions myAnalysisOptions;
74 @Nullable private final PsiElement myContext;
76 public BaseAnalysisActionDialog(@NotNull String title,
77 @NotNull String analysisNoon,
78 @NotNull Project project,
79 @NotNull final AnalysisScope scope,
80 final String moduleName,
81 final boolean rememberScope,
82 @NotNull AnalysisUIOptions analysisUIOptions,
83 @Nullable PsiElement context) {
84 super(true);
85 Disposer.register(myDisposable, myScopeCombo);
86 myAnalysisOptions = analysisUIOptions;
87 myContext = context;
88 if (!analysisUIOptions.ANALYZE_TEST_SOURCES) {
89 myAnalysisOptions.ANALYZE_TEST_SOURCES = scope.isAnalyzeTestsByDefault();
91 myProject = project;
92 myFileName = scope.getShortenName();
93 myModuleName = moduleName;
94 myRememberScope = rememberScope;
95 myAnalysisNoon = analysisNoon;
96 init();
97 setTitle(title);
98 onScopeRadioButtonPressed();
101 public void setOKActionEnabled(boolean isEnabled) {
102 super.setOKActionEnabled(isEnabled);
105 protected JComponent createCenterPanel() {
106 myTitledSeparator.setText(myAnalysisNoon);
108 //include test option
109 myInspectTestSource.setSelected(myAnalysisOptions.ANALYZE_TEST_SOURCES);
111 //module scope if applicable
112 myModuleButton.setText(AnalysisScopeBundle.message("scope.option.module.with.mnemonic", myModuleName));
113 boolean useModuleScope = false;
114 if (myModuleName != null) {
115 useModuleScope = myAnalysisOptions.SCOPE_TYPE == AnalysisScope.MODULE;
116 myModuleButton.setSelected(myRememberScope && useModuleScope);
119 myModuleButton.setVisible(myModuleName != null && ModuleManager.getInstance(myProject).getModules().length > 1);
121 boolean useUncommitedFiles = false;
122 final ChangeListManager changeListManager = ChangeListManager.getInstance(myProject);
123 final boolean hasVCS = !changeListManager.getAffectedFiles().isEmpty();
124 if (hasVCS){
125 useUncommitedFiles = myAnalysisOptions.SCOPE_TYPE == AnalysisScope.UNCOMMITED_FILES;
126 myUncommitedFilesButton.setSelected(myRememberScope && useUncommitedFiles);
128 myUncommitedFilesButton.setVisible(hasVCS);
130 DefaultComboBoxModel model = new DefaultComboBoxModel();
131 model.addElement(ALL);
132 final List<? extends ChangeList> changeLists = changeListManager.getChangeListsCopy();
133 for (ChangeList changeList : changeLists) {
134 model.addElement(changeList.getName());
136 myChangeLists.setModel(model);
137 myChangeLists.setEnabled(myUncommitedFilesButton.isSelected());
138 myChangeLists.setVisible(hasVCS);
140 //file/package/directory/module scope
141 myFileButton.setText(myFileName);
142 myFileButton.setMnemonic(myFileName.charAt(0));
144 VirtualFile file = PsiUtilBase.getVirtualFile(myContext);
145 ProjectFileIndex fileIndex = ProjectRootManager.getInstance(myProject).getFileIndex();
146 boolean searchInLib = file != null && (fileIndex.isInLibraryClasses(file) || fileIndex.isInLibrarySource(file));
148 String preselect = StringUtil.isEmptyOrSpaces(myAnalysisOptions.CUSTOM_SCOPE_NAME)
149 ? FindSettings.getInstance().getDefaultScopeName()
150 : myAnalysisOptions.CUSTOM_SCOPE_NAME;
151 if (searchInLib && GlobalSearchScope.projectScope(myProject).getDisplayName().equals(preselect)) {
152 preselect = GlobalSearchScope.allScope(myProject).getDisplayName();
154 if (GlobalSearchScope.allScope(myProject).getDisplayName().equals(preselect)) {
155 myAnalysisOptions.SCOPE_TYPE = AnalysisScope.CUSTOM;
156 myAnalysisOptions.CUSTOM_SCOPE_NAME = preselect;
157 searchInLib = true;
160 //custom scope
161 myCustomScopeButton.setSelected(myRememberScope && myAnalysisOptions.SCOPE_TYPE == AnalysisScope.CUSTOM);
163 myScopeCombo.init(myProject, searchInLib, true, preselect);
165 //correct selection
166 myProjectButton.setSelected(myRememberScope && myAnalysisOptions.SCOPE_TYPE == AnalysisScope.PROJECT);
167 myFileButton.setSelected(!myRememberScope ||
168 myAnalysisOptions.SCOPE_TYPE != AnalysisScope.PROJECT && !useModuleScope && myAnalysisOptions.SCOPE_TYPE != AnalysisScope.CUSTOM && !useUncommitedFiles);
170 myScopeCombo.setEnabled(myCustomScopeButton.isSelected());
172 final ActionListener radioButtonPressed = new ActionListener() {
173 public void actionPerformed(ActionEvent e) {
174 onScopeRadioButtonPressed();
177 final Enumeration<AbstractButton> enumeration = myGroup.getElements();
178 while (enumeration.hasMoreElements()) {
179 enumeration.nextElement().addActionListener(radioButtonPressed);
182 //additional panel - inspection profile chooser
183 JPanel wholePanel = new JPanel(new BorderLayout());
184 wholePanel.add(myPanel, BorderLayout.NORTH);
185 final JComponent additionalPanel = getAdditionalActionSettings(myProject);
186 if (additionalPanel!= null){
187 wholePanel.add(additionalPanel, BorderLayout.CENTER);
189 return wholePanel;
192 private void onScopeRadioButtonPressed() {
193 myScopeCombo.setEnabled(myCustomScopeButton.isSelected());
194 myChangeLists.setEnabled(myUncommitedFilesButton.isSelected());
197 @Nullable
198 protected JComponent getAdditionalActionSettings(final Project project) {
199 return null;
202 public boolean isProjectScopeSelected() {
203 return myProjectButton.isSelected();
206 public boolean isModuleScopeSelected() {
207 return myModuleButton != null && myModuleButton.isSelected();
210 public boolean isUncommitedFilesSelected(){
211 return myUncommitedFilesButton != null && myUncommitedFilesButton.isSelected();
214 @Nullable
215 public SearchScope getCustomScope(){
216 if (myCustomScopeButton.isSelected()){
217 return myScopeCombo.getSelectedScope();
219 return null;
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());
277 FindSettings.getInstance().setDefaultScopeName(scope.getDisplayName());
278 return scope;