Search in sources :

Example 6 with TextNode

use of org.apache.pivot.wtk.text.TextNode 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 TextNode

use of org.apache.pivot.wtk.text.TextNode 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 8 with TextNode

use of org.apache.pivot.wtk.text.TextNode 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)

Example 9 with TextNode

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

the class TextPaneSkinTextNodeView method detach.

@Override
protected void detach() {
    super.detach();
    TextNode textNode = (TextNode) getNode();
    textNode.getTextNodeListeners().remove(this);
}
Also used : TextNode(org.apache.pivot.wtk.text.TextNode)

Example 10 with TextNode

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

the class TextPaneSkinTextNodeView method attach.

@Override
protected void attach() {
    super.attach();
    TextNode textNode = (TextNode) getNode();
    textNode.getTextNodeListeners().add(this);
}
Also used : TextNode(org.apache.pivot.wtk.text.TextNode)

Aggregations

TextNode (org.apache.pivot.wtk.text.TextNode)11 Element (org.apache.pivot.wtk.text.Element)5 Node (org.apache.pivot.wtk.text.Node)4 Paragraph (org.apache.pivot.wtk.text.Paragraph)4 TextSpan (org.apache.pivot.wtk.text.TextSpan)4 ComponentNode (org.apache.pivot.wtk.text.ComponentNode)3 ImageNode (org.apache.pivot.wtk.text.ImageNode)3 Font (java.awt.Font)2 FontRenderContext (java.awt.font.FontRenderContext)2 LineBreakMeasurer (java.awt.font.LineBreakMeasurer)2 TextLayout (java.awt.font.TextLayout)2 AttributedCharacterIterator (java.text.AttributedCharacterIterator)2 AttributedStringCharacterIterator (org.apache.pivot.text.AttributedStringCharacterIterator)2 Dimensions (org.apache.pivot.wtk.Dimensions)2 Span (org.apache.pivot.wtk.Span)2 TextPane (org.apache.pivot.wtk.TextPane)2 ArrayList (org.apache.pivot.collections.ArrayList)1 CharSpan (org.apache.pivot.text.CharSpan)1 CompositeIterator (org.apache.pivot.text.CompositeIterator)1 ApplicationContext (org.apache.pivot.wtk.ApplicationContext)1