Search in sources :

Example 76 with Element

use of javax.swing.text.Element in project omegat by omegat-org.

the class Document3 method setAlignment.

/**
 * Set alignment for specified part of text.
 *
 * @param beginOffset
 *            begin offset
 * @param endOffset
 *            end offset
 * @param isRightAlignment
 *            false - left alignment, true - right alignment
 */
protected void setAlignment(int beginOffset, int endOffset, boolean isRightAlignment) {
    try {
        writeLock();
        DefaultDocumentEvent changes = new DefaultDocumentEvent(beginOffset, endOffset - beginOffset, DocumentEvent.EventType.CHANGE);
        Element root = getDefaultRootElement();
        int parBeg = root.getElementIndex(beginOffset);
        int parEnd = root.getElementIndex(endOffset - 1);
        for (int par = parBeg; par <= parEnd; par++) {
            Element el = root.getElement(par);
            MutableAttributeSet attr = (MutableAttributeSet) el.getAttributes();
            attr.addAttribute(StyleConstants.Alignment, isRightAlignment ? StyleConstants.ALIGN_RIGHT : StyleConstants.ALIGN_LEFT);
        }
        changes.end();
        fireChangedUpdate(changes);
    } finally {
        writeUnlock();
    }
}
Also used : MutableAttributeSet(javax.swing.text.MutableAttributeSet) Element(javax.swing.text.Element)

Example 77 with Element

use of javax.swing.text.Element in project SKMCLauncher by SKCraft.

the class LimitLinesDocumentListener method removeFromStart.

private void removeFromStart(Document document, Element root) {
    Element line = root.getElement(0);
    int end = line.getEndOffset();
    try {
        document.remove(0, end);
    } catch (BadLocationException ble) {
        System.out.println(ble);
    }
}
Also used : Element(javax.swing.text.Element) BadLocationException(javax.swing.text.BadLocationException)

Example 78 with Element

use of javax.swing.text.Element in project SKMCLauncher by SKCraft.

the class LimitLinesDocumentListener method removeFromEnd.

private void removeFromEnd(Document document, Element root) {
    // We use start minus 1 to make sure we remove the newline
    // character of the previous line
    Element line = root.getElement(root.getElementCount() - 1);
    int start = line.getStartOffset();
    int end = line.getEndOffset();
    try {
        document.remove(start - 1, end - start);
    } catch (BadLocationException ble) {
        System.out.println(ble);
    }
}
Also used : Element(javax.swing.text.Element) BadLocationException(javax.swing.text.BadLocationException)

Example 79 with Element

use of javax.swing.text.Element in project SKMCLauncher by SKCraft.

the class LimitLinesDocumentListener method removeLines.

private void removeLines(DocumentEvent e) {
    // The root Element of the Document will tell us the total number
    // of line in the Document.
    Document document = e.getDocument();
    Element root = document.getDefaultRootElement();
    while (root.getElementCount() > maximumLines) {
        if (isRemoveFromStart) {
            removeFromStart(document, root);
        } else {
            removeFromEnd(document, root);
        }
    }
}
Also used : Element(javax.swing.text.Element) Document(javax.swing.text.Document)

Example 80 with Element

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

the class SyntaxDocument method highlightLinesAfter.

/*
   * Highlight lines to start or end delimiter
   */
private void highlightLinesAfter(final String content, final int line) {
    final int offset = rootElement.getElement(line).getEndOffset();
    // Start/End delimiter not found, nothing to do
    int startDelimiter = indexOf(content, getStartDelimiter(), offset);
    int endDelimiter = indexOf(content, getEndDelimiter(), offset);
    if (startDelimiter < 0) {
        startDelimiter = content.length();
    }
    if (endDelimiter < 0) {
        endDelimiter = content.length();
    }
    final int delimiter = Math.min(startDelimiter, endDelimiter);
    if (delimiter < offset) {
        return;
    }
    // Start/End delimiter found, reapply highlighting
    final int endLine = rootElement.getElementIndex(delimiter);
    for (int i = line + 1; i < endLine; i++) {
        final Element branch = rootElement.getElement(i);
        final Element leaf = doc.getCharacterElement(branch.getStartOffset());
        final AttributeSet as = leaf.getAttributes();
        if (as.isEqual(comment)) {
            applyHighlighting(content, i);
        }
    }
}
Also used : SimpleAttributeSet(javax.swing.text.SimpleAttributeSet) AttributeSet(javax.swing.text.AttributeSet) MutableAttributeSet(javax.swing.text.MutableAttributeSet) Element(javax.swing.text.Element)

Aggregations

Element (javax.swing.text.Element)100 BadLocationException (javax.swing.text.BadLocationException)35 Point (java.awt.Point)19 AttributeSet (javax.swing.text.AttributeSet)15 Document (javax.swing.text.Document)15 HTMLDocument (javax.swing.text.html.HTMLDocument)11 FontMetrics (java.awt.FontMetrics)6 Dimension (java.awt.Dimension)5 Rectangle (java.awt.Rectangle)5 View (javax.swing.text.View)5 Font (java.awt.Font)4 Insets (java.awt.Insets)4 IOException (java.io.IOException)4 AbstractDocument (javax.swing.text.AbstractDocument)4 StyledDocument (javax.swing.text.StyledDocument)4 java.awt (java.awt)3 Objects (java.util.Objects)3 javax.swing (javax.swing)3 AbstractElement (javax.swing.text.AbstractDocument.AbstractElement)3 HTML (javax.swing.text.html.HTML)3