コード例 #1
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 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;
  }
コード例 #2
0
  public void replace(boolean back) {
    SearchContext context = new SearchContext();
    String text = this.replaceTextField.getText();
    if (text.length() == 0) {
      return;
    }
    String findText = this.findTextField.getText();

    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 isReplaced = SearchEngine.replace(selectedArea, context);

    if (!isReplaced.wasFound()) {
      JOptionPane.showMessageDialog(
          this, "Unable to replace the text", "DomainMath IDE", JOptionPane.INFORMATION_MESSAGE);
    }
  }