public boolean find(JComponent subject, String text,
                        boolean forward, boolean matchCase,
                        boolean wholeWord, boolean regex)
            throws PatternSyntaxException {
        JTextComponent component = (JTextComponent) subject;
        // 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 = component.getCaret();
        int start = forward ? Math.max(c.getDot(), c.getMark()) :
                Math.min(c.getDot(), c.getMark());

        String findIn = getFindInText(component, 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;
                selectAndPossiblyCenter(component, 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, 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);
                }
                selectAndPossiblyCenter(component, regExPos.x, regExPos.y);
                return true;
            }
        }

        // No match.
        return false;
    }