public static final class TextFormatter.Change extends Object implements Cloneable
formatter
on the TextInputControl whenever the text
for the TextInputControl is modified.
This class contains state and convenience methods for determining what change occurred on the control. It also has a reference to the TextInputControl itself so that the developer may query any other state on the control. Note that you should never modify the state of the control directly from within the formatter handler.
The Change of the text is described by range (getRangeStart()
, getRangeEnd()
) and
text (getText()
. There are 3 cases that can occur:
text
is empty and range
denotes the range
of deleted text.
E.g. In text "Lorem ipsum dolor sit amet", removal of the second word would result in range
being (6,11) and
an empty text
. Similarly, if you want to delete some different or additional text, just set the range
.
If you want to remove first word instead of the second, just call setRange(0,5)
range
is empty (means nothing was deleted), but it's value is still important.
Both the start and end of the range
point to the index wheret the new text was added. E.g. adding "ipsum " to "Lorem dolor sit amet"
would result in a change with range
of (6,6) and text
containing the String "ipsum ".text
and range
are not empty. The text in range
is deleted
and replaced by text
in the Change. The new text is added instead of the old text, which is at the beginning of the range
.
E.g. when some text is being deleted, you can simply replace it by some placeholder text just by setting a new text
(setText("new text")
)The Change is mutable, but not observable. It should be used only for the life of a single change. It is intended that the Change will be modified from within the formatter.
Modifier and Type | Method and Description |
---|---|
TextFormatter.Change |
clone() |
int |
getAnchor()
Gets the new anchor.
|
int |
getCaretPosition()
Gets the new caret position.
|
Control |
getControl()
Gets the control associated with this change.
|
int |
getControlAnchor()
Gets the current anchor position of the control.
|
int |
getControlCaretPosition()
Gets the current caret position of the control.
|
String |
getControlNewText()
Gets the complete new text which will be used on the control after
this change.
|
String |
getControlText()
This is the full text that control has before the change.
|
int |
getRangeEnd()
Gets the end index into the
TextInputControl.getText()
for the modification. |
int |
getRangeStart()
Gets the start index into the
TextInputControl.getText()
for the modification. |
IndexRange |
getSelection()
Gets the selection of this change.
|
String |
getText()
Gets the text used in this change.
|
boolean |
isAdded()
Gets whether this change was in response to text being added.
|
boolean |
isContentChange()
The content change is any of add, delete or replace changes.
|
boolean |
isDeleted()
Gets whether this change was in response to text being deleted.
|
boolean |
isReplaced()
Gets whether this change was in response to text being replaced.
|
void |
selectRange(int newAnchor,
int newCaretPosition)
Sets the selection.
|
void |
setAnchor(int newAnchor)
Sets the anchor.
|
void |
setCaretPosition(int newCaretPosition)
Sets the caret position.
|
void |
setRange(int start,
int end)
A method assigning both the start and end values
together, in such a way as to ensure they are valid with respect to
each other.
|
void |
setText(String value)
Sets the text to use in this change.
|
String |
toString() |
public final Control getControl()
public final int getRangeStart()
TextInputControl.getText()
for the modification. This will always be a value > 0 and
<= TextInputControl.getLength()
.public final int getRangeEnd()
TextInputControl.getText()
for the modification. This will always be a value > getRangeStart()
and
<= TextInputControl.getLength()
.public final void setRange(int start, int end)
start
- The new start value. Must be a valid start valueend
- The new end value. Must be a valid end valuepublic final int getCaretPosition()
getControlNewText()
.getLength()}public final int getAnchor()
getControlNewText()
.getLength()}public final int getControlCaretPosition()
public final int getControlAnchor()
public final void selectRange(int newAnchor, int newCaretPosition)
getControlNewText()
.getLength()}. Note that there
is an order dependence here, in that the positions should be
specified after the new text has been specified.newAnchor
- The new anchor positionnewCaretPosition
- The new caret positionpublic final IndexRange getSelection()
getControlNewText()
, not
the current control text.public final void setAnchor(int newAnchor)
getControlNewText()
.getLength()}. Note that there
is an order dependence here, in that the position should be
specified after the new text has been specified.newAnchor
- The new anchor positionpublic final void setCaretPosition(int newCaretPosition)
getControlNewText()
.getLength()}. Note that there
is an order dependence here, in that the position should be
specified after the new text has been specified.newCaretPosition
- The new caret positionpublic final String getText()
public final void setText(String value)
value
- The text. This cannot be null.public final String getControlText()
getControlNewText()
.public final String getControlNewText()
public final boolean isAdded()
public final boolean isDeleted()
public final boolean isReplaced()
public final boolean isContentChange()
c.isAdded() || c.isDeleted()
;public TextFormatter.Change clone()
Copyright (c) 2008, 2015, Oracle and/or its affiliates. All rights reserved.