Package org.fife.ui.rtextarea
Class ConfigurableCaret
java.lang.Object
java.awt.geom.RectangularShape
java.awt.geom.Rectangle2D
java.awt.Rectangle
javax.swing.text.DefaultCaret
org.fife.ui.rtextarea.ConfigurableCaret
- All Implemented Interfaces:
FocusListener
,MouseListener
,MouseMotionListener
,Shape
,Serializable
,Cloneable
,EventListener
,Caret
The caret used by
RTextArea
. This caret has all the properties
that javax.swing.text.DefaultCaret
does, as well as adding the
following niceties:
- This caret can render itself many different ways; see the
setStyle(CaretStyle)
method andCaretStyle
for more information. - On Microsoft Windows and other operating systems that do not support system selection (i.e., selecting text, then pasting via the middle mouse button), clicking the middle mouse button will cause a regular paste operation to occur. On systems that support system selection (i.e., all UNIX variants), the middle mouse button will behave normally.
- Version:
- 0.6
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate class
Keeps the caret out of folded regions in edge cases where it doesn't happen automatically, for example, when the caret moves automatically in response to Document.insert() and Document.remove() calls.static enum
Describes the current selection behavior.Nested classes/interfaces inherited from class java.awt.geom.Rectangle2D
Rectangle2D.Double, Rectangle2D.Float
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate boolean
private boolean
Whether this caret will try to paste into the editor (assuming it is editable) on middle-mouse clicks.private Segment
Used for fastest-possible retrieval of the character at the caret's position in the document.private int
The offset into the document where selection ended.private ChangeableHighlightPainter
The selection painter.private int
The offset into the document where selection started.private ConfigurableCaret.SelectionType
Defines the current selection behavior.private CaretStyle
Whether the caret is a vertical line, a horizontal line, or a block.Fields inherited from class javax.swing.text.DefaultCaret
ALWAYS_UPDATE, changeEvent, listenerList, NEVER_UPDATE, UPDATE_WHEN_ON_EDT
Fields inherited from class java.awt.geom.Rectangle2D
OUT_BOTTOM, OUT_LEFT, OUT_RIGHT, OUT_TOP
-
Constructor Summary
ConstructorsConstructorDescriptionCreates the caret usingCaretStyle.THICK_VERTICAL_LINE_STYLE
.ConfigurableCaret
(CaretStyle style) Constructs a newConfigurableCaret
. -
Method Summary
Modifier and TypeMethodDescriptionprivate void
Adjusts the caret location based on the MouseEvent.private void
adjustFocus
(boolean inWindow) Adjusts the focus, if necessary.protected void
Overridden to damage the correct width of the caret, since this caret can be different sizes.void
Called when the UI is being removed from the interface of a JTextComponent.boolean
Returns whether this caret will paste the contents of the clipboard into the editor (assuming it is editable) on middle-mouse-button clicks.boolean
Returns whether this caret's selection uses rounded edges.protected Highlighter.HighlightPainter
Gets the painter for the Highlighter.getStyle()
Gets the current style of this caret.protected RTextArea
Gets the text editor component that this caret is bound to.void
Installs this caret on a text component.boolean
Returns whether this caret is always visible (as opposed to blinking, or not visible when the editor's window is not focused).void
Called when the mouse is clicked.void
void
Called when the mouse is clicked in the editor.void
void
Paints the cursor.private void
select
(int mark, int dot) void
setAlwaysVisible
(boolean alwaysVisible) Toggles whether this caret should always be visible (as opposed to blinking, or not visible when the editor's window is not focused).void
setPasteOnMiddleMouseClick
(boolean paste) Sets whether this caret will paste the contents of the clipboard into the editor (assuming it is editable) on middle-mouse-button clicks.void
setRoundedSelectionEdges
(boolean rounded) Sets whether this caret's selection should have rounded edges.void
setSelectionVisible
(boolean visible) Overridden to always render the selection, even when the text component loses focus.void
setStyle
(CaretStyle style) Sets the style used when painting the caret.private void
validateWidth
(Rectangle rect) Helper function used by the block and underline carets to ensure the width of the painted caret is valid.Methods inherited from class javax.swing.text.DefaultCaret
addChangeListener, adjustVisibility, equals, fireStateChanged, focusGained, focusLost, getBlinkRate, getChangeListeners, getComponent, getDot, getDotBias, getListeners, getMagicCaretPosition, getMark, getMarkBias, getUpdatePolicy, isActive, isSelectionVisible, isVisible, mouseEntered, mouseExited, mouseMoved, moveCaret, moveDot, moveDot, positionCaret, removeChangeListener, repaint, setBlinkRate, setDot, setDot, setMagicCaretPosition, setUpdatePolicy, setVisible, toString
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.awt.geom.Rectangle2D
add, add, add, contains, contains, getPathIterator, getPathIterator, hashCode, intersect, intersects, intersectsLine, intersectsLine, outcode, setFrame, setRect, union
Methods inherited from class java.awt.geom.RectangularShape
clone, contains, contains, getCenterX, getCenterY, getFrame, getMaxX, getMaxY, getMinX, getMinY, intersects, setFrame, setFrame, setFrameFromCenter, setFrameFromCenter, setFrameFromDiagonal, setFrameFromDiagonal
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.awt.Shape
contains, contains, contains, contains, getPathIterator, getPathIterator, intersects, intersects
-
Field Details
-
seg
Used for fastest-possible retrieval of the character at the caret's position in the document. -
style
Whether the caret is a vertical line, a horizontal line, or a block. -
selectionPainter
The selection painter. By default, this paints selections with the text area's selection color. -
alwaysVisible
private boolean alwaysVisible -
pasteOnMiddleMouseClick
private boolean pasteOnMiddleMouseClickWhether this caret will try to paste into the editor (assuming it is editable) on middle-mouse clicks. -
selectionStart
private int selectionStartThe offset into the document where selection started. -
selectionEnd
private int selectionEndThe offset into the document where selection ended. -
selectionType
Defines the current selection behavior.
-
-
Constructor Details
-
ConfigurableCaret
public ConfigurableCaret()Creates the caret usingCaretStyle.THICK_VERTICAL_LINE_STYLE
. -
ConfigurableCaret
Constructs a newConfigurableCaret
.- Parameters:
style
- The style to use when painting the caret. If this is invalid, thenCaretStyle.THICK_VERTICAL_LINE_STYLE
is used.
-
-
Method Details
-
adjustCaret
Adjusts the caret location based on the MouseEvent. -
adjustFocus
private void adjustFocus(boolean inWindow) Adjusts the focus, if necessary.- Parameters:
inWindow
- if true indicates requestFocusInWindow should be used
-
damage
Overridden to damage the correct width of the caret, since this caret can be different sizes.- Overrides:
damage
in classDefaultCaret
- Parameters:
r
- The current location of the caret.
-
deinstall
Called when the UI is being removed from the interface of a JTextComponent. This is used to unregister any listeners that were attached.- Specified by:
deinstall
in interfaceCaret
- Overrides:
deinstall
in classDefaultCaret
- Parameters:
c
- The text component. If this is not anRTextArea
, anException
will be thrown.
-
getPasteOnMiddleMouseClick
public boolean getPasteOnMiddleMouseClick()Returns whether this caret will paste the contents of the clipboard into the editor (assuming it is editable) on middle-mouse-button clicks.- Returns:
- Whether a paste operation will be performed.
- See Also:
-
getTextArea
Gets the text editor component that this caret is bound to.- Returns:
- The
RTextArea
.
-
getRoundedSelectionEdges
public boolean getRoundedSelectionEdges()Returns whether this caret's selection uses rounded edges.- Returns:
- Whether this caret's edges are rounded.
- See Also:
-
getSelectionPainter
Gets the painter for the Highlighter. This is overridden to return our custom selection painter.- Overrides:
getSelectionPainter
in classDefaultCaret
- Returns:
- The painter.
-
getStyle
Gets the current style of this caret.- Returns:
- The caret's style.
- See Also:
-
install
Installs this caret on a text component.- Specified by:
install
in interfaceCaret
- Overrides:
install
in classDefaultCaret
- Parameters:
c
- The text component. If this is not anRTextArea
, anException
will be thrown.
-
isAlwaysVisible
public boolean isAlwaysVisible()Returns whether this caret is always visible (as opposed to blinking, or not visible when the editor's window is not focused). This can be used by popup windows that want the caret's location to still be visible for contextual purposes while they are displayed.- Returns:
- Whether this caret is always visible.
- See Also:
-
mouseClicked
Called when the mouse is clicked. Implements pasting the system selection when the middle mouse button is clicked.- Specified by:
mouseClicked
in interfaceMouseListener
- Overrides:
mouseClicked
in classDefaultCaret
- Parameters:
e
- The mouse event.
-
mouseDragged
- Specified by:
mouseDragged
in interfaceMouseMotionListener
- Overrides:
mouseDragged
in classDefaultCaret
-
mousePressed
Called when the mouse is clicked in the editor. Implements the following behaviors:- Select-by-word on double-click
- Select-buy-line on triple-click
- Focus the editor on right-clicks (e.g. when popup menu is shown)
- Specified by:
mousePressed
in interfaceMouseListener
- Overrides:
mousePressed
in classDefaultCaret
- Parameters:
e
- The mouse event.
-
mouseReleased
- Specified by:
mouseReleased
in interfaceMouseListener
- Overrides:
mouseReleased
in classDefaultCaret
-
paint
Paints the cursor.- Specified by:
paint
in interfaceCaret
- Overrides:
paint
in classDefaultCaret
- Parameters:
g
- The graphics context in which to paint.
-
select
private void select(int mark, int dot) -
setAlwaysVisible
public void setAlwaysVisible(boolean alwaysVisible) Toggles whether this caret should always be visible (as opposed to blinking, or not visible when the editor's window is not focused). This can be used by popup windows that want the caret's location to still be visible for contextual purposes while they are displayed.- Parameters:
alwaysVisible
- Whether this caret should always be visible.- See Also:
-
setPasteOnMiddleMouseClick
public void setPasteOnMiddleMouseClick(boolean paste) Sets whether this caret will paste the contents of the clipboard into the editor (assuming it is editable) on middle-mouse-button clicks.- Parameters:
paste
- Whether a paste operation will be performed.- See Also:
-
setRoundedSelectionEdges
public void setRoundedSelectionEdges(boolean rounded) Sets whether this caret's selection should have rounded edges.- Parameters:
rounded
- Whether it should have rounded edges.- See Also:
-
setSelectionVisible
public void setSelectionVisible(boolean visible) Overridden to always render the selection, even when the text component loses focus.- Specified by:
setSelectionVisible
in interfaceCaret
- Overrides:
setSelectionVisible
in classDefaultCaret
- Parameters:
visible
- Whether the selection should be visible. This parameter is ignored.
-
setStyle
Sets the style used when painting the caret.- Parameters:
style
- The style to use. This should not benull
.- See Also:
-
validateWidth
Helper function used by the block and underline carets to ensure the width of the painted caret is valid. This is done for the following reasons:- The
View
classes in the javax.swing.text package always return a width of "1" whenmodelToView
is called. We'll be needing the actual width. - Even in smart views, such as
RSyntaxTextArea
'sSyntaxView
andWrappedSyntaxView
that return the width of the current character, if the caret is at the end of a line for example, the width returned frommodelToView
will be 0 (as the width of unprintable characters such as '\n' is calculated as 0). In this case, we'll use a default width value.
- Parameters:
rect
- The rectangle returned by the currentView
'smodelToView
method for the caret position.
- The
-