Class Paragraph<PS,​SEG,​S>

  • Type Parameters:
    PS - The type of the paragraph style.
    SEG - The type of the content segments in the paragraph (e.g. String). Every paragraph, even an empty paragraph, must have at least one Paragraph object (even if that Paragraph object itself represents an empty segment).
    S - The type of the style of individual segments.

    public final class Paragraph<PS,​SEG,​S>
    extends java.lang.Object
    One paragraph in the document that can itself be styled and which contains a list of styled segments.

    It corresponds to a single line when the text is not wrapped or spans multiple lines when the text is wrapped. A Paragraph contains of a list of Paragraph objects which make up the individual segments of the Paragraph. By providing a specific segment object and an associated segment operations object, all required data and the necessary operations on this data for a single segment can be provided.

    For more complex requirements (for example, when both text and images shall be part of the document), a different segment type must be provided. One should use something like Either<String, Image> for their segment type. Note that Paragraph is an immutable class - to modify a Paragraph, a new Paragraph object must be created. Paragraph itself contains some methods which take care of this, such as concat(), which appends some Paragraph to the current one and returns a new Paragraph.

    • Constructor Detail

      • Paragraph

        public Paragraph​(PS paragraphStyle,
                         SegmentOps<SEG,​S> segmentOps,
                         java.util.List<StyledSegment<SEG,​S>> styledSegments)
        Creates a paragraph using a list of styled segments
      • Paragraph

        public Paragraph​(PS paragraphStyle,
                         SegmentOps<SEG,​S> segmentOps,
                         SEG segment,
                         S style)
        Creates a paragraph that has only one segment that has the same given style throughout.
      • Paragraph

        public Paragraph​(PS paragraphStyle,
                         SegmentOps<SEG,​S> segmentOps,
                         SEG segment,
                         StyleSpans<S> styles)
        Creates a paragraph that has only one segment but a number of different styles throughout that segment
      • Paragraph

        public Paragraph​(PS paragraphStyle,
                         SegmentOps<SEG,​S> segmentOps,
                         java.util.List<SEG> segments,
                         StyleSpans<S> styles)
        Creates a paragraph that has multiple segments with multiple styles throughout those segments
    • Method Detail

      • getStyledSegments

        public java.util.List<StyledSegment<SEG,​S>> getStyledSegments()
        Since the segments and styles in a paragraph are stored separate from another, combines these two collections into a single collection where each segment and its corresponding style are grouped into the same object. Essentially, returns List<Tuple2<Segment, Style>>.
      • getSegments

        public java.util.List<SEG> getSegments()
      • getParagraphStyle

        public PS getParagraphStyle()
      • length

        public int length()
      • charAt

        public char charAt​(int index)
      • substring

        public java.lang.String substring​(int from,
                                          int to)
      • substring

        public java.lang.String substring​(int from)
      • concat

        public Paragraph<PS,​SEG,​S> concat​(Paragraph<PS,​SEG,​S> p)
        Concatenates this paragraph with the given paragraph p. The paragraph style of the result will be that of this paragraph, unless this paragraph is empty and p is non-empty, in which case the paragraph style of the result will be that of p.
      • subSequence

        public Paragraph<PS,​SEG,​S> subSequence​(int start,
                                                           int end)
      • subSequence

        public Paragraph<PS,​SEG,​S> subSequence​(int start)
      • delete

        public Paragraph<PS,​SEG,​S> delete​(int start,
                                                      int end)
      • restyle

        public Paragraph<PS,​SEG,​S> restyle​(S style)
        Restyles every segment in the paragraph to have the given style. Note: because Paragraph is immutable, this method returns a new Paragraph. The current Paragraph is unchanged.
        Parameters:
        style - The new style for each segment in the paragraph.
        Returns:
        The new paragraph with the restyled segments.
      • restyle

        public Paragraph<PS,​SEG,​S> restyle​(int from,
                                                       int to,
                                                       S style)
      • setParagraphStyle

        public Paragraph<PS,​SEG,​S> setParagraphStyle​(PS paragraphStyle)
        Creates a new Paragraph which has the same contents as the current Paragraph, but the given paragraph style. Note that because Paragraph is immutable, a new Paragraph is returned. Despite the setX name, the current object is unchanged.
        Parameters:
        paragraphStyle - The new paragraph style
        Returns:
        A new paragraph with the same segment contents, but a new paragraph style.
      • getStyleOfChar

        public S getStyleOfChar​(int charIdx)
        Returns the style of character with the given index. If charIdx < 0, returns the style at the beginning of this paragraph. If charIdx >= this.length(), returns the style at the end of this paragraph.
      • getStyleAtPosition

        public S getStyleAtPosition​(int position)
        Returns the style at the given position. That is the style of the character immediately preceding position. If position is 0, then the style of the first character (index 0) in this paragraph is returned. If this paragraph is empty, then some style previously used in this paragraph is returned. If position > this.length(), then it is equivalent to position == this.length().

        In other words, getStyleAtPosition(p) is equivalent to getStyleOfChar(p-1).

      • getStyleRangeAtPosition

        public javafx.scene.control.IndexRange getStyleRangeAtPosition​(int position)
        Returns the range of homogeneous style that includes the given position. If position points to a boundary between two styled ranges, then the range preceding position is returned.
      • getStyleSpans

        public StyleSpans<S> getStyleSpans​(int from,
                                           int to)
      • getText

        public java.lang.String getText()
        Returns the plain text content of this paragraph, not including the line terminator.
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • equals

        public boolean equals​(java.lang.Object other)
        Two paragraphs are defined to be equal if they have the same style (as defined by PS.equals) and the same list of segments (as defined by SEG.equals).
        Overrides:
        equals in class java.lang.Object
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class java.lang.Object