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 void getText(int offset, int length, Segment txt) throws BadLocationException;

Source Link

Document

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

Usage

From source file:LiveParenMatcher.java

public void insertUpdate_3(DocumentEvent de) {
    Document doc = de.getDocument();
    int offset = de.getOffset();
    int length = de.getLength();
    Segment seg = new Segment();
    try {/*from  w  w  w.  ja  v a 2 s .c o m*/
        doc.getText(offset, length, seg); // text placed in Segment
    } catch (BadLocationException ble) {
    }

    // iterate through the Segment
    for (char ch = seg.first(); ch != seg.DONE; ch = seg.next())
        if (ch == '(' || ch == '[' || ch == '{' || ch == ')' || ch == ']' || ch == '}') {
            SwingUtilities.invokeLater(this); // will call run()
            return; // no need to check further
        }
}

From source file:com.mirth.connect.client.ui.components.rsta.ac.js.MirthSourceCompletionProvider.java

@Override
public List<Completion> getCompletionsAt(JTextComponent tc, Point p) {
    Set<Completion> completions = new HashSet<Completion>();
    List<Completion> parentCompletions = super.getCompletionsAt(tc, p);
    if (CollectionUtils.isNotEmpty(parentCompletions)) {
        completions.addAll(parentCompletions);
    }//from  w w w . ja v a  2  s.c o  m

    int offset = tc.viewToModel(p);
    if (offset < 0 || offset >= tc.getDocument().getLength()) {
        lastCompletionsAtText = null;
        lastParameterizedCompletionsAt = null;
        return new ArrayList<Completion>(completions);
    }

    Segment s = new Segment();
    Document doc = tc.getDocument();
    Element root = doc.getDefaultRootElement();
    int line = root.getElementIndex(offset);
    Element elem = root.getElement(line);
    int start = elem.getStartOffset();
    int end = elem.getEndOffset() - 1;

    try {

        doc.getText(start, end - start, s);

        // Get the valid chars before the specified offset.
        int startOffs = s.offset + (offset - start) - 1;
        while (startOffs >= s.offset && Character.isLetterOrDigit(s.array[startOffs])) {
            startOffs--;
        }

        // Get the valid chars at and after the specified offset.
        int endOffs = s.offset + (offset - start);
        while (endOffs < s.offset + s.count && Character.isLetterOrDigit(s.array[endOffs])) {
            endOffs++;
        }

        int len = endOffs - startOffs - 1;
        if (len <= 0) {
            lastParameterizedCompletionsAt = null;
            return new ArrayList<Completion>(completions);
        }
        String text = new String(s.array, startOffs + 1, len);

        if (text.equals(lastCompletionsAtText)) {
            if (CollectionUtils.isNotEmpty(lastParameterizedCompletionsAt)) {
                completions.addAll(lastParameterizedCompletionsAt);
            }
            return new ArrayList<Completion>(completions);
        }

        lastCompletionsAtText = text;
        lastParameterizedCompletionsAt = completionCache.getClassCompletions(tc, text);

        if (CollectionUtils.isNotEmpty(lastParameterizedCompletionsAt)) {
            completions.addAll(lastParameterizedCompletionsAt);
        }
        return new ArrayList<Completion>(completions);

    } catch (BadLocationException ble) {
        ble.printStackTrace(); // Never happens
    }

    lastCompletionsAtText = null;
    lastParameterizedCompletionsAt = null;

    return new ArrayList<Completion>(completions);
}

From source file:Console.java

void returnPressed() {
    Document doc = getDocument();
    int len = doc.getLength();
    Segment segment = new Segment();
    try {/*from   w  w  w.  j av  a2s  .  com*/
        synchronized (doc) {
            doc.getText(outputMark, len - outputMark, segment);
        }
    } catch (javax.swing.text.BadLocationException ignored) {
        ignored.printStackTrace();
    }
    if (segment.count > 0) {
        history.addElement(segment.toString());
    }
    historyIndex = history.size();
    inPipe.write(segment.array, segment.offset, segment.count);
    append("\n");
    synchronized (doc) {
        outputMark = doc.getLength();
    }
    inPipe.write("\n");
    inPipe.flush();
    console1.flush();
}

From source file:FormattedTextFieldExample.java

protected void createContent() {
    try {//www. jav a 2s. c  o  m
        Document doc = getDocument();
        int startOffset = element.getStartOffset();
        int endOffset = element.getEndOffset();
        int length = endOffset - startOffset - 1;

        // If there is no format, use the raw data.
        if (formatSize != 0) {
            // Get the document content
            doc.getText(startOffset, length, workBuff);

            // Initialize the output buffer with the
            // format string.
            System.arraycopy(formatChars, 0, formattedContent, 0, formatSize);

            // Insert the model content into
            // the target string.
            int count = Math.min(length, markerCount);
            int firstOffset = workBuff.offset;

            // Place the model data into the output array
            for (int i = 0; i < count; i++) {
                formattedContent[offsets[i]] = workBuff.array[i + firstOffset];
            }
        } else {
            doc.getText(startOffset, length, contentBuff);
        }
    } catch (BadLocationException bl) {
        contentBuff.count = 0;
    }
}