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:
java.awt.event.FocusListener,java.awt.event.MouseListener,java.awt.event.MouseMotionListener,java.awt.Shape,java.io.Serializable,java.lang.Cloneable,java.util.EventListener,javax.swing.text.Caret
public class ConfigurableCaret extends javax.swing.text.DefaultCaretThe caret used byRTextArea. This caret has all the properties thatjavax.swing.text.DefaultCaretdoes, as well as adding the following niceties:- This caret can render itself many different ways; see the
setStyle(CaretStyle)method andCaretStylefor 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:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private classConfigurableCaret.FoldAwareNavigationFilterKeeps 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 classConfigurableCaret.SelectionTypeDescribes the current selection behavior.
-
Field Summary
Fields Modifier and Type Field Description private booleanalwaysVisibleprivate booleanpasteOnMiddleMouseClickWhether this caret will try to paste into the editor (assuming it is editable) on middle-mouse clicks.private javax.swing.text.SegmentsegUsed for fastest-possible retrieval of the character at the caret's position in the document.private intselectionEndThe offset into the document where selection ended.private ChangeableHighlightPainterselectionPainterThe selection painter.private intselectionStartThe offset into the document where selection started.private ConfigurableCaret.SelectionTypeselectionTypeDefines the current selection behavior.private CaretStylestyleWhether the caret is a vertical line, a horizontal line, or a block.
-
Constructor Summary
Constructors Constructor Description ConfigurableCaret()Creates the caret usingCaretStyle.THICK_VERTICAL_LINE_STYLE.ConfigurableCaret(CaretStyle style)Constructs a newConfigurableCaret.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description private voidadjustCaret(java.awt.event.MouseEvent e)Adjusts the caret location based on the MouseEvent.private voidadjustFocus(boolean inWindow)Adjusts the focus, if necessary.protected voiddamage(java.awt.Rectangle r)Overridden to damage the correct width of the caret, since this caret can be different sizes.voiddeinstall(javax.swing.text.JTextComponent c)Called when the UI is being removed from the interface of a JTextComponent.booleangetPasteOnMiddleMouseClick()Returns whether this caret will paste the contents of the clipboard into the editor (assuming it is editable) on middle-mouse-button clicks.booleangetRoundedSelectionEdges()Returns whether this caret's selection uses rounded edges.protected javax.swing.text.Highlighter.HighlightPaintergetSelectionPainter()Gets the painter for the Highlighter.CaretStylegetStyle()Gets the current style of this caret.protected RTextAreagetTextArea()Gets the text editor component that this caret is bound to.voidinstall(javax.swing.text.JTextComponent c)Installs this caret on a text component.booleanisAlwaysVisible()Returns whether this caret is always visible (as opposed to blinking, or not visible when the editor's window is not focused).voidmouseClicked(java.awt.event.MouseEvent e)Called when the mouse is clicked.voidmouseDragged(java.awt.event.MouseEvent e)voidmousePressed(java.awt.event.MouseEvent e)Called when the mouse is clicked in the editor.voidmouseReleased(java.awt.event.MouseEvent e)voidpaint(java.awt.Graphics g)Paints the cursor.private voidselect(int mark, int dot)voidsetAlwaysVisible(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).voidsetPasteOnMiddleMouseClick(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.voidsetRoundedSelectionEdges(boolean rounded)Sets whether this caret's selection should have rounded edges.voidsetSelectionVisible(boolean visible)Overridden to always render the selection, even when the text component loses focus.voidsetStyle(CaretStyle style)Sets the style used when painting the caret.private voidvalidateWidth(java.awt.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
-
-
-
-
Field Detail
-
seg
private transient javax.swing.text.Segment seg
Used for fastest-possible retrieval of the character at the caret's position in the document.
-
style
private CaretStyle style
Whether the caret is a vertical line, a horizontal line, or a block.
-
selectionPainter
private ChangeableHighlightPainter selectionPainter
The selection painter. By default, this paints selections with the text area's selection color.
-
alwaysVisible
private boolean alwaysVisible
-
pasteOnMiddleMouseClick
private boolean pasteOnMiddleMouseClick
Whether this caret will try to paste into the editor (assuming it is editable) on middle-mouse clicks.
-
selectionStart
private int selectionStart
The offset into the document where selection started.
-
selectionEnd
private int selectionEnd
The offset into the document where selection ended.
-
selectionType
private ConfigurableCaret.SelectionType selectionType
Defines the current selection behavior.
-
-
Constructor Detail
-
ConfigurableCaret
public ConfigurableCaret()
Creates the caret usingCaretStyle.THICK_VERTICAL_LINE_STYLE.
-
ConfigurableCaret
public ConfigurableCaret(CaretStyle style)
Constructs a newConfigurableCaret.- Parameters:
style- The style to use when painting the caret. If this is invalid, thenCaretStyle.THICK_VERTICAL_LINE_STYLEis used.
-
-
Method Detail
-
adjustCaret
private void adjustCaret(java.awt.event.MouseEvent e)
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
protected void damage(java.awt.Rectangle r)
Overridden to damage the correct width of the caret, since this caret can be different sizes.- Overrides:
damagein classjavax.swing.text.DefaultCaret- Parameters:
r- The current location of the caret.
-
deinstall
public void deinstall(javax.swing.text.JTextComponent c)
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:
deinstallin interfacejavax.swing.text.Caret- Overrides:
deinstallin classjavax.swing.text.DefaultCaret- Parameters:
c- The text component. If this is not anRTextArea, anExceptionwill 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:
setPasteOnMiddleMouseClick(boolean)
-
getTextArea
protected RTextArea 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:
setRoundedSelectionEdges(boolean)
-
getSelectionPainter
protected javax.swing.text.Highlighter.HighlightPainter getSelectionPainter()
Gets the painter for the Highlighter. This is overridden to return our custom selection painter.- Overrides:
getSelectionPainterin classjavax.swing.text.DefaultCaret- Returns:
- The painter.
-
getStyle
public CaretStyle getStyle()
Gets the current style of this caret.- Returns:
- The caret's style.
- See Also:
setStyle(CaretStyle)
-
install
public void install(javax.swing.text.JTextComponent c)
Installs this caret on a text component.- Specified by:
installin interfacejavax.swing.text.Caret- Overrides:
installin classjavax.swing.text.DefaultCaret- Parameters:
c- The text component. If this is not anRTextArea, anExceptionwill 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:
setAlwaysVisible(boolean)
-
mouseClicked
public void mouseClicked(java.awt.event.MouseEvent e)
Called when the mouse is clicked. Implements pasting the system selection when the middle mouse button is clicked.- Specified by:
mouseClickedin interfacejava.awt.event.MouseListener- Overrides:
mouseClickedin classjavax.swing.text.DefaultCaret- Parameters:
e- The mouse event.
-
mouseDragged
public void mouseDragged(java.awt.event.MouseEvent e)
- Specified by:
mouseDraggedin interfacejava.awt.event.MouseMotionListener- Overrides:
mouseDraggedin classjavax.swing.text.DefaultCaret
-
mousePressed
public void mousePressed(java.awt.event.MouseEvent e)
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:
mousePressedin interfacejava.awt.event.MouseListener- Overrides:
mousePressedin classjavax.swing.text.DefaultCaret- Parameters:
e- The mouse event.
-
mouseReleased
public void mouseReleased(java.awt.event.MouseEvent e)
- Specified by:
mouseReleasedin interfacejava.awt.event.MouseListener- Overrides:
mouseReleasedin classjavax.swing.text.DefaultCaret
-
paint
public void paint(java.awt.Graphics g)
Paints the cursor.- Specified by:
paintin interfacejavax.swing.text.Caret- Overrides:
paintin classjavax.swing.text.DefaultCaret- 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:
isAlwaysVisible()
-
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:
getPasteOnMiddleMouseClick()
-
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:
getRoundedSelectionEdges()
-
setSelectionVisible
public void setSelectionVisible(boolean visible)
Overridden to always render the selection, even when the text component loses focus.- Specified by:
setSelectionVisiblein interfacejavax.swing.text.Caret- Overrides:
setSelectionVisiblein classjavax.swing.text.DefaultCaret- Parameters:
visible- Whether the selection should be visible. This parameter is ignored.
-
setStyle
public void setStyle(CaretStyle style)
Sets the style used when painting the caret.- Parameters:
style- The style to use. This should not benull.- See Also:
getStyle()
-
validateWidth
private void validateWidth(java.awt.Rectangle rect)
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
Viewclasses in the javax.swing.text package always return a width of "1" whenmodelToViewis called. We'll be needing the actual width. - Even in smart views, such as
RSyntaxTextArea'sSyntaxViewandWrappedSyntaxViewthat return the width of the current character, if the caret is at the end of a line for example, the width returned frommodelToViewwill 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'smodelToViewmethod for the caret position.
- The
-
-