Search in sources :

Example 6 with Element

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

the class TextPaneDemo method applyStyleToTextNode.

private static void applyStyleToTextNode(Span selectionSpan, StyleApplicator styleApplicator, org.apache.pivot.wtk.text.TextNode textNode, int characterCount, Span textSpan) {
    if (selectionSpan.contains(textSpan)) {
        // if the text-node is contained wholly inside the selection, remove
        // the text-node, replace it with a Span, and apply the style
        Element parent = textNode.getParent();
        TextSpan newSpanNode = new TextSpan();
        newSpanNode.add(new TextNode(textNode.getText()));
        styleApplicator.apply(newSpanNode);
        int index = parent.remove(textNode);
        parent.insert(newSpanNode, index);
    } else if (selectionSpan.start <= textSpan.start) {
        // if the selection covers the first part of the text-node, split
        // off the first part of the text-node, and apply the style to it
        int intersectionLength = selectionSpan.end - textSpan.start + 1;
        String part1 = textNode.getSubstring(0, intersectionLength);
        String part2 = textNode.getSubstring(intersectionLength, characterCount);
        TextSpan newSpanNode = new TextSpan();
        newSpanNode.add(new TextNode(part1));
        styleApplicator.apply(newSpanNode);
        Element parent = textNode.getParent();
        int index = parent.remove(textNode);
        parent.insert(newSpanNode, index);
        parent.insert(new TextNode(part2), index + 1);
    } else if (selectionSpan.end >= textSpan.end) {
        // if the selection covers the last part of the text-node, split off
        // the last part of the text-node, and apply the style to it
        int intersectionStart = selectionSpan.start - textSpan.start;
        String part1 = textNode.getSubstring(0, intersectionStart);
        String part2 = textNode.getSubstring(intersectionStart, characterCount);
        TextSpan newSpanNode = new TextSpan();
        newSpanNode.add(new TextNode(part2));
        styleApplicator.apply(newSpanNode);
        Element parent = textNode.getParent();
        int index = parent.remove(textNode);
        parent.insert(new TextNode(part1), index);
        parent.insert(newSpanNode, index + 1);
    } else {
        // if the selection covers an internal part of the text-node, split the
        // text-node into 3 parts, and apply the style to the second part
        int part2Start = selectionSpan.start - textSpan.start;
        int part2End = selectionSpan.end - textSpan.start + 1;
        String part1 = textNode.getSubstring(0, part2Start);
        String part2 = textNode.getSubstring(part2Start, part2End);
        String part3 = textNode.getSubstring(part2End, characterCount);
        TextSpan newSpanNode = new TextSpan();
        newSpanNode.add(new TextNode(part2));
        styleApplicator.apply(newSpanNode);
        Element parent = textNode.getParent();
        int index = parent.remove(textNode);
        parent.insert(new TextNode(part1), index);
        parent.insert(newSpanNode, index + 1);
        parent.insert(new TextNode(part3), index + 2);
    }
}
Also used : TextSpan(org.apache.pivot.wtk.text.TextSpan) Element(org.apache.pivot.wtk.text.Element) TextNode(org.apache.pivot.wtk.text.TextNode)

Example 7 with Element

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

the class TextPaneDemo method collectNodes.

private void collectNodes(org.apache.pivot.wtk.text.Node node, List<Node> nodeList) {
    // don't worry about the text-nodes that are children of Span nodes.
    if (node instanceof TextSpan) {
        return;
    }
    if (node instanceof org.apache.pivot.wtk.text.Element) {
        Element element = (Element) node;
        for (Node child : element) {
            nodeList.add(child);
            collectNodes(child, nodeList);
        }
    }
}
Also used : TextSpan(org.apache.pivot.wtk.text.TextSpan) Element(org.apache.pivot.wtk.text.Element) Node(org.apache.pivot.wtk.text.Node) TextNode(org.apache.pivot.wtk.text.TextNode)

Example 8 with Element

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

the class TextPaneDemo method dumpDocumentNode.

/**
 * debugging tools
 */
private void dumpDocumentNode(Node node, PrintStream printStream, final int indent) {
    for (int i = 0; i < indent; i++) {
        printStream.append("  ");
    }
    printStream.append("<" + node.getClass().getSimpleName() + ">");
    if (node instanceof TextNode) {
        TextNode textNode = (TextNode) node;
        String text = textNode.getText();
        printStream.append(text);
        printStream.append("</" + node.getClass().getSimpleName() + ">");
        printStream.println();
    } else {
        printStream.println();
        if (node instanceof Element) {
            Element element = (Element) node;
            for (Node childNode : element) {
                dumpDocumentNode(childNode, printStream, indent + 1);
            }
        } else {
            String text = node.toString();
            printStream.append(text);
        }
        for (int i = 0; i < indent; i++) {
            printStream.append("  ");
        }
        printStream.append("</" + node.getClass().getSimpleName() + ">");
        printStream.println();
    }
}
Also used : Element(org.apache.pivot.wtk.text.Element) Node(org.apache.pivot.wtk.text.Node) TextNode(org.apache.pivot.wtk.text.TextNode) TextNode(org.apache.pivot.wtk.text.TextNode)

Example 9 with Element

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

the class TextPaneDemo method applyStyleToSpanNode.

private static void applyStyleToSpanNode(Span selectionSpan, StyleApplicator styleApplicator, TextSpan spanNode, int characterCount, Span textSpan) {
    if (selectionSpan.contains(textSpan)) {
        // if the span-node is contained wholly inside the
        // selection, apply the style
        styleApplicator.apply(spanNode);
    } else if (selectionSpan.start <= textSpan.start) {
        // if the selection covers the first part of the span-node, split
        // off the first part of the span-node, and apply the style to it
        int intersectionLength = selectionSpan.end - textSpan.start + 1;
        TextSpan node1 = spanNode.getRange(0, intersectionLength);
        styleApplicator.apply(node1);
        Node node2 = spanNode.getRange(intersectionLength, characterCount - intersectionLength);
        Element parent = spanNode.getParent();
        int index = parent.remove(spanNode);
        parent.insert(node1, index);
        parent.insert(node2, index + 1);
    } else if (selectionSpan.end >= textSpan.end) {
        // if the selection covers the last part of the span-node, split off
        // the last part of the span-node, and apply the style to it
        int intersectionStart = selectionSpan.start - textSpan.start;
        TextSpan part1 = spanNode.getRange(0, intersectionStart);
        TextSpan part2 = spanNode.getRange(intersectionStart, characterCount - intersectionStart);
        styleApplicator.apply(part2);
        Element parent = spanNode.getParent();
        int index = parent.remove(spanNode);
        parent.insert(part1, index);
        parent.insert(part2, index + 1);
    } else {
        // if the selection covers an internal part of the span-node, split the
        // span-node into 3 parts, and apply the style to the second part
        int part2Start = selectionSpan.start - textSpan.start;
        int part2End = selectionSpan.end - textSpan.start + 1;
        TextSpan part1 = spanNode.getRange(0, part2Start);
        TextSpan part2 = spanNode.getRange(part2Start, part2End - part2Start);
        TextSpan part3 = spanNode.getRange(part2End, characterCount - part2End);
        styleApplicator.apply(part2);
        Element parent = spanNode.getParent();
        int index = parent.remove(spanNode);
        parent.insert(part1, index);
        parent.insert(part2, index + 1);
        parent.insert(part3, index + 2);
    }
}
Also used : TextSpan(org.apache.pivot.wtk.text.TextSpan) Node(org.apache.pivot.wtk.text.Node) TextNode(org.apache.pivot.wtk.text.TextNode) Element(org.apache.pivot.wtk.text.Element)

Example 10 with Element

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

the class TextPane method insertText.

public void insertText(String text, int index) {
    Utils.checkNull(text, "text");
    checkDocumentNull();
    if (selectionLength > 0) {
        delete(false);
    }
    if (document.getCharacterCount() == 0) {
        // the document is currently empty
        document.insert(new Paragraph(text), 0);
    } else {
        Node descendant = document.getDescendantAt(index);
        int offset = index - descendant.getDocumentOffset();
        if (descendant instanceof TextNode) {
            // The caret is positioned within an existing text node
            TextNode textNode = (TextNode) descendant;
            textNode.insertText(text, offset);
        } else if (descendant instanceof Paragraph) {
            // The caret is positioned on the paragraph terminator
            // so get to the bottom rightmost descendant and add there
            Paragraph paragraph = (Paragraph) descendant;
            Node node = getRightmostDescendant(paragraph);
            if (node instanceof TextNode) {
                // Insert the text into the existing node
                TextNode textNode = (TextNode) node;
                textNode.insertText(text, index - textNode.getDocumentOffset());
            } else if (node instanceof Element) {
                // Append a new text node
                Element element = (Element) node;
                element.add(new TextNode(text));
            } else {
                // The paragraph is currently empty
                paragraph.add(new TextNode(text));
            }
        } else {
            // The caret is positioned on a non-text character node; insert
            // the text into the descendant's parent
            Element parent = descendant.getParent();
            int elemIndex = parent.indexOf(descendant);
            parent.insert(new TextNode(text), elemIndex);
        }
    }
    // Set the selection start to the character following the insertion
    setSelection(index + text.length(), 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) TextNode(org.apache.pivot.wtk.text.TextNode) Paragraph(org.apache.pivot.wtk.text.Paragraph)

Aggregations

Element (org.apache.pivot.wtk.text.Element)14 TextNode (org.apache.pivot.wtk.text.TextNode)12 Node (org.apache.pivot.wtk.text.Node)11 ComponentNode (org.apache.pivot.wtk.text.ComponentNode)7 ImageNode (org.apache.pivot.wtk.text.ImageNode)7 Paragraph (org.apache.pivot.wtk.text.Paragraph)5 TextSpan (org.apache.pivot.wtk.text.TextSpan)5 Block (org.apache.pivot.wtk.text.Block)2 Font (java.awt.Font)1 FontRenderContext (java.awt.font.FontRenderContext)1 LineBreakMeasurer (java.awt.font.LineBreakMeasurer)1 TextLayout (java.awt.font.TextLayout)1 AttributedCharacterIterator (java.text.AttributedCharacterIterator)1 AttributedStringCharacterIterator (org.apache.pivot.text.AttributedStringCharacterIterator)1 CharSpan (org.apache.pivot.text.CharSpan)1 CompositeIterator (org.apache.pivot.text.CompositeIterator)1 Dimensions (org.apache.pivot.wtk.Dimensions)1 Span (org.apache.pivot.wtk.Span)1 TextPane (org.apache.pivot.wtk.TextPane)1