Class Paragraph

  • All Implemented Interfaces:
    java.lang.Iterable<Node>, Sequence<Node>

    public class Paragraph
    extends Block
    Element representing a paragraph.

    TODO Add indent property.

    Every paragraph has a trailing newline ('\n') character, which is not actually present in any of its child text nodes. Therefore all the logic in here must account for this extra phantom character.

    • Constructor Detail

      • Paragraph

        public Paragraph()
      • Paragraph

        public Paragraph​(java.lang.String text)
      • Paragraph

        public Paragraph​(Paragraph paragraph,
                         boolean recursive)
    • Method Detail

      • removeRange

        public Node removeRange​(int offset,
                                int characterCount)
        Remove a range of characters from this paragraph.
        Overrides:
        removeRange in class Element
        Parameters:
        offset - Offset into this paragraph.
        characterCount - How many characters to remove.
        Returns:
        A new Node containing the removed characters.
      • getRange

        public Paragraph getRange​(int offset,
                                  int characterCount)
        Get a new Paragraph containing the given range of characters from this paragraph.
        Overrides:
        getRange in class Element
        Parameters:
        offset - Offset into this paragraph.
        characterCount - How many characters to get.
        Returns:
        New Paragraph with these characters.
      • getCharacterAt

        public char getCharacterAt​(int offset)
        Retrieve the character at the given offset in this paragraph.
        Overrides:
        getCharacterAt in class Element
        Parameters:
        offset - Offset into this paragraph.
        Returns:
        The character at that position.
      • getCharacterCount

        public int getCharacterCount()
        Overrides:
        getCharacterCount in class Element
        Returns:
        The count of characters in this paragraph, which is one more than the number of characters in all child nodes (because of the trailing newline implicitly present).
      • add

        public int add​(java.lang.String text)
        Add a piece of text to this paragraph (at the end, but before the trailing newline). If the paragraph had no text previously then a new TextNode child is created containing this text. Otherwise, if the last child node contains text, then just append this new text to the existing node.
        Parameters:
        text - The text to add.
        Returns:
        The index of the (new or existing) text node where the text was added.
      • insert

        public void insert​(Node node,
                           int index)
        Description copied from interface: Sequence
        Inserts an item into the sequence at a specific index.
        Specified by:
        insert in interface Sequence<Node>
        Overrides:
        insert in class Element
        Parameters:
        node - The item to be added to the sequence.
        index - The index at which the item should be inserted. Must be a value between 0 and getLength().
      • getPathAt

        public Sequence<java.lang.Integer> getPathAt​(int offset)
        Get the path through our descendants for the given offset into this paragraph.
        Overrides:
        getPathAt in class Element
        Parameters:
        offset - Offset into this paragraph.
        Returns:
        The path to that offset, which will be empty for the trailing newline character.
      • getDescendantAt

        public Node getDescendantAt​(int offset)
        Get the descendant node at the given offset. If the offset is the last character in this paragraph (namely the phantom newline) then return ourselves, otherwise the normal descendant at that offset.
        Overrides:
        getDescendantAt in class Element
        Parameters:
        offset - Offset into this paragraph.
        Returns:
        The descendant node at that offset.
      • duplicate

        public Paragraph duplicate​(boolean recursive)
        Description copied from class: Node
        Creates a copy of this node.
        Specified by:
        duplicate in class Element
        Parameters:
        recursive - Whether to duplicate the children also.
        Returns:
        A copy of the current node.