// 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();
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 (regex==false) {
int pos = getNextMatchPos(text, findIn, forward,
matchCase, wholeWord);
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;
c.setDot(pos);
c.moveDot(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, matchCase, wholeWord);
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);
}
c.setDot(regExPos.x);
c.moveDot(regExPos.y);
return true;
}
}
// No match.