javax.swing.text

Class DefaultCaret

Implemented Interfaces:
Caret, Cloneable, EventListener, FocusListener, MouseListener, MouseMotionListener, Serializable, Shape
Known Direct Subclasses:
BasicTextUI.BasicCaret

public class DefaultCaret
extends Rectangle
implements Caret, FocusListener, MouseListener, MouseMotionListener

The default implementation of the Caret interface.
See Also:
Serialized Form

Nested Class Summary

Nested classes/interfaces inherited from class java.awt.geom.Rectangle2D

Rectangle2D.Double, Rectangle2D.Float

Field Summary

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.
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.
protected ChangeEvent
changeEvent
The ChangeEvent that is fired by fireStateChanged().
protected EventListenerList
listenerList
Stores all registered event listeners.

Fields inherited from class java.awt.Rectangle

height, width, x, y

Fields inherited from class java.awt.geom.Rectangle2D

OUT_BOTTOM, OUT_LEFT, OUT_RIGHT, OUT_TOP

Constructor Summary

DefaultCaret()
Creates a new DefaultCaret instance.

Method Summary

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.
extends EventListener> T[] getListeners(Class listenerType)
Returns all registered event listeners of the specified type.
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.
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.

Methods inherited from class java.awt.Rectangle

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, union

Methods inherited from class java.awt.geom.Rectangle2D

add, add, add, contains, contains, createIntersection, createUnion, equals, getBounds2D, getPathIterator, getPathIterator, hashCode, intersect, intersects, intersectsLine, intersectsLine, outcode, outcode, setFrame, setRect, setRect, union

Methods inherited from class java.awt.geom.RectangularShape

clone, contains, contains, getBounds, getCenterX, getCenterY, getFrame, getHeight, getMaxX, getMaxY, getMinX, getMinY, getPathIterator, getWidth, getX, getY, intersects, isEmpty, setFrame, setFrame, setFrame, setFrameFromCenter, setFrameFromCenter, setFrameFromDiagonal, setFrameFromDiagonal

Methods inherited from class java.lang.Object

clone, equals, extends Object> getClass, finalize, hashCode, notify, notifyAll, toString, wait, wait, wait

Field Details

ALWAYS_UPDATE

public static final 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.
Field Value:
2
Since:
1.5

NEVER_UPDATE

public static final 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.
Field Value:
1
Since:
1.5

UPDATE_WHEN_ON_EDT

public static final int UPDATE_WHEN_ON_EDT
Indicates the Caret position should be updated only if Document changes are made on the Event Dispatcher thread.
Field Value:
0
Since:
1.5

changeEvent

protected ChangeEvent changeEvent
The ChangeEvent that is fired by fireStateChanged().

listenerList

protected EventListenerList listenerList
Stores all registered event listeners.

Constructor Details

DefaultCaret

public DefaultCaret()
Creates a new DefaultCaret instance.

Method Details

addChangeListener

public void addChangeListener(ChangeListener listener)
Registers a ChangeListener that is notified whenever that state of this Caret changes.
Specified by:
addChangeListener in interface Caret
Parameters:
listener - the listener to register to this caret

adjustVisibility

protected void adjustVisibility(Rectangle rect)
Adjusts the text component so that the caret is visible. This default implementation simply calls JComponent.scrollRectToVisible(Rectangle) on the text component. Subclasses may wish to change this.

damage

protected void damage(Rectangle r)
Updates the carets rectangle properties to the specified rectangle and repaints the caret.
Parameters:
r - the rectangle to set as the caret rectangle

deinstall

public void deinstall(JTextComponent c)
Deinstalls this Caret from the specified JTextComponent. This removes any listeners that have been registered by this Caret.
Specified by:
deinstall in interface Caret
Parameters:
c - the text component from which to install this caret

extends EventListener> T[] getListeners

public extends EventListener> T[] getListeners(Class listenerType)
Returns all registered event listeners of the specified type.
Parameters:
listenerType - the type of listener to return
Returns:
all registered event listeners of the specified type

fireStateChanged

protected void fireStateChanged()
Notifies all registered ChangeListeners that the state of this Caret has changed.

focusGained

public void focusGained(FocusEvent event)
Sets the caret to visible if the text component is editable.
Specified by:
focusGained in interface FocusListener
Parameters:
event - the FocusEvent

focusLost

public void focusLost(FocusEvent event)
Sets the caret to invisible.
Specified by:
focusLost in interface FocusListener
Parameters:
event - the FocusEvent

getBlinkRate

public int getBlinkRate()
Returns the blink rate of this Caret in milliseconds. A value of 0 means that the caret does not blink.
Specified by:
getBlinkRate in interface Caret
Returns:
the blink rate of this Caret or 0 if this caret does not blink

getChangeListeners

public ChangeListener[] getChangeListeners()
Returns all registered ChangeListeners of this Caret.
Returns:
all registered ChangeListeners of this Caret

getComponent

protected final JTextComponent getComponent()
Returns the JTextComponent on which this Caret is installed.
Returns:
the JTextComponent on which this Caret is installed

getDot

public int getDot()
Returns the current position of this Caret within the Document.
Specified by:
getDot in interface Caret
Returns:
the current position of this Caret within the Document

getMagicCaretPosition

public Point getMagicCaretPosition()
Returns the current visual position of this Caret.
Specified by:
getMagicCaretPosition in interface Caret
Returns:
the current visual position of this Caret

getMark

public int getMark()
Returns the current position of the 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.
Specified by:
getMark in interface Caret
Returns:
the current position of the mark

getSelectionPainter

protected Highlighter.HighlightPainter getSelectionPainter()
Returns the Highlighter.HighlightPainter that should be used to paint the selection.
Returns:
the Highlighter.HighlightPainter that should be used to paint the selection

getUpdatePolicy

public int getUpdatePolicy()
Gets the caret update policy.
Returns:
the caret update policy.
Since:
1.5

install

public void install(JTextComponent c)
Installs this Caret on the specified JTextComponent. This registers a couple of listeners on the text component.
Specified by:
install in interface Caret
Parameters:
c - the text component on which to install this caret

isActive

public boolean isActive()
Returns 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().
Returns:
true if this Caret is blinking, and false if not.
Since:
1.5
See Also:
isVisible()

isSelectionVisible

public boolean isSelectionVisible()
Returns true if the selection is currently visible, false otherwise.
Specified by:
isSelectionVisible in interface Caret
Returns:
true if the selection is currently visible, false otherwise

isVisible

public boolean isVisible()
Returns true if this Caret is currently visible, and false if it is not.
Specified by:
isVisible in interface Caret
Returns:
true if this Caret is currently visible, and false if it is not

mouseClicked

public 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. If there is no word at the pointer the gap is selected instead.
  • If we receive a triple click, the caret position (dot) is set to the position associated to the mouse click and the line at this location is selected.
Specified by:
mouseClicked in interface MouseListener
Parameters:
event - the MouseEvent describing the click operation

mouseDragged

public void mouseDragged(MouseEvent event)
Moves the caret position when the mouse is dragged over the text component, modifying the selectiony.

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.

Specified by:
mouseDragged in interface MouseMotionListener
Parameters:
event - the MouseEvent describing the drag operation

mouseEntered

public void mouseEntered(MouseEvent event)
Indicates that the mouse has entered the text component. Nothing is done here.
Specified by:
mouseEntered in interface MouseListener
Parameters:
event - the MouseEvent describing the mouse operation

mouseExited

public void mouseExited(MouseEvent event)
Indicates that the mouse has exited the text component. Nothing is done here.
Specified by:
mouseExited in interface MouseListener
Parameters:
event - the MouseEvent describing the mouse operation

mouseMoved

public void mouseMoved(MouseEvent event)
Indicates a mouse movement over the text component. Does nothing here.
Specified by:
mouseMoved in interface MouseMotionListener
Parameters:
event - the MouseEvent describing the mouse operation

mousePressed

public 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. If the SHIFT key is held down, the caret will be moved, which might select the text between the old and new location.
Specified by:
mousePressed in interface MouseListener
Parameters:
event - the MouseEvent describing the press operation

mouseReleased

public void mouseReleased(MouseEvent event)
Indicates that a mouse button has been released on the text component. Nothing is done here.
Specified by:
mouseReleased in interface MouseListener
Parameters:
event - the MouseEvent describing the mouse operation

moveCaret

protected void moveCaret(MouseEvent event)
Moves the caret to the position specified in the MouseEvent. This will cause a selection if the dot and mark are different.
Parameters:
event - the MouseEvent from which to fetch the position

moveDot

public void moveDot(int dot)
Moves the 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.

Specified by:
moveDot in interface Caret
Parameters:
dot - the location where to move the dot
See Also:
setDot(int)

paint

public void paint(Graphics g)
Paints this Caret using the specified Graphics context.
Specified by:
paint in interface Caret
Parameters:
g - the graphics context to use

positionCaret

protected void positionCaret(MouseEvent event)
Repositions the caret to the position specified in the MouseEvent.
Parameters:
event - the MouseEvent from which to fetch the position

removeChangeListener

public void removeChangeListener(ChangeListener listener)
Removes a ChangeListener from the list of registered listeners.
Specified by:
removeChangeListener in interface Caret
Parameters:
listener - the listener to remove

repaint

protected final void repaint()
Causes the Caret to repaint itself.

setBlinkRate

public void setBlinkRate(int rate)
Sets the blink rate of this Caret in milliseconds. A value of 0 means that the caret does not blink.
Specified by:
setBlinkRate in interface Caret
Parameters:
rate - the new blink rate to set

setDot

public void setDot(int dot)
Sets the current position of this 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.

Specified by:
setDot in interface Caret
Parameters:
dot - the new position to be set
See Also:
moveDot(int)

setMagicCaretPosition

public void setMagicCaretPosition(Point p)
Sets the current visual position of this Caret.
Specified by:
setMagicCaretPosition in interface Caret
Parameters:
p - the Point to use for the saved location. May be null to indicate that there is no visual location

setSelectionVisible

public void setSelectionVisible(boolean v)
Sets the visiblity state of the selection.
Specified by:
setSelectionVisible in interface Caret
Parameters:
v - true if the selection should be visible, false otherwise

setUpdatePolicy

public void setUpdatePolicy(int policy)
Sets the Caret update policy.
Parameters:
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.
Throws:
IllegalArgumentException - if policy is not one of the above.
Since:
1.5

setVisible

public void setVisible(boolean v)
Sets the visibility state of the caret. true shows the Caret, false hides it.
Specified by:
setVisible in interface Caret
Parameters:
v - the visibility to set

DefaultCaret.java -- Copyright (C) 2002, 2004, 2005, 2006 Free Software Foundation, Inc. This file is part of GNU Classpath. GNU Classpath is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. GNU Classpath is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with GNU Classpath; see the file COPYING. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. Linking this library statically or dynamically with other modules is making a combined work based on this library. Thus, the terms and conditions of the GNU General Public License cover the whole combination. As a special exception, the copyright holders of this library give you permission to link this library with independent modules to produce an executable, regardless of the license terms of these independent modules, and to copy and distribute the resulting executable under terms of your choice, provided that you also meet, for each linked independent module, the terms and conditions of the license of that module. An independent module is a module which is not derived from or based on this library. If you modify this library, you may extend this exception to your version of the library, but you are not obligated to do so. If you do not wish to do so, delete this exception statement from your version.