Example usage for javax.swing.text Element getElement

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

Introduction

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

Prototype

public Element getElement(int index);

Source Link

Document

Fetches the child element at the given index.

Usage

From source file:net.sf.jasperreports.engine.util.JEditorPaneRtfMarkupProcessor.java

/**
 * //from  w w  w.ja  v a  2s  .co m
 */
protected void addElements(List<Element> elements, Element element) {
    if (element instanceof LeafElement) {
        elements.add(element);
    }
    for (int i = 0; i < element.getElementCount(); i++) {
        Element child = element.getElement(i);
        addElements(elements, child);
    }
}

From source file:com.mirth.connect.client.ui.components.rsta.ac.js.MirthSourceCompletionProvider.java

@Override
public List<Completion> getCompletionsAt(JTextComponent tc, Point p) {
    Set<Completion> completions = new HashSet<Completion>();
    List<Completion> parentCompletions = super.getCompletionsAt(tc, p);
    if (CollectionUtils.isNotEmpty(parentCompletions)) {
        completions.addAll(parentCompletions);
    }/*from  w w w.  j  av  a2  s . c  o  m*/

    int offset = tc.viewToModel(p);
    if (offset < 0 || offset >= tc.getDocument().getLength()) {
        lastCompletionsAtText = null;
        lastParameterizedCompletionsAt = null;
        return new ArrayList<Completion>(completions);
    }

    Segment s = new Segment();
    Document doc = tc.getDocument();
    Element root = doc.getDefaultRootElement();
    int line = root.getElementIndex(offset);
    Element elem = root.getElement(line);
    int start = elem.getStartOffset();
    int end = elem.getEndOffset() - 1;

    try {

        doc.getText(start, end - start, s);

        // Get the valid chars before the specified offset.
        int startOffs = s.offset + (offset - start) - 1;
        while (startOffs >= s.offset && Character.isLetterOrDigit(s.array[startOffs])) {
            startOffs--;
        }

        // Get the valid chars at and after the specified offset.
        int endOffs = s.offset + (offset - start);
        while (endOffs < s.offset + s.count && Character.isLetterOrDigit(s.array[endOffs])) {
            endOffs++;
        }

        int len = endOffs - startOffs - 1;
        if (len <= 0) {
            lastParameterizedCompletionsAt = null;
            return new ArrayList<Completion>(completions);
        }
        String text = new String(s.array, startOffs + 1, len);

        if (text.equals(lastCompletionsAtText)) {
            if (CollectionUtils.isNotEmpty(lastParameterizedCompletionsAt)) {
                completions.addAll(lastParameterizedCompletionsAt);
            }
            return new ArrayList<Completion>(completions);
        }

        lastCompletionsAtText = text;
        lastParameterizedCompletionsAt = completionCache.getClassCompletions(tc, text);

        if (CollectionUtils.isNotEmpty(lastParameterizedCompletionsAt)) {
            completions.addAll(lastParameterizedCompletionsAt);
        }
        return new ArrayList<Completion>(completions);

    } catch (BadLocationException ble) {
        ble.printStackTrace(); // Never happens
    }

    lastCompletionsAtText = null;
    lastParameterizedCompletionsAt = null;

    return new ArrayList<Completion>(completions);
}

From source file:net.sf.jasperreports.engine.util.JEditorPaneHtmlMarkupProcessor.java

/**
 * /*ww w  . j  a  v  a2  s  .c  om*/
 */
protected void addElements(List<Element> elements, Element element) {
    //if(element instanceof LeafElement)
    {
        elements.add(element);
    }
    for (int i = 0; i < element.getElementCount(); i++) {
        Element child = element.getElement(i);
        addElements(elements, child);
    }
}

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("  ");
    }/*from   w w  w  .  ja  v  a 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: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 ww.  j a  v  a2  s.co m
                }
            }
            headingText = headingText.trim();
            return new Heading(headingText, level, elem.getStartOffset());
        }
    }
    return null;
}

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 a  v a  2 s .co 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: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);
    }//  ww  w.  j  a v  a  2  s .c  om
}

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);/*  www . j  a v  a2  s .c o  m*/
    }
    return children;
}

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. j  ava 2  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   www . j a  v  a 2s .  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;
}