Search in sources :

Example 1 with Node

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

the class TextPane method paste.

public void paste() {
    if (document == null) {
        setDocument(new Document());
    }
    Manifest clipboardContent = Clipboard.getContent();
    if (clipboardContent != null && clipboardContent.containsText()) {
        // Paste the string representation of the content
        String text = null;
        try {
            // Replace \r\n with just \n and plain \r with \n instead
            // so we can deal with lines uniformly below.
            text = clipboardContent.getText().replace("\r\n", "\n").replace("\r", "\n");
        } catch (IOException exception) {
        // No-op
        }
        if (text != null && text.length() > 0) {
            // Insert the clipboard contents
            int n = 0;
            int start = selectionStart;
            int tabWidth = ((TextPane.Skin) getSkin()).getTabWidth();
            Node currentNode = document.getDescendantAt(start);
            while (!(currentNode instanceof Paragraph)) {
                currentNode = currentNode.getParent();
            }
            int paragraphStartOffset = start - currentNode.getDocumentOffset();
            bulkOperation = true;
            // If there is only a line fragment here, then just insert it
            int eolIndex = text.indexOf('\n');
            if (eolIndex < 0) {
                if (expandTabs) {
                    n = insertWithTabSubstitution(text, start, paragraphStartOffset, tabWidth);
                } else {
                    insertText(text, start);
                    n = text.length();
                }
            } else {
                int textOffset = 0;
                // Insert each line into place, with a new paragraph following
                while (eolIndex >= 0) {
                    String fragment = text.substring(textOffset, eolIndex);
                    int len;
                    if (expandTabs) {
                        len = insertWithTabSubstitution(fragment, start, paragraphStartOffset, tabWidth);
                    } else {
                        insertText(fragment, start);
                        len = fragment.length();
                    }
                    insertParagraph();
                    n += len + 1;
                    start += len + 1;
                    paragraphStartOffset = 0;
                    textOffset = eolIndex + 1;
                    eolIndex = text.indexOf('\n', textOffset);
                }
                // Now deal with any leftover at end of string
                if (textOffset < text.length()) {
                    String lastFragment = text.substring(textOffset);
                    if (expandTabs) {
                        n += insertWithTabSubstitution(lastFragment, start, paragraphStartOffset, tabWidth);
                    } else {
                        insertText(lastFragment, start);
                        n += lastFragment.length();
                    }
                }
            }
            bulkOperation = false;
            textPaneCharacterListeners.charactersInserted(this, start, n);
        }
    }
}
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) IOException(java.io.IOException) Document(org.apache.pivot.wtk.text.Document) Paragraph(org.apache.pivot.wtk.text.Paragraph)

Example 2 with Node

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

the class TextPane method insertParagraph.

public void insertParagraph() {
    checkDocumentExists();
    if (selectionLength > 0) {
        removeDocumentRange(selectionStart, selectionLength);
    }
    // Walk up the tree until we find a paragraph
    Node descendant = document.getDescendantAt(selectionStart);
    while (!(descendant instanceof Paragraph)) {
        descendant = descendant.getParent();
    }
    // Split the paragraph at the insertion point
    Paragraph leadingSegment = (Paragraph) descendant;
    int offset = selectionStart - leadingSegment.getDocumentOffset();
    int characterCount = leadingSegment.getCharacterCount() - offset;
    Paragraph trailingSegment = (Paragraph) leadingSegment.removeRange(offset, characterCount);
    Element parent = leadingSegment.getParent();
    int index = parent.indexOf(leadingSegment);
    parent.insert(trailingSegment, 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) Paragraph(org.apache.pivot.wtk.text.Paragraph)

Example 3 with Node

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

the class TextPane method addToText.

/**
 * Add the text from the given element (and its children) to the given buffer,
 * respecting the range of characters to be included.
 *
 * @param text The buffer we're building.
 * @param element The current element in the document.
 * @param includeSpan The range of text to be included (in document-relative
 * coordinates).
 */
private void addToText(StringBuilder text, Element element, Span includeSpan) {
    Span elementSpan = element.getDocumentSpan();
    Span elementIntersection = elementSpan.intersect(includeSpan);
    if (elementIntersection != null) {
        for (Node node : element) {
            if (node instanceof Element) {
                addToText(text, (Element) node, includeSpan);
            } else {
                Span nodeSpan = node.getDocumentSpan();
                Span nodeIntersection = nodeSpan.intersect(includeSpan);
                if (nodeIntersection != null) {
                    Span currentSpan = nodeIntersection.offset(-nodeSpan.start);
                    if (node instanceof TextNode) {
                        text.append(((TextNode) node).getCharacters(currentSpan));
                    } else if (node instanceof ComponentNode) {
                        text.append(((ComponentNode) node).getCharacters(currentSpan));
                    }
                // TODO: anything more that could/should be handled?
                // lists for instance???
                }
            }
        }
        if (element instanceof Paragraph && elementIntersection.end == elementSpan.end) {
            // TODO: unclear if this is included in the character count for a paragraph or not
            // or what that means for the intersection range above
            text.append('\n');
        }
    }
}
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) CharSpan(org.apache.pivot.text.CharSpan) TextSpan(org.apache.pivot.wtk.text.TextSpan) ComponentNode(org.apache.pivot.wtk.text.ComponentNode) Paragraph(org.apache.pivot.wtk.text.Paragraph)

Example 4 with Node

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

the class TextPaneSkinElementView method attach.

@Override
protected void attach() {
    super.attach();
    Element element = (Element) getNode();
    element.getElementListeners().add(this);
    // Attach child node views
    for (Node node : element) {
        add(createNodeView(getTextPaneSkin(), node));
    }
}
Also used : Element(org.apache.pivot.wtk.text.Element) Node(org.apache.pivot.wtk.text.Node)

Example 5 with Node

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

the class TextPaneDemo method applyStyle.

private void applyStyle(Document document, Span selectionSpan, StyleApplicator styleApplicator) {
    // I can't apply the styles while iterating over the tree, because I
    // need to update the tree.
    // So first collect a list of all the nodes in the tree.
    List<Node> nodeList = new ArrayList<>();
    collectNodes(document, nodeList);
    final int selectionStart = textPane.getSelectionStart();
    final int selectionLength = textPane.getSelectionLength();
    for (Node node : nodeList) {
        if (node instanceof TextSpan) {
            TextSpan span = (TextSpan) node;
            int documentOffset = node.getDocumentOffset();
            int characterCount = node.getCharacterCount();
            Span textSpan = new Span(documentOffset, documentOffset + characterCount - 1);
            if (selectionSpan.intersects(textSpan)) {
                applyStyleToSpanNode(selectionSpan, styleApplicator, span, characterCount, textSpan);
            }
        }
        if (node instanceof org.apache.pivot.wtk.text.TextNode) {
            org.apache.pivot.wtk.text.TextNode textNode = (org.apache.pivot.wtk.text.TextNode) node;
            int documentOffset = node.getDocumentOffset();
            int characterCount = node.getCharacterCount();
            Span textSpan = new Span(documentOffset, documentOffset + characterCount - 1);
            if (selectionSpan.intersects(textSpan)) {
                applyStyleToTextNode(selectionSpan, styleApplicator, textNode, characterCount, textSpan);
            }
        }
    }
    // maintain the selected range
    textPane.setSelection(selectionStart, selectionLength);
}
Also used : TextNode(org.apache.pivot.wtk.text.TextNode) Node(org.apache.pivot.wtk.text.Node) TextNode(org.apache.pivot.wtk.text.TextNode) ArrayList(org.apache.pivot.collections.ArrayList) TextNode(org.apache.pivot.wtk.text.TextNode) Span(org.apache.pivot.wtk.Span) TextSpan(org.apache.pivot.wtk.text.TextSpan) TextSpan(org.apache.pivot.wtk.text.TextSpan) DesktopApplicationContext(org.apache.pivot.wtk.DesktopApplicationContext) ApplicationContext(org.apache.pivot.wtk.ApplicationContext)

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