Example usage for javax.swing.text Segment Segment

List of usage examples for javax.swing.text Segment Segment

Introduction

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

Prototype

public Segment() 

Source Link

Document

Creates a new segment.

Usage

From source file:Main.java

private static Segment getLineBuffer() {
    if (lineBuffer == null) {
        lineBuffer = new Segment();
    }//from www .  j a  v  a  2s  . com
    return lineBuffer;
}

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 {// www .  j a va2 s. com
        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   www  .  j a v a 2s.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   www .j ava 2 s  .co  m
        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

public FormattedFieldView(Element elem, FormattedTextField.FormatSpec formatSpec) {
    super(elem);/*from www. j  a  v  a2  s. c  o  m*/

    this.contentBuff = new Segment();
    this.measureBuff = new Segment();
    this.workBuff = new Segment();
    this.element = elem;

    buildMapping(formatSpec); // Build the model -> view map
    createContent(); // Update content string
}

From source file:ro.nextreports.designer.ui.sqleditor.syntax.SyntaxDocument.java

/**
 * Parse the entire document and return list of tokens that do not already
 * exist in the tokens list. There may be overlaps, and replacements, 
 * which we will cleanup later./*from   www .  j av a2s.  co  m*/
 * 
 * @return list of tokens that do not exist in the tokens field 
 */
private void parse() {
    // if we have no lexer, then we must have no tokens...
    if (lexer == null) {
        tokens = null;
        return;
    }

    List<Token> tokens = new ArrayList<Token>(getLength() / 10);
    long time = System.nanoTime();
    int length = getLength();
    try {
        Segment segment = new Segment();
        getText(0, getLength(), segment);
        CharArrayReader reader = new CharArrayReader(segment.array, segment.offset, segment.count);
        lexer.yyreset(reader);
        Token token;
        while ((token = lexer.yylex()) != null) {
            tokens.add(token);
        }
    } catch (BadLocationException e) {
        LOG.error(e.getMessage(), e);
    } catch (IOException e) {
        // This will not be thrown from the Lexer
        LOG.error(e.getMessage(), e);
    } finally {
        // Benchmarks:
        // Parsed 574038 chars in 81 ms, giving 74584 tokens
        //                System.out.printf("Parsed %d in %d ms, giving %d tokens",
        //                        len, (System.nanoTime() - ts) / 1000000, toks.size());
        if (LOG.isDebugEnabled()) {
            LOG.debug(String.format("Parsed %d in %d ms, giving %d tokens", length,
                    (System.nanoTime() - time) / 1000000, tokens.size()));
        }
        this.tokens = tokens;
    }
}