public final class EditorCaret extends Object implements Caret
CaretInfo
object.
Caret
interface is not aware of multiple carets and a call
to setDot(int)
will only retain a single caret. For multiple
carets a call to moveDot(int)
will move the last caret only
(but it retains other existing carets).
AbstractDocument
based document.Constructor and Description |
---|
EditorCaret() |
Modifier and Type | Method and Description |
---|---|
int |
addCaret(Position dotPos,
Position.Bias dotBias,
Position markPos,
Position.Bias markBias)
Create a new caret at the given position with a possible selection.
|
int |
addCarets(List<Position> dotAndMarkPosPairs,
List<Position.Bias> dotAndMarkBiases)
Add multiple carets at once.
|
void |
addChangeListener(ChangeListener l)
Adds listener to track caret position changes (to fulfil
Caret interface). |
void |
addEditorCaretListener(EditorCaretListener listener)
Adds listener to track caret changes in detail.
|
void |
deinstall(JTextComponent c) |
int |
getBlinkRate() |
CaretInfo |
getCaretAt(int offset)
Get information about the caret at the specified offset.
|
List<CaretInfo> |
getCarets()
Get information about all existing carets in the order they were created.
|
int |
getDot()
Get dot offset of the last created caret in the underlying document.
|
Position.Bias |
getDotBias()
Get a bias of the dot position which is either
Position.Bias.Forward or Position.Bias.Backward depending
on whether the caret biases towards the next character or previous one. |
CaretInfo |
getLastCaret()
Get info about the most recently created caret.
|
Point |
getMagicCaretPosition() |
int |
getMark()
Get mark offset of the last created caret in the underlying document.
|
Position.Bias |
getMarkBias()
Get a bias of the mark position which is either
Position.Bias.Forward or Position.Bias.Backward depending
on whether the caret biases towards the next character or previous one. |
static NavigationFilter |
getNavigationFilter(JTextComponent component,
MoveCaretsOrigin origin)
Returns the navigation filter for a certain operation.
|
List<CaretInfo> |
getSortedCarets()
Get information about all existing carets sorted by dot positions in ascending order.
|
void |
install(JTextComponent c) |
boolean |
isSelectionVisible() |
boolean |
isVisible()
Determines if the caret is currently visible (it may be blinking depending on settings).
|
int |
moveCarets(CaretMoveHandler moveHandler)
Move multiple carets or create/modify selections.
|
int |
moveCarets(CaretMoveHandler moveHandler,
MoveCaretsOrigin origin)
Move multiple carets or create/modify selections, specifies the originating operation.
|
void |
moveDot(int offset)
Moves the caret position (dot) to some other position, leaving behind the
mark.
|
void |
moveDot(int offset,
Position.Bias bias,
MoveCaretsOrigin origin)
Moves the caret position (dot) to some other position, leaving behind the
mark.
|
void |
paint(Graphics g) |
void |
removeChangeListener(ChangeListener l)
Removes listener to track caret position changes (to fulfil
Caret interface). |
void |
removeEditorCaretListener(EditorCaretListener listener) |
int |
removeLastCaret()
Remove last added caret (determined by
EditorCaret.getLastCaret() ). |
int |
replaceCarets(List<Position> dotAndMarkPosPairs,
List<Position.Bias> dotAndMarkBiases)
Replace all current carets with the new ones.
|
int |
retainLastCaretOnly()
Switch to single caret mode by removing all carets except the last caret.
|
void |
setBlinkRate(int rate) |
void |
setDot(int offset)
Assign a new offset to the caret in the underlying document.
|
void |
setDot(int offset,
Position.Bias bias,
MoveCaretsOrigin origin)
Assign a new offset to the caret and identify the operation which
originated the caret movement.
|
void |
setMagicCaretPosition(Point p) |
static void |
setNavigationFilter(JTextComponent component,
MoveCaretsOrigin origin,
NavigationFilter naviFilter)
Sets navigation filter for a certain operation type, defined by
MoveCaretsOrigin . |
void |
setSelectionVisible(boolean v) |
void |
setVisible(boolean visible)
Sets the caret visibility, and repaints the caret.
|
public int getDot()
@NonNull public Position.Bias getDotBias()
Position.Bias.Forward
or Position.Bias.Backward
depending
on whether the caret biases towards the next character or previous one.
The bias is always forward for non bidirectional text document.public int getMark()
@NonNull public Position.Bias getMarkBias()
Position.Bias.Forward
or Position.Bias.Backward
depending
on whether the caret biases towards the next character or previous one.
The bias is always forward for non bidirectional text document.@NonNull public List<CaretInfo> getCarets()
CaretInfo.getDot()
and CaretInfo.getMark()
and prevent
caret merging as a possible effect of document modifications.@NonNull public List<CaretInfo> getSortedCarets()
ComplexPositions
their order will reflect the increasing split offset.
CaretInfo.getDot()
and CaretInfo.getMark()
and prevent
caret merging as a possible effect of document modifications.@NonNull public CaretInfo getLastCaret()
EditorCaret.getCarets()
.
EditorCaret.getCarets()
.@CheckForNull public CaretInfo getCaretAt(int offset)
offset
- the offset of the caretpublic void setDot(int offset)
setDot
in interface Caret
offset
- Caret.setDot(int)
public void setDot(int offset, Position.Bias bias, MoveCaretsOrigin origin)
In addition to EditorCaret.setDot(int)
,
the caller may identify the operation that originated the caret movement.
This information is received by NavigationFilter
s or ChangeListener
s
and may be used to react or modify the caret movements.
Use null
or MoveCaretsOrigin.DEFAULT
if the operation not known. Use
MoveCaretsOrigin.DIRECT_NAVIGATION
action type to identify simple navigational
actions (pg up, pg down, left, right, ...).
offset
- new offset for the caretbias
- new bias for the caret. Use either Position.Bias.Forward
or Position.Bias.Backward
depending on whether the caret should bias
towards the next character or previous one. Use forward bias for non-bidirectional text document.origin
- specifies the operation which caused the caret to move.EditorCaret.setDot(int)
public void moveDot(int offset)
moveDot
in interface Caret
offset
- Caret.moveDot(int)
public void moveDot(int offset, Position.Bias bias, MoveCaretsOrigin origin)
In addition to EditorCaret.setDot(int)
,
the caller may identify the operation that originated the caret movement.
This information is received by NavigationFilter
s or EditorCaretListener
s
and may be used to react or modify the caret movements.
Use null
or MoveCaretsOrigin.DEFAULT
if the operation not known. Use
MoveCaretsOrigin.DIRECT_NAVIGATION
action type to identify simple navigational
actions (pg up, pg down, left, right, ...).
offset
- new offset for the caretbias
- new bias for the caret. Use either Position.Bias.Forward
or Position.Bias.Backward
depending on whether the caret should bias
towards the next character or previous one. Use forward bias for non-bidirectional text document.origin
- specifies the operation which caused the caret to move.EditorCaret.moveDot(int)
public int moveCarets(@NonNull CaretMoveHandler moveHandler)
// Go one line up with all carets
editorCaret.moveCarets(new CaretMoveHandler() {
@Override public void moveCarets(CaretMoveContext context) {
for (CaretInfo caretInfo : context.getOriginalSortedCarets()) {
try {
int dot = caretInfo.getDot();
dot = Utilities.getPositionAbove(target, dot, p.x);
Position dotPos = doc.createPosition(dot);
context.setDot(caretInfo, dotPos);
} catch (BadLocationException e) {
// the position stays the same
}
}
}
});
moveHandler
- non-null move handler to perform the changes. The handler's methods
will be given a context to operate on.public int moveCarets(@NonNull CaretMoveHandler moveHandler, MoveCaretsOrigin origin)
In addition to EditorCaret.moveCarets(org.netbeans.spi.editor.caret.CaretMoveHandler)
, the caller may specify
what operation causes the caret movements in the `origin' parameter, see MoveCaretsOrigin
class.
This information is received by NavigationFilter
s or EditorCaretListener
s
and may be used to react or modify the caret movements.
Use null
or MoveCaretsOrigin.DEFAULT
if the operation not known. Use
MoveCaretsOrigin.DIRECT_NAVIGATION
action type to identify simple navigational
actions (pg up, pg down, left, right, ...).
See the EditorCaret.moveCarets(org.netbeans.spi.editor.caret.CaretMoveHandler)
for detailed description of
how carets are moved.
moveHandler
- handler which moves individual caretsorigin
- description of the originating operation. Use null
or MoveCaretsOrigin.DEFAULT
for default/unspecified operation.EditorCaret.moveCarets(org.netbeans.spi.editor.caret.CaretMoveHandler)
.EditorCaret.moveCarets(org.netbeans.spi.editor.caret.CaretMoveHandler)
public int addCaret(@NonNull Position dotPos, @NonNull Position.Bias dotBias, @NonNull Position markPos, @NonNull Position.Bias markBias)
EditorCaret.getCarets()
.
editorCaret.addCaret(pos, pos); // Add a new caret at pos.getOffset()
Position pos2 = doc.createPosition(pos.getOffset() + 2);
// Add a new caret with selection starting at pos and extending to pos2 with caret located at pos2
editorCaret.addCaret(pos2, pos);
// Add a new caret with selection starting at pos and extending to pos2 with caret located at pos
editorCaret.addCaret(pos, pos2);
dotPos
- position of the newly created caret.dotBias
- bias of the new caret. Use either Position.Bias.Forward
or Position.Bias.Backward
depending on whether the caret should bias
towards the next character or previous one. Use forward bias for non-bidirectional text document.markPos
- beginning of the selection (the other end is dotPos) or the same position like dotPos for no selection.
The markPos may have higher offset than dotPos to select in a backward direction.markBias
- bias of the begining of the selection. Use either Position.Bias.Forward
or Position.Bias.Backward
depending on whether the caret should bias
towards the next character or previous one. Use forward bias for non-bidirectional text document.public int addCarets(@NonNull List<Position> dotAndMarkPosPairs, List<Position.Bias> dotAndMarkBiases)
EditorCaret.addCaret(javax.swing.text.Position, javax.swing.text.Position.Bias, javax.swing.text.Position, javax.swing.text.Position.Bias)
multiple times but this method is more efficient (it only fires caret change once).
List<Position> pairs = new ArrayList<>();
pairs.add(dotPos);
pairs.add(dotPos);
pairs.add(dot2Pos);
pairs.add(mark2Pos);
// Add caret located at dotPos.getOffset() and another one with selection
// starting at mark2Pos and extending to dot2Pos with caret located at dot2Pos
editorCaret.addCaret(pairs);
dotAndMarkPosPairs
- list of position pairs consisting of dot position
and mark position (selection start position) which may be the same position like the dot
if the particular caret has no selection. The list must have even size.dotAndMarkBiases
- list of position biases (corresponding to the dot and mark positions
in the previous parameter) or null may be passed if all dotAndMarkPosPairs positions should have a forward bias.EditorCaret.addCaret(javax.swing.text.Position, javax.swing.text.Position.Bias, javax.swing.text.Position, javax.swing.text.Position.Bias)
public int replaceCarets(@NonNull List<Position> dotAndMarkPosPairs, List<Position.Bias> dotAndMarkBiases)
dotAndMarkPosPairs
- list of position pairs consisting of dot position
and mark position (selection start position) which may be the same position like dot
if the particular caret has no selection. The list must have even size.dotAndMarkBiases
- list of position biases (corresponding to the dot and mark positions
in the previous parameter) or null may be passed if all dotAndMarkPosPairs positions should have a forward bias.public int removeLastCaret()
EditorCaret.getLastCaret()
).
public int retainLastCaretOnly()
public void addEditorCaretListener(@NonNull EditorCaretListener listener)
listener
- non-null listener.public void addChangeListener(@NonNull ChangeListener l)
Caret
interface).addChangeListener
in interface Caret
public void removeEditorCaretListener(@NonNull EditorCaretListener listener)
public void removeChangeListener(@NonNull ChangeListener l)
Caret
interface).removeChangeListener
in interface Caret
public boolean isVisible()
Caret becomes visible after setVisible(true)
gets called on it.
public void setVisible(boolean visible)
setVisible
in interface Caret
visible
- the visibility specifierCaret.setVisible(boolean)
public boolean isSelectionVisible()
isSelectionVisible
in interface Caret
public void setSelectionVisible(boolean v)
setSelectionVisible
in interface Caret
public void install(JTextComponent c)
public void deinstall(JTextComponent c)
public void setMagicCaretPosition(Point p)
setMagicCaretPosition
in interface Caret
public final Point getMagicCaretPosition()
getMagicCaretPosition
in interface Caret
public void setBlinkRate(int rate)
setBlinkRate
in interface Caret
@CheckForNull public static NavigationFilter getNavigationFilter(@NonNull JTextComponent component, @NonNull MoveCaretsOrigin origin)
NavigationFilter
can be
registered to receive only limited set of operations. This method returns the filter
for the specified operation. Use MoveCaretsOrigin.DEFAULT
to get text
component's navigation filter (equivalent to JTextComponent.getNavigationFilter()
. That filter receives all caret movements.component
- the component whose filter should be returnedorigin
- the operation descriptionpublic static void setNavigationFilter(JTextComponent component, MoveCaretsOrigin origin, @NullAllowed NavigationFilter naviFilter)
MoveCaretsOrigin
.
The registered filter will receive only those caret movements, which correspond to the
passed MoveCaretsOrigin
. To receive all caret movements, register for MoveCaretsOrigin.DEFAULT
or use JTextComponent.setNavigationFilter(javax.swing.text.NavigationFilter)
.
All the key part(s) of MoveCaretOrigin of a caret operation and `origin' parameter in this function must match in order for the filter to be invoked.
The NavigationFilter implementation may downcast the passed FilterBypass
parameter to NavigationFilterBypass
to get full infomration about the movement.
component
- the component which will use the filterorigin
- the originnaviFilter
- the installed filterJTextComponent.setNavigationFilter(javax.swing.text.NavigationFilter)
,
NavigationFilterBypass
public int getBlinkRate()
getBlinkRate
in interface Caret