Merge remote-tracking branch 'origin/master'
[idea/community.git] / platform / lang-impl / src / com / intellij / find / impl / FindDialog.java
1 /*
2  * Copyright 2000-2014 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
17 package com.intellij.find.impl;
18
19 import com.intellij.CommonBundle;
20 import com.intellij.find.FindBundle;
21 import com.intellij.find.FindModel;
22 import com.intellij.find.FindSettings;
23 import com.intellij.ide.util.scopeChooser.ScopeChooserCombo;
24 import com.intellij.openapi.diagnostic.Logger;
25 import com.intellij.openapi.editor.event.DocumentAdapter;
26 import com.intellij.openapi.editor.event.DocumentEvent;
27 import com.intellij.openapi.fileChooser.FileChooser;
28 import com.intellij.openapi.fileChooser.FileChooserDescriptor;
29 import com.intellij.openapi.fileChooser.FileChooserDescriptorFactory;
30 import com.intellij.openapi.fileTypes.FileType;
31 import com.intellij.openapi.fileTypes.PlainTextFileType;
32 import com.intellij.openapi.help.HelpManager;
33 import com.intellij.openapi.module.Module;
34 import com.intellij.openapi.module.ModuleManager;
35 import com.intellij.openapi.module.ModuleUtilCore;
36 import com.intellij.openapi.project.Project;
37 import com.intellij.openapi.ui.*;
38 import com.intellij.openapi.util.Disposer;
39 import com.intellij.openapi.util.text.StringUtil;
40 import com.intellij.openapi.vfs.LocalFileSystem;
41 import com.intellij.openapi.vfs.VirtualFile;
42 import com.intellij.projectImport.ProjectAttachProcessor;
43 import com.intellij.psi.PsiDirectory;
44 import com.intellij.psi.PsiDocumentManager;
45 import com.intellij.psi.PsiFile;
46 import com.intellij.psi.PsiFileFactory;
47 import com.intellij.psi.search.SearchScope;
48 import com.intellij.ui.EditorComboBoxRenderer;
49 import com.intellij.ui.EditorTextField;
50 import com.intellij.ui.IdeBorderFactory;
51 import com.intellij.ui.StateRestoringCheckBox;
52 import com.intellij.util.ArrayUtil;
53 import com.intellij.util.Consumer;
54 import com.intellij.util.ui.UIUtil;
55 import org.intellij.lang.regexp.RegExpFileType;
56 import org.jetbrains.annotations.NotNull;
57 import org.jetbrains.annotations.Nullable;
58
59 import javax.swing.*;
60 import java.awt.*;
61 import java.awt.event.*;
62 import java.util.Arrays;
63 import java.util.HashMap;
64 import java.util.List;
65 import java.util.Map;
66 import java.util.regex.Pattern;
67 import java.util.regex.PatternSyntaxException;
68
69 public class FindDialog extends DialogWrapper {
70   private static final Logger LOG = Logger.getInstance("#com.intellij.find.impl.FindDialog");
71
72   private ComboBox myInputComboBox;
73   private ComboBox myReplaceComboBox;
74   private StateRestoringCheckBox myCbCaseSensitive;
75   private StateRestoringCheckBox myCbPreserveCase;
76   private StateRestoringCheckBox myCbWholeWordsOnly;
77   private ComboBox mySearchContext;
78   private StateRestoringCheckBox myCbRegularExpressions;
79   private JRadioButton myRbGlobal;
80   private JRadioButton myRbSelectedText;
81   private JRadioButton myRbForward;
82   private JRadioButton myRbBackward;
83   private JRadioButton myRbFromCursor;
84   private JRadioButton myRbEntireScope;
85   private JRadioButton myRbProject;
86   private JRadioButton myRbDirectory;
87   private JRadioButton myRbModule;
88   private ComboBox myModuleComboBox;
89   private ComboBox myDirectoryComboBox;
90   private StateRestoringCheckBox myCbWithSubdirectories;
91   private JCheckBox myCbToOpenInNewTab;
92   private FindModel myModel;
93   private Consumer<FindModel> myOkHandler;
94   private FixedSizeButton mySelectDirectoryButton;
95   private StateRestoringCheckBox myUseFileFilter;
96   private ComboBox myFileFilter;
97   private JCheckBox myCbToSkipResultsWhenOneUsage;
98   private final Project myProject;
99   private final Map<EditorTextField, DocumentAdapter> myComboBoxListeners = new HashMap<EditorTextField, DocumentAdapter>();
100
101   private Action myFindAllAction;
102   private JRadioButton myRbCustomScope;
103   private ScopeChooserCombo myScopeCombo;
104   protected JLabel myReplacePrompt;
105
106   public FindDialog(@NotNull Project project, @NotNull FindModel model, @NotNull Consumer<FindModel> myOkHandler){
107     super(project, true);
108     myProject = project;
109     myModel = model;
110
111     this.myOkHandler = myOkHandler;
112
113     updateTitle();
114     setOKButtonText(FindBundle.message("find.button"));
115     init();
116     initByModel();
117     updateReplaceVisibility();
118   }
119
120   private void updateTitle() {
121     if (myModel.isReplaceState()){
122       if (myModel.isMultipleFiles()){
123         setTitle(FindBundle.message("find.replace.in.project.dialog.title"));
124       }
125       else{
126         setTitle(FindBundle.message("find.replace.text.dialog.title"));
127       }
128     }
129     else{
130       setButtonsMargin(null);
131       if (myModel.isMultipleFiles()){
132         setTitle(FindBundle.message("find.in.path.dialog.title"));
133       }
134       else{
135         setTitle(FindBundle.message("find.text.dialog.title"));
136       }
137     }
138   }
139
140   @Override
141   protected void dispose() {
142     for(Map.Entry<EditorTextField, DocumentAdapter> e: myComboBoxListeners.entrySet()) {
143       e.getKey().removeDocumentListener(e.getValue());
144     }
145     myComboBoxListeners.clear();
146     super.dispose();
147   }
148
149   @Override
150   public JComponent getPreferredFocusedComponent() {
151     return myInputComboBox;
152   }
153
154   @Override
155   protected String getDimensionServiceKey() {
156     return myModel.isReplaceState() ? "replaceTextDialog" : "findTextDialog";
157   }
158
159   @NotNull
160   @Override
161   protected Action[] createActions() {
162     if (!myModel.isMultipleFiles() && !myModel.isReplaceState() && myModel.isFindAllEnabled()) {
163       return new Action[] { getFindAllAction(), getOKAction(), getCancelAction(), getHelpAction() };
164     }
165     return new Action[] { getOKAction(), getCancelAction(), getHelpAction() };
166   }
167
168   @NotNull
169   private Action getFindAllAction() {
170     return myFindAllAction = new AbstractAction(FindBundle.message("find.all.button")) {
171       @Override
172       public void actionPerformed(ActionEvent e) {
173         doOKAction(true);
174       }
175     };
176   }
177
178   @Override
179   public JComponent createNorthPanel() {
180     JPanel panel = new JPanel(new GridBagLayout());
181
182     JLabel prompt = new JLabel(FindBundle.message("find.text.to.find.label"));
183     panel.add(prompt, new GridBagConstraints(0,0,1,1,0,1, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0,0,UIUtil.DEFAULT_VGAP,UIUtil.DEFAULT_HGAP), 0,0));
184
185     myInputComboBox = new ComboBox(300);
186     revealWhitespaces(myInputComboBox);
187     initCombobox(myInputComboBox);
188
189     myReplaceComboBox = new ComboBox(300);
190     revealWhitespaces(myReplaceComboBox);
191
192     initCombobox(myReplaceComboBox);
193     final Component editorComponent = myReplaceComboBox.getEditor().getEditorComponent();
194     editorComponent.addFocusListener(
195       new FocusAdapter() {
196         @Override
197         public void focusGained(FocusEvent e) {
198           myReplaceComboBox.getEditor().selectAll();
199           editorComponent.removeFocusListener(this);
200         }
201       }
202     );
203
204
205     panel.add(myInputComboBox, new GridBagConstraints(1,0,1,1,1,1, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0,0,UIUtil.DEFAULT_VGAP,0), 0,0));
206     prompt.setLabelFor(myInputComboBox.getEditor().getEditorComponent());
207
208     myReplacePrompt = new JLabel(FindBundle.message("find.replace.with.label"));
209     panel.add(myReplacePrompt, new GridBagConstraints(0,1,1,1,0,1, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0,0,UIUtil.DEFAULT_VGAP,UIUtil.DEFAULT_HGAP), 0,0));
210
211     panel.add(myReplaceComboBox, new GridBagConstraints(1, 1, 1, 1, 1, 1, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
212                                                         new Insets(0, 0, UIUtil.DEFAULT_VGAP, 0), 0, 0));
213     myReplacePrompt.setLabelFor(myReplaceComboBox.getEditor().getEditorComponent());
214     return panel;
215   }
216
217   private void updateReplaceVisibility() {
218     myReplacePrompt.setVisible(myModel.isReplaceState());
219     myReplaceComboBox.setVisible(myModel.isReplaceState());
220     if (myCbToSkipResultsWhenOneUsage != null) {
221       myCbToSkipResultsWhenOneUsage.setVisible(myModel.isReplaceState());
222     }
223     myCbPreserveCase.setVisible(myModel.isReplaceState());
224   }
225
226   private void revealWhitespaces(@NotNull ComboBox comboBox) {
227     ComboBoxEditor comboBoxEditor = new RevealingSpaceComboboxEditor(myProject, comboBox);
228     comboBox.setEditor(comboBoxEditor);
229     comboBox.setRenderer(new EditorComboBoxRenderer(comboBoxEditor));
230   }
231
232   private void initCombobox(@NotNull final ComboBox comboBox) {
233     comboBox.setEditable(true);
234     comboBox.setMaximumRowCount(8);
235
236     comboBox.addActionListener(new ActionListener() {
237       @Override
238       public void actionPerformed(ActionEvent e) {
239         validateFindButton();
240       }
241     });
242
243     Component editorComponent = comboBox.getEditor().getEditorComponent();
244
245     if (editorComponent instanceof EditorTextField) {
246       final EditorTextField etf = (EditorTextField) editorComponent;
247
248       DocumentAdapter listener = new DocumentAdapter() {
249         @Override
250         public void documentChanged(final DocumentEvent e) {
251           handleComboBoxValueChanged(comboBox);
252         }
253       };
254       etf.addDocumentListener(listener);
255       myComboBoxListeners.put(etf, listener);
256     }
257     else {
258       editorComponent.addKeyListener(
259         new KeyAdapter() {
260           @Override
261           public void keyReleased(KeyEvent e) {
262             handleComboBoxValueChanged(comboBox);
263           }
264         }
265       );
266     }
267   }
268
269   private void handleComboBoxValueChanged(@NotNull ComboBox comboBox) {
270     Object item = comboBox.getEditor().getItem();
271     if (item != null && !item.equals(comboBox.getSelectedItem())){
272       int caretPosition = getCaretPosition(comboBox);
273       comboBox.setSelectedItem(item);
274       setCaretPosition(comboBox, caretPosition);
275     }
276     validateFindButton();
277   }
278
279   @NotNull
280   public FindModel getModel() {
281     return myModel;
282   }
283
284   public void setOkHandler(@NotNull Consumer<FindModel> okHandler) {
285     myOkHandler = okHandler;
286   }
287
288   public void setModel(@NotNull FindModel model) {
289     myModel = model;
290     updateReplaceVisibility();
291     updateTitle();
292   }
293
294   private static int getCaretPosition(@NotNull JComboBox comboBox) {
295     Component editorComponent = comboBox.getEditor().getEditorComponent();
296     if (editorComponent instanceof JTextField){
297       JTextField textField = (JTextField)editorComponent;
298       return textField.getCaretPosition();
299     }
300     return 0;
301   }
302
303   private static void setCaretPosition(@NotNull JComboBox comboBox, int position) {
304     Component editorComponent = comboBox.getEditor().getEditorComponent();
305     if (editorComponent instanceof JTextField){
306       JTextField textField = (JTextField)editorComponent;
307       textField.setCaretPosition(position);
308     }
309   }
310
311   private void validateFindButton() {
312     boolean okStatus = canSearchThisString() ||
313                        myRbDirectory != null && myRbDirectory.isSelected() && StringUtil.isEmpty(getDirectory());
314     setOKStatus(okStatus);
315   }
316
317   private boolean canSearchThisString() {
318     return !StringUtil.isEmpty(getStringToFind()) || !myModel.isReplaceState() && !myModel.isFindAllEnabled() && getFileTypeMask() != null;
319   }
320
321   private void setOKStatus(boolean value) {
322     setOKActionEnabled(value);
323     if (myFindAllAction != null) {
324       myFindAllAction.setEnabled(value);
325     }
326   }
327
328   @Override
329   public JComponent createCenterPanel() {
330     JPanel optionsPanel = new JPanel();
331     optionsPanel.setLayout(new GridBagLayout());
332
333     GridBagConstraints gbConstraints = new GridBagConstraints();
334     gbConstraints.weightx = 1;
335     gbConstraints.weighty = 1;
336     gbConstraints.fill = GridBagConstraints.BOTH;
337     gbConstraints.gridwidth = GridBagConstraints.REMAINDER;
338
339     JPanel topOptionsPanel = new JPanel();
340     topOptionsPanel.setLayout(new GridLayout(1, 2, UIUtil.DEFAULT_HGAP, 0));
341     topOptionsPanel.add(createFindOptionsPanel());
342     optionsPanel.add(topOptionsPanel, gbConstraints);
343     if (myModel.isMultipleFiles()) {
344       optionsPanel.add(createGlobalScopePanel(), gbConstraints);
345       gbConstraints.weightx = 1;
346       gbConstraints.weighty = 1;
347       gbConstraints.fill = GridBagConstraints.HORIZONTAL;
348
349       gbConstraints.gridwidth = GridBagConstraints.REMAINDER;
350       optionsPanel.add(createFilterPanel(),gbConstraints);
351
352       myCbToSkipResultsWhenOneUsage = createCheckbox(FindSettings.getInstance().isSkipResultsWithOneUsage(), FindBundle.message("find.options.skip.results.tab.with.one.usage.checkbox"));
353       optionsPanel.add(myCbToSkipResultsWhenOneUsage, gbConstraints);
354       myCbToSkipResultsWhenOneUsage.setVisible(myModel.isReplaceState());
355     }
356     else {
357       if (FindManagerImpl.ourHasSearchInCommentsAndLiterals) {
358         JPanel leftOptionsPanel = new JPanel();
359         leftOptionsPanel.setLayout(new GridLayout(3, 1, 0, 4));
360
361         leftOptionsPanel.add(createDirectionPanel());
362         leftOptionsPanel.add(createOriginPanel());
363         leftOptionsPanel.add(createScopePanel());
364         topOptionsPanel.add(leftOptionsPanel);
365       } else {
366         topOptionsPanel.add(createDirectionPanel());
367         gbConstraints.gridwidth = GridBagConstraints.RELATIVE;
368         JPanel bottomOptionsPanel = new JPanel();
369         bottomOptionsPanel.setLayout(new GridLayout(1, 2, 8, 0));
370         optionsPanel.add(bottomOptionsPanel, gbConstraints);
371         bottomOptionsPanel.add(createScopePanel());
372         bottomOptionsPanel.add(createOriginPanel());
373       }
374     }
375
376     if (myModel.isOpenInNewTabVisible()){
377       JPanel openInNewTabWindowPanel = new JPanel(new BorderLayout());
378       myCbToOpenInNewTab = new JCheckBox(FindBundle.message("find.open.in.new.tab.checkbox"));
379       myCbToOpenInNewTab.setFocusable(false);
380       myCbToOpenInNewTab.setSelected(myModel.isOpenInNewTab());
381       myCbToOpenInNewTab.setEnabled(myModel.isOpenInNewTabEnabled());
382       openInNewTabWindowPanel.add(myCbToOpenInNewTab, BorderLayout.EAST);
383       optionsPanel.add(openInNewTabWindowPanel, gbConstraints);
384     }
385
386     return optionsPanel;
387   }
388
389   @NotNull
390   private JComponent createFilterPanel() {
391     JPanel filterPanel = new JPanel();
392     filterPanel.setLayout(new BorderLayout());
393     filterPanel.setBorder(IdeBorderFactory.createTitledBorder(FindBundle.message("find.filter.file.name.group"),
394                                                               true));
395
396     myFileFilter = new ComboBox(100);
397     initCombobox(myFileFilter);
398     filterPanel.add(myUseFileFilter = createCheckbox(FindBundle.message("find.filter.file.mask.checkbox")),BorderLayout.WEST);
399     filterPanel.add(myFileFilter,BorderLayout.CENTER);
400     initFileFilter(myFileFilter, myUseFileFilter);
401     myUseFileFilter.addActionListener(new ActionListener() {
402       @Override
403       public void actionPerformed(ActionEvent e) {
404         validateFindButton();
405       }
406     });
407     return filterPanel;
408   }
409
410   public static void initFileFilter(@NotNull final JComboBox fileFilter, @NotNull final JCheckBox useFileFilter) {
411     fileFilter.setEditable(true);
412     String[] fileMasks = FindSettings.getInstance().getRecentFileMasks();
413     for(int i=fileMasks.length-1; i >= 0; i--) {
414       fileFilter.addItem(fileMasks[i]);
415     }
416     fileFilter.setEnabled(false);
417
418     useFileFilter.addActionListener(
419       new ActionListener() {
420         @Override
421         public void actionPerformed(ActionEvent e) {
422           if (useFileFilter.isSelected()) {
423             fileFilter.setEnabled(true);
424             fileFilter.getEditor().selectAll();
425             fileFilter.getEditor().getEditorComponent().requestFocusInWindow();
426           }
427           else {
428             fileFilter.setEnabled(false);
429           }
430         }
431       }
432     );
433   }
434
435   @Override
436   public void doOKAction() {
437     doOKAction(false);
438   }
439
440   private void doOKAction(boolean findAll) {
441     FindModel validateModel = (FindModel)myModel.clone();
442     applyTo(validateModel, findAll);
443
444     ValidationInfo validationInfo = getValidationInfo(validateModel);
445
446     if (validationInfo == null) {
447       myModel.copyFrom(validateModel);
448       updateFindSettings();
449
450       super.doOKAction();
451       myOkHandler.consume(myModel);
452     }
453     else {
454       String message = validationInfo.message;
455       Messages.showMessageDialog(
456         myProject,
457         message,
458         CommonBundle.getErrorTitle(),
459         Messages.getErrorIcon()
460       );
461     }
462   }
463
464   private void updateFindSettings() {
465     FindSettings findSettings = FindSettings.getInstance();
466     findSettings.setCaseSensitive(myModel.isCaseSensitive());
467     if (myModel.isReplaceState()) {
468       findSettings.setPreserveCase(myModel.isPreserveCase());
469     }
470
471     findSettings.setWholeWordsOnly(myModel.isWholeWordsOnly());
472     findSettings.setInStringLiteralsOnly(myModel.isInStringLiteralsOnly());
473     findSettings.setInCommentsOnly(myModel.isInCommentsOnly());
474     findSettings.setExceptComments(myModel.isExceptComments());
475     findSettings.setExceptStringLiterals(myModel.isExceptStringLiterals());
476     findSettings.setExceptCommentsAndLiterals(myModel.isExceptCommentsAndStringLiterals());
477
478     findSettings.setRegularExpressions(myModel.isRegularExpressions());
479     if (!myModel.isMultipleFiles()){
480       findSettings.setForward(myModel.isForward());
481       findSettings.setFromCursor(myModel.isFromCursor());
482
483       findSettings.setGlobal(myModel.isGlobal());
484     } else{
485       String directoryName = myModel.getDirectoryName();
486       if (directoryName != null && !directoryName.isEmpty()) {
487         findSettings.setWithSubdirectories(myModel.isWithSubdirectories());
488       }
489       else if (myRbModule.isSelected()) {
490       }
491       else if (myRbCustomScope.isSelected()) {
492         SearchScope selectedScope = myScopeCombo.getSelectedScope();
493         String customScopeName = selectedScope == null ? null : selectedScope.getDisplayName();
494         findSettings.setCustomScope(customScopeName);
495       }
496     }
497
498     if (myCbToSkipResultsWhenOneUsage != null){
499       findSettings.setSkipResultsWithOneUsage(
500         isSkipResultsWhenOneUsage()
501       );
502     }
503
504     findSettings.setFileMask(myModel.getFileFilter());
505   }
506
507   @Override
508   protected boolean postponeValidation() {
509     return true;
510   }
511
512   @Nullable("null means OK")
513   private ValidationInfo getValidationInfo(@NotNull FindModel model) {
514     if (myRbDirectory != null && myRbDirectory.isEnabled() && myRbDirectory.isSelected()) {
515       PsiDirectory directory = FindInProjectUtil.getPsiDirectory(model, myProject);
516       if (directory == null) {
517         return new ValidationInfo(FindBundle.message("find.directory.not.found.error", getDirectory()), myDirectoryComboBox);
518       }
519     }
520
521     if (!canSearchThisString()) {
522       return new ValidationInfo("String to find is empty", myInputComboBox);
523     }
524
525     if (myCbRegularExpressions != null && myCbRegularExpressions.isSelected() && myCbRegularExpressions.isEnabled()) {
526       String toFind = getStringToFind();
527       try {
528         boolean isCaseSensitive = myCbCaseSensitive != null && myCbCaseSensitive.isSelected() && myCbCaseSensitive.isEnabled();
529         Pattern pattern =
530           Pattern.compile(toFind, isCaseSensitive ? Pattern.MULTILINE : Pattern.MULTILINE | Pattern.CASE_INSENSITIVE);
531         if (pattern.matcher("").matches() && !toFind.endsWith("$") && !toFind.startsWith("^")) {
532           return new ValidationInfo(FindBundle.message("find.empty.match.regular.expression.error"), myInputComboBox);
533         }
534       }
535       catch (PatternSyntaxException e) {
536         return new ValidationInfo(FindBundle.message("find.invalid.regular.expression.error", toFind, e.getDescription()), myInputComboBox);
537       }
538     }
539
540     final String mask = getFileTypeMask();
541
542     if (mask != null) {
543       if (mask.isEmpty()) {
544         return new ValidationInfo(FindBundle.message("find.filter.empty.file.mask.error"), myFileFilter);
545       }
546       else {
547         try {
548           FindInProjectUtil.createFileMaskRegExp(mask);   // verify that the regexp compiles
549         }
550         catch (PatternSyntaxException ex) {
551           return new ValidationInfo(FindBundle.message("find.filter.invalid.file.mask.error", mask), myFileFilter);
552         }
553       }
554     }
555     return null;
556   }
557
558   @Override
559   protected ValidationInfo doValidate() {
560     FindModel validateModel = (FindModel)myModel.clone();
561     applyTo(validateModel, false);
562
563     ValidationInfo result = getValidationInfo(validateModel);
564
565     setOKStatus(result == null);
566
567     return result;
568   }
569
570   @Override
571   public void doHelpAction() {
572     String id = myModel.isReplaceState()
573                 ? myModel.isMultipleFiles() ? HelpID.REPLACE_IN_PATH : HelpID.REPLACE_OPTIONS
574                 : myModel.isMultipleFiles() ? HelpID.FIND_IN_PATH : HelpID.FIND_OPTIONS;
575     HelpManager.getInstance().invokeHelp(id);
576   }
577
578   private boolean isSkipResultsWhenOneUsage() {
579     return myCbToSkipResultsWhenOneUsage!=null && myCbToSkipResultsWhenOneUsage.isSelected();
580   }
581
582   @NotNull
583   private JPanel createFindOptionsPanel() {
584     JPanel findOptionsPanel = new JPanel();
585     findOptionsPanel.setBorder(IdeBorderFactory.createTitledBorder(FindBundle.message("find.options.group"), true));
586     findOptionsPanel.setLayout(new BoxLayout(findOptionsPanel, BoxLayout.Y_AXIS));
587
588     myCbCaseSensitive = createCheckbox(FindBundle.message("find.options.case.sensitive"));
589     findOptionsPanel.add(myCbCaseSensitive);
590     myCbPreserveCase = createCheckbox(FindBundle.message("find.options.replace.preserve.case"));
591     findOptionsPanel.add(myCbPreserveCase);
592     myCbPreserveCase.setVisible(myModel.isReplaceState());
593     myCbWholeWordsOnly = createCheckbox(FindBundle.message("find.options.whole.words.only"));
594
595     findOptionsPanel.add(myCbWholeWordsOnly);
596
597     myCbRegularExpressions = createCheckbox(FindBundle.message("find.options.regular.expressions"));
598
599     final JPanel regExPanel = new JPanel();
600     regExPanel.setAlignmentX(Component.LEFT_ALIGNMENT);
601     regExPanel.setLayout(new BoxLayout(regExPanel, BoxLayout.X_AXIS));
602     regExPanel.add(myCbRegularExpressions);
603
604     regExPanel.add(RegExHelpPopup.createRegExLink("[Help]", regExPanel, LOG));
605
606     findOptionsPanel.add(regExPanel);
607
608     mySearchContext = new ComboBox(new Object[] {FindBundle.message("find.context.anywhere.scope.label", 200),
609       FindBundle.message("find.context.in.comments.scope.label"), FindBundle.message("find.context.in.literals.scope.label"),
610       FindBundle.message("find.context.except.comments.scope.label"),
611       FindBundle.message("find.context.except.literals.scope.label"),
612       FindBundle.message("find.context.except.comments.and.literals.scope.label")});
613     final JPanel searchContextPanel = new JPanel(new GridBagLayout());
614     searchContextPanel.setAlignmentX(Component.LEFT_ALIGNMENT);
615
616     GridBagConstraints gbConstraints = new GridBagConstraints();
617     gbConstraints.fill = GridBagConstraints.HORIZONTAL;
618     gbConstraints.anchor = GridBagConstraints.WEST;
619
620     gbConstraints.gridx = 0;
621     gbConstraints.gridy = 0;
622     gbConstraints.gridwidth = 1;
623     gbConstraints.weightx = 1;
624
625     JLabel searchContextLabel = new JLabel(FindBundle.message("find.context.combo.label"));
626     searchContextLabel.setLabelFor(mySearchContext);
627     searchContextPanel.add(searchContextLabel, gbConstraints);
628
629     ++gbConstraints.gridx;
630
631     searchContextPanel.add(mySearchContext, gbConstraints);
632
633     if (FindManagerImpl.ourHasSearchInCommentsAndLiterals) {
634       findOptionsPanel.add(searchContextPanel);
635     }
636
637     ActionListener actionListener = new ActionListener() {
638       @Override
639       public void actionPerformed(ActionEvent e) {
640         updateControls();
641       }
642     };
643     myCbRegularExpressions.addActionListener(actionListener);
644     myCbRegularExpressions.addItemListener(new ItemListener() {
645       @Override
646       public void itemStateChanged(final ItemEvent e) {
647         setupRegExpSetting();
648       }
649     });
650
651     myCbCaseSensitive.addActionListener(actionListener);
652     myCbPreserveCase.addActionListener(actionListener);
653
654     return findOptionsPanel;
655   }
656
657   private void setupRegExpSetting() {
658     updateFileTypeForEditorComponent(myInputComboBox);
659     if (myReplaceComboBox != null) updateFileTypeForEditorComponent(myReplaceComboBox);
660   }
661
662   private void updateFileTypeForEditorComponent(@NotNull ComboBox inputComboBox) {
663     final Component editorComponent = inputComboBox.getEditor().getEditorComponent();
664
665     if (editorComponent instanceof EditorTextField) {
666       boolean isRegexp = myCbRegularExpressions.isSelectedWhenSelectable();
667       FileType fileType = isRegexp ? RegExpFileType.INSTANCE : PlainTextFileType.INSTANCE;
668       String fileName = isRegexp ? "a.regexp" : "a.txt";
669       final PsiFile file = PsiFileFactory.getInstance(myProject).createFileFromText(fileName, fileType, ((EditorTextField)editorComponent).getText(), -1, true);
670
671       ((EditorTextField)editorComponent).setNewDocumentAndFileType(fileType, PsiDocumentManager.getInstance(myProject).getDocument(file));
672     }
673   }
674
675   private void updateControls() {
676     if (myCbRegularExpressions.isSelected()) {
677       myCbWholeWordsOnly.makeUnselectable(false);
678     }
679     else {
680       myCbWholeWordsOnly.makeSelectable();
681     }
682     if (myModel.isReplaceState()) {
683       if (myCbRegularExpressions.isSelected() || myCbCaseSensitive.isSelected()) {
684         myCbPreserveCase.makeUnselectable(false);
685       }
686       else {
687         myCbPreserveCase.makeSelectable();
688       }
689
690       if (myCbPreserveCase.isSelected()) {
691         myCbRegularExpressions.makeUnselectable(false);
692         myCbCaseSensitive.makeUnselectable(false);
693       }
694       else {
695         myCbRegularExpressions.makeSelectable();
696         myCbCaseSensitive.makeSelectable();
697       }
698     }
699
700     if (!myModel.isMultipleFiles()) {
701       myRbFromCursor.setEnabled(myRbGlobal.isSelected());
702       myRbEntireScope.setEnabled(myRbGlobal.isSelected());
703     }
704   }
705
706   @NotNull
707   private JPanel createDirectionPanel() {
708     JPanel directionPanel = new JPanel();
709     directionPanel.setBorder(IdeBorderFactory.createTitledBorder(FindBundle.message("find.direction.group"), true));
710     directionPanel.setLayout(new BoxLayout(directionPanel, BoxLayout.Y_AXIS));
711
712     myRbForward = new JRadioButton(FindBundle.message("find.direction.forward.radio"), true);
713     directionPanel.add(myRbForward);
714     myRbBackward = new JRadioButton(FindBundle.message("find.direction.backward.radio"));
715     directionPanel.add(myRbBackward);
716     ButtonGroup bgDirection = new ButtonGroup();
717     bgDirection.add(myRbForward);
718     bgDirection.add(myRbBackward);
719
720     return directionPanel;
721   }
722
723   @NotNull
724   private JComponent createGlobalScopePanel() {
725     JPanel scopePanel = new JPanel();
726     scopePanel.setLayout(new GridBagLayout());
727     scopePanel.setBorder(IdeBorderFactory.createTitledBorder(FindBundle.message("find.scope.group"), true));
728     GridBagConstraints gbConstraints = new GridBagConstraints();
729     gbConstraints.fill = GridBagConstraints.HORIZONTAL;
730     gbConstraints.anchor = GridBagConstraints.WEST;
731
732     gbConstraints.gridx = 0;
733     gbConstraints.gridy = 0;
734     gbConstraints.gridwidth = 3;
735     gbConstraints.weightx = 1;
736     final boolean canAttach = ProjectAttachProcessor.canAttachToProject();
737     myRbProject = new JRadioButton(canAttach
738                                    ? FindBundle.message("find.scope.all.projects.radio")
739                                    : FindBundle.message("find.scope.whole.project.radio"), true);
740     scopePanel.add(myRbProject, gbConstraints);
741
742     gbConstraints.gridx = 0;
743     gbConstraints.gridy++;
744     gbConstraints.weightx = 0;
745     gbConstraints.gridwidth = 1;
746     myRbModule = new JRadioButton(canAttach
747                                   ? FindBundle.message("find.scope.project.radio")
748                                   : FindBundle.message("find.scope.module.radio"), false);
749     scopePanel.add(myRbModule, gbConstraints);
750
751     gbConstraints.gridx = 1;
752     gbConstraints.gridwidth = 2;
753     gbConstraints.weightx = 1;
754     Module[] modules = ModuleManager.getInstance(myProject).getModules();
755     String[] names = new String[modules.length];
756     for (int i = 0; i < modules.length; i++) {
757       names[i] = modules[i].getName();
758     }
759
760     Arrays.sort(names,String.CASE_INSENSITIVE_ORDER);
761     myModuleComboBox = new ComboBox(names);
762     scopePanel.add(myModuleComboBox, gbConstraints);
763
764     if (modules.length == 1) {
765       myModuleComboBox.setVisible(false);
766       myRbModule.setVisible(false);
767     }
768
769     gbConstraints.gridx = 0;
770     gbConstraints.gridy++;
771     gbConstraints.weightx = 0;
772     gbConstraints.gridwidth = 1;
773     myRbDirectory = new JRadioButton(FindBundle.message("find.scope.directory.radio"), false);
774     scopePanel.add(myRbDirectory, gbConstraints);
775
776     gbConstraints.gridx = 1;
777     gbConstraints.weightx = 1;
778     myDirectoryComboBox = new ComboBox(200);
779     Component editorComponent = myDirectoryComboBox.getEditor().getEditorComponent();
780     if (editorComponent instanceof JTextField) {
781       JTextField field = (JTextField)editorComponent;
782       field.setColumns(40);
783     }
784     initCombobox(myDirectoryComboBox);
785     scopePanel.add(myDirectoryComboBox, gbConstraints);
786
787     gbConstraints.weightx = 0;
788     gbConstraints.gridx = 2;
789     mySelectDirectoryButton = new FixedSizeButton(myDirectoryComboBox);
790     TextFieldWithBrowseButton.MyDoClickAction.addTo(mySelectDirectoryButton, myDirectoryComboBox);
791     mySelectDirectoryButton.setMargin(new Insets(0, 0, 0, 0));
792     scopePanel.add(mySelectDirectoryButton, gbConstraints);
793
794     gbConstraints.gridx = 0;
795     gbConstraints.gridy++;
796     gbConstraints.weightx = 1;
797     gbConstraints.gridwidth = 3;
798     gbConstraints.insets = new Insets(0, 16, 0, 0);
799     myCbWithSubdirectories = createCheckbox(true, FindBundle.message("find.scope.directory.recursive.checkbox"));
800     myCbWithSubdirectories.setSelected(true);
801     scopePanel.add(myCbWithSubdirectories, gbConstraints);
802
803
804     gbConstraints.gridx = 0;
805     gbConstraints.gridy++;
806     gbConstraints.weightx = 0;
807     gbConstraints.gridwidth = 1;
808     gbConstraints.insets = new Insets(0, 0, 0, 0);
809     myRbCustomScope = new JRadioButton(FindBundle.message("find.scope.custom.radio"), false);
810     scopePanel.add(myRbCustomScope, gbConstraints);
811
812     gbConstraints.gridx++;
813     gbConstraints.weightx = 1;
814     gbConstraints.gridwidth = 2;
815     myScopeCombo = new ScopeChooserCombo(myProject, true, true, FindSettings.getInstance().getDefaultScopeName());
816     Disposer.register(myDisposable, myScopeCombo);
817     scopePanel.add(myScopeCombo, gbConstraints);
818
819
820     ButtonGroup bgScope = new ButtonGroup();
821     bgScope.add(myRbDirectory);
822     bgScope.add(myRbProject);
823     bgScope.add(myRbModule);
824     bgScope.add(myRbCustomScope);
825
826     ActionListener validateAll = new ActionListener() {
827       @Override
828       public void actionPerformed(ActionEvent e) {
829         validateScopeControls();
830         validateFindButton();
831       }
832     };
833     myRbProject.addActionListener(validateAll);
834     myRbCustomScope.addActionListener(validateAll);
835
836     myRbDirectory.addActionListener(new ActionListener() {
837       @Override
838       public void actionPerformed(ActionEvent e) {
839         validateScopeControls();
840         validateFindButton();
841         myDirectoryComboBox.getEditor().getEditorComponent().requestFocusInWindow();
842       }
843     });
844
845     myRbModule.addActionListener(new ActionListener() {
846       @Override
847       public void actionPerformed(ActionEvent e) {
848         validateScopeControls();
849         validateFindButton();
850         myModuleComboBox.requestFocusInWindow();
851       }
852     });
853
854     mySelectDirectoryButton.addActionListener(new ActionListener() {
855       @Override
856       public void actionPerformed(ActionEvent e) {
857         FileChooserDescriptor descriptor = FileChooserDescriptorFactory.createSingleFolderDescriptor();
858         FileChooser.chooseFiles(descriptor, myProject, null, new Consumer<List<VirtualFile>>() {
859           @Override
860           public void consume(final List<VirtualFile> files) {
861             myDirectoryComboBox.setSelectedItem(files.get(0).getPresentableUrl());
862           }
863         });
864       }
865     });
866
867     return scopePanel;
868   }
869
870   @NotNull
871   private static StateRestoringCheckBox createCheckbox(@NotNull String message) {
872     final StateRestoringCheckBox cb = new StateRestoringCheckBox(message);
873     cb.setFocusable(false);
874     return cb;
875   }
876
877   @NotNull
878   private static StateRestoringCheckBox createCheckbox(boolean selected, @NotNull String message) {
879     final StateRestoringCheckBox cb = new StateRestoringCheckBox(message, selected);
880     cb.setFocusable(false);
881     return cb;
882   }
883
884   private void validateScopeControls() {
885     if (myRbDirectory.isSelected()) {
886       myCbWithSubdirectories.makeSelectable();
887     }
888     else {
889       myCbWithSubdirectories.makeUnselectable(myCbWithSubdirectories.isSelected());
890     }
891     myDirectoryComboBox.setEnabled(myRbDirectory.isSelected());
892     mySelectDirectoryButton.setEnabled(myRbDirectory.isSelected());
893
894     myModuleComboBox.setEnabled(myRbModule.isSelected());
895     myScopeCombo.setEnabled(myRbCustomScope.isSelected());
896   }
897
898   @NotNull
899   private JPanel createScopePanel() {
900     JPanel scopePanel = new JPanel();
901     scopePanel.setBorder(IdeBorderFactory.createTitledBorder(FindBundle.message("find.scope.group"), true));
902     scopePanel.setLayout(new BoxLayout(scopePanel, BoxLayout.Y_AXIS));
903
904     myRbGlobal = new JRadioButton(FindBundle.message("find.scope.global.radio"), true);
905     scopePanel.add(myRbGlobal);
906     myRbSelectedText = new JRadioButton(FindBundle.message("find.scope.selected.text.radio"));
907     scopePanel.add(myRbSelectedText);
908     ButtonGroup bgScope = new ButtonGroup();
909     bgScope.add(myRbGlobal);
910     bgScope.add(myRbSelectedText);
911
912     ActionListener actionListener = new ActionListener() {
913       @Override
914       public void actionPerformed(ActionEvent e) {
915         updateControls();
916       }
917     };
918     myRbGlobal.addActionListener(actionListener);
919     myRbSelectedText.addActionListener(actionListener);
920
921     return scopePanel;
922   }
923
924   @NotNull
925   private JPanel createOriginPanel() {
926     JPanel originPanel = new JPanel();
927     originPanel.setBorder(IdeBorderFactory.createTitledBorder(FindBundle.message("find.origin.group"), true));
928     originPanel.setLayout(new BoxLayout(originPanel, BoxLayout.Y_AXIS));
929
930     myRbFromCursor = new JRadioButton(FindBundle.message("find.origin.from.cursor.radio"), true);
931     originPanel.add(myRbFromCursor);
932     myRbEntireScope = new JRadioButton(FindBundle.message("find.origin.entire.scope.radio"));
933     originPanel.add(myRbEntireScope);
934     ButtonGroup bgOrigin = new ButtonGroup();
935     bgOrigin.add(myRbFromCursor);
936     bgOrigin.add(myRbEntireScope);
937
938     return originPanel;
939   }
940
941   @NotNull
942   private String getStringToFind() {
943     String string = (String)myInputComboBox.getEditor().getItem();
944     return string == null ? "" : string;
945   }
946
947   @NotNull
948   private String getStringToReplace() {
949     String item = (String)myReplaceComboBox.getEditor().getItem();
950     return item == null ? "" : item;
951   }
952
953   private String getDirectory() {
954     return (String)myDirectoryComboBox.getSelectedItem();
955   }
956
957   private static void setStringsToComboBox(@NotNull String[] strings, @NotNull ComboBox combo, String selected) {
958     if (combo.getItemCount() > 0){
959       combo.removeAllItems();
960     }
961     if (selected != null && selected.indexOf('\n') < 0) {
962       strings = ArrayUtil.remove(strings, selected);
963       // this ensures that last searched string will be selected if selected == ""
964       if (!selected.isEmpty()) strings = ArrayUtil.append(strings, selected);
965     }
966     for(int i = strings.length - 1; i >= 0; i--){
967       combo.addItem(strings[i]);
968     }
969   }
970
971   private void setDirectories(@NotNull List<String> strings, String directoryName) {
972     if (myDirectoryComboBox.getItemCount() > 0){
973       myReplaceComboBox.removeAllItems();
974     }
975     if (directoryName != null && !directoryName.isEmpty()){
976       if (strings.contains(directoryName)){
977         strings.remove(directoryName);
978       }
979       myDirectoryComboBox.addItem(directoryName);
980     }
981     for(int i = strings.size() - 1; i >= 0; i--){
982       myDirectoryComboBox.addItem(strings.get(i));
983     }
984     if (myDirectoryComboBox.getItemCount() == 0){
985       myDirectoryComboBox.addItem("");
986     }
987   }
988
989
990
991   private void applyTo(@NotNull FindModel model, boolean findAll) {
992     model.setCaseSensitive(myCbCaseSensitive.isSelected());
993
994     if (model.isReplaceState()) {
995       model.setPreserveCase(myCbPreserveCase.isSelected());
996     }
997
998     model.setWholeWordsOnly(myCbWholeWordsOnly.isSelected());
999
1000     String selectedSearchContextInUi = (String)mySearchContext.getSelectedItem();
1001     FindModel.SearchContext searchContext = FindModel.SearchContext.ANY;
1002     if (FindBundle.message("find.context.in.literals.scope.label").equals(selectedSearchContextInUi)) {
1003       searchContext = FindModel.SearchContext.IN_STRING_LITERALS;
1004     }
1005     else if (FindBundle.message("find.context.in.comments.scope.label").equals(selectedSearchContextInUi)) {
1006       searchContext = FindModel.SearchContext.IN_COMMENTS;
1007     }
1008     else if (FindBundle.message("find.context.except.comments.scope.label").equals(selectedSearchContextInUi)) {
1009       searchContext = FindModel.SearchContext.EXCEPT_COMMENTS;
1010     }
1011     else if (FindBundle.message("find.context.except.literals.scope.label").equals(selectedSearchContextInUi)) {
1012       searchContext = FindModel.SearchContext.EXCEPT_STRING_LITERALS;
1013     } else if (FindBundle.message("find.context.except.comments.and.literals.scope.label").equals(selectedSearchContextInUi)) {
1014       searchContext = FindModel.SearchContext.EXCEPT_COMMENTS_AND_STRING_LITERALS;
1015     }
1016
1017     model.setSearchContext(searchContext);
1018
1019     model.setRegularExpressions(myCbRegularExpressions.isSelected());
1020     String stringToFind = getStringToFind();
1021     model.setStringToFind(stringToFind);
1022
1023     if (model.isReplaceState()){
1024       model.setPromptOnReplace(true);
1025       model.setReplaceAll(false);
1026       String stringToReplace = getStringToReplace();
1027       model.setStringToReplace(StringUtil.convertLineSeparators(stringToReplace));
1028     }
1029
1030     if (!model.isMultipleFiles()){
1031       model.setForward(myRbForward.isSelected());
1032       model.setFromCursor(myRbFromCursor.isSelected());
1033       model.setGlobal(myRbGlobal.isSelected());
1034     }
1035     else{
1036       if (myCbToOpenInNewTab != null){
1037         model.setOpenInNewTab(myCbToOpenInNewTab.isSelected());
1038       }
1039
1040       model.setProjectScope(myRbProject.isSelected());
1041       model.setDirectoryName(null);
1042       model.setModuleName(null);
1043       model.setCustomScopeName(null);
1044       model.setCustomScope(null);
1045       model.setCustomScope(false);
1046
1047       if (myRbDirectory.isSelected()) {
1048         String directory = getDirectory();
1049         model.setDirectoryName(directory == null ? "" : directory);
1050         model.setWithSubdirectories(myCbWithSubdirectories.isSelected());
1051       }
1052       else if (myRbModule.isSelected()) {
1053         model.setModuleName((String)myModuleComboBox.getSelectedItem());
1054       }
1055       else if (myRbCustomScope.isSelected()) {
1056         SearchScope selectedScope = myScopeCombo.getSelectedScope();
1057         String customScopeName = selectedScope == null ? null : selectedScope.getDisplayName();
1058         model.setCustomScopeName(customScopeName);
1059         model.setCustomScope(selectedScope == null ? null : selectedScope);
1060         model.setCustomScope(true);
1061       }
1062     }
1063
1064     model.setFindAll(findAll);
1065
1066     String mask = getFileTypeMask();
1067     model.setFileFilter(mask);
1068   }
1069
1070   @Nullable
1071   private String getFileTypeMask() {
1072     String mask = null;
1073     if (myUseFileFilter !=null && myUseFileFilter.isSelected()) {
1074       mask = (String)myFileFilter.getSelectedItem();
1075     }
1076     return mask;
1077   }
1078
1079
1080   private void initByModel() {
1081     myCbCaseSensitive.setSelected(myModel.isCaseSensitive());
1082     myCbWholeWordsOnly.setSelected(myModel.isWholeWordsOnly());
1083     String searchContext = FindBundle.message("find.context.anywhere.scope.label");
1084     if (myModel.isInCommentsOnly()) searchContext = FindBundle.message("find.context.in.comments.scope.label");
1085     else if (myModel.isInStringLiteralsOnly()) searchContext = FindBundle.message("find.context.in.literals.scope.label");
1086     else if (myModel.isExceptStringLiterals()) searchContext = FindBundle.message("find.context.except.literals.scope.label");
1087     else if (myModel.isExceptComments()) searchContext = FindBundle.message("find.context.except.comments.scope.label");
1088     else if (myModel.isExceptCommentsAndStringLiterals()) searchContext = FindBundle.message("find.context.except.comments.and.literals.scope.label");
1089     mySearchContext.setSelectedItem(searchContext);
1090
1091     myCbRegularExpressions.setSelected(myModel.isRegularExpressions());
1092
1093     if (myModel.isMultipleFiles()) {
1094       final String dirName = myModel.getDirectoryName();
1095       setDirectories(FindSettings.getInstance().getRecentDirectories(), dirName);
1096
1097       if (!StringUtil.isEmptyOrSpaces(dirName)) {
1098         VirtualFile dir = LocalFileSystem.getInstance().findFileByPath(dirName);
1099         if (dir != null) {
1100           Module module = ModuleUtilCore.findModuleForFile(dir, myProject);
1101           if (module != null) {
1102             myModuleComboBox.setSelectedItem(module.getName());
1103           }
1104         }
1105       }
1106       if (myModel.isCustomScope()) {
1107         myRbCustomScope.setSelected(true);
1108
1109         myScopeCombo.setEnabled(true);
1110
1111         myCbWithSubdirectories.setEnabled(false);
1112         myDirectoryComboBox.setEnabled(false);
1113         mySelectDirectoryButton.setEnabled(false);
1114         myModuleComboBox.setEnabled(false);
1115       } else if (myModel.isProjectScope()) {
1116         myRbProject.setSelected(true);
1117
1118         myCbWithSubdirectories.setEnabled(false);
1119         myDirectoryComboBox.setEnabled(false);
1120         mySelectDirectoryButton.setEnabled(false);
1121         myModuleComboBox.setEnabled(false);
1122         myScopeCombo.setEnabled(false);
1123       }
1124       else if (dirName != null) {
1125         myRbDirectory.setSelected(true);
1126         myCbWithSubdirectories.setEnabled(true);
1127         myDirectoryComboBox.setEnabled(true);
1128         mySelectDirectoryButton.setEnabled(true);
1129         myModuleComboBox.setEnabled(false);
1130         myScopeCombo.setEnabled(false);
1131       }
1132       else if (myModel.getModuleName() != null) {
1133         myRbModule.setSelected(true);
1134
1135         myCbWithSubdirectories.setEnabled(false);
1136         myDirectoryComboBox.setEnabled(false);
1137         mySelectDirectoryButton.setEnabled(false);
1138         myModuleComboBox.setEnabled(true);
1139         myModuleComboBox.setSelectedItem(myModel.getModuleName());
1140         myScopeCombo.setEnabled(false);
1141
1142         // force showing even if we have only one module
1143         myRbModule.setVisible(true);
1144         myModuleComboBox.setVisible(true);
1145       }
1146       else {
1147         assert false : myModel;
1148       }
1149
1150       myCbWithSubdirectories.setSelected(myModel.isWithSubdirectories());
1151
1152       if (myModel.getFileFilter()!=null && !myModel.getFileFilter().isEmpty()) {
1153         myFileFilter.setSelectedItem(myModel.getFileFilter());
1154         myFileFilter.setEnabled(true);
1155         myUseFileFilter.setSelected(true);
1156       }
1157     }
1158     else {
1159       if (myModel.isForward()){
1160         myRbForward.setSelected(true);
1161       }
1162       else{
1163         myRbBackward.setSelected(true);
1164       }
1165
1166       if (myModel.isFromCursor()){
1167         myRbFromCursor.setSelected(true);
1168       }
1169       else{
1170         myRbEntireScope.setSelected(true);
1171       }
1172
1173       if (myModel.isGlobal()){
1174         myRbGlobal.setSelected(true);
1175       }
1176       else{
1177         myRbSelectedText.setSelected(true);
1178       }
1179     }
1180
1181     setStringsToComboBox(FindSettings.getInstance().getRecentFindStrings(), myInputComboBox, myModel.getStringToFind());
1182     if (myModel.isReplaceState()){
1183       myCbPreserveCase.setSelected(myModel.isPreserveCase());
1184       setStringsToComboBox(FindSettings.getInstance().getRecentReplaceStrings(), myReplaceComboBox, myModel.getStringToReplace());
1185     }
1186     updateControls();
1187   }
1188 }
1189