// 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;