Пример #1
0
  private void search(int direction) {
    String searchText = searchField.getText();
    if (searchText == null || searchText.length() == 0 || rTextArea.getText() == null) {
      return;
    }

    boolean forward = direction >= 0;
    boolean matchCase = matchCaseCB.isSelected();
    boolean regex = regexCB.isSelected();
    boolean wholeWord = wholeWordCB.isSelected();

    if (markAllCB.isSelected()) {
      rTextArea.markAll(searchText, matchCase, wholeWord, regex);
    } else {
      rTextArea.clearMarkAllHighlights();
    }

    SearchContext context = new SearchContext();
    context.setSearchFor(searchText);
    context.setMatchCase(matchCase);
    context.setRegularExpression(regex);
    context.setSearchForward(forward);
    context.setWholeWord(wholeWord);

    // TODO hack: move cursor before previous search for not jump to next occurrence
    if (direction == 0 && !COLOR_BG_ERROR.equals(searchField.getBackground())) {
      try {
        int caretPos = rTextArea.getCaretPosition();
        int lineNum = rTextArea.getLineOfOffset(caretPos) - 1;
        if (lineNum > 1) {
          rTextArea.setCaretPosition(rTextArea.getLineStartOffset(lineNum));
        }
      } catch (BadLocationException e) {
        LOG.error("Caret move error", e);
      }
    }

    boolean found = SearchEngine.find(rTextArea, context);
    if (!found) {
      int pos =
          SearchEngine.getNextMatchPos(
              searchText, rTextArea.getText(), forward, matchCase, wholeWord);
      if (pos != -1) {
        rTextArea.setCaretPosition(forward ? 0 : rTextArea.getDocument().getLength() - 1);
        search(direction);
        searchField.setBackground(COLOR_BG_WARN);
        return;
      }
      searchField.setBackground(COLOR_BG_ERROR);
    } else {
      searchField.setBackground(COLOR_BG_NORMAL);
    }
  }
Пример #2
0
  @Override
  public void actionPerformed(ActionEvent e) {
    String command = e.getActionCommand();
    if ("FindNext".equals(command)) {
      String text = findTF.getText();
      if (text.length() == 0) {
        return;
      }
      boolean markAll = markAllCB.isSelected();
      boolean forward = downCB.isSelected();
      boolean matchCase = matchCaseCB.isSelected();
      boolean wholeWord = wholeWrdsCB.isSelected();
      boolean regex = regexCB.isSelected();
      if (markAll) {
        rSyntaxTextArea.clearMarkAllHighlights();
        rSyntaxTextArea.markAll(text, matchCase, wholeWord, regex);
      }

      SearchContext c = new SearchContext();
      c.setMatchCase(matchCase);
      c.setSearchFor(text);
      c.setWholeWord(wholeWord);
      c.setRegularExpression(regex);
      c.setSearchForward(forward);
      boolean found = SearchEngine.find(rSyntaxTextArea, c);
      if (!found) {
        JOptionPane.showMessageDialog(
            this, I18N.getString("orbisgis.org.orbisgis.ui.findReplace.textNotFound"));
      }
    } else if ("Close".equals(command)) {
      setVisible(false);
    } else if ("Replace".equals(command)) {
      String text = findTF.getText();
      if (text.length() == 0) {
        return;
      }
      String textReplace = replaceTF.getText();
      if (textReplace.equals(text)) {
        return;
      } else {
        boolean forward = downCB.isSelected();
        boolean matchCase = matchCaseCB.isSelected();
        boolean wholeWord = wholeWrdsCB.isSelected();
        boolean regex = regexCB.isSelected();
        SearchContext c = new SearchContext();
        c.setMatchCase(matchCase);
        c.setSearchFor(text);
        c.setReplaceWith(textReplace);
        c.setWholeWord(wholeWord);
        c.setRegularExpression(regex);
        c.setSearchForward(forward);
        boolean found = SearchEngine.find(rSyntaxTextArea, c);
        if (!found) {
          JOptionPane.showMessageDialog(
              this, I18N.getString("orbisgis.org.orbisgis.ui.findReplace.textNotFound"));
        }
      }

    } else if ("ReplaceAll".equals(command)) {
      String text = findTF.getText();
      if (text.length() == 0) {
        return;
      }
      String textReplace = replaceTF.getText();
      if (textReplace.equals(text)) {
        return;
      } else {
        boolean matchCase = matchCaseCB.isSelected();
        boolean wholeWord = wholeWrdsCB.isSelected();
        boolean regex = regexCB.isSelected();
        SearchContext c = new SearchContext();
        c.setMatchCase(matchCase);
        c.setSearchFor(text);
        c.setWholeWord(wholeWord);
        c.setRegularExpression(regex);
        c.setReplaceWith(textReplace);
        SearchEngine.find(rSyntaxTextArea, c);
      }
    }
  }