Search in sources :

Example 11 with Node

use of org.apache.pivot.wtk.text.Node in project pivot by apache.

the class TextPane method removeDocumentRange.

/**
 * Helper function to remove a range of characters from the document and
 * notify the listeners just once (instead of once per node).
 *
 * @param start The starting location (document offset) of the characters
 * to be removed.
 * @param count The number of characters to remove from that location.
 * @return The document node where the characters were removed.
 */
private Node removeDocumentRange(int start, int count) {
    bulkOperation = true;
    Node node = document.removeRange(start, count);
    bulkOperation = false;
    textPaneCharacterListeners.charactersRemoved(this, start, count);
    return node;
}
Also used : ImageNode(org.apache.pivot.wtk.text.ImageNode) ComponentNode(org.apache.pivot.wtk.text.ComponentNode) Node(org.apache.pivot.wtk.text.Node) TextNode(org.apache.pivot.wtk.text.TextNode)

Example 12 with Node

use of org.apache.pivot.wtk.text.Node in project pivot by apache.

the class TextPane method removeText.

/**
 * Remove the text from the document starting at the given position
 * for the given number of characters.
 * @param offset Starting location to remove text.
 * @param characterCount The number of characters to remove.
 */
public void removeText(int offset, int characterCount) {
    checkDocumentExists();
    if (offset >= 0 && offset < document.getCharacterCount()) {
        Node descendant = document.getDescendantAt(offset);
        // Used to be: if (selectionLength == 0 && ...
        if (characterCount <= 1 && descendant instanceof Paragraph) {
            // We are deleting a paragraph terminator
            Paragraph paragraph = (Paragraph) descendant;
            Element parent = paragraph.getParent();
            int index = parent.indexOf(paragraph);
            // Attempt to merge any successive content into the paragraph
            if (index < parent.getLength() - 1) {
                // TODO This won't always be a paragraph - we'll need to
                // find the next paragraph by walking the tree, then
                // remove any empty nodes
                Sequence<Node> removed = parent.remove(index + 1, 1);
                Paragraph nextParagraph = (Paragraph) removed.get(0);
                paragraph.insertRange(nextParagraph, paragraph.getCharacterCount() - 1);
            }
        } else {
            removeDocumentRange(offset, characterCount);
        }
    }
    // Ensure that the document remains editable
    if (document.getCharacterCount() == 0) {
        document.add(new Paragraph());
    }
    // Move the caret to the removal point
    if (offset >= 0) {
        setSelection(offset, 0);
    }
}
Also used : ImageNode(org.apache.pivot.wtk.text.ImageNode) ComponentNode(org.apache.pivot.wtk.text.ComponentNode) Node(org.apache.pivot.wtk.text.Node) TextNode(org.apache.pivot.wtk.text.TextNode) Element(org.apache.pivot.wtk.text.Element) Paragraph(org.apache.pivot.wtk.text.Paragraph)

Example 13 with Node

use of org.apache.pivot.wtk.text.Node in project pivot by apache.

the class TextPane method insertComponent.

public void insertComponent(Component component) {
    Utils.checkNull(component, "component");
    checkDocumentExists();
    if (selectionLength > 0) {
        removeDocumentRange(selectionStart, selectionLength);
    }
    // If the insertion is at the end of the document, then just add
    if (selectionStart >= document.getCharacterCount() - 1) {
        document.add(new ComponentNode(component));
    } else {
        // Walk up the tree until we find a block
        Node descendant = document.getDescendantAt(selectionStart);
        while (!(descendant instanceof Block)) {
            descendant = descendant.getParent();
        }
        Element parent = descendant.getParent();
        if (parent != null) {
            int index = parent.indexOf(descendant);
            parent.insert(new ComponentNode(component), index + 1);
        }
    }
    // Set the selection start to the character following the insertion
    setSelection(selectionStart + 1, selectionLength);
}
Also used : ImageNode(org.apache.pivot.wtk.text.ImageNode) ComponentNode(org.apache.pivot.wtk.text.ComponentNode) Node(org.apache.pivot.wtk.text.Node) TextNode(org.apache.pivot.wtk.text.TextNode) Element(org.apache.pivot.wtk.text.Element) Block(org.apache.pivot.wtk.text.Block) ComponentNode(org.apache.pivot.wtk.text.ComponentNode)

Example 14 with Node

use of org.apache.pivot.wtk.text.Node in project pivot by apache.

the class TextPane method dumpNode.

public static void dumpNode(String msg, Node node, int indent) {
    if (msg != null && !msg.isEmpty()) {
        System.out.println(msg + ":");
    }
    String indenting = StringUtils.fromNChars(' ', indent * 2);
    System.out.format(FORMAT, indenting, node.getClass().getSimpleName(), node.getDocumentOffset(), node.getCharacterCount());
    if (node instanceof Element) {
        for (Node n : (Element) node) {
            dumpNode("", n, indent + 1);
        }
    }
}
Also used : Element(org.apache.pivot.wtk.text.Element) ImageNode(org.apache.pivot.wtk.text.ImageNode) ComponentNode(org.apache.pivot.wtk.text.ComponentNode) Node(org.apache.pivot.wtk.text.Node) TextNode(org.apache.pivot.wtk.text.TextNode)

Example 15 with Node

use of org.apache.pivot.wtk.text.Node in project pivot by apache.

the class TextPane method insertImage.

public void insertImage(Image image) {
    Utils.checkNull(image, "image");
    checkDocumentExists();
    if (selectionLength > 0) {
        removeDocumentRange(selectionStart, selectionLength);
    }
    // If the insertion is at the end of the document, then just add
    if (selectionStart >= document.getCharacterCount() - 1) {
        document.add(new ImageNode(image));
    } else {
        // Walk up the tree until we find a block
        Node descendant = document.getDescendantAt(selectionStart);
        while (!(descendant instanceof Block)) {
            descendant = descendant.getParent();
        }
        Element parent = descendant.getParent();
        if (parent != null) {
            int index = parent.indexOf(descendant);
            parent.insert(new ImageNode(image), index + 1);
        }
    }
    // Set the selection start to the character following the insertion
    setSelection(selectionStart + 1, selectionLength);
}
Also used : ImageNode(org.apache.pivot.wtk.text.ImageNode) ComponentNode(org.apache.pivot.wtk.text.ComponentNode) Node(org.apache.pivot.wtk.text.Node) TextNode(org.apache.pivot.wtk.text.TextNode) Element(org.apache.pivot.wtk.text.Element) Block(org.apache.pivot.wtk.text.Block) ImageNode(org.apache.pivot.wtk.text.ImageNode)

Aggregations

Node (org.apache.pivot.wtk.text.Node)15 TextNode (org.apache.pivot.wtk.text.TextNode)14 Element (org.apache.pivot.wtk.text.Element)11 ComponentNode (org.apache.pivot.wtk.text.ComponentNode)9 ImageNode (org.apache.pivot.wtk.text.ImageNode)9 Paragraph (org.apache.pivot.wtk.text.Paragraph)6 TextSpan (org.apache.pivot.wtk.text.TextSpan)4 Block (org.apache.pivot.wtk.text.Block)2 IOException (java.io.IOException)1 ArrayList (org.apache.pivot.collections.ArrayList)1 CharSpan (org.apache.pivot.text.CharSpan)1 ApplicationContext (org.apache.pivot.wtk.ApplicationContext)1 DesktopApplicationContext (org.apache.pivot.wtk.DesktopApplicationContext)1 Span (org.apache.pivot.wtk.Span)1 Document (org.apache.pivot.wtk.text.Document)1