public class DefaultCaret extends Rectangle implements Caret, FocusListener, MouseListener, MouseMotionListener
Caret interface.Rectangle2D.Double, Rectangle2D.Float| Modifier and Type | Field and Description |
|---|---|
static int |
ALWAYS_UPDATE
Indicates the Caret position should always be updated after Document
changes even if the updates are not performed on the Event Dispatching
thread.
|
protected ChangeEvent |
changeEvent
The
ChangeEvent that is fired by fireStateChanged(). |
protected EventListenerList |
listenerList
Stores all registered event listeners.
|
static int |
NEVER_UPDATE
Indicates the Caret position should not be changed unless the Document
length becomes less than the Caret position, in which case the Caret
is moved to the end of the Document.
|
static int |
UPDATE_WHEN_ON_EDT
Indicates the Caret position should be updated only if Document changes
are made on the Event Dispatcher thread.
|
OUT_BOTTOM, OUT_LEFT, OUT_RIGHT, OUT_TOP| Constructor and Description |
|---|
DefaultCaret()
Creates a new
DefaultCaret instance. |
| Modifier and Type | Method and Description |
|---|---|
void |
addChangeListener(ChangeListener listener)
Registers a
ChangeListener that is notified whenever that state
of this Caret changes. |
protected void |
adjustVisibility(Rectangle rect)
Adjusts the text component so that the caret is visible.
|
protected void |
damage(Rectangle r)
Updates the carets rectangle properties to the specified rectangle and
repaints the caret.
|
void |
deinstall(JTextComponent c)
Deinstalls this
Caret from the specified
JTextComponent. |
protected void |
fireStateChanged()
Notifies all registered
ChangeListeners that the state
of this Caret has changed. |
void |
focusGained(FocusEvent event)
Sets the caret to
visible if the text component is editable. |
void |
focusLost(FocusEvent event)
Sets the caret to
invisible. |
int |
getBlinkRate()
Returns the blink rate of this
Caret in milliseconds. |
ChangeListener[] |
getChangeListeners()
Returns all registered
ChangeListeners of this Caret. |
protected JTextComponent |
getComponent()
Returns the
JTextComponent on which this Caret
is installed. |
int |
getDot()
Returns the current position of this
Caret within the
Document. |
<T extends EventListener> |
getListeners(Class<T> listenerType)
Returns all registered event listeners of the specified type.
|
Point |
getMagicCaretPosition()
Returns the current visual position of this
Caret. |
int |
getMark()
Returns the current position of the
mark. |
protected Highlighter.HighlightPainter |
getSelectionPainter()
Returns the
Highlighter.HighlightPainter that should be used
to paint the selection. |
int |
getUpdatePolicy()
Gets the caret update policy.
|
void |
install(JTextComponent c)
Installs this
Caret on the specified
JTextComponent. |
boolean |
isActive()
Returns
true if this Caret is blinking,
and false if not. |
boolean |
isSelectionVisible()
Returns
true if the selection is currently visible,
false otherwise. |
boolean |
isVisible()
Returns
true if this Caret is currently visible,
and false if it is not. |
void |
mouseClicked(MouseEvent event)
When the click is received from Button 1 then the following actions
are performed here:
If we receive a double click, the caret position (dot) is set
to the position associated to the mouse click and the word at
this location is selected.
|
void |
mouseDragged(MouseEvent event)
Moves the caret position when the mouse is dragged over the text
component, modifying the selectiony.
|
void |
mouseEntered(MouseEvent event)
Indicates that the mouse has entered the text component.
|
void |
mouseExited(MouseEvent event)
Indicates that the mouse has exited the text component.
|
void |
mouseMoved(MouseEvent event)
Indicates a mouse movement over the text component.
|
void |
mousePressed(MouseEvent event)
If the button 1 is pressed, the caret position is updated to the
position of the mouse click and the text component requests the input
focus if it is enabled.
|
void |
mouseReleased(MouseEvent event)
Indicates that a mouse button has been released on the text component.
|
protected void |
moveCaret(MouseEvent event)
Moves the caret to the position specified in the
MouseEvent. |
void |
moveDot(int dot)
Moves the
dot location without touching the
mark. |
void |
paint(Graphics g)
Paints this
Caret using the specified Graphics
context. |
protected void |
positionCaret(MouseEvent event)
Repositions the caret to the position specified in the
MouseEvent. |
void |
removeChangeListener(ChangeListener listener)
Removes a
ChangeListener from the list of registered listeners. |
protected void |
repaint()
Causes the
Caret to repaint itself. |
void |
setBlinkRate(int rate)
Sets the blink rate of this
Caret in milliseconds. |
void |
setDot(int dot)
Sets the current position of this
Caret within the
Document. |
void |
setMagicCaretPosition(Point p)
Sets the current visual position of this
Caret. |
void |
setSelectionVisible(boolean v)
Sets the visiblity state of the selection.
|
void |
setUpdatePolicy(int policy)
Sets the Caret update policy.
|
void |
setVisible(boolean v)
Sets the visibility state of the caret.
|
add, add, add, contains, contains, contains, contains, createIntersection, createUnion, equals, getBounds, getBounds2D, getHeight, getLocation, getSize, getWidth, getX, getY, grow, inside, intersection, intersects, isEmpty, move, outcode, reshape, resize, setBounds, setBounds, setLocation, setLocation, setRect, setSize, setSize, toString, translate, unionadd, add, add, contains, contains, getPathIterator, getPathIterator, hashCode, intersect, intersects, intersectsLine, intersectsLine, outcode, setFrame, setRect, unionclone, contains, contains, getCenterX, getCenterY, getFrame, getMaxX, getMaxY, getMinX, getMinY, intersects, setFrame, setFrame, setFrameFromCenter, setFrameFromCenter, setFrameFromDiagonal, setFrameFromDiagonalfinalize, getClass, notify, notifyAll, wait, wait, waitcontains, contains, contains, contains, getPathIterator, getPathIterator, intersects, intersectspublic static final int ALWAYS_UPDATE
public static final int NEVER_UPDATE
public static final int UPDATE_WHEN_ON_EDT
protected ChangeEvent changeEvent
ChangeEvent that is fired by fireStateChanged().protected EventListenerList listenerList
public DefaultCaret()
DefaultCaret instance.public void setUpdatePolicy(int policy)
policy - the new policy. Valid values are:
ALWAYS_UPDATE: always update the Caret position, even when Document
updates don't occur on the Event Dispatcher thread.
NEVER_UPDATE: don't update the Caret position unless the Document
length becomes less than the Caret position (then update the
Caret to the end of the Document).
UPDATE_WHEN_ON_EDT: update the Caret position when the
Document updates occur on the Event Dispatcher thread. This is the
default.IllegalArgumentException - if policy is not one of the above.public int getUpdatePolicy()
public void mouseDragged(MouseEvent event)
When the text component where the caret is installed is disabled, the selection is not change but you can still scroll the text and update the caret's location.
mouseDragged in interface MouseMotionListenerevent - the MouseEvent describing the drag operationpublic void mouseMoved(MouseEvent event)
mouseMoved in interface MouseMotionListenerevent - the MouseEvent describing the mouse operationpublic void mouseClicked(MouseEvent event)
mouseClicked in interface MouseListenerevent - the MouseEvent describing the click operationpublic void mouseEntered(MouseEvent event)
mouseEntered in interface MouseListenerevent - the MouseEvent describing the mouse operationpublic void mouseExited(MouseEvent event)
mouseExited in interface MouseListenerevent - the MouseEvent describing the mouse operationpublic void mousePressed(MouseEvent event)
mousePressed in interface MouseListenerevent - the MouseEvent describing the press operationpublic void mouseReleased(MouseEvent event)
mouseReleased in interface MouseListenerevent - the MouseEvent describing the mouse operationpublic void focusGained(FocusEvent event)
visible if the text component is editable.focusGained in interface FocusListenerevent - the FocusEventpublic void focusLost(FocusEvent event)
invisible.focusLost in interface FocusListenerevent - the FocusEventprotected void moveCaret(MouseEvent event)
MouseEvent.
This will cause a selection if the dot and mark are different.event - the MouseEvent from which to fetch the positionprotected void positionCaret(MouseEvent event)
MouseEvent.event - the MouseEvent from which to fetch the positionpublic void deinstall(JTextComponent c)
Caret from the specified
JTextComponent. This removes any listeners that have been
registered by this Caret.public void install(JTextComponent c)
Caret on the specified
JTextComponent. This registers a couple of listeners
on the text component.public void setMagicCaretPosition(Point p)
Caret.setMagicCaretPosition in interface Caretp - the Point to use for the saved location. May be null
to indicate that there is no visual locationpublic Point getMagicCaretPosition()
Caret.getMagicCaretPosition in interface CaretCaretsetMagicCaretPosition(java.awt.Point)public int getMark()
mark. The
mark marks the location in the Document that
is the end of a selection. If there is no selection, the mark
is the same as the dot.public void setSelectionVisible(boolean v)
setSelectionVisible in interface Caretv - true if the selection should be visible,
false otherwisepublic boolean isSelectionVisible()
true if the selection is currently visible,
false otherwise.isSelectionVisible in interface Carettrue if the selection is currently visible,
false otherwiseprotected final void repaint()
Caret to repaint itself.public <T extends EventListener> T[] getListeners(Class<T> listenerType)
listenerType - the type of listener to returnpublic void addChangeListener(ChangeListener listener)
ChangeListener that is notified whenever that state
of this Caret changes.addChangeListener in interface Caretlistener - the listener to register to this caretpublic void removeChangeListener(ChangeListener listener)
ChangeListener from the list of registered listeners.removeChangeListener in interface Caretlistener - the listener to removepublic ChangeListener[] getChangeListeners()
ChangeListeners of this Caret.ChangeListeners of this Caretprotected void fireStateChanged()
ChangeListeners that the state
of this Caret has changed.protected final JTextComponent getComponent()
JTextComponent on which this Caret
is installed.JTextComponent on which this Caret
is installedpublic int getBlinkRate()
Caret in milliseconds.
A value of 0 means that the caret does not blink.getBlinkRate in interface CaretCaret or 0 if
this caret does not blinkpublic void setBlinkRate(int rate)
Caret in milliseconds.
A value of 0 means that the caret does not blink.setBlinkRate in interface Caretrate - the new blink rate to setpublic int getDot()
Caret within the
Document.public void moveDot(int dot)
dot location without touching the
mark. This is used when making a selection.
If the underlying text component has a NavigationFilter
installed the caret will call the corresponding method of that object.
moveDot in interface Caretdot - the location where to move the dotsetDot(int)public void setDot(int dot)
Caret within the
Document. This also sets the mark to the new
location.
If the underlying text component has a NavigationFilter
installed the caret will call the corresponding method of that object.
setDot in interface Caretdot - the new position to be setmoveDot(int)public boolean isActive()
true if this Caret is blinking,
and false if not. The returned value is independent of
the visiblity of this Caret as returned by isVisible().true if this Caret is blinking,
and false if not.isVisible()public boolean isVisible()
true if this Caret is currently visible,
and false if it is not.public void setVisible(boolean v)
true shows the
Caret, false hides it.setVisible in interface Caretv - the visibility to setprotected Highlighter.HighlightPainter getSelectionPainter()
Highlighter.HighlightPainter that should be used
to paint the selection.Highlighter.HighlightPainter that should be used
to paint the selectionprotected void damage(Rectangle r)
r - the rectangle to set as the caret rectangleprotected void adjustVisibility(Rectangle rect)
JComponent.scrollRectToVisible(Rectangle) on the text component.
Subclasses may wish to change this.