Search in sources :

Example 6 with Element

use of javax.swing.text.Element in project processing by processing.

the class LineID method startTracking.

/**
   * Attach a {@link Document} to enable line number tracking when editing.
   * The position to track is before the first non-whitespace character on the
   * line. Edits happening before that position will cause the line number to
   * update accordingly. Multiple {@link #startTracking} calls will replace
   * the tracked document. Whoever wants a tracked line should track it and
   * add itself as listener if necessary.
   * ({@link LineHighlight}, {@link LineBreakpoint})
   *
   * @param doc the {@link Document} to use for line number tracking
   */
public synchronized void startTracking(Document doc) {
    //System.out.println("tracking: " + this);
    if (doc == null) {
        // null arg
        return;
    }
    if (doc == this.doc) {
        // already tracking that doc
        return;
    }
    try {
        Element line = doc.getDefaultRootElement().getElement(lineIdx);
        if (line == null) {
            // line doesn't exist
            return;
        }
        String lineText = doc.getText(line.getStartOffset(), line.getEndOffset() - line.getStartOffset());
        // set tracking position at (=before) first non-white space character on line,
        // or, if the line consists of entirely white spaces, just before the newline
        // character
        pos = doc.createPosition(line.getStartOffset() + nonWhiteSpaceOffset(lineText));
        this.doc = doc;
        doc.addDocumentListener(this);
    } catch (BadLocationException ex) {
        Messages.loge(null, ex);
        pos = null;
        this.doc = null;
    }
}
Also used : Element(javax.swing.text.Element) BadLocationException(javax.swing.text.BadLocationException)

Example 7 with Element

use of javax.swing.text.Element in project jadx by skylot.

the class LineNumbers method getTextLineNumber.

protected String getTextLineNumber(int rowStartOffset) {
    Element root = codeArea.getDocument().getDefaultRootElement();
    int index = root.getElementIndex(rowStartOffset);
    Element line = root.getElement(index);
    if (line.getStartOffset() == rowStartOffset) {
        int lineNumber = index + 1;
        if (useSourceLines) {
            Integer sourceLine = codeArea.getSourceLine(lineNumber);
            if (sourceLine != null) {
                return String.valueOf(sourceLine);
            }
        } else {
            return String.valueOf(lineNumber);
        }
    }
    return "";
}
Also used : Element(javax.swing.text.Element) Point(java.awt.Point)

Example 8 with Element

use of javax.swing.text.Element in project jadx by skylot.

the class LineNumbers method isCurrentLine.

private boolean isCurrentLine(int rowStartOffset) {
    int caretPosition = codeArea.getCaretPosition();
    Element root = codeArea.getDocument().getDefaultRootElement();
    return root.getElementIndex(rowStartOffset) == root.getElementIndex(caretPosition);
}
Also used : Element(javax.swing.text.Element) Point(java.awt.Point)

Example 9 with Element

use of javax.swing.text.Element in project binnavi by google.

the class ConsoleHelpers method getWord.

/**
   * Return all the characters forming a word (alpha-chars) from the given position backwards, up to
   * the when the first delimiter is found.
   * 
   * @param position location within the document from where to retrieve a word
   */
public String getWord(final int position) {
    int offset = position;
    final Element element = document.getParagraphElement(offset);
    String elementText;
    try {
        elementText = document.getText(element.getStartOffset(), element.getEndOffset() - element.getStartOffset());
    } catch (final Exception excp) {
        return "";
    }
    final int elementTextLength = elementText.length();
    if (elementTextLength == 0) {
        return "";
    }
    int i = 0;
    if (element.getStartOffset() > 0) {
        offset = offset - element.getStartOffset();
    }
    for (i = offset - 1; i >= 0; i--) {
        final char c = elementText.charAt(i);
        if (document.isDelimiter("" + c) || (i == 0)) {
            return elementText.substring(i + 1, offset).trim();
        }
    }
    return "";
}
Also used : Element(javax.swing.text.Element) BadLocationException(javax.swing.text.BadLocationException)

Example 10 with Element

use of javax.swing.text.Element in project binnavi by google.

the class SyntaxDocument method getLine.

private String getLine(final String content, final int offset) {
    final int line = rootElement.getElementIndex(offset);
    final Element lineElement = rootElement.getElement(line);
    final int start = lineElement.getStartOffset();
    final int end = lineElement.getEndOffset();
    return content.substring(start, end - 1);
}
Also used : Element(javax.swing.text.Element)

Aggregations

Element (javax.swing.text.Element)40 BadLocationException (javax.swing.text.BadLocationException)15 Point (java.awt.Point)13 AttributeSet (javax.swing.text.AttributeSet)6 HTMLDocument (javax.swing.text.html.HTMLDocument)6 FontMetrics (java.awt.FontMetrics)4 Dimension (java.awt.Dimension)3 IOException (java.io.IOException)3 Document (javax.swing.text.Document)3 TreePath (javax.swing.tree.TreePath)3 ExtendedHTMLDocument (gmgen.gui.ExtendedHTMLDocument)2 Font (java.awt.Font)2 Insets (java.awt.Insets)2 Rectangle (java.awt.Rectangle)2 Accessible (javax.accessibility.Accessible)2 JTextComponent (javax.swing.text.JTextComponent)2 SimpleAttributeSet (javax.swing.text.SimpleAttributeSet)2 HTMLEditorKit (javax.swing.text.html.HTMLEditorKit)2 Reader (java.io.Reader)1 StringReader (java.io.StringReader)1