Search in sources :

Example 1 with Paragraph

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

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

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

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

the class TextPane method setText.

public void setText(Reader textReader) throws IOException {
    Utils.checkNull(textReader, "Reader");
    int tabPosition = 0;
    int tabWidth = ((TextPane.Skin) getSkin()).getTabWidth();
    Document doc = new Document();
    StringBuilder text = new StringBuilder();
    int c = textReader.read();
    while (c != -1) {
        // Deal with the various forms of line endings:  CR only, LF only or CR,LF
        if (c == '\r') {
            int c2 = textReader.read();
            if (c2 == -1) {
                break;
            } else if (c2 == '\n') {
                // Only add the \n (the paragraph separator)
                c = c2;
            } else {
                // Change the paragraph separator to \n instead
                // but push back the last character read
                Paragraph paragraph = new Paragraph(text.toString());
                doc.add(paragraph);
                text.setLength(0);
                tabPosition = 0;
                c = c2;
                continue;
            }
        }
        if (c == '\n') {
            Paragraph paragraph = new Paragraph(text.toString());
            doc.add(paragraph);
            text.setLength(0);
            tabPosition = 0;
        } else if (c == '\t') {
            if (expandTabs) {
                int spaces = tabWidth - (tabPosition % tabWidth);
                for (int i = 0; i < spaces; i++) {
                    text.append(' ');
                }
                tabPosition += spaces;
            } else {
                text.append('\t');
            }
        } else {
            text.append((char) c);
            tabPosition++;
        }
        c = textReader.read();
    }
    if (text.length() != 0) {
        Paragraph paragraph = new Paragraph(text.toString());
        doc.add(paragraph);
    }
    setDocument(doc);
}
Also used : Document(org.apache.pivot.wtk.text.Document) Paragraph(org.apache.pivot.wtk.text.Paragraph)

Example 5 with Paragraph

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

the class TextPaneSkinParagraphView method childLayout.

@Override
protected void childLayout(int breakWidth) {
    // Break the views into multiple rows
    Paragraph paragraph = (Paragraph) getNode();
    rows = new ArrayList<>();
    int offset = 0;
    ParagraphChildLayouter layouter = new ParagraphChildLayouter();
    Row row = new Row();
    for (TextPaneSkinNodeView nodeView : this) {
        nodeView.layout(Math.max(breakWidth - (row.width + PARAGRAPH_TERMINATOR_WIDTH), 0));
        int nodeViewWidth = nodeView.getWidth();
        if (row.width + nodeViewWidth > breakWidth && row.width > 0) {
            // The view is too big to fit in the remaining space,
            // and it is not the only view in this row
            rows.add(row);
            layouter.endRow(row);
            row = new Row();
            row.width = 0;
        }
        // Add the view to the row
        RowSegment segment = new RowSegment(nodeView, offset);
        row.rowSegments.add(segment);
        layouter.startSegment(segment);
        offset += nodeView.getCharacterCount();
        row.width += nodeViewWidth;
        // If the view was split into multiple views, add them to their own rows
        nodeView = getNext(nodeView);
        while (nodeView != null) {
            rows.add(row);
            layouter.endRow(row);
            row = new Row();
            nodeView.layout(breakWidth);
            segment = new RowSegment(nodeView, offset);
            row.rowSegments.add(segment);
            layouter.startSegment(segment);
            offset += nodeView.getCharacterCount();
            row.width = nodeView.getWidth();
            nodeView = getNext(nodeView);
        }
    }
    // Add the last row
    if (row.rowSegments.getLength() > 0) {
        rows.add(row);
        layouter.endRow(row);
    }
    // calculate paragraph width and adjust for alignment
    layouter.end(paragraph, rows);
    // Recalculate terminator bounds
    FontRenderContext fontRenderContext = Platform.getFontRenderContext();
    LineMetrics lm = getTextPaneSkin().getFont().getLineMetrics("", 0, 0, fontRenderContext);
    int terminatorHeight = (int) Math.ceil(lm.getHeight());
    int terminatorY;
    if (getCharacterCount() == 1) {
        // The terminator is the only character in this paragraph
        terminatorY = 0;
    } else {
        terminatorY = layouter.rowY - terminatorHeight;
    }
    terminatorBounds = new Bounds(layouter.x, terminatorY, PARAGRAPH_TERMINATOR_WIDTH, terminatorHeight);
    // Ensure that the paragraph is visible even when empty
    layouter.paragraphWidth += terminatorBounds.width;
    int height = Math.max(layouter.rowY, terminatorBounds.height);
    setSize(layouter.paragraphWidth, height);
}
Also used : Bounds(org.apache.pivot.wtk.Bounds) FontRenderContext(java.awt.font.FontRenderContext) LineMetrics(java.awt.font.LineMetrics) Paragraph(org.apache.pivot.wtk.text.Paragraph)

Aggregations

Paragraph (org.apache.pivot.wtk.text.Paragraph)11 TextNode (org.apache.pivot.wtk.text.TextNode)8 ComponentNode (org.apache.pivot.wtk.text.ComponentNode)6 ImageNode (org.apache.pivot.wtk.text.ImageNode)6 Node (org.apache.pivot.wtk.text.Node)6 Element (org.apache.pivot.wtk.text.Element)5 FontRenderContext (java.awt.font.FontRenderContext)3 Document (org.apache.pivot.wtk.text.Document)3 LineMetrics (java.awt.font.LineMetrics)2 Bounds (org.apache.pivot.wtk.Bounds)2 Dimensions (org.apache.pivot.wtk.Dimensions)2 TextPane (org.apache.pivot.wtk.TextPane)2 TextSpan (org.apache.pivot.wtk.text.TextSpan)2 Font (java.awt.Font)1 LineBreakMeasurer (java.awt.font.LineBreakMeasurer)1 TextLayout (java.awt.font.TextLayout)1 IOException (java.io.IOException)1 AttributedCharacterIterator (java.text.AttributedCharacterIterator)1 ArrayList (org.apache.pivot.collections.ArrayList)1 AttributedStringCharacterIterator (org.apache.pivot.text.AttributedStringCharacterIterator)1