Class Paragraph<PS,SEG,S>
- java.lang.Object
-
- org.fxmisc.richtext.model.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 oneParagraph
object (even if thatParagraph
object itself represents an empty segment).S
- The type of the style of individual segments.
public final class Paragraph<PS,SEG,S> extends 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 associatedsegment 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 Summary
Constructors Constructor Description Paragraph(PS paragraphStyle, SegmentOps<SEG,S> segmentOps, List<StyledSegment<SEG,S>> styledSegments)
Creates a paragraph using a list of styled segmentsParagraph(PS paragraphStyle, SegmentOps<SEG,S> segmentOps, List<SEG> segments, StyleSpans<S> styles)
Creates a paragraph that has multiple segments with multiple styles throughout those segmentsParagraph(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 segmentParagraph(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.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description char
charAt(int index)
Paragraph<PS,SEG,S>
concat(Paragraph<PS,SEG,S> p)
Concatenates this paragraph with the given paragraphp
.Paragraph<PS,SEG,S>
delete(int start, int end)
boolean
equals(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).PS
getParagraphStyle()
List<SEG>
getSegments()
S
getStyleAtPosition(int position)
Returns the style at the given position.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.S
getStyleOfChar(int charIdx)
Returns the style of character with the given index.IndexRange
getStyleRangeAtPosition(int position)
Returns the range of homogeneous style that includes the given position.StyleSpans<S>
getStyleSpans()
StyleSpans<S>
getStyleSpans(int from, int to)
String
getText()
Returns the plain text content of this paragraph, not including the line terminator.int
hashCode()
int
length()
Paragraph<PS,SEG,S>
restyle(int from, int to, S style)
Paragraph<PS,SEG,S>
restyle(int from, StyleSpans<? extends S> styleSpans)
Paragraph<PS,SEG,S>
restyle(S style)
Restyles every segment in the paragraph to have the given style.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.Paragraph<PS,SEG,S>
subSequence(int start)
Paragraph<PS,SEG,S>
subSequence(int start, int end)
String
substring(int from)
String
substring(int from, int to)
String
toString()
Paragraph<PS,SEG,S>
trim(int length)
-
-
-
Constructor Detail
-
Paragraph
public Paragraph(PS paragraphStyle, SegmentOps<SEG,S> segmentOps, 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, List<SEG> segments, StyleSpans<S> styles)
Creates a paragraph that has multiple segments with multiple styles throughout those segments
-
-
Method Detail
-
getStyledSegments
public 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, returnsList<Tuple2<Segment, Style>>
.
-
getParagraphStyle
public PS getParagraphStyle()
-
length
public int length()
-
charAt
public char charAt(int index)
-
substring
public String substring(int from, int to)
-
substring
public String substring(int from)
-
concat
public Paragraph<PS,SEG,S> concat(Paragraph<PS,SEG,S> p)
Concatenates this paragraph with the given paragraphp
. The paragraph style of the result will be that of this paragraph, unless this paragraph is empty andp
is non-empty, in which case the paragraph style of the result will be that ofp
.
-
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.
-
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. IfcharIdx < 0
, returns the style at the beginning of this paragraph. IfcharIdx >= 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 precedingposition
. Ifposition
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. Ifposition > this.length()
, then it is equivalent toposition == this.length()
.In other words,
getStyleAtPosition(p)
is equivalent togetStyleOfChar(p-1)
.
-
getStyleRangeAtPosition
public IndexRange getStyleRangeAtPosition(int position)
Returns the range of homogeneous style that includes the given position. Ifposition
points to a boundary between two styled ranges, then the range precedingposition
is returned.
-
getStyleSpans
public StyleSpans<S> getStyleSpans()
-
getStyleSpans
public StyleSpans<S> getStyleSpans(int from, int to)
-
getText
public String getText()
Returns the plain text content of this paragraph, not including the line terminator.
-
equals
public boolean equals(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).
-
-