Example usage for javax.swing.text Document getText

List of usage examples for javax.swing.text Document getText

Introduction

In this page you can find the example usage for javax.swing.text Document getText.

Prototype

public String getText(int offset, int length) throws BadLocationException;

Source Link

Document

Fetches the text contained within the given portion of the document.

Usage

From source file:simplealbum.mvc.autocomplete.JTextPaneX.java

protected void fireText(DocumentEvent e) {
    Document document = e.getDocument();
    String text = "";
    try {/*from ww w . j a v a 2 s.c  om*/
        text = document.getText(0, document.getLength());
    } catch (BadLocationException ex) {
        Logger.getLogger(JTextPaneX.class.getName()).log(Level.SEVERE, null, ex);
    }
    firePropertyChange("text", null, text);
}

From source file:tk.tomby.tedit.core.snr.FindReplaceWorker.java

/**
 * DOCUMENT ME!// w  w w . j  a  v  a2 s. c  om
 *
 * @param lineNumber DOCUMENT ME!
 *
 * @return DOCUMENT ME!
 */
private Element init(int lineNumber) {
    Document doc = buffer.getDocument();
    Element line = doc.getDefaultRootElement().getElement(lineNumber);

    try {
        int options = Pattern.DOTALL;

        String find = PreferenceManager.getString("snr.find", "");

        if ((find != null) && !find.equals("")) {
            if (PreferenceManager.getBoolean("snr.case", false)) {
                find = find.toLowerCase();

                options |= Pattern.CASE_INSENSITIVE;
            }

            if (PreferenceManager.getBoolean("snr.whole", false)) {
                find = "\\b" + find + "\\b";
            }

            int offset = line.getStartOffset();
            int length = line.getEndOffset() - offset;

            if (PreferenceManager.getInt("snr.direction", FORWARD) == FORWARD) {
                if ((buffer.getSelectionEnd() > line.getStartOffset())
                        && (buffer.getSelectionEnd() <= line.getEndOffset())) {
                    offset = buffer.getSelectionEnd();
                    length = line.getEndOffset() - offset;
                }
            } else {
                if ((buffer.getSelectionStart() > line.getStartOffset())
                        && (buffer.getSelectionStart() <= line.getEndOffset())) {
                    length = buffer.getSelectionStart() - offset;
                }
            }

            String text = doc.getText(offset, length);

            Pattern pattern = Pattern.compile(find, options);

            this.matcher = pattern.matcher(text);
        }
    } catch (BadLocationException e) {
        log.error(e.getMessage(), e);
    }

    return line;
}