Example #1
0
  private void updateUIWithFindModel() {
    boolean needToResetSearchFocus = mySearchTextComponent.hasFocus();
    boolean needToResetReplaceFocus = myReplaceTextComponent.hasFocus();
    updateSearchComponent();
    updateReplaceComponent();
    if (myFindModel.isReplaceState()) {
      if (myReplaceFieldWrapper.getParent() == null) {
        myLeftPanel.add(myReplaceFieldWrapper, BorderLayout.CENTER);
      }
      if (myReplaceToolbarWrapper.getParent() == null) {
        myRightPanel.add(myReplaceToolbarWrapper, BorderLayout.CENTER);
      }
      if (needToResetReplaceFocus) {
        myReplaceTextComponent.requestFocusInWindow();
      }
    } else {
      if (myReplaceFieldWrapper.getParent() != null) {
        myLeftPanel.remove(myReplaceFieldWrapper);
      }
      if (myReplaceToolbarWrapper.getParent() != null) {
        myRightPanel.remove(myReplaceToolbarWrapper);
      }
    }
    if (needToResetSearchFocus) mySearchTextComponent.requestFocusInWindow();
    mySearchActionsToolbar1.updateActionsImmediately();
    mySearchActionsToolbar2.updateActionsImmediately();
    myReplaceActionsToolbar1.updateActionsImmediately();
    myReplaceActionsToolbar2.updateActionsImmediately();
    myReplaceToolbarWrapper.revalidate();
    revalidate();
    repaint();

    myLivePreviewController.setTrackingSelection(!myFindModel.isGlobal());
  }
Example #2
0
  private void updateResults(final boolean allowedToChangedEditorSelection) {
    final String text = myFindModel.getStringToFind();
    if (text.length() == 0) {
      nothingToSearchFor();
    } else {

      if (myFindModel.isRegularExpressions()) {
        try {
          Pattern.compile(text);
        } catch (Exception e) {
          setNotFoundBackground();
          myClickToHighlightLabel.setVisible(false);
          mySearchResults.clear();
          myMatchInfoLabel.setText("Incorrect regular expression");
          return;
        }
      }

      final FindManager findManager = FindManager.getInstance(myProject);
      if (allowedToChangedEditorSelection) {
        findManager.setFindWasPerformed();
        FindModel copy = new FindModel();
        copy.copyFrom(myFindModel);
        copy.setReplaceState(false);
        findManager.setFindNextModel(copy);
      }
      if (myLivePreviewController != null) {
        myLivePreviewController.updateInBackground(myFindModel, allowedToChangedEditorSelection);
      }
    }
  }
Example #3
0
 private static FindModel createDefaultFindModel(Project project, Editor editor) {
   FindModel findModel = new FindModel();
   findModel.copyFrom(FindManager.getInstance(project).getFindInFileModel());
   if (editor.getSelectionModel().hasSelection()) {
     String selectedText = editor.getSelectionModel().getSelectedText();
     if (selectedText != null) {
       findModel.setStringToFind(selectedText);
     }
   }
   findModel.setPromptOnReplace(false);
   return findModel;
 }
Example #4
0
 private void replaceFieldDocumentChanged() {
   setMatchesLimit(LivePreviewController.MATCHES_LIMIT);
   myFindModel.setStringToReplace(myReplaceTextComponent.getText());
   if (myReplaceTextComponent instanceof JTextArea) {
     adjustRows((JTextArea) myReplaceTextComponent);
   }
   updateMultiLineStateIfNeed();
 }
Example #5
0
  private void updateReplaceComponent() {
    final int oldCaretPosition =
        myReplaceTextComponent != null ? myReplaceTextComponent.getCaretPosition() : 0;
    String oldText =
        myReplaceTextComponent != null
            ? myReplaceTextComponent.getText()
            : myFindModel.getStringToReplace();

    if (!updateTextComponent(false)) {
      return;
    }
    if (oldText != null) {
      myReplaceTextComponent.setText(oldText);
    }

    myReplaceTextComponent
        .getDocument()
        .addDocumentListener(
            new DocumentAdapter() {
              @Override
              protected void textChanged(DocumentEvent e) {
                ApplicationManager.getApplication()
                    .invokeLater(
                        new Runnable() {
                          @Override
                          public void run() {
                            replaceFieldDocumentChanged();
                          }
                        });
              }
            });

    if (!getFindModel().isMultiline()) {
      new ReplaceOnEnterAction(this, myReplaceTextComponent);
    }

    // myReplaceTextComponent.setText(myFindModel.getStringToReplace());
    ApplicationManager.getApplication()
        .invokeLater(
            new Runnable() {
              @Override
              public void run() {
                myReplaceTextComponent.setCaretPosition(oldCaretPosition);
              }
            });

    new VariantsCompletionAction(myReplaceTextComponent);
    new NextOccurrenceAction(this, myReplaceFieldWrapper);
    new PrevOccurrenceAction(this, myReplaceFieldWrapper);
    myReplaceFieldWrapper.revalidate();
    myReplaceFieldWrapper.repaint();
  }
Example #6
0
 private void searchFieldDocumentChanged() {
   setMatchesLimit(LivePreviewController.MATCHES_LIMIT);
   String text = mySearchTextComponent.getText();
   myFindModel.setStringToFind(text);
   if (!StringUtil.isEmpty(text)) {
     updateResults(true);
   } else {
     nothingToSearchFor();
   }
   if (mySearchTextComponent instanceof JTextArea) {
     adjustRows((JTextArea) mySearchTextComponent);
   }
   updateMultiLineStateIfNeed();
 }
Example #7
0
  public EditorSearchComponent(
      @NotNull final Editor editor, final Project project, FindModel findModel) {
    myFindModel = findModel;

    myProject = project;
    myEditor = editor;

    mySearchResults = new SearchResults(myEditor, myProject);
    myLivePreviewController = new LivePreviewController(mySearchResults, this);

    myDefaultBackground = new JTextField().getBackground();
    initUI();

    new SwitchToFind(this);
    new SwitchToReplace(this, editor);

    myFindModel.addObserver(
        new FindModel.FindModelObserver() {
          @Override
          public void findModelChanged(FindModel findModel) {
            String stringToFind = myFindModel.getStringToFind();
            if (!wholeWordsApplicable(stringToFind)) {
              myFindModel.setWholeWordsOnly(false);
            }
            updateUIWithFindModel();
            updateResults(true);
            syncFindModels(FindManager.getInstance(myProject).getFindInFileModel(), myFindModel);
          }
        });

    updateUIWithFindModel();

    if (ApplicationManager.getApplication().isUnitTestMode()) {
      initLivePreview();
    }
    updateMultiLineStateIfNeed();
  }
Example #8
0
 public void setTextInField(final String text) {
   mySearchTextComponent.setText(text);
   myFindModel.setStringToFind(text);
 }
Example #9
0
  private boolean updateTextComponent(final boolean search) {
    JTextComponent oldComponent = search ? mySearchTextComponent : myReplaceTextComponent;
    Color oldBackground = oldComponent != null ? oldComponent.getBackground() : null;
    Wrapper wrapper = search ? mySearchFieldWrapper : myReplaceFieldWrapper;
    boolean multiline = myFindModel.isMultiline();
    if (multiline && oldComponent instanceof JTextArea) return false;
    if (!multiline && oldComponent instanceof JTextField) return false;

    final JTextComponent textComponent;
    if (multiline) {
      textComponent = new JTextArea();
      ((JTextArea) textComponent).setColumns(25);
      ((JTextArea) textComponent).setRows(2);
      wrapper.setContent(
          new SearchWrapper(textComponent, new ShowHistoryAction(textComponent, this)));
    } else {
      SearchTextField searchTextField = new SearchTextField(true);
      searchTextField.setOpaque(false);
      textComponent = searchTextField.getTextEditor();
      searchTextField.getTextEditor().setColumns(25);
      if (UIUtil.isUnderGTKLookAndFeel()) {
        textComponent.setOpaque(false);
      }
      setupHistoryToSearchField(
          searchTextField,
          search
              ? FindSettings.getInstance().getRecentFindStrings()
              : FindSettings.getInstance().getRecentReplaceStrings());
      textComponent.registerKeyboardAction(
          new ActionListener() {
            @Override
            public void actionPerformed(final ActionEvent e) {
              final String text = textComponent.getText();
              myFindModel.setMultiline(true);
              ApplicationManager.getApplication()
                  .invokeLater(
                      new Runnable() {
                        @Override
                        public void run() {
                          if (search) {
                            mySearchTextComponent.setText(text + "\n");
                          } else {
                            myReplaceTextComponent.setText(text + "\n");
                          }
                        }
                      });
            }
          },
          KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, InputEvent.ALT_DOWN_MASK),
          JComponent.WHEN_FOCUSED);
      wrapper.setContent(searchTextField);
    }

    if (search) {
      mySearchTextComponent = textComponent;
    } else {
      myReplaceTextComponent = textComponent;
    }

    UIUtil.addUndoRedoActions(textComponent);
    Utils.setSmallerFont(textComponent);

    textComponent.putClientProperty("AuxEditorComponent", Boolean.TRUE);
    if (oldBackground != null) {
      textComponent.setBackground(oldBackground);
    }
    textComponent.addFocusListener(
        new FocusListener() {
          @Override
          public void focusGained(final FocusEvent e) {
            textComponent.repaint();
          }

          @Override
          public void focusLost(final FocusEvent e) {
            textComponent.repaint();
          }
        });
    new CloseOnESCAction(this, textComponent);
    return true;
  }
Example #10
0
 private static void syncFindModels(FindModel to, FindModel from) {
   to.setCaseSensitive(from.isCaseSensitive());
   to.setWholeWordsOnly(from.isWholeWordsOnly());
   to.setRegularExpressions(from.isRegularExpressions());
   to.setSearchContext(from.getSearchContext());
   if (from.isReplaceState()) {
     to.setPreserveCase(from.isPreserveCase());
   }
 }
Example #11
0
 private void updateMultiLineStateIfNeed() {
   myFindModel.setMultiline(
       mySearchTextComponent.getText().contains("\n")
           || myReplaceTextComponent.getText().contains("\n"));
 }
Example #12
0
  private void updateSearchComponent() {
    final int oldCaretPosition =
        mySearchTextComponent != null ? mySearchTextComponent.getCaretPosition() : 0;
    String oldText =
        mySearchTextComponent != null
            ? mySearchTextComponent.getText()
            : myFindModel.getStringToFind();

    if (!updateTextComponent(true)) {
      return;
    }

    if (oldText != null) {
      mySearchTextComponent.setText(oldText);
    }
    mySearchTextComponent
        .getDocument()
        .addDocumentListener(
            new DocumentAdapter() {
              @Override
              protected void textChanged(DocumentEvent e) {
                ApplicationManager.getApplication()
                    .invokeLater(
                        new Runnable() {
                          @Override
                          public void run() {
                            ApplicationManager.getApplication()
                                .invokeLater(
                                    new Runnable() {
                                      @Override
                                      public void run() {
                                        searchFieldDocumentChanged();
                                      }
                                    });
                          }
                        });
              }
            });

    mySearchTextComponent.registerKeyboardAction(
        new ActionListener() {
          @Override
          public void actionPerformed(final ActionEvent e) {
            if (StringUtil.isEmpty(mySearchTextComponent.getText())) {
              close();
            } else {
              requestFocus(myEditor.getContentComponent());
              addTextToRecent(EditorSearchComponent.this.mySearchTextComponent);
            }
          }
        },
        KeyStroke.getKeyStroke(
            KeyEvent.VK_ENTER,
            SystemInfo.isMac ? InputEvent.META_DOWN_MASK : InputEvent.CTRL_DOWN_MASK),
        JComponent.WHEN_FOCUSED);

    ApplicationManager.getApplication()
        .invokeLater(
            new Runnable() {
              @Override
              public void run() {
                mySearchTextComponent.setCaretPosition(oldCaretPosition);
              }
            });

    new RestorePreviousSettingsAction(this, mySearchTextComponent);
    new VariantsCompletionAction(
        mySearchTextComponent); // It registers a shortcut set automatically on construction
  }