Search in sources :

Example 1 with SearchResult

use of org.fife.ui.rtextarea.SearchResult in project jadx by skylot.

the class SearchBar method search.

private void search(int direction) {
    String searchText = searchField.getText();
    if (searchText == null || searchText.length() == 0 || rTextArea.getText() == null) {
        return;
    }
    boolean forward = direction >= 0;
    boolean matchCase = matchCaseCB.isSelected();
    boolean regex = regexCB.isSelected();
    boolean wholeWord = wholeWordCB.isSelected();
    SearchContext context = new SearchContext();
    context.setSearchFor(searchText);
    context.setMatchCase(matchCase);
    context.setRegularExpression(regex);
    context.setSearchForward(forward);
    context.setWholeWord(wholeWord);
    context.setMarkAll(markAllCB.isSelected());
    // TODO hack: move cursor before previous search for not jump to next occurrence
    if (direction == 0 && !COLOR_BG_ERROR.equals(searchField.getBackground())) {
        try {
            int caretPos = rTextArea.getCaretPosition();
            int lineNum = rTextArea.getLineOfOffset(caretPos) - 1;
            if (lineNum > 1) {
                rTextArea.setCaretPosition(rTextArea.getLineStartOffset(lineNum));
            }
        } catch (BadLocationException e) {
            LOG.error("Caret move error", e);
        }
    }
    SearchResult result = SearchEngine.find(rTextArea, context);
    if (!result.wasFound()) {
        int pos = SearchEngine.getNextMatchPos(searchText, rTextArea.getText(), forward, matchCase, wholeWord);
        if (pos != -1) {
            rTextArea.setCaretPosition(forward ? 0 : rTextArea.getDocument().getLength() - 1);
            search(direction);
            searchField.setBackground(COLOR_BG_WARN);
            return;
        }
        searchField.setBackground(COLOR_BG_ERROR);
    } else {
        searchField.setBackground(COLOR_BG_NORMAL);
    }
}
Also used : SearchContext(org.fife.ui.rtextarea.SearchContext) SearchResult(org.fife.ui.rtextarea.SearchResult) BadLocationException(javax.swing.text.BadLocationException)

Aggregations

BadLocationException (javax.swing.text.BadLocationException)1 SearchContext (org.fife.ui.rtextarea.SearchContext)1 SearchResult (org.fife.ui.rtextarea.SearchResult)1