Example #1
0
  @Override
  public void actionPerformed(ActionEvent e) {
    // "FindNext" => search forward, "FindPrev" => search backward
    String command = e.getActionCommand();
    if (command != null && command.toLowerCase().equals("tab closing")) {
      closeTextPanel();
    } else {
      boolean forward = "FindNext".equals(command);

      // Create an object defining our search parameters.
      SearchContext context = new SearchContext();
      String text = searchField.getText();
      if (text.length() == 0) {
        return;
      }
      context.setSearchFor(text);
      context.setMatchCase(matchCaseCB.isSelected());
      context.setRegularExpression(regexCB.isSelected());
      context.setSearchForward(forward);
      context.setWholeWord(false);

      SearchResult found = SearchEngine.find(rsTextArea, context);
      if (!found.wasFound()) {
        rsTextArea.setCaretPosition(forward ? 0 : rsTextArea.getDocument().getLength() - 1);
        found = SearchEngine.find(rsTextArea, context);
        if (!found.wasFound()) {
          JOptionPane.showMessageDialog(this, "Text not found");
        }
      }
    }
  }
Example #2
0
  public void actionPerformed(ActionEvent e) {

    String command = e.getActionCommand();

    if ("FindNext".equals(command)) {
      String text = searchField.getText();
      if (text.length() == 0) {
        return;
      }
      boolean forward = true;
      boolean matchCase = matchCaseCB.isSelected();
      boolean wholeWord = false;
      boolean regex = regexCB.isSelected();
      boolean found = SearchEngine.find(xmlViewTA, text, forward, matchCase, wholeWord, regex);
      if (!found) {
        JOptionPane.showMessageDialog(this, "Text not found");
      }
    } else if ("FindPrev".equals(command)) {
      String text = searchField.getText();
      if (text.length() == 0) {
        return;
      }
      boolean forward = false;
      boolean matchCase = matchCaseCB.isSelected();
      boolean wholeWord = false;
      boolean regex = regexCB.isSelected();
      boolean found = SearchEngine.find(xmlViewTA, text, forward, matchCase, wholeWord, regex);
      if (!found) {
        JOptionPane.showMessageDialog(this, "Text not found");
      }
    }
  }
Example #3
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);
    }
  }
Example #4
0
  /**
   * Replaces all instances of the text/regular expression specified in the specified document with
   * the specified replacement.
   *
   * @param textArea The text area in which to search.
   * @param toFind The text/regular expression to search for.
   * @param replaceWith The string to replace the found text with.
   * @param matchCase Whether the search should be case-sensitive.
   * @param wholeWord Whether there should be spaces or tabs on either side of the match.
   * @param regex Whether or not this is a regular expression search.
   * @return The number of replacements done.
   * @throws PatternSyntaxException If <code>regex</code> is <code>true</code> and <code>toFind
   *     </code> is an invalid regular expression.
   * @throws IndexOutOfBoundsException If <code>replaceWith</code> references an invalid group (less
   *     than zero or greater than the number of groups matched).
   * @see #replace
   * @see #regexReplace
   * @see #find
   */
  public static int replaceAll(
      RTextArea textArea,
      String toFind,
      String replaceWith,
      boolean matchCase,
      boolean wholeWord,
      boolean regex)
      throws PatternSyntaxException {

    int count = 0;

    textArea.beginAtomicEdit();
    try {

      if (regex) {

        if (replaceWith == null) {
          replaceWith = ""; // Needed by getReplacementText() below.
        }

        int oldOffs = textArea.getCaretPosition();
        textArea.setCaretPosition(0);
        int flags = Pattern.MULTILINE; // '^' and '$' are done per line.
        flags |= matchCase ? 0 : Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE;
        Pattern p = Pattern.compile(toFind, flags);
        while (SearchEngine.find(textArea, toFind, true, matchCase, wholeWord, true)) {
          Matcher m = p.matcher(textArea.getSelectedText());
          String replacement = getReplacementText(m, replaceWith);
          textArea.replaceSelection(replacement);
          count++;
        }
        if (count == 0) { // If nothing was found, don't move the caret.
          textArea.setCaretPosition(oldOffs);
        }

      } else { // Non-regular expression search.
        textArea.setCaretPosition(0);
        while (SearchEngine.find(textArea, toFind, true, matchCase, wholeWord, false)) {
          textArea.replaceSelection(replaceWith);
          count++;
        }
      }

    } finally {
      textArea.endAtomicEdit();
    }

    return count;
  }
  /**
   * Replaces all instances of the text/regular expression specified in the specified document with
   * the specified replacement.
   *
   * @param textArea The text area in which to search.
   * @param context What to search for and all search options.
   * @throws PatternSyntaxException If this is a regular expression search but the replacement text
   *     is an invalid regular expression.
   * @throws IndexOutOfBoundsException If this is a regular expression search but the replacement
   *     text references an invalid group (less than zero or greater than the number of groups
   *     matched).
   * @see #replace(RTextArea, SearchContext)
   * @see #find(JTextArea, SearchContext)
   */
  public static int replaceAll(RTextArea textArea, SearchContext context)
      throws PatternSyntaxException {

    String toFind = context.getSearchFor();
    if (toFind == null || toFind.length() == 0) {
      return 0;
    }

    int count = 0;

    textArea.beginAtomicEdit();
    try {
      int oldOffs = textArea.getCaretPosition();
      textArea.setCaretPosition(0);
      while (SearchEngine.replace(textArea, context)) {
        count++;
      }
      if (count == 0) { // If nothing was found, don't move the caret.
        textArea.setCaretPosition(oldOffs);
      }
    } finally {
      textArea.endAtomicEdit();
    }

    return count;
  }
  public void find(boolean forward) {
    SearchContext context = new SearchContext();
    String text = findTextField.getText();
    if (text.length() == 0) {
      return;
    }
    context.setSearchFor(text);

    context.setMatchCase(this.matchCaseCheckBox.isSelected());
    context.setRegularExpression(this.regExCheckBox.isSelected());
    context.setSearchForward(forward);

    context.setWholeWord(this.wholeWordsOnlyCheckBox.isSelected());

    SearchResult found = SearchEngine.find(selectedArea, context);
    if (this.markAllComboBox.isSelected()) {
      selectedArea.setMarkOccurrences(true);
    } else {
      selectedArea.setMarkOccurrences(false);
    }
    if (!found.wasFound()) {
      JOptionPane.showMessageDialog(
          this, "Text not found", "DomainMath IDE", JOptionPane.INFORMATION_MESSAGE);
    }
  }
  public void replaceAll(boolean back) {
    SearchContext context = new SearchContext();
    String findText = this.findTextField.getText();
    String text = this.replaceTextField.getText();
    if (text.length() == 0) {
      return;
    }
    context.setSearchFor(findText);
    context.setReplaceWith(text);
    context.setMatchCase(this.matchCaseCheckBox.isSelected());
    context.setRegularExpression(this.regExCheckBox.isSelected());
    context.setSearchForward(back);
    context.setWholeWord(this.wholeWordsOnlyCheckBox.isSelected());

    SearchResult found = SearchEngine.replaceAll(selectedArea, context);

    if (found.wasFound()) {
      JOptionPane.showMessageDialog(
          this, "Unable to replace the text", "DomainMath IDE", JOptionPane.INFORMATION_MESSAGE);
    }
  }
  public void actionPerformed(ActionEvent e) {

    // "FindNext" => search forward, "FindPrev" => search backward
    String command = e.getActionCommand();
    boolean forward = "FindNext".equals(command);

    // Create an object defining our search parameters.
    SearchContext context = new SearchContext();
    String text = searchField.getText();
    if (text.length() == 0) {
      return;
    }
    context.setSearchFor(text);
    context.setMatchCase(matchCaseCB.isSelected());
    context.setRegularExpression(regexCB.isSelected());
    context.setSearchForward(forward);
    context.setWholeWord(false);

    boolean found = SearchEngine.find(textArea, context);
    if (!found) {
      JOptionPane.showMessageDialog(this, "Text not found");
    }
  }
Example #9
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);
      }
    }
  }