Search in sources :

Example 11 with Paragraph

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

the class TextPane method removeText.

/**
 * Remove the text from the document starting at the given position
 * for the given number of characters.
 * @param offset Starting location to remove text.
 * @param characterCount The number of characters to remove.
 */
public void removeText(int offset, int characterCount) {
    checkDocumentExists();
    if (offset >= 0 && offset < document.getCharacterCount()) {
        Node descendant = document.getDescendantAt(offset);
        // Used to be: if (selectionLength == 0 && ...
        if (characterCount <= 1 && descendant instanceof Paragraph) {
            // We are deleting a paragraph terminator
            Paragraph paragraph = (Paragraph) descendant;
            Element parent = paragraph.getParent();
            int index = parent.indexOf(paragraph);
            // Attempt to merge any successive content into the paragraph
            if (index < parent.getLength() - 1) {
                // TODO This won't always be a paragraph - we'll need to
                // find the next paragraph by walking the tree, then
                // remove any empty nodes
                Sequence<Node> removed = parent.remove(index + 1, 1);
                Paragraph nextParagraph = (Paragraph) removed.get(0);
                paragraph.insertRange(nextParagraph, paragraph.getCharacterCount() - 1);
            }
        } else {
            removeDocumentRange(offset, characterCount);
        }
    }
    // Ensure that the document remains editable
    if (document.getCharacterCount() == 0) {
        document.add(new Paragraph());
    }
    // Move the caret to the removal point
    if (offset >= 0) {
        setSelection(offset, 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) 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