Search in sources :

Example 26 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 27 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 28 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 29 with Element

use of javax.swing.text.Element in project sonarqube by SonarSource.

the class TextLineNumber method setPreferredWidth.

/**
   *  Calculate the width needed to display the maximum line number
   */
private void setPreferredWidth() {
    Element root = component.getDocument().getDefaultRootElement();
    int lines = root.getElementCount();
    int digits = Math.max(String.valueOf(lines).length(), minimumDisplayDigits);
    if (lastDigits != digits) {
        lastDigits = digits;
        FontMetrics fontMetrics = getFontMetrics(getFont());
        int width = fontMetrics.charWidth('0') * digits;
        Insets insets = getInsets();
        int preferredWidth = insets.left + insets.right + width;
        Dimension d = getPreferredSize();
        d.setSize(preferredWidth, HEIGHT);
        setPreferredSize(d);
        setSize(d);
    }
}
Also used : Insets(java.awt.Insets) FontMetrics(java.awt.FontMetrics) Element(javax.swing.text.Element) Dimension(java.awt.Dimension) Point(java.awt.Point)

Example 30 with Element

use of javax.swing.text.Element in project sonarqube by SonarSource.

the class TextLineNumber method caretUpdate.

//
// Implement CaretListener interface
//
@Override
public void caretUpdate(CaretEvent e) {
    // Get the line the caret is positioned on
    int caretPosition = component.getCaretPosition();
    Element root = component.getDocument().getDefaultRootElement();
    int currentLine = root.getElementIndex(caretPosition);
    if (lastLine != currentLine) {
        repaint();
        lastLine = currentLine;
    }
}
Also used : Element(javax.swing.text.Element) Point(java.awt.Point)

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