/** * Finds the next instance of the string/regular expression specified from the caret position. If * a match is found, it is selected in this text area. * * @param textArea The text area in which to search. * @param context What to search for and all search options. * @return Whether a match was found (and thus selected). * @throws PatternSyntaxException If this is a regular expression search but the search text is an * invalid regular expression. * @see #replace(RTextArea, SearchContext) * @see #replaceAll(RTextArea, SearchContext) */ public static boolean find(JTextArea textArea, SearchContext context) { String text = context.getSearchFor(); if (text == null || text.length() == 0) { return false; } // Be smart about what position we're "starting" at. We don't want // to find a match in the currently selected text (if any), so we // start searching AFTER the selection if searching forward, and // BEFORE the selection if searching backward. Caret c = textArea.getCaret(); boolean forward = context.getSearchForward(); int start = forward ? Math.max(c.getDot(), c.getMark()) : Math.min(c.getDot(), c.getMark()); String findIn = getFindInText(textArea, start, forward); if (findIn == null || findIn.length() == 0) return false; // Find the next location of the text we're searching for. if (!context.isRegularExpression()) { int pos = getNextMatchPos(text, findIn, forward, context.getMatchCase(), context.getWholeWord()); findIn = null; // May help garbage collecting. if (pos != -1) { // Without this, if JTextArea isn't in focus, selection // won't appear selected. c.setSelectionVisible(true); pos = forward ? start + pos : pos; selectAndPossiblyCenter(textArea, pos, pos + text.length()); return true; } } else { // Regex matches can have varying widths. The returned point's // x- and y-values represent the start and end indices of the // match in findIn. Point regExPos = getNextMatchPosRegEx( text, findIn, forward, context.getMatchCase(), context.getWholeWord()); findIn = null; // May help garbage collecting. if (regExPos != null) { // Without this, if JTextArea isn't in focus, selection // won't appear selected. c.setSelectionVisible(true); if (forward) { regExPos.translate(start, start); } selectAndPossiblyCenter(textArea, regExPos.x, regExPos.y); return true; } } // No match. return false; }
/** * Finds the next instance of the regular expression specified from the caret position. If a match * is found, it is replaced with the specified replacement string. * * @param textArea The text area in which to search. * @param toFind The regular expression to search for. * @param replaceWith The string to replace the found regex with. * @param forward Whether to search forward from the caret position or backward from it. * @param matchCase Whether the search should be case-sensitive. * @param wholeWord Whether there should be spaces or tabs on either side of the match. * @return Whether a match was found (and thus replaced). * @throws PatternSyntaxException If <code>toFind</code> is not a valid 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 #find */ protected static boolean regexReplace( JTextArea textArea, String toFind, String replaceWith, boolean forward, boolean matchCase, boolean wholeWord) throws PatternSyntaxException { // Be smart about what position we're "starting" at. For example, // if they are searching backwards and there is a selection such that // the dot is past the mark, and the selection is the text for which // you're searching, this search will find and return the current // selection. So, in that case we start at the beginning of the // selection. Caret c = textArea.getCaret(); int start = makeMarkAndDotEqual(textArea, forward); String findIn = getFindInText(textArea, start, forward); if (findIn == null) return false; // Find the next location of the text we're searching for. RegExReplaceInfo info = getRegExReplaceInfo(toFind, findIn, forward, matchCase, wholeWord, replaceWith); findIn = null; // May help garbage collecting. // If a match was found, do the replace and return! if (info != null) { // Without this, if JTextArea isn't in focus, selection won't // appear selected. c.setSelectionVisible(true); int matchStart = info.getStartIndex(); int matchEnd = info.getEndIndex(); if (forward) { matchStart += start; matchEnd += start; } selectAndPossiblyCenter(textArea, matchStart, matchEnd); textArea.replaceSelection(info.getReplacement()); return true; } // No match. return false; }