Search in sources :

Example 1 with Document

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

the class TextPane method getText.

/**
 * Convenience method to get a portion of the document text into a single string.
 *
 * @param beginIndex The 0-based offset where to start retrieving text.
 * @param endIndex The ending offset + 1 of the text to retrieve.
 * @return The specified portion of the document text if there is any, or
 * {@code null} if there is no document.
 */
public String getText(int beginIndex, int endIndex) {
    Utils.checkTwoIndexBounds(beginIndex, endIndex, 0, getCharacterCount());
    int count = endIndex - beginIndex;
    if (count == 0) {
        return "";
    }
    Document doc = getDocument();
    if (doc != null) {
        StringBuilder text = new StringBuilder(count);
        addToText(text, doc, new Span(beginIndex, endIndex - 1));
        return text.toString();
    }
    return null;
}
Also used : Document(org.apache.pivot.wtk.text.Document) CharSpan(org.apache.pivot.text.CharSpan) TextSpan(org.apache.pivot.wtk.text.TextSpan)

Example 2 with Document

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

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

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

the class TextPaneSkin method documentChanged.

// Text pane events
@Override
public void documentChanged(TextPane textPane, Document previousDocument) {
    if (documentView != null) {
        documentView.detach();
        documentView = null;
    }
    Document document = textPane.getDocument();
    if (document != null) {
        documentView = (TextPaneSkinDocumentView) TextPaneSkinNodeView.createNodeView(this, document);
        documentView.attach();
    }
    invalidateComponent();
}
Also used : Document(org.apache.pivot.wtk.text.Document)

Example 5 with Document

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

the class TextPaneSkin method install.

@Override
public void install(Component component) {
    super.install(component);
    TextPane textPane = (TextPane) component;
    textPane.getTextPaneListeners().add(this);
    textPane.getTextPaneSelectionListeners().add(this);
    textPane.setCursor(Cursor.TEXT);
    Document document = textPane.getDocument();
    if (document != null) {
        documentView = (TextPaneSkinDocumentView) TextPaneSkinNodeView.createNodeView(this, document);
        documentView.attach();
        updateSelection();
    }
}
Also used : TextPane(org.apache.pivot.wtk.TextPane) Document(org.apache.pivot.wtk.text.Document)

Aggregations

Document (org.apache.pivot.wtk.text.Document)11 TextPane (org.apache.pivot.wtk.TextPane)5 CharSpan (org.apache.pivot.text.CharSpan)3 Paragraph (org.apache.pivot.wtk.text.Paragraph)3 ComponentNode (org.apache.pivot.wtk.text.ComponentNode)2 ImageNode (org.apache.pivot.wtk.text.ImageNode)2 TextNode (org.apache.pivot.wtk.text.TextNode)2 TextSpan (org.apache.pivot.wtk.text.TextSpan)2 IOException (java.io.IOException)1 Bounds (org.apache.pivot.wtk.Bounds)1 Frame (org.apache.pivot.wtk.Frame)1 HyperlinkButton (org.apache.pivot.wtk.HyperlinkButton)1 Node (org.apache.pivot.wtk.text.Node)1