Top Description Fields Constructors Methods
org.fxmisc.richtext

public Class MultiChangeBuilder<PS, SEG, S>

extends Object
Class Inheritance
Imports
javafx.scene.control.IndexRange, org.fxmisc.richtext.model.ReadOnlyStyledDocument, .Replacement, .StyledDocument, java.util.ArrayList, .Collections, .List

Constructs a list of Replacements that are used to update an org.fxmisc.richtext.model.EditableStyledDocument in one call via commit().

Note

this builder cannot be reused and a new one must be created for each multi-change.

Relative vs Absolute Changes

Let's say that a document has the text |(|t|e|x|t||)| where each | represents a position in-between the characters of the text . If one wants to remove the opening and closing parenthesis in two update calls, they would write:


// Text:     |(|t|e|x|t|)|
// Position: 0 1 2 3 4 5 6
area.deleteText(0, 1); // delete the first parenthesis

// now the second parenthesis is positioned in a different spot than before
// Text:     |t|e|x|t|)|
// Position: 0 1 2 3 4 5
area.deleteText(4, 5); // delete the second parenthesis

MultiChangeBuilder can do the same thing in one call and works by applying earlier changes before later ones. However, this creates a problem. Later changes' start and end positions must be updated to still point to the correct spot in the text. Wouldn't it be easier to be able to define both changes in the original coordinate system of the document before any changes were applied? Returning to the previous example, wouldn't it be better if one could write:


// Text:     |(|t|e|x|t|)|
// Position: 0 1 2 3 4 5 6

// start multi change
area.deleteText(0, 1); // delete the 1st parenthesis
area.deleteText(5, 6); // delete the 2nd parenthesis
// end multi change

Fortunately, that is already handled for you. Such changes are known as "relative" changes: their start and end positions are updated to point to where they should point once all the earlier changes before them have been applied. To execute the same code as before, we would write:


area.createMultiChange()
     // deletes the 1st parenthesis
     .deleteText(0, 1)

     // internally updates this to delete(4, 5), so that it deletes the 2nd parenthesis
     .deleteText(5, 6)

     // executes the call and updates the document
     .commit();

However, a developer may sometimes want an "absolute" change: their start and end positions are exactly what the developer specifies regardless of how changes before them modify the underlying document. For example, the following code would still delete both parenthesis:


area.createMultiChange()
     .deleteText(0, 1) // deletes the 1st parenthesis
     .deleteTextAbsolutely(4, 5) // deletes the 2nd parenthesis
     .commit();

Thus, absolute changes (e.g. replaceAbsolutely(int, int, StyledDocument)) are all the methods which have "absolute" as a suffix and relative changes (e.g. replace(int, int, StyledDocument)) do not. To make things easier for the developer, the methods declared here are similar to those declared in EditActions, minus a few (e.g. EditActions#append(StyledDocument)).

Other Considerations

Field Summary

Modifier and TypeField and Description
private boolean
private final GenericStyledArea<PS, SEG, S>
private final List<Replacement<PS, SEG, S>>

Constructor Summary

AccessConstructor and Description
pack-priv
pack-priv
MultiChangeBuilder(GenericStyledArea<PS, SEG, S> area, int initialListSize)

private
MultiChangeBuilder(GenericStyledArea<PS, SEG, S> area, List<Replacement<PS, SEG, S>> list)

Method Summary

Modifier and TypeMethod and Description
private MultiChangeBuilder<PS, SEG, S>
absoluteReplace(int start, int end, ReadOnlyStyledDocument<PS, SEG, S> replacement)

public final void
commit()

Applies all the changes stored in this object to the underlying document of the area.

public MultiChangeBuilder<PS, SEG, S>
deleteText(IndexRange
The range of text to delete. It must not be null. Its start and end values specify the start and end positions within the area.
range
)

Removes a range of text.

public MultiChangeBuilder<PS, SEG, S>
deleteText(int
Start position of the range to remove
start
,
int
End position of the range to remove
end
)

Removes a range of text.

public MultiChangeBuilder<PS, SEG, S>
deleteText(int startParagraph, int startColumn, int endParagraph, int endColumn)

Removes a range of text.

public MultiChangeBuilder<PS, SEG, S>
deleteTextAbsolutely(IndexRange
The range of text to delete. It must not be null. Its start and end values specify the start and end positions within the area.
range
)

Removes a range of text.

public MultiChangeBuilder<PS, SEG, S>
deleteTextAbsolutely(int
Start position of the range to remove
start
,
int
End position of the range to remove
end
)

Removes a range of text.

public MultiChangeBuilder<PS, SEG, S>
deleteTextAbsolutely(int startParagraph, int startColumn, int endParagraph, int endColumn)

Removes a range of text.

private void
private void
public boolean
public MultiChangeBuilder<PS, SEG, S>
insert(int
The position to insert the text.
position
,
StyledDocument<PS, SEG, S>
The rich-text content to insert.
document
)

Inserts the given rich-text content at the given position.

public MultiChangeBuilder<PS, SEG, S>
insert(int paragraphIndex, int columnPosition, StyledDocument<PS, SEG, S>
The rich-text content to insert.
document
)

Inserts the given rich-text content at the position returned from getAbsolutePosition(paragraphIndex, columnPosition).

public MultiChangeBuilder<PS, SEG, S>
insertAbsolutely(int
The position to insert the text.
position
,
StyledDocument<PS, SEG, S>
The rich-text content to insert.
document
)

Inserts the given rich-text content at the given position.

public MultiChangeBuilder<PS, SEG, S>
insertAbsolutely(int paragraphIndex, int columnPosition, StyledDocument<PS, SEG, S>
The rich-text content to insert.
document
)

Inserts the given rich-text content at the position returned from getAbsolutePosition(paragraphIndex, columnPosition).

public MultiChangeBuilder<PS, SEG, S>
insertText(int
The position to insert the text.
position
,
String
The text to insert.
text
)

Inserts the given text at the given position.

public MultiChangeBuilder<PS, SEG, S>
insertText(int paragraphIndex, int columnPosition, String
The text to insert
text
)

Inserts the given text at the position returned from getAbsolutePosition(paragraphIndex, columnPosition).

public MultiChangeBuilder<PS, SEG, S>
insertTextAbsolutely(int
The position to insert the text.
position
,
String
The text to insert.
text
)

Inserts the given text at the given position.

public MultiChangeBuilder<PS, SEG, S>
insertTextAbsolutely(int paragraphIndex, int columnPosition, String
The text to insert
text
)

Inserts the given text at the position returned from getAbsolutePosition(paragraphIndex, columnPosition).

private MultiChangeBuilder<PS, SEG, S>
relativeReplace(int start, int end, ReadOnlyStyledDocument<PS, SEG, S> replacement)

public MultiChangeBuilder<PS, SEG, S>
replace(int start, int end, StyledDocument<PS, SEG, S> replacement)

Replaces a range of characters with the given rich-text document.

public MultiChangeBuilder<PS, SEG, S>
replaceAbsolutely(int start, int end, StyledDocument<PS, SEG, S> replacement)

Replaces a range of characters with the given rich-text document.

public MultiChangeBuilder<PS, SEG, S>
replaceText(int
Start index of the range to replace, inclusive.
start
,
int
End index of the range to replace, exclusive.
end
,
String
The text to put in place of the deleted range. It must not be null.
text
)

Replaces a range of characters with the given text.

public MultiChangeBuilder<PS, SEG, S>
replaceTextAbsolutely(int
Start index of the range to replace, inclusive.
start
,
int
End index of the range to replace, exclusive.
end
,
String
The text to put in place of the deleted range. It must not be null.
text
)

Replaces a range of characters with the given text.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

alreadyCreatedback to summary
private boolean alreadyCreated
areaback to summary
private final GenericStyledArea<PS, SEG, S> area
listback to summary
private final List<Replacement<PS, SEG, S>> list

Constructor Detail

MultiChangeBuilderback to summary
pack-priv MultiChangeBuilder(GenericStyledArea<PS, SEG, S> area)
MultiChangeBuilderback to summary
pack-priv MultiChangeBuilder(GenericStyledArea<PS, SEG, S> area, int initialListSize)
MultiChangeBuilderback to summary
private MultiChangeBuilder(GenericStyledArea<PS, SEG, S> area, List<Replacement<PS, SEG, S>> list)

Method Detail

absoluteReplaceback to summary
private MultiChangeBuilder<PS, SEG, S> absoluteReplace(int start, int end, ReadOnlyStyledDocument<PS, SEG, S> replacement)
commitback to summary
public final void commit()

Applies all the changes stored in this object to the underlying document of the area.

Note

this builder cannot be reused and a new one must be created for each multi-change.

deleteTextback to summary
public MultiChangeBuilder<PS, SEG, S> deleteText(IndexRange range)

Removes a range of text.

Parameters
range:IndexRange

The range of text to delete. It must not be null. Its start and end values specify the start and end positions within the area.

See Also
deleteText(int, int)
deleteTextback to summary
public MultiChangeBuilder<PS, SEG, S> deleteText(int start, int end)

Removes a range of text. It must hold 0 <= start <= end <= getLength().

Parameters
start:int

Start position of the range to remove

end:int

End position of the range to remove

deleteTextback to summary
public MultiChangeBuilder<PS, SEG, S> deleteText(int startParagraph, int startColumn, int endParagraph, int endColumn)

Removes a range of text. It must hold 0 <= start <= end <= getLength() where start = getAbsolutePosition(startParagraph, startColumn); and is inclusive, and int end = getAbsolutePosition(endParagraph, endColumn); and is exclusive.

Caution

see StyledDocument#getAbsolutePosition(int, int) to know how the column index argument can affect the returned position.

deleteTextAbsolutelyback to summary
public MultiChangeBuilder<PS, SEG, S> deleteTextAbsolutely(IndexRange range)

Removes a range of text.

Parameters
range:IndexRange

The range of text to delete. It must not be null. Its start and end values specify the start and end positions within the area.

See Also
deleteText(int, int)
deleteTextAbsolutelyback to summary
public MultiChangeBuilder<PS, SEG, S> deleteTextAbsolutely(int start, int end)

Removes a range of text. It must hold 0 <= start <= end <= getLength().

Parameters
start:int

Start position of the range to remove

end:int

End position of the range to remove

deleteTextAbsolutelyback to summary
public MultiChangeBuilder<PS, SEG, S> deleteTextAbsolutely(int startParagraph, int startColumn, int endParagraph, int endColumn)

Removes a range of text. It must hold 0 <= start <= end <= getLength() where start = getAbsolutePosition(startParagraph, startColumn); and is inclusive, and int end = getAbsolutePosition(endParagraph, endColumn); and is exclusive.

Caution

see StyledDocument#getAbsolutePosition(int, int) to know how the column index argument can affect the returned position.

ensureHasChangesback to summary
private void ensureHasChanges()
ensureNotYetCreatedback to summary
private void ensureNotYetCreated()
hasChangesback to summary
public boolean hasChanges()
insertback to summary
public MultiChangeBuilder<PS, SEG, S> insert(int position, StyledDocument<PS, SEG, S> document)

Inserts the given rich-text content at the given position.

Parameters
position:int

The position to insert the text.

document:StyledDocument<PS, SEG, S>

The rich-text content to insert.

insertback to summary
public MultiChangeBuilder<PS, SEG, S> insert(int paragraphIndex, int columnPosition, StyledDocument<PS, SEG, S> document)

Inserts the given rich-text content at the position returned from getAbsolutePosition(paragraphIndex, columnPosition).

Caution

see StyledDocument#getAbsolutePosition(int, int) to know how the column index argument can affect the returned position.

Parameters
document:StyledDocument<PS, SEG, S>

The rich-text content to insert.

insertAbsolutelyback to summary
public MultiChangeBuilder<PS, SEG, S> insertAbsolutely(int position, StyledDocument<PS, SEG, S> document)

Inserts the given rich-text content at the given position.

Parameters
position:int

The position to insert the text.

document:StyledDocument<PS, SEG, S>

The rich-text content to insert.

insertAbsolutelyback to summary
public MultiChangeBuilder<PS, SEG, S> insertAbsolutely(int paragraphIndex, int columnPosition, StyledDocument<PS, SEG, S> document)

Inserts the given rich-text content at the position returned from getAbsolutePosition(paragraphIndex, columnPosition).

Caution

see StyledDocument#getAbsolutePosition(int, int) to know how the column index argument can affect the returned position.

Parameters
document:StyledDocument<PS, SEG, S>

The rich-text content to insert.

insertTextback to summary
public MultiChangeBuilder<PS, SEG, S> insertText(int position, String text)

Inserts the given text at the given position.

Parameters
position:int

The position to insert the text.

text:String

The text to insert.

insertTextback to summary
public MultiChangeBuilder<PS, SEG, S> insertText(int paragraphIndex, int columnPosition, String text)

Inserts the given text at the position returned from getAbsolutePosition(paragraphIndex, columnPosition).

Caution

see StyledDocument#getAbsolutePosition(int, int) to know how the column index argument can affect the returned position.

Parameters
text:String

The text to insert

insertTextAbsolutelyback to summary
public MultiChangeBuilder<PS, SEG, S> insertTextAbsolutely(int position, String text)

Inserts the given text at the given position.

Parameters
position:int

The position to insert the text.

text:String

The text to insert.

insertTextAbsolutelyback to summary
public MultiChangeBuilder<PS, SEG, S> insertTextAbsolutely(int paragraphIndex, int columnPosition, String text)

Inserts the given text at the position returned from getAbsolutePosition(paragraphIndex, columnPosition).

Caution

see StyledDocument#getAbsolutePosition(int, int) to know how the column index argument can affect the returned position.

Parameters
text:String

The text to insert

relativeReplaceback to summary
private MultiChangeBuilder<PS, SEG, S> relativeReplace(int start, int end, ReadOnlyStyledDocument<PS, SEG, S> replacement)
replaceback to summary
public MultiChangeBuilder<PS, SEG, S> replace(int start, int end, StyledDocument<PS, SEG, S> replacement)

Replaces a range of characters with the given rich-text document.

replaceAbsolutelyback to summary
public MultiChangeBuilder<PS, SEG, S> replaceAbsolutely(int start, int end, StyledDocument<PS, SEG, S> replacement)

Replaces a range of characters with the given rich-text document.

replaceTextback to summary
public MultiChangeBuilder<PS, SEG, S> replaceText(int start, int end, String text)

Replaces a range of characters with the given text. It must hold 0 <= start <= end <= getLength().

Parameters
start:int

Start index of the range to replace, inclusive.

end:int

End index of the range to replace, exclusive.

text:String

The text to put in place of the deleted range. It must not be null.

replaceTextAbsolutelyback to summary
public MultiChangeBuilder<PS, SEG, S> replaceTextAbsolutely(int start, int end, String text)

Replaces a range of characters with the given text. It must hold 0 <= start <= end <= getLength().

Parameters
start:int

Start index of the range to replace, inclusive.

end:int

End index of the range to replace, exclusive.

text:String

The text to put in place of the deleted range. It must not be null.