APIdia Launch Partner
Top Description Inners Fields Constructors Methods
jfx.incubator.scene.control.richtext.model

public abstract Class StyledTextModel

extends Object
Class Inheritance
Known Direct Subclasses
jfx.incubator.scene.control.richtext.model.StyledTextModelViewOnlyBase, jfx.incubator.scene.control.richtext.model.BasicTextModel, jfx.incubator.scene.control.richtext.model.RichTextModel
Imports
java.io.IOException, .InputStream, .OutputStream, java.util.ArrayList, .Collections, .HashMap, .List, .Map, .Objects, .Set, java.util.concurrent.CopyOnWriteArrayList, java.util.function.Supplier, java.util.stream.Collectors, javafx.beans.property.ReadOnlyBooleanProperty, .ReadOnlyBooleanWrapper, javafx.scene.input.DataFormat, javafx.scene.layout.Region, com.sun.javafx.ModuleUtil, com.sun.jfx.incubator.scene.control.richtext.Markers, .StyleAttributeMapHelper, .StyledTextModelHelper, .UndoableChange, com.sun.jfx.incubator.scene.control.richtext.util.RichUtils, jfx.incubator.scene.control.richtext.LineEnding, .Marker, .StyleResolver, .TextPos

The base class for styled text models used by the RichTextArea.

This class handles the following functionality with the intent to simplify custom models:

Editing

The model supports editing when isWritable() returns true. Three methods participate in modification of the content: replace(StyleResolver, TextPos, TextPos, String), replace(StyleResolver, TextPos, TextPos, StyledInput), applyStyle(TextPos, TextPos, StyleAttributeMap, boolean). These methods decompose the main modification into operations with individual paragraphs and delegate these to subclasses.

At the end of this process, an event is sent to all the Listeners, followed by the skin requesting the updated paragraphs when required.

Creating a Paragraph

The model presents its content to the view(s) via immutable RichParagraph. There are two ways of adding styles to the model: The latter method is intended for applications where an editable control is needed, such as general purpose rich text editor, the former is designed for view-only informational controls that must follow the application theme and therefore are coupled to the stylesheet.

Extending the Model

The subclasses are free to choose how the data is stored, the only limitation is that the model neither stores nor caches any Nodes, since multiple skins might be attached to the same model. When required, the model may contain properties which can be bound to the Nodes created in getParagraph(int). It is the responsibility of the model to store and restore the values of such properties.
Since
24

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static record
private static record
StyledTextModel.FHPriority

stores the handler and its priority

public static interface
StyledTextModel.Listener

Receives information about modifications of the model.

Field Summary

Modifier and TypeField and Description
private final HashMap<StyledTextModel.FHKey, StyledTextModel.FHPriority>
private final UndoableChange
private LineEnding
private final CopyOnWriteArrayList<StyledTextModel.Listener>
private final Markers
private final ReadOnlyBooleanWrapper
private UndoableChange
private final ReadOnlyBooleanWrapper
private boolean

Constructor Summary

AccessConstructor and Description
public
StyledTextModel()

Constructs the instance of the model.

Method Summary

Modifier and TypeMethod and Description
private void
add(UndoableChange
the change
ch
,
TextPos
the caret position after the change
end
)

Adds an UndoableChange to the undo/redo buffer.

public final void
addListener(StyledTextModel.Listener
a non-null listener
listener
)

Adds a Listener to this model.

protected abstract void
applyParagraphStyle(int
the paragraph index
index
,
StyleAttributeMap
the paragraph attributes
paragraphAttrs
)

Applies paragraph styles in the specified paragraph.

protected abstract void
applyStyle(int
the paragraph index
index
,
int
the start offset
start
,
int
the end offset
end
,
StyleAttributeMap
the character attributes
a
,
boolean
determines whether to merge with or completely replace the existing attribute map
merge
)

Applies style to the specified text range within a single paragraph.

public final void
applyStyle(TextPos
the start of text range
start
,
TextPos
the end of text range
end
,
StyleAttributeMap
the style attributes to set
attrs
,
boolean
whether to merge or replace the attribute map
mergeAttributes
)

Applies the style attributes to the specified range in the document.

private void
public final TextPos

Returns:

the text position within the document and paragraph limits
clamp
(TextPos
the text position, cannot be null
p
)

Returns the text position guaranteed to be within the document and paragraph limits.

public final void
clearUndoRedo()

Clears the undo-redo stack.

protected Map<String, String>

Returns:

the document properties, or null
documentProperties
()

This method is called by export(TextPos, TextPos, StyledOutput) to ensure that the document properties, if any, are exported.

private String
dump()

for debugging

public final void
export(TextPos
start of the range
start
,
TextPos
end of the range
end
,
StyledOutput
StyledOutput to receive the stream
out
)

Exports the stream of StyledSegments in the given range to the specified StyledOutput.

protected final void
exportParagraph(int
the paragraph index in the model
index
,
int
the start offset
start
,
int
the end offset (may exceed the paragraph length)
end
,
boolean
determines whether to emit paragraph attributes
withParAttrs
,
StyledOutput
the target StyledOutput
out
)

Exports part of the paragraph as a sequence of styled segments.

public void
fireChangeEvent(TextPos
start of the affected range
start
,
TextPos
end of the affected range
end
,
int
number of characters added before any added paragraphs
charsTop
,
int
number of paragraphs inserted
linesAdded
,
int
number of characters added after any inserted paragraphs
charsBottom
)

Fires a text modification event for the given range.

public void
fireStyleChangeEvent(TextPos
the start position
start
,
TextPos
the end position, must be greater than the start position
end
)

Fires a style change event for the given range.

public final DataFormatHandler

Returns:

DataFormatHandler or null
getDataFormatHandler
(DataFormat
data format
format
,
boolean
for export (true) or for input (false)
forExport
)

Returns a DataFormatHandler instance corresponding to the given DataFormat.

public final TextPos

Returns:

the text position
getDocumentEnd
()

Returns the text position corresponding to the end of the document.

public final TextPos

Returns:

the text position
getEndOfParagraphTextPos
(int
the paragraph index
index
)

Returns a TextPos corresponding to the end of paragraph at the given index.

public final LineEnding

Returns:

the line ending value
getLineEnding
()

Specifies the line ending characters.

public final Marker

Returns:

Marker instance
getMarker
(TextPos
text position
pos
)

Returns the Marker at the specified position.

public abstract RichParagraph

Returns:

the instance of RichParagraph
getParagraph
(int
the paragraph index in the range (0...size())
index
)

Returns a RichParagraph at the given model index.

public int

Returns:

the length
getParagraphLength
(int
the paragraph index
index
)

Returns the length of text in a paragraph at the specified index.

public abstract String

Returns:

the non-null paragraph text string
getPlainText
(int
the paragraph index in the range (0...size())
index
)

Returns the plain text string for the specified paragraph.

public abstract StyleAttributeMap

Returns:

the style attributes, non-null
getStyleAttributeMap
(StyleResolver
the style resolver
resolver
,
TextPos
the text position
pos
,
boolean
whether to pick preceding style at the segment boundary
forInsert
)

Returns the StyleAttributeMap of the character at the specified position's charIndex.

protected Set<StyleAttribute<?>>

Returns:

the set of supported attributes, or null if the model requires no filtering
getSupportedAttributes
()

Returns the set of attributes supported attributes.

public final List<DataFormat>

Returns:

the immutable list of supported formats
getSupportedDataFormats
(boolean
determines whether the operation is export (true) or import (false)
forExport
)

Returns an immutable list of supported data formats for either export or import operations, in the order of priority - from high to low.

protected void
handleDocumentProperties(Map<String, String>
the document properties
props
,
boolean
indicates whether replacing the model's content completely
completeReplacement
)

This method is called by any of the replace() methods when encountering a StyledSegment.Type#DOCUMENT_PROPERTIES segment.

private static void
protected abstract void
insertLineBreak(int
the model index
index
,
int
the text offset
offset
)

Inserts a line break at the specified position.

protected abstract void
insertParagraph(int
model index
index
,
Supplier<Region>
code that will be used to create a Node instance
generator
)

Inserts a paragraph that contains a single Region.

protected abstract int

Returns:

the number of characters inserted
insertTextSegment
(int
the paragraph index
index
,
int
the insertion offset within the paragraph
offset
,
String
the text to insert
text
,
StyleAttributeMap
the style attributes
attrs
)

This method is called to insert a single styled text segment at the given position.

public final boolean
public final boolean
public final boolean

Returns:

true if undo/redo functionality is enabled
isUndoRedoEnabled
()

Indicates whether undo/redo functionality is enabled.

public abstract boolean

Returns:

true if the model supports content modifications
isWritable
()

Indicates whether the model supports content modifications made via applyStyle(), replace(), undo(), redo() methods.

public final void
read(StyleResolver
the style resolver
r
,
DataFormat
the data format
f
,
InputStream
the input stream
input
)

Replaces the content of the model with the data read from the input stream, using the specified DataFormat.

public final TextPos[]

Returns:

the [start, end] text positions prior to the change
redo
(StyleResolver
the StyleResolver to use
resolver
)

Redoes the recent change, if possible, returning an array comprising [start, end] text positions prior to the change.

public final ReadOnlyBooleanProperty

Returns:

the read-only property
redoableProperty
()

The property describes if it's currently possible to redo the latest change of the content that was undone.

protected final void
registerDataFormatHandler(DataFormatHandler
data format handler
h
,
boolean
true if the handler supports export operations
forExport
,
boolean
true if the handler supports import operations
forImport
,
int
from 0 (lowest, usually plain text) to Integer.MAX_VALUE
priority
)

Registers a format handler for export and/or import operations.

protected final void
removeDataFormatHandler(DataFormat
the data format
f
,
boolean
whether to remove the export handler
forExport
,
boolean
whether to remove the import handler
forImport
)

Removes the data format handler registered previously with registerDataFormatHandler(DataFormatHandler, boolean, boolean, int).

public final void
removeListener(StyledTextModel.Listener
a non-null listener
listener
)

Removes a Listener from this model.

protected abstract void
removeRange(TextPos
the start of the range to be removed
start
,
TextPos
the end of the range to be removed, expected to be greater than the start position
end
)

Removes the specified text range.

public final TextPos

Returns:

the text position at the end of the inserted text, or null if the model is read only
replace
(StyleResolver
the StyleResolver to use
resolver
,
TextPos
start text position
start
,
TextPos
end text position
end
,
String
text string to insert
text
)

Replaces the given range with the provided plain text.

public final TextPos

Returns:

the text position at the end of the inserted text, or null if the model is read only
replace
(StyleResolver
the StyleResolver to use, can be null
resolver
,
TextPos
the start text position
start
,
TextPos
the end text position
end
,
StyledInput
the input content stream
input
)

Replaces the given range with the provided styled text input.

private final TextPos
replace(StyleResolver resolver, TextPos start, TextPos end, StyledInput input, boolean allowUndo, boolean isEdit)

public final void
setLineEnding(LineEnding
the line ending value, cannot be null
value
)

Sets the line ending characters.

protected abstract void
setParagraphStyle(int
the paragraph index
index
,
StyleAttributeMap
the paragraph attributes
paragraphAttrs
)

Replaces the paragraph styles in the specified paragraph.

private void
setRedoable(boolean on)

private void
setUndoable(boolean on)

public final void
setUndoRedoEnabled(boolean
true to enable undo/redo
on
)

Controls whether undo/redo functionality is enabled.

public abstract int

Returns:

number of paragraphs
size
()

Returns the number of paragraphs in the model.

public final TextPos[]

Returns:

the [start, end] text positions prior to the change
undo
(StyleResolver
the StyleResolver to use
resolver
)

Undoes the recent change, if possible, returning an array comprising [start, end] text positions prior to the change.

public final ReadOnlyBooleanProperty

Returns:

the read-only property
undoableProperty
()

The property describes if it's currently possible to undo the latest change of the content that was done.

private void
protected String

Returns:

the model version string, or null
versionString
()

This method is called by export(TextPos, TextPos, StyledOutput) to export the model's version string, if any.

public final void
write(StyleResolver
the style resolver
r
,
DataFormat
the data format
f
,
OutputStream
the output stream
out
)

Writes the model content to the output stream using the specified DataFormat.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait