Example usage for javax.swing.text Element isLeaf

List of usage examples for javax.swing.text Element isLeaf

Introduction

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

Prototype

public boolean isLeaf();

Source Link

Document

Is this element a leaf element?

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    JTextArea textArea = new JTextArea("word1 word2\nword3\nword4");
    Document doc = textArea.getDocument();
    ElementIterator it = new ElementIterator(doc.getDefaultRootElement());
    Element e;
    while ((e = it.next()) != null) {
        if (e.isLeaf()) {
            int rangeStart = e.getStartOffset();
            int rangeEnd = e.getEndOffset();

            String line = textArea.getText(rangeStart, rangeEnd - rangeStart);
            System.out.println(line);
        }/*from   ww w. j  a  v  a  2  s .c o m*/
    }
}

From source file:ShowHTMLViews.java

public static void displayElement(Document doc, Element e, int indent, PrintStream out) {
    for (int i = 0; i < indent; i++) {
        out.print("  ");
    }/*  ww  w  .j a  va 2 s  .co  m*/
    out.println("===== Element Class: " + getShortClassName(e.getClass()));
    for (int i = 0; i < indent; i++) {
        out.print("  ");
    }
    int startOffset = e.getStartOffset();
    int endOffset = e.getEndOffset();
    out.println("Offsets [" + startOffset + ", " + endOffset + "]");
    AttributeSet a = e.getAttributes();
    Enumeration x = a.getAttributeNames();
    for (int i = 0; i < indent; i++) {
        out.print("  ");
    }
    out.println("ATTRIBUTES:");
    while (x.hasMoreElements()) {
        for (int i = 0; i < indent; i++) {
            out.print("  ");
        }
        Object attr = x.nextElement();
        out.println(" (" + attr + ", " + a.getAttribute(attr) + ")" + " [" + getShortClassName(attr.getClass())
                + "/" + getShortClassName(a.getAttribute(attr).getClass()) + "] ");
    }

    // Display the text for a leaf element
    if (e.isLeaf()) {
        try {
            String str = doc.getText(startOffset, endOffset - startOffset);
            if (str.length() > 40) {
                str = str.substring(0, 40);
            }
            if (str.length() > 0) {
                for (int i = 0; i < indent; i++) {
                    out.print("  ");
                }
                out.println("[" + str + "]");
            }
        } catch (BadLocationException ex) {
        }
    }

    // Display child elements
    int count = e.getElementCount();
    for (int i = 0; i < count; i++) {
        displayElement(doc, e.getElement(i), indent + 1, out);
    }
}

From source file:au.org.ala.delta.ui.rtf.RTFWriter.java

private void writeElement(Element element, int pos, int length) throws BadLocationException, IOException {
    AttributeSet elementAttributes = element.getAttributes();

    writeAttributeChangesAsRTF(elementAttributes);

    if (element.isLeaf()) {
        String plainText = _document.getText(pos, length);
        for (int i = 0; i < plainText.length(); ++i) {
            char ch = plainText.charAt(i);
            if (ch > 127) {
                CharacterKeyword kwd = Keyword.findKeywordForCharacter(ch);
                if (kwd != null) {
                    _writer.write("\\");
                    _writer.write(kwd.getKeyword());
                    _writer.write(" ");
                } else {
                    _writer.write("\\u");
                    _writer.write(Integer.toString(ch));
                    _writer.write("?");
                }//from   w w w  .  j av  a  2s . c o  m
            } else {
                if (ch == '\n') {
                    _writer.write("\\par ");
                } else {
                    _writer.write(ch);
                }
            }
        }
    } else {
        for (int i = 0; i < element.getElementCount(); i++) {
            writeElement(element.getElement(i));
        }
    }

}

From source file:forge.screens.deckeditor.DeckImport.java

private void readInput() {
    this.tokens.clear();
    final ElementIterator it = new ElementIterator(this.txtInput.getDocument().getDefaultRootElement());
    Element e;

    DeckRecognizer recognizer = new DeckRecognizer(newEditionCheck.isSelected(), onlyCoreExpCheck.isSelected(),
            FModel.getMagicDb().getCommonCards());
    if (dateTimeCheck.isSelected()) {
        recognizer.setDateConstraint(monthDropdown.getSelectedIndex(),
                (Integer) yearDropdown.getSelectedItem());
    }/*from  w  ww  . j  ava2s .c  om*/
    while ((e = it.next()) != null) {
        if (!e.isLeaf()) {
            continue;
        }
        final int rangeStart = e.getStartOffset();
        final int rangeEnd = e.getEndOffset();
        try {
            final String line = this.txtInput.getText(rangeStart, rangeEnd - rangeStart);
            this.tokens.add(recognizer.recognizeLine(line));
        } catch (final BadLocationException ex) {
        }
    }
}

From source file:ru.gelin.fictionbook.reader.models.FBSimpleElementTest.java

@Test
public void testIsLeaf() {
    Node node = fb.getDocument().selectSingleNode("//fb:section[@id='section1']/fb:title");
    Element parent = document.getElement(node);
    assertFalse(parent.isLeaf());
    assertTrue(element.isLeaf());//w  ww  .ja  va  2 s  .c o  m
}