- java.lang.Object
-
- javafx.scene.control.TextFormatter.Change
-
- All Implemented Interfaces:
Cloneable
- Enclosing class:
- TextFormatter<V>
public static final class TextFormatter.Change extends Object implements Cloneable
Contains the state representing a change in the content or selection for a TextInputControl. This object is passed to any registeredformatter
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:- Some text was deleted: In this case,
text
is empty andrange
denotes therange
of deleted text. E.g. In text "Lorem ipsum dolor sit amet", removal of the second word would result inrange
being (6,11) and an emptytext
. Similarly, if you want to delete some different or additional text, just set therange
. If you want to remove first word instead of the second, just callsetRange(0,5)
- Some text was added: Now the
range
is empty (means nothing was deleted), but it's value is still important. Both the start and end of therange
point to the index wheret the new text was added. E.g. adding "ipsum " to "Lorem dolor sit amet" would result in a change withrange
of (6,6) andtext
containing the String "ipsum ". - Some text was replaced: The combination of the 2 cases above. Both
text
andrange
are not empty. The text inrange
is deleted and replaced bytext
in the Change. The new text is added instead of the old text, which is at the beginning of therange
. 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.
- Since:
- JavaFX 8u40
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description TextFormatter.Change
clone()
Creates and returns a copy of this object.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 theTextInputControl.getText()
for the modification.int
getRangeStart()
Gets the start index into theTextInputControl.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()
Returns a string representation of the object.
-
-
-
Method Detail
-
getControl
public final Control getControl()
Gets the control associated with this change.- Returns:
- The control associated with this change. This will never be null.
-
getRangeStart
public final int getRangeStart()
Gets the start index into theTextInputControl.getText()
for the modification. This will always be a value > 0 and <=TextInputControl.getLength()
.- Returns:
- The start index
-
getRangeEnd
public final int getRangeEnd()
Gets the end index into theTextInputControl.getText()
for the modification. This will always be a value >getRangeStart()
and <=TextInputControl.getLength()
.- Returns:
- The end index
-
setRange
public final 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. The start must be less than or equal to the end.- Parameters:
start
- The new start value. Must be a valid start valueend
- The new end value. Must be a valid end value
-
getCaretPosition
public final int getCaretPosition()
Gets the new caret position. This value will always be > 0 and <=getControlNewText()
.getLength()}- Returns:
- The new caret position
-
getAnchor
public final int getAnchor()
Gets the new anchor. This value will always be > 0 and <=getControlNewText()
.getLength()}- Returns:
- The new anchor position
-
getControlCaretPosition
public final int getControlCaretPosition()
Gets the current caret position of the control.- Returns:
- The previous caret position
-
getControlAnchor
public final int getControlAnchor()
Gets the current anchor position of the control.- Returns:
- The previous anchor
-
selectRange
public final void selectRange(int newAnchor, int newCaretPosition)
Sets the selection. The anchor and caret position values must be > 0 and <=getControlNewText()
.getLength()}. Note that there is an order dependence here, in that the positions should be specified after the new text has been specified.- Parameters:
newAnchor
- The new anchor positionnewCaretPosition
- The new caret position
-
getSelection
public final IndexRange getSelection()
Gets the selection of this change. Note that the selection range refers togetControlNewText()
, not the current control text.- Returns:
- The selected range of this change.
-
setAnchor
public final void setAnchor(int newAnchor)
Sets the anchor. The anchor value must be > 0 and <=getControlNewText()
.getLength()}. Note that there is an order dependence here, in that the position should be specified after the new text has been specified.- Parameters:
newAnchor
- The new anchor position
-
setCaretPosition
public final void setCaretPosition(int newCaretPosition)
Sets the caret position. The caret position value must be > 0 and <=getControlNewText()
.getLength()}. Note that there is an order dependence here, in that the position should be specified after the new text has been specified.- Parameters:
newCaretPosition
- The new caret position
-
getText
public final String getText()
Gets the text used in this change. For example, this may be new text being added, or text which is replacing all the control's text within the range of start and end. Typically it is an empty string only for cases where the range is being deleted.- Returns:
- The text involved in this change. This will never be null.
-
setText
public final void setText(String value)
Sets the text to use in this change. This is used to replace the range from start to end, if such a range exists, or to insert text at the position represented by start == end.- Parameters:
value
- The text. This cannot be null.
-
getControlText
public final String getControlText()
This is the full text that control has before the change. To get the text after this change, usegetControlNewText()
.- Returns:
- the previous text of control
-
getControlNewText
public final String getControlNewText()
Gets the complete new text which will be used on the control after this change. Note that some controls (such as TextField) may do further filtering after the change is made (such as stripping out newlines) such that you cannot assume that the newText will be exactly the same as what is finally set as the content on the control, however it is correct to assume that this is the case for the purpose of computing the new caret position and new anchor position (as those values supplied will be modified as necessary after the control has stripped any additional characters that the control might strip).- Returns:
- The controls proposed new text at the time of this call, according to the state set for start, end, and text properties on this Change object.
-
isAdded
public final boolean isAdded()
Gets whether this change was in response to text being added. Note that after the Change object is modified by the formatter (by one of the setters) the return value of this method is not altered. It answers as to whether this change was fired as a result of text being added, not whether text will end up being added in the end.- Returns:
- true if text was being added
-
isDeleted
public final boolean isDeleted()
Gets whether this change was in response to text being deleted. Note that after the Change object is modified by the formatter (by one of the setters) the return value of this method is not altered. It answers as to whether this change was fired as a result of text being deleted, not whether text will end up being deleted in the end.- Returns:
- true if text was being deleted
-
isReplaced
public final boolean isReplaced()
Gets whether this change was in response to text being replaced. Note that after the Change object is modified by the formatter (by one of the setters) the return value of this method is not altered. It answers as to whether this change was fired as a result of text being replaced, not whether text will end up being replaced in the end.- Returns:
- true if text was being replaced
-
isContentChange
public final boolean isContentChange()
The content change is any of add, delete or replace changes. Basically it's a shortcut forc.isAdded() || c.isDeleted()
;- Returns:
- true if the content changed
-
toString
public String toString()
Description copied from class:Object
Returns a string representation of the object. In general, thetoString
method returns a string that "textually represents" this object. The result should be a concise but informative representation that is easy for a person to read. It is recommended that all subclasses override this method.The
toString
method for classObject
returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@
', and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of:getClass().getName() + '@' + Integer.toHexString(hashCode())
-
clone
public TextFormatter.Change clone()
Description copied from class:Object
Creates and returns a copy of this object. The precise meaning of "copy" may depend on the class of the object. The general intent is that, for any objectx
, the expression:x.clone() != x
x.clone().getClass() == x.getClass()
true
, but these are not absolute requirements. While it is typically the case that:x.clone().equals(x)
true
, this is not an absolute requirement.By convention, the returned object should be obtained by calling
super.clone
. If a class and all of its superclasses (exceptObject
) obey this convention, it will be the case thatx.clone().getClass() == x.getClass()
.By convention, the object returned by this method should be independent of this object (which is being cloned). To achieve this independence, it may be necessary to modify one or more fields of the object returned by
super.clone
before returning it. Typically, this means copying any mutable objects that comprise the internal "deep structure" of the object being cloned and replacing the references to these objects with references to the copies. If a class contains only primitive fields or references to immutable objects, then it is usually the case that no fields in the object returned bysuper.clone
need to be modified.The method
clone
for classObject
performs a specific cloning operation. First, if the class of this object does not implement the interfaceCloneable
, then aCloneNotSupportedException
is thrown. Note that all arrays are considered to implement the interfaceCloneable
and that the return type of theclone
method of an array typeT[]
isT[]
where T is any reference or primitive type. Otherwise, this method creates a new instance of the class of this object and initializes all its fields with exactly the contents of the corresponding fields of this object, as if by assignment; the contents of the fields are not themselves cloned. Thus, this method performs a "shallow copy" of this object, not a "deep copy" operation.The class
Object
does not itself implement the interfaceCloneable
, so calling theclone
method on an object whose class isObject
will result in throwing an exception at run time.
-
-