|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--java.awt.geom.RectangularShape | +--java.awt.geom.Rectangle2D | +--java.awt.Rectangle | +--javax.swing.text.DefaultCaret
Safe: A default implementation of Caret. The caret is rendered as a vertical line in the color specified by the CaretColor property of the associated JTextComponent. It can blink at the rate specified by the BlinkRate property.
This implementation expects two sources of asynchronous notification.
The timer thread fires asynchronously, and causes the caret to simply
repaint the most recent bounding box. The caret also tracks change
as the document is modified. Typically this will happen on the
event thread as a result of some mouse or keyboard event. Updates
can also occur from some other thread mutating the document. There
is a property AsynchronousMovement
that determines if
the caret will move on asynchronous updates. The default behavior
is to not update on asynchronous updates. If asynchronous
updates are allowed, the update thread will fire the caret position
change to listeners asynchronously. The repaint of the new caret
location will occur on the event thread in any case, as calls to
modelToView
are only safe on the event thread.
The caret acts as a mouse and focus listener on the text component it has been installed in, and defines the caret semantics based upon those events. The listener methods can be reimplemented to change the semantics. By default, the first mouse button will be used to set focus and caret position. Dragging the mouse pointer with the first mouse button will sweep out a selection that is contiguous in the model. If the associated text component is editable, the caret will become visible when focus is gained, and invisible when focus is lost.
The Highlighter bound to the associated text component is used to
render the selection by default.
Selection appearance can be customized by supplying a
painter to use for the highlights. By default a painter is used that
will render a solid color as specified in the associated text component
in the SelectionColor
property. This can easily be changed
by reimplementing the
getSelectionHighlighter
method.
A customized caret appearance can be achieved by reimplementing the paint method. If the paint method is changed, the damage method should also be reimplemented to cause a repaint for the area needed to render the caret. The caret extends the Rectangle class which is used to hold the bounding box for where the caret was last rendered. This enables the caret to repaint in a thread-safe manner when the caret moves without making a call to modelToView which is unstable between model updates and view repair (i.e. the order of delivery to DocumentListeners is not guaranteed).
The magic caret position is set to null when the caret position changes. A timer is used to determine the new location (after the caret change). When the timer fires, if the magic caret position is still null it is reset to the current caret position. Any actions that change the caret position and want the magic caret position to remain the same, must remember the magic caret position, change the cursor, and then set the magic caret position to its original value. This has the benefit that only actions that want the magic caret position to persist (such as open/down) need to know about it.
Warning:
Serialized objects of this class will not be compatible with
future Swing releases. The current serialization support is
appropriate for short term storage or RMI between applications running
the same version of Swing. As of 1.4, support for long term storage
of all JavaBeansTM
has been added to the java.beans
package.
Please see java.beans.XMLEncoder
.
Caret
,
Serialized FormField Summary | |
(package private) boolean |
async
flag to indicate if async updates should move the caret. |
protected ChangeEvent |
changeEvent
The change event for the model. |
private ClipboardOwner |
clipboardOwner
|
(package private) JTextComponent |
component
|
(package private) int |
dot
|
(package private) javax.swing.text.Position.Bias |
dotBias
|
(package private) boolean |
dotLTR
|
private javax.swing.text.NavigationFilter.FilterBypass |
filterBypass
|
private int[] |
flagXPoints
|
private int[] |
flagYPoints
|
(package private) Timer |
flasher
|
private FocusListener |
focusListener
|
private boolean |
forceCaretPositionChange
If this is true, the location of the dot is updated regardless of the current location. |
protected EventListenerList |
listenerList
The event listener list. |
(package private) Point |
magicCaretPosition
|
(package private) int |
mark
|
(package private) javax.swing.text.Position.Bias |
markBias
|
(package private) boolean |
markLTR
|
private boolean |
ownsSelection
This is used to indicate if the caret currently owns the selection. |
(package private) Object |
selectionTag
|
(package private) boolean |
selectionVisible
|
private boolean |
shouldHandleRelease
Whether or not mouseReleased should adjust the caret and focus. |
(package private) javax.swing.text.DefaultCaret.UpdateHandler |
updateHandler
|
(package private) boolean |
visible
|
Fields inherited from class java.awt.Rectangle |
height, width, x, y |
Constructor Summary | |
DefaultCaret()
Enabled: Constructs a default caret. |
Method Summary | |
private boolean |
_contains(int X,
int Y,
int W,
int H)
|
void |
addChangeListener(ChangeListener l)
Enabled: Adds a listener to track whenever the caret position has been changed. |
private void |
adjustCaret(MouseEvent e)
Adjusts the caret location based on the MouseEvent. |
(package private) void |
adjustCaretAndFocus(MouseEvent e)
|
private void |
adjustFocus(boolean inWindow)
Adjusts the focus, if necessary. |
protected void |
adjustVisibility(Rectangle nloc)
Scrolls the associated view (if necessary) to make the caret visible. |
(package private) void |
changeCaretPosition(int dot,
javax.swing.text.Position.Bias dotBias)
Sets the caret position (dot) to a new location. |
protected void |
damage(Rectangle r)
Damages the area surrounding the caret to cause it to be repainted in a new location. |
void |
deinstall(JTextComponent c)
Suppressed: Called when the UI is being removed from the interface of a JTextComponent. |
private void |
ensureValidPosition()
This is invoked after the document changes to verify the current dot/mark is valid. |
boolean |
equals(Object obj)
Suppressed: Compares this object to the specified object. |
protected void |
fireStateChanged()
Notifies all listeners that have registered interest for notification on this event type. |
void |
focusGained(FocusEvent e)
Suppressed: Called when the component containing the caret gains focus. |
void |
focusLost(FocusEvent e)
Suppressed: Called when the component containing the caret loses focus. |
(package private) boolean |
getAsynchronousMovement()
Get the flag that determines whether or not asynchronous updates will move the caret. |
int |
getBlinkRate()
Enabled: Gets the caret blink rate. |
ChangeListener[] |
getChangeListeners()
Suppressed: Returns an array of all the change listeners registered on this caret. |
private ClipboardOwner |
getClipboardOwner()
|
protected JTextComponent |
getComponent()
Gets the text editor component that this caret is is bound to. |
int |
getDot()
Enabled: Fetches the current position of the caret. |
(package private) javax.swing.text.Position.Bias |
getDotBias()
|
private javax.swing.text.NavigationFilter.FilterBypass |
getFilterBypass()
|
EventListener[] |
getListeners(Class listenerType)
Suppressed: Returns an array of all the objects currently registered as FooListener s
upon this caret. |
Point |
getMagicCaretPosition()
Enabled: Gets the saved caret position. |
int |
getMark()
Enabled: Fetches the current position of the mark. |
(package private) javax.swing.text.Position.Bias |
getMarkBias()
|
protected javax.swing.text.Highlighter.HighlightPainter |
getSelectionPainter()
Gets the painter for the Highlighter. |
private Clipboard |
getSystemSelection()
|
(package private) javax.swing.text.Position.Bias |
guessBiasForOffset(int offset,
javax.swing.text.Position.Bias lastBias,
boolean lastLTR)
|
(package private) void |
handleMoveDot(int dot,
javax.swing.text.Position.Bias dotBias)
|
(package private) void |
handleSetDot(int dot,
javax.swing.text.Position.Bias dotBias)
|
void |
install(JTextComponent c)
Suppressed: Called when the UI is being installed into the interface of a JTextComponent. |
(package private) boolean |
isDotLeftToRight()
|
(package private) boolean |
isMarkLeftToRight()
|
(package private) boolean |
isPositionLTR(int position,
javax.swing.text.Position.Bias bias)
|
boolean |
isSelectionVisible()
Enabled: Checks whether the current selection is visible. |
boolean |
isVisible()
Enabled: Determines if the caret is currently visible. |
void |
mouseClicked(MouseEvent e)
Suppressed: Called when the mouse is clicked. |
void |
mouseDragged(MouseEvent e)
Suppressed: Moves the caret position according to the mouse pointer's current location. |
void |
mouseEntered(MouseEvent e)
Suppressed: Called when the mouse enters a region. |
void |
mouseExited(MouseEvent e)
Suppressed: Called when the mouse exits a region. |
void |
mouseMoved(MouseEvent e)
Suppressed: Called when the mouse is moved. |
void |
mousePressed(MouseEvent e)
Suppressed: If button 1 is pressed, this is implemented to request focus on the associated text component, and to set the caret position. |
void |
mouseReleased(MouseEvent e)
Suppressed: Called when the mouse is released. |
protected void |
moveCaret(MouseEvent e)
Tries to move the position of the caret from the coordinates of a mouse event, using viewToModel(). |
void |
moveDot(int dot)
Enabled: Moves the caret position to some other position. |
(package private) void |
moveDot(int dot,
javax.swing.text.Position.Bias dotBias)
|
void |
paint(Graphics g)
Suppressed: Renders the caret as a vertical line. |
protected void |
positionCaret(MouseEvent e)
Tries to set the position of the caret from the coordinates of a mouse event, using viewToModel(). |
private void |
readObject(ObjectInputStream s)
|
void |
removeChangeListener(ChangeListener l)
Enabled: Removes a listener that was tracking caret position changes. |
protected void |
repaint()
Cause the caret to be painted. |
(package private) void |
repaintNewCaret()
Repaints the new caret position, with the assumption that this is happening on the event thread so that calling modelToView
is safe. |
(package private) void |
setAsynchronousMovement(boolean m)
Set the flag that determines whether or not asynchronous updates will move the caret. |
void |
setBlinkRate(int rate)
Enabled: Sets the caret blink rate. |
void |
setDot(int dot)
Enabled: Sets the caret position and mark to some position. |
(package private) void |
setDot(int dot,
javax.swing.text.Position.Bias dotBias)
|
void |
setMagicCaretPosition(Point p)
Enabled: Saves the current caret position. |
void |
setSelectionVisible(boolean vis)
Enabled: Changes the selection visibility. |
void |
setVisible(boolean e)
Enabled: Sets the caret visibility, and repaints the caret. |
String |
toString()
Suppressed: |
private void |
updateSystemSelection()
|
private void |
writeObject(ObjectOutputStream s)
|
Methods inherited from class java.awt.Rectangle |
add, add, add, contains, contains, contains, contains, createIntersection, createUnion, getBounds, getBounds2D, getHeight, getLocation, getSize, getWidth, getX, getY, grow, inside, intersection, intersects, isEmpty, move, outcode, reshape, resize, setBounds, setBounds, setLocation, setLocation, setRect, setSize, setSize, translate, union |
Methods inherited from class java.lang.Object |
clone, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Methods inherited from interface java.awt.Shape |
contains, contains, contains, contains, getPathIterator, getPathIterator, intersects, intersects |
Field Detail |
protected EventListenerList listenerList
protected transient ChangeEvent changeEvent
JTextComponent component
boolean async
boolean visible
int dot
int mark
Object selectionTag
boolean selectionVisible
Timer flasher
Point magicCaretPosition
transient javax.swing.text.Position.Bias dotBias
transient javax.swing.text.Position.Bias markBias
boolean dotLTR
boolean markLTR
transient javax.swing.text.DefaultCaret.UpdateHandler updateHandler
private transient int[] flagXPoints
private transient int[] flagYPoints
private transient FocusListener focusListener
private transient javax.swing.text.NavigationFilter.FilterBypass filterBypass
private transient ClipboardOwner clipboardOwner
private boolean ownsSelection
private boolean forceCaretPositionChange
private transient boolean shouldHandleRelease
Constructor Detail |
public DefaultCaret()
Method Detail |
boolean getAsynchronousMovement()
void setAsynchronousMovement(boolean m)
m
- move the caret on asynchronous
updates if true.protected final JTextComponent getComponent()
protected final void repaint()
This method is thread safe, although most Swing methods are not. Please see Threads and Swing for more information.
protected void damage(Rectangle r)
r
- the current location of the caretpaint(java.awt.Graphics)
protected void adjustVisibility(Rectangle nloc)
nloc
- the new position to scroll toprotected javax.swing.text.Highlighter.HighlightPainter getSelectionPainter()
protected void positionCaret(MouseEvent e)
e
- the mouse eventprotected void moveCaret(MouseEvent e)
e
- the mouse eventpublic void focusGained(FocusEvent e)
focusGained
in interface FocusListener
e
- the focus eventFocusListener.focusGained(java.awt.event.FocusEvent)
public void focusLost(FocusEvent e)
focusLost
in interface FocusListener
e
- the focus eventFocusListener.focusLost(java.awt.event.FocusEvent)
public void mouseClicked(MouseEvent e)
mouseClicked
in interface MouseListener
e
- the mouse eventMouseListener.mouseClicked(java.awt.event.MouseEvent)
public void mousePressed(MouseEvent e)
mousePressed
in interface MouseListener
e
- the mouse eventMouseListener.mousePressed(java.awt.event.MouseEvent)
void adjustCaretAndFocus(MouseEvent e)
private void adjustCaret(MouseEvent e)
private void adjustFocus(boolean inWindow)
inWindow
- if true indicates requestFocusInWindow should be usedpublic void mouseReleased(MouseEvent e)
mouseReleased
in interface MouseListener
e
- the mouse eventMouseListener.mouseReleased(java.awt.event.MouseEvent)
public void mouseEntered(MouseEvent e)
mouseEntered
in interface MouseListener
e
- the mouse eventMouseListener.mouseEntered(java.awt.event.MouseEvent)
public void mouseExited(MouseEvent e)
mouseExited
in interface MouseListener
e
- the mouse eventMouseListener.mouseExited(java.awt.event.MouseEvent)
public void mouseDragged(MouseEvent e)
mouseDragged
in interface MouseMotionListener
e
- the mouse eventMouseMotionListener.mouseDragged(java.awt.event.MouseEvent)
public void mouseMoved(MouseEvent e)
mouseMoved
in interface MouseMotionListener
e
- the mouse eventMouseMotionListener.mouseMoved(java.awt.event.MouseEvent)
public void paint(Graphics g)
If there are multiple text directions present in the associated document, a flag indicating the caret bias will be rendered. This will occur only if the associated document is a subclass of AbstractDocument and there are multiple bidi levels present in the bidi element structure (i.e. the text has multiple directions associated with it).
paint
in interface Caret
g
- the graphics contextdamage(java.awt.Rectangle)
public void install(JTextComponent c)
install
in interface Caret
c
- the componentCaret.install(javax.swing.text.JTextComponent)
public void deinstall(JTextComponent c)
deinstall
in interface Caret
c
- the componentCaret.deinstall(javax.swing.text.JTextComponent)
public void addChangeListener(ChangeListener l)
addChangeListener
in interface Caret
l
- the listenerCaret.addChangeListener(javax.swing.event.ChangeListener)
public void removeChangeListener(ChangeListener l)
removeChangeListener
in interface Caret
l
- the listenerCaret.removeChangeListener(javax.swing.event.ChangeListener)
public ChangeListener[] getChangeListeners()
ChangeListener
s
or an empty
array if no change listeners are currently registeredaddChangeListener(javax.swing.event.ChangeListener)
,
removeChangeListener(javax.swing.event.ChangeListener)
protected void fireStateChanged()
EventListenerList
public EventListener[] getListeners(Class listenerType)
FooListener
s
upon this caret.
FooListener
s are registered using the
addFooListener
method.
You can specify the listenerType
argument
with a class literal,
such as
FooListener.class
.
For example, you can query a
DefaultCaret
c
for its change listeners with the following code:
ChangeListener[] cls = (ChangeListener[])(c.getListeners(ChangeListener.class));If no such listeners exist, this method returns an empty array.
listenerType
- the type of listeners requested; this parameter
should specify an interface that descends from
java.util.EventListener
FooListener
s on this component,
or an empty array if no such
listeners have been addedgetChangeListeners()
public void setSelectionVisible(boolean vis)
setSelectionVisible
in interface Caret
vis
- the new visibilitypublic boolean isSelectionVisible()
isSelectionVisible
in interface Caret
public boolean isVisible()
isVisible
in interface Caret
Caret.isVisible()
public void setVisible(boolean e)
setVisible
in interface Caret
e
- the visibility specifierCaret.setVisible(boolean)
public void setBlinkRate(int rate)
setBlinkRate
in interface Caret
rate
- the rate in milliseconds, 0 to stop blinkingCaret.setBlinkRate(int)
public int getBlinkRate()
getBlinkRate
in interface Caret
Caret.getBlinkRate()
public int getDot()
getDot
in interface Caret
Caret.getDot()
public int getMark()
getMark
in interface Caret
Caret.getMark()
public void setDot(int dot)
setDot
in interface Caret
dot
- the position >= 0Caret.setDot(int)
public void moveDot(int dot)
moveDot
in interface Caret
dot
- the position >= 0Caret.moveDot(int)
void moveDot(int dot, javax.swing.text.Position.Bias dotBias)
void handleMoveDot(int dot, javax.swing.text.Position.Bias dotBias)
void setDot(int dot, javax.swing.text.Position.Bias dotBias)
void handleSetDot(int dot, javax.swing.text.Position.Bias dotBias)
javax.swing.text.Position.Bias getDotBias()
javax.swing.text.Position.Bias getMarkBias()
boolean isDotLeftToRight()
boolean isMarkLeftToRight()
boolean isPositionLTR(int position, javax.swing.text.Position.Bias bias)
javax.swing.text.Position.Bias guessBiasForOffset(int offset, javax.swing.text.Position.Bias lastBias, boolean lastLTR)
void changeCaretPosition(int dot, javax.swing.text.Position.Bias dotBias)
void repaintNewCaret()
modelToView
is safe.
private void updateSystemSelection()
private Clipboard getSystemSelection()
private ClipboardOwner getClipboardOwner()
private void ensureValidPosition()
NavigationFilter
changed where to position the dot, that resulted in the current location
being bogus.
public void setMagicCaretPosition(Point p)
setMagicCaretPosition
in interface Caret
p
- the positiongetMagicCaretPosition()
public Point getMagicCaretPosition()
getMagicCaretPosition
in interface Caret
Caret.setMagicCaretPosition(java.awt.Point)
public boolean equals(Object obj)
equals
in class Rectangle
obj
- the object to compare this font with
true
if the objects are equal;
false
otherwisepublic String toString()
toString
in class Rectangle
String
representing this
Rectangle
object's coordinate and size values.private javax.swing.text.NavigationFilter.FilterBypass getFilterBypass()
private boolean _contains(int X, int Y, int W, int H)
private void readObject(ObjectInputStream s) throws ClassNotFoundException, IOException
ClassNotFoundException
IOException
private void writeObject(ObjectOutputStream s) throws IOException
IOException
|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |