Example usage for javax.swing.text Element getElementCount

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

Introduction

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

Prototype

public int getElementCount();

Source Link

Document

Gets the number of child elements contained by this element.

Usage

From source file:EditorPaneExample16.java

public Heading getNextHeading(Document doc, ElementIterator iter) {
    Element elem;

    while ((elem = iter.next()) != null) {
        AttributeSet attrs = elem.getAttributes();
        Object type = attrs.getAttribute(StyleConstants.NameAttribute);
        int level = getHeadingLevel(type);
        if (level > 0) {
            // It is a heading - get the text
            String headingText = "";
            int count = elem.getElementCount();
            for (int i = 0; i < count; i++) {
                Element child = elem.getElement(i);
                AttributeSet cattrs = child.getAttributes();
                if (cattrs.getAttribute(StyleConstants.NameAttribute) == HTML.Tag.CONTENT) {
                    try {
                        int offset = child.getStartOffset();
                        headingText += doc.getText(offset, child.getEndOffset() - offset);
                    } catch (BadLocationException e) {
                    }/* w w  w.j  a  v a2s .c o m*/
                }
            }
            headingText = headingText.trim();
            return new Heading(headingText, level, elem.getStartOffset());
        }
    }
    return null;
}

From source file:com.centurylink.mdw.designer.pages.ExportHelper.java

private List<Object> generateElementChildrenHtml(Element element, int depth, Font font) {
    int n = element.getElementCount();
    List<Object> children = new ArrayList<Object>();
    for (int i = 0; i < n; i++) {
        Element subelem = element.getElement(i);
        Object child = generateElementHtml(subelem, depth, font);
        if (child != null)
            children.add(child);// w  w w  .  j  av  a2s  .c om
    }
    return children;
}

From source file:com.centurylink.mdw.designer.pages.ExportHelper.java

private void printElementChildrenHtml(Element element, Object parent, int depth, Font font, int parent_level) {
    int n = element.getElementCount();
    for (int i = 0; i < n; i++) {
        Element subelem = element.getElement(i);
        printElementHtml(subelem, parent, depth, font, parent_level);
    }/*from  w w w  . j a v  a2 s  .com*/
}

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("?");
                }/*ww  w .  j  a  v  a 2  s. 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:net.java.sip.communicator.impl.gui.main.chat.ChatConversationPanel.java

/**
 * Appends the given string at the end of the contained in this panel
 * document./*w  w  w  .  jav a2  s  . co  m*/
 *
 * Note: Currently, it looks like appendMessageToEnd is only called for
 * messages that are already converted to HTML. So It is quite possible that
 * we can remove the content type without any issues.
 *
 * @param original the message string to append
 * @param contentType the message's content type
 */
public void appendMessageToEnd(final String original, final String contentType) {
    if (!SwingUtilities.isEventDispatchThread()) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                appendMessageToEnd(original, contentType);
            }
        });
        return;
    }

    if (original == null) {
        return;
    }

    final String message;
    if (ChatHtmlUtils.HTML_CONTENT_TYPE.equalsIgnoreCase(contentType)) {
        message = original;
    } else {
        message = StringEscapeUtils.escapeHtml4(original);
    }

    synchronized (scrollToBottomRunnable) {
        Element root = document.getDefaultRootElement();

        try {
            document.insertBeforeEnd(
                    // the body element
                    root.getElement(root.getElementCount() - 1),
                    // the message to insert
                    message);

            // Need to call explicitly scrollToBottom, because for some
            // reason the componentResized event isn't fired every time we
            // add text.
            SwingUtilities.invokeLater(scrollToBottomRunnable);
        } catch (BadLocationException e) {
            logger.error("Insert in the HTMLDocument failed.", e);
        } catch (IOException e) {
            logger.error("Insert in the HTMLDocument failed.", e);
        }
    }

    String lastElemContent = getElementContent(lastMessageUID, message);

    if (lastElemContent != null) {
        finishMessageAdd(lastElemContent);
    }
}

From source file:net.java.sip.communicator.impl.gui.main.chat.ChatConversationPanel.java

/**
 * Finds the first element with <tt>name</tt> among the child elements of
 * <tt>element</tt>.//from  w w w  .ja  va  2  s.  c  o  m
 * @param element the element to searh for.
 * @param name the name to search for.
 * @return the first element with <tt>name</tt>.
 */
private Element findFirstElement(Element element, String name) {
    if (element.getName().equalsIgnoreCase(name))
        return element;

    Element resultElement = null;

    // Count how many messages we have in the document.
    for (int i = 0; i < element.getElementCount(); i++) {
        resultElement = findFirstElement(element.getElement(i), name);

        if (resultElement != null)
            return resultElement;
    }

    return null;
}

From source file:net.java.sip.communicator.impl.gui.main.chat.ChatConversationPanel.java

/**
 *
 * @param element/*from w  ww .j a  va2 s. c o  m*/
 * @param attrName
 * @param matchStrings
 * @return
 */
private Element findFirstElement(Element element, HTML.Attribute attrName, String[] matchStrings) {
    String attr = (String) element.getAttributes().getAttribute(attrName);

    if (attr != null)
        for (String matchString : matchStrings)
            if (attr.startsWith(matchString))
                return element;

    Element resultElement = null;

    // Count how many messages we have in the document.
    for (int i = 0; i < element.getElementCount(); i++) {
        resultElement = findFirstElement(element.getElement(i), attrName, matchStrings);
        if (resultElement != null)
            return resultElement;
    }

    return null;
}

From source file:com.hexidec.ekit.EkitCore.java

/**
 * Traverses nodes for the describing method
 */// w  ww . j a v  a2s.co  m
private void traverseElement(Element element, StringBuilder sb) {
    indent += indentStep;
    for (int i = 0; i < element.getElementCount(); i++) {
        for (int j = 0; j < indent; j++) {
            sb.append(" ");
        }
        sb.append(element.getElement(i));
        traverseElement(element.getElement(i), sb);
    }
    indent -= indentStep;
}

From source file:com.hexidec.ekit.EkitCore.java

public void removeEmptyLists() {
    javax.swing.text.ElementIterator ei = new javax.swing.text.ElementIterator(htmlDoc);
    Element ele;
    while ((ele = ei.next()) != null) {
        if (ele.getName().equals("ul") || ele.getName().equals("ol")) {
            int listChildren = 0;
            for (int i = 0; i < ele.getElementCount(); i++) {
                if (ele.getElement(i).getName().equals("li")) {
                    listChildren++;/*from  w w w.j a  va2 s  .  co m*/
                }
            }
            if (listChildren <= 0) {
                htmlUtilities.removeTag(ele, true);
            }
        }
    }
    refreshOnUpdate();
}

From source file:com.hexidec.ekit.EkitCore.java

private void moveCaretOnTable(Element tdElement, boolean up, boolean selecting) {

    int caretPos = jtpMain.getCaretPosition();

    Element rowElement = tdElement.getParentElement();
    Element tableElement = rowElement.getParentElement();

    int rowIndex = DocumentUtil.getIndexInParent(rowElement);
    if (up) {/*  w ww. j  ava 2 s . c  om*/
        if (rowIndex == 0) {
            moveCaret(Math.max(tableElement.getStartOffset() - 1, 0), selecting);
        } else {
            rowElement = tableElement.getElement(rowIndex - 1);
            int posInCell = caretPos - tdElement.getStartOffset();
            int colIndex = DocumentUtil.getIndexInParent(tdElement);
            tdElement = rowElement.getElement(colIndex);
            moveCaret(
                    tdElement.getStartOffset()
                            + Math.min(posInCell, tdElement.getEndOffset() - tdElement.getStartOffset() - 1),
                    selecting);
        }
    } else { // down
        if (rowIndex >= tableElement.getElementCount() - 1) {
            moveCaret(Math.min(tableElement.getEndOffset(), htmlDoc.getLength() - 1), selecting);
        } else {
            rowElement = tableElement.getElement(rowIndex + 1);
            int posInCell = caretPos - tdElement.getStartOffset();
            int colIndex = DocumentUtil.getIndexInParent(tdElement);
            tdElement = rowElement.getElement(colIndex);
            moveCaret(
                    tdElement.getStartOffset()
                            + Math.min(posInCell, tdElement.getEndOffset() - tdElement.getStartOffset() - 1),
                    selecting);
        }
    }

}