Example usage for javax.swing JEditorPane getCaret

List of usage examples for javax.swing JEditorPane getCaret

Introduction

In this page you can find the example usage for javax.swing JEditorPane getCaret.

Prototype

@Transient
public Caret getCaret() 

Source Link

Document

Fetches the caret that allows text-oriented navigation over the view.

Usage

From source file:ja.lingo.application.gui.main.describer.DescriberGui.java

private void find(String searchText, boolean fromStart, boolean forwardDirection, boolean caseSensetive,
        boolean wholeWordsOnly) {
    JEditorPane editorPane = articlePanel.getEditorPane();

    String article;//from   w w w. j  a v  a 2  s  . c  o  m
    try {
        article = editorPane.getDocument().getText(0, editorPane.getDocument().getLength());
    } catch (BadLocationException e) {
        throw States.shouldNeverReachHere(e);
    }

    if (fromStart) {
        editorPane.select(0, 0);
    }

    // try search twice: after caret, before caret (the order depends on search direction)
    int attempt1 = forwardDirection ? editorPane.getSelectionEnd() : editorPane.getSelectionStart() - 1;
    int attempt2 = forwardDirection ? 0 : article.length();

    int index = TextSearcher.indexOf(forwardDirection, article, searchText, attempt1, caseSensetive,
            wholeWordsOnly);
    if (index == -1) {
        index = TextSearcher.indexOf(forwardDirection, article, searchText, attempt2, caseSensetive,
                wholeWordsOnly);

        if (index == -1) {
            // reset selction
            editorPane.select(0, 0);
            model.find_sendFeedback(false);
            return;
        }
    }

    // highlight on success
    editorPane.select(index, index + searchText.length());
    editorPane.getCaret().setSelectionVisible(true);
    model.find_sendFeedback(true);
}