reload scopes if they were changed (IDEA-24326 ); merge events on apply
[fedora-idea.git] / platform / lang-impl / src / com / intellij / find / findUsages / AbstractFindUsagesDialog.java
blob3c241d045748464d86667cdeb0b47bead1279cc6
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.find.findUsages;
18 import com.intellij.find.FindBundle;
19 import com.intellij.find.FindSettings;
20 import com.intellij.ide.util.scopeChooser.ScopeChooserCombo;
21 import com.intellij.openapi.project.Project;
22 import com.intellij.openapi.ui.DialogWrapper;
23 import com.intellij.openapi.util.Disposer;
24 import com.intellij.openapi.util.IconLoader;
25 import com.intellij.psi.search.GlobalSearchScope;
26 import com.intellij.ui.IdeBorderFactory;
27 import com.intellij.ui.StateRestoringCheckBox;
28 import com.intellij.usageView.UsageViewManager;
29 import org.jetbrains.annotations.NotNull;
30 import org.jetbrains.annotations.Nullable;
32 import javax.swing.*;
33 import java.awt.*;
34 import java.awt.event.ActionEvent;
35 import java.awt.event.ActionListener;
37 /**
38 * @author peter
40 public abstract class AbstractFindUsagesDialog extends DialogWrapper {
41 private final Project myProject;
42 protected final FindUsagesOptions myFindUsagesOptions;
44 private final boolean myToShowInNewTab;
45 private final boolean myIsShowInNewTabEnabled;
46 private final boolean myIsShowInNewTabVisible;
48 private final boolean mySearchForTextOccurencesAvailable;
50 private final boolean mySearchInLibrariesAvailable;
52 private JCheckBox myCbToOpenInNewTab;
54 protected StateRestoringCheckBox myCbToSearchForTextOccurences;
55 protected JCheckBox myCbToSkipResultsWhenOneUsage;
57 private final ActionListener myUpdateAction;
59 private ScopeChooserCombo myScopeCombo;
61 protected AbstractFindUsagesDialog(@NotNull Project project,
62 @NotNull FindUsagesOptions findUsagesOptions,
63 boolean toShowInNewTab,
64 boolean mustOpenInNewTab,
65 boolean isSingleFile,
66 boolean searchForTextOccurencesAvailable,
67 boolean searchInLibrariesAvailable) {
68 super(project, true);
69 myProject = project;
70 myFindUsagesOptions = findUsagesOptions;
71 myToShowInNewTab = toShowInNewTab;
72 myIsShowInNewTabEnabled = !mustOpenInNewTab && UsageViewManager.getInstance(myProject).getReusableContentsCount() > 0;
73 myIsShowInNewTabVisible = !isSingleFile;
74 mySearchForTextOccurencesAvailable = searchForTextOccurencesAvailable;
75 mySearchInLibrariesAvailable = searchInLibrariesAvailable;
77 myUpdateAction = new ActionListener() {
78 public void actionPerformed(ActionEvent event) {
79 update();
83 setButtonsMargin(null);
85 setOKButtonText(FindBundle.message("find.dialog.find.button"));
86 setOKButtonIcon(IconLoader.getIcon("/actions/find.png"));
87 setTitle(isSingleFile ? FindBundle.message("find.usages.in.file.dialog.title") : FindBundle.message("find.usages.dialog.title"));
90 protected Action[] createActions() {
91 return new Action[]{getOKAction(), getCancelAction(), getHelpAction()};
94 protected boolean isInFileOnly() {
95 return !myIsShowInNewTabVisible;
98 protected JComponent createNorthPanel() {
99 JPanel panel = new JPanel(new GridBagLayout());
100 GridBagConstraints gbConstraints = new GridBagConstraints();
102 gbConstraints.insets = new Insets(4, 4, 4, 4);
103 gbConstraints.fill = GridBagConstraints.BOTH;
104 gbConstraints.weightx = 1;
105 gbConstraints.weighty = 1;
106 gbConstraints.anchor = GridBagConstraints.EAST;
107 final JLabel promptLabel = new JLabel(getLabelText());
108 panel.add(promptLabel, gbConstraints);
110 return panel;
113 public abstract String getLabelText();
115 protected JComponent createCenterPanel() {
116 JPanel panel = new JPanel(new GridBagLayout());
118 JPanel _panel = new JPanel(new BorderLayout());
119 _panel.setBorder(BorderFactory.createEmptyBorder(0, 4, 0, 0));
120 panel.add(_panel, new GridBagConstraints(0, 1, 1, 1, 1.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
121 new Insets(0, 0, 0, 0), 0, 0));
123 if (myIsShowInNewTabVisible) {
124 myCbToOpenInNewTab = new JCheckBox(FindBundle.message("find.open.in.new.tab.checkbox"));
125 myCbToOpenInNewTab.setSelected(myToShowInNewTab);
126 myCbToOpenInNewTab.setEnabled(myIsShowInNewTabEnabled);
127 _panel.add(myCbToOpenInNewTab, BorderLayout.EAST);
130 JPanel allOptionsPanel = createAllOptionsPanel();
131 if (allOptionsPanel != null) {
132 panel.add(allOptionsPanel, new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH,
133 new Insets(0, 0, 0, 0), 0, 0));
135 return panel;
138 public final FindUsagesOptions calcFindUsagesOptions() {
139 calcFindUsagesOptions(myFindUsagesOptions);
140 return myFindUsagesOptions;
143 protected void init() {
144 super.init();
145 update();
148 public void calcFindUsagesOptions(FindUsagesOptions options) {
149 options.searchScope = myScopeCombo == null ? GlobalSearchScope.allScope(myProject) : myScopeCombo.getSelectedScope();
151 options.isSearchForTextOccurences = isToChange(myCbToSearchForTextOccurences) && isSelected(myCbToSearchForTextOccurences);
154 protected void update() {
157 public boolean isShowInSeparateWindow() {
158 return myCbToOpenInNewTab != null && myCbToOpenInNewTab.isSelected();
161 public boolean isSkipResultsWhenOneUsage() {
162 return myCbToSkipResultsWhenOneUsage != null && myCbToSkipResultsWhenOneUsage.isSelected();
165 protected void doOKAction() {
166 if (!shouldDoOkAction()) return;
168 FindSettings settings = FindSettings.getInstance();
170 if (myScopeCombo != null) {
171 settings.setDefaultScopeName(myScopeCombo.getSelectedScopeName());
173 if (mySearchForTextOccurencesAvailable && myCbToSearchForTextOccurences != null && myCbToSearchForTextOccurences.isEnabled()) {
174 settings.setSearchForTextOccurences(myCbToSearchForTextOccurences.isSelected());
177 if (myCbToSkipResultsWhenOneUsage != null) {
178 settings.setSkipResultsWithOneUsage(isSkipResultsWhenOneUsage());
181 super.doOKAction();
184 protected boolean shouldDoOkAction() {
185 return myScopeCombo == null || myScopeCombo.getSelectedScope() != null;
188 protected static boolean isToChange(JCheckBox cb) {
189 return cb != null && cb.getParent() != null;
192 protected static boolean isSelected(JCheckBox cb) {
193 return cb != null && cb.getParent() != null && cb.isSelected();
196 protected StateRestoringCheckBox addCheckboxToPanel(String name, boolean toSelect, JPanel panel, boolean toUpdate) {
197 StateRestoringCheckBox cb = new StateRestoringCheckBox(name);
198 cb.setSelected(toSelect);
199 panel.add(cb);
200 if (toUpdate) {
201 cb.addActionListener(myUpdateAction);
203 return cb;
206 protected JPanel createAllOptionsPanel() {
207 JPanel allOptionsPanel = new JPanel();
209 JPanel findWhatPanel = createFindWhatPanel();
210 JPanel usagesOptionsPanel = createUsagesOptionsPanel();
211 int grids = 0;
212 if (findWhatPanel != null) {
213 grids++;
215 if (usagesOptionsPanel != null) {
216 grids++;
218 if (grids != 0) {
219 allOptionsPanel.setLayout(new GridLayout(1, grids, 8, 0));
220 if (findWhatPanel != null) {
221 allOptionsPanel.add(findWhatPanel);
223 if (usagesOptionsPanel != null) {
224 allOptionsPanel.add(usagesOptionsPanel);
228 JComponent scopePanel = createSearchScopePanel();
229 if (scopePanel != null) {
230 JPanel panel = new JPanel(new BorderLayout());
231 panel.add(allOptionsPanel, BorderLayout.CENTER);
232 panel.add(scopePanel, BorderLayout.SOUTH);
233 return panel;
236 return allOptionsPanel;
239 @Nullable
240 protected abstract JPanel createFindWhatPanel();
242 protected void addUsagesOptions(JPanel optionsPanel) {
243 if (mySearchForTextOccurencesAvailable) {
244 myCbToSearchForTextOccurences = addCheckboxToPanel(FindBundle.message("find.options.search.for.text.occurences.checkbox"),
245 FindSettings.getInstance().isSearchForTextOccurences(), optionsPanel, false);
249 if (myIsShowInNewTabVisible) {
250 myCbToSkipResultsWhenOneUsage = addCheckboxToPanel(FindBundle.message("find.options.skip.results.tab.with.one.usage.checkbox"),
251 FindSettings.getInstance().isSkipResultsWithOneUsage(), optionsPanel, false);
256 @Nullable
257 protected JPanel createUsagesOptionsPanel() {
258 JPanel optionsPanel = new JPanel();
259 optionsPanel.setBorder(IdeBorderFactory.createTitledBorder(FindBundle.message("find.options.group")));
260 optionsPanel.setLayout(new BoxLayout(optionsPanel, BoxLayout.Y_AXIS));
261 addUsagesOptions(optionsPanel);
262 return optionsPanel.getComponents().length == 0 ? null : optionsPanel;
265 @Nullable
266 private JComponent createSearchScopePanel() {
267 if (isInFileOnly()) return null;
268 JPanel optionsPanel = new JPanel(new BorderLayout());
269 JLabel label = new JLabel(FindBundle.message("find.scope.label"));
270 optionsPanel.add(label, BorderLayout.WEST);
271 String scope = myFindUsagesOptions.searchScope.getDisplayName();
272 myScopeCombo = new ScopeChooserCombo(myProject, mySearchInLibrariesAvailable, true, scope);
273 Disposer.register(myDisposable, myScopeCombo);
274 optionsPanel.add(myScopeCombo, BorderLayout.CENTER);
275 label.setLabelFor(myScopeCombo.getComboBox());
276 return optionsPanel;
279 @Nullable
280 protected JComponent getPreferredFocusedControl() {
281 return null;
284 public JComponent getPreferredFocusedComponent() {
285 if (myScopeCombo != null) {
286 return myScopeCombo.getComboBox();
288 return getPreferredFocusedControl();