Package org.fife.ui.rtextarea
Class LineNumberList
- java.lang.Object
-
- java.awt.Component
-
- java.awt.Container
-
- javax.swing.JComponent
-
- javax.swing.JPanel
-
- org.fife.ui.rtextarea.AbstractGutterComponent
-
- org.fife.ui.rtextarea.LineNumberList
-
- All Implemented Interfaces:
java.awt.event.MouseListener
,java.awt.event.MouseMotionListener
,java.awt.image.ImageObserver
,java.awt.MenuContainer
,java.io.Serializable
,java.util.EventListener
,javax.accessibility.Accessible
,javax.swing.event.MouseInputListener
public class LineNumberList extends AbstractGutterComponent implements javax.swing.event.MouseInputListener
Renders line numbers in the gutter.- Version:
- 1.0
- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private class
LineNumberList.Listener
Listens for events in the text area we're interested in.private static class
LineNumberList.SimpleLineNumberFormatter
-
Nested classes/interfaces inherited from class javax.swing.JPanel
javax.swing.JPanel.AccessibleJPanel
-
Nested classes/interfaces inherited from class javax.swing.JComponent
javax.swing.JComponent.AccessibleJComponent
-
-
Field Summary
Fields Modifier and Type Field Description private java.util.Map<?,?>
aaHints
private int
ascent
private int
cellHeight
private int
cellWidth
private int
currentLine
private java.awt.Color
currentLineNumberColor
The color of current line number.static java.awt.Color
DEFAULT_LINE_NUMBER_COLOR
static LineNumberFormatter
DEFAULT_LINE_NUMBER_FORMATTER
private LineNumberList.Listener
l
Listens for events from the current text area.private int
lastVisibleLine
private int
lastY
private LineNumberFormatter
lineNumberFormatter
Formats line numbers into a string to be displayed.private int
lineNumberingStartIndex
The index at which line numbering should start.private int
mouseDragStartOffset
private java.awt.Insets
textAreaInsets
Used inpaintComponent(Graphics)
to prevent reallocation on each paint.private java.awt.Rectangle
visibleRect
Used inpaintComponent(Graphics)
to prevent reallocation on each paint.-
Fields inherited from class org.fife.ui.rtextarea.AbstractGutterComponent
currentLineCount, textArea
-
Fields inherited from class javax.swing.JComponent
listenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW
-
-
Constructor Summary
Constructors Constructor Description LineNumberList(RTextArea textArea)
Constructs a newLineNumberList
using default values for line number color (gray) and highlighting the current line.LineNumberList(RTextArea textArea, java.awt.Color numberColor)
Constructs a newLineNumberList
.LineNumberList(RTextArea textArea, java.awt.Color numberColor, java.awt.Color currentLineNumberColor)
Constructs a newLineNumberList
.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addNotify()
Overridden to set width of this component correctly when we are first displayed (as keying off of the RTextArea gives us (0,0) when it isn't yet displayed.private int
calculateLastVisibleLineNumber()
Calculates the last line number index painted in this component.java.awt.Color
getCurrentLineNumberColor()
Returns the color to use when painting the current line's line number.LineNumberFormatter
getLineNumberFormatter()
Returns the line number formatter.int
getLineNumberingStartIndex()
Returns the starting line's line number.java.awt.Dimension
getPreferredSize()
private int
getRhsBorderWidth()
Returns the width of the empty border on this component's right-hand side (or left-hand side, if the orientation is RTL).(package private) void
handleDocumentEvent(javax.swing.event.DocumentEvent e)
Called when text is inserted to or removed from the text area.protected void
init()
Called by the constructor before the text area is set.(package private) void
lineHeightsChanged()
Called when the line heights of the text area change.void
mouseClicked(java.awt.event.MouseEvent e)
void
mouseDragged(java.awt.event.MouseEvent e)
void
mouseEntered(java.awt.event.MouseEvent e)
void
mouseExited(java.awt.event.MouseEvent e)
void
mouseMoved(java.awt.event.MouseEvent e)
void
mousePressed(java.awt.event.MouseEvent e)
void
mouseReleased(java.awt.event.MouseEvent e)
protected void
paintComponent(java.awt.Graphics g)
Paints this component.private void
paintWrappedLineNumbers(java.awt.Graphics g, java.awt.Rectangle visibleRect)
Paints line numbers for text areas with line wrap enabled.void
removeNotify()
Called when this component is removed from the view hierarchy.private void
repaintLine(int line)
Repaints a single line in this list.void
setCurrentLineNumberColor(java.awt.Color color)
Sets the color to use when painting the current line's line number.void
setFont(java.awt.Font font)
Overridden to ensure line number cell sizes are updated with the font size change.void
setLineNumberFormatter(LineNumberFormatter formatter)
Sets a custom line number formatter.void
setLineNumberingStartIndex(int index)
Sets the starting line's line number.void
setTextArea(RTextArea textArea)
Sets the text area being displayed.private void
updateCellHeights()
Changes the height of the cells in the JList so that they are as tall as font.(package private) void
updateCellWidths()
Changes the width of the cells in the JList so you can see every digit of each.-
Methods inherited from class org.fife.ui.rtextarea.AbstractGutterComponent
getChildViewBounds, getGutter
-
Methods inherited from class javax.swing.JPanel
getAccessibleContext, getUI, getUIClassID, paramString, setUI, updateUI
-
Methods inherited from class javax.swing.JComponent
addAncestorListener, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, firePropertyChange, firePropertyChange, firePropertyChange, fireVetoableChange, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBaseline, getBaselineResizeBehavior, getBorder, getBounds, getClientProperty, getComponentGraphics, getComponentPopupMenu, getConditionForKeyStroke, getDebugGraphicsOptions, getDefaultLocale, getFontMetrics, getGraphics, getHeight, getInheritsPopupMenu, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getListeners, getLocation, getMaximumSize, getMinimumSize, getNextFocusableComponent, getPopupLocation, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getToolTipText, getTopLevelAncestor, getTransferHandler, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, hide, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOpaque, isOptimizedDrawingEnabled, isPaintingForPrint, isPaintingOrigin, isPaintingTile, isRequestFocusEnabled, isValidateRoot, paint, paintBorder, paintChildren, paintImmediately, paintImmediately, print, printAll, printBorder, printChildren, printComponent, processComponentKeyEvent, processKeyBinding, processKeyEvent, processMouseEvent, processMouseMotionEvent, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removeVetoableChangeListener, repaint, repaint, requestDefaultFocus, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, resetKeyboardActions, reshape, revalidate, scrollRectToVisible, setActionMap, setAlignmentX, setAlignmentY, setAutoscrolls, setBackground, setBorder, setComponentPopupMenu, setDebugGraphicsOptions, setDefaultLocale, setDoubleBuffered, setEnabled, setFocusTraversalKeys, setForeground, setInheritsPopupMenu, setInputMap, setInputVerifier, setMaximumSize, setMinimumSize, setNextFocusableComponent, setOpaque, setPreferredSize, setRequestFocusEnabled, setToolTipText, setTransferHandler, setUI, setVerifyInputWhenFocusTarget, setVisible, unregisterKeyboardAction, update
-
Methods inherited from class java.awt.Container
add, add, add, add, add, addContainerListener, addImpl, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getComponentZOrder, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getLayout, getMousePosition, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicyProvider, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paintComponents, preferredSize, printComponents, processContainerEvent, processEvent, remove, remove, removeAll, removeContainerListener, setComponentZOrder, setFocusCycleRoot, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, setLayout, transferFocusDownCycle, validate, validateTree
-
Methods inherited from class java.awt.Component
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, bounds, checkImage, checkImage, coalesceEvents, contains, createImage, createImage, createVolatileImage, createVolatileImage, disableEvents, dispatchEvent, enable, enableEvents, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getBackground, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getForeground, getGraphicsConfiguration, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocale, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getToolkit, getTreeLock, gotFocus, handleEvent, hasFocus, imageUpdate, inside, isBackgroundSet, isCursorSet, isDisplayable, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isPreferredSizeSet, isShowing, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, postEvent, prepareImage, prepareImage, processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, requestFocus, requestFocus, requestFocusInWindow, resize, resize, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, setLocation, setLocation, setMixingCutoutShape, setName, setSize, setSize, show, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCycle
-
-
-
-
Field Detail
-
currentLine
private int currentLine
-
lastY
private int lastY
-
lastVisibleLine
private int lastVisibleLine
-
cellHeight
private int cellHeight
-
cellWidth
private int cellWidth
-
ascent
private int ascent
-
aaHints
private java.util.Map<?,?> aaHints
-
mouseDragStartOffset
private int mouseDragStartOffset
-
l
private LineNumberList.Listener l
Listens for events from the current text area.
-
textAreaInsets
private java.awt.Insets textAreaInsets
Used inpaintComponent(Graphics)
to prevent reallocation on each paint.
-
visibleRect
private java.awt.Rectangle visibleRect
Used inpaintComponent(Graphics)
to prevent reallocation on each paint.
-
lineNumberingStartIndex
private int lineNumberingStartIndex
The index at which line numbering should start. The default value is1
, but applications can change this if, for example, they are displaying a subset of lines in a file.
-
lineNumberFormatter
private LineNumberFormatter lineNumberFormatter
Formats line numbers into a string to be displayed.
-
currentLineNumberColor
private java.awt.Color currentLineNumberColor
The color of current line number.
-
DEFAULT_LINE_NUMBER_COLOR
public static final java.awt.Color DEFAULT_LINE_NUMBER_COLOR
-
DEFAULT_LINE_NUMBER_FORMATTER
public static final LineNumberFormatter DEFAULT_LINE_NUMBER_FORMATTER
-
-
Constructor Detail
-
LineNumberList
public LineNumberList(RTextArea textArea)
Constructs a newLineNumberList
using default values for line number color (gray) and highlighting the current line.- Parameters:
textArea
- The text component for which line numbers will be displayed.
-
LineNumberList
public LineNumberList(RTextArea textArea, java.awt.Color numberColor)
Constructs a newLineNumberList
.- Parameters:
textArea
- The text component for which line numbers will be displayed.numberColor
- The color to use for the line numbers. If this isnull
, gray will be used.
-
LineNumberList
public LineNumberList(RTextArea textArea, java.awt.Color numberColor, java.awt.Color currentLineNumberColor)
Constructs a newLineNumberList
.- Parameters:
textArea
- The text component for which line numbers will be displayed.numberColor
- The color to use for the line numbers. If this isnull
, gray will be used.currentLineNumberColor
- The color to use for the current line number. If this isnull
, the current line's number will not have a special color.
-
-
Method Detail
-
addNotify
public void addNotify()
Overridden to set width of this component correctly when we are first displayed (as keying off of the RTextArea gives us (0,0) when it isn't yet displayed.- Overrides:
addNotify
in classAbstractGutterComponent
-
calculateLastVisibleLineNumber
private int calculateLastVisibleLineNumber()
Calculates the last line number index painted in this component.- Returns:
- The last line number index painted in this component.
-
getCurrentLineNumberColor
public java.awt.Color getCurrentLineNumberColor()
Returns the color to use when painting the current line's line number.- Returns:
- The color to use when painting the current line's line
number. If this is
null
, the regular line number color will be used. - See Also:
setCurrentLineNumberColor(Color)
,Component.getForeground()
-
getLineNumberingStartIndex
public int getLineNumberingStartIndex()
Returns the starting line's line number. The default value is1
.- Returns:
- The index
- See Also:
setLineNumberingStartIndex(int)
-
getLineNumberFormatter
public LineNumberFormatter getLineNumberFormatter()
Returns the line number formatter. The default value isDEFAULT_LINE_NUMBER_FORMATTER
- Returns:
- The formatter
- See Also:
setLineNumberFormatter(LineNumberFormatter)
-
getPreferredSize
public java.awt.Dimension getPreferredSize()
- Overrides:
getPreferredSize
in classjavax.swing.JComponent
-
getRhsBorderWidth
private int getRhsBorderWidth()
Returns the width of the empty border on this component's right-hand side (or left-hand side, if the orientation is RTL).- Returns:
- The border width.
-
handleDocumentEvent
void handleDocumentEvent(javax.swing.event.DocumentEvent e)
Description copied from class:AbstractGutterComponent
Called when text is inserted to or removed from the text area. Implementations can take this opportunity to repaint, revalidate, etc.- Specified by:
handleDocumentEvent
in classAbstractGutterComponent
- Parameters:
e
- The document event.
-
init
protected void init()
Description copied from class:AbstractGutterComponent
Called by the constructor before the text area is set. This is a hook to allow subclasses to do any needed initialization. The default implementation does nothing.- Overrides:
init
in classAbstractGutterComponent
-
lineHeightsChanged
void lineHeightsChanged()
Description copied from class:AbstractGutterComponent
Called when the line heights of the text area change. This is usually the result of one or more of the fonts in the editor changing.- Specified by:
lineHeightsChanged
in classAbstractGutterComponent
-
mouseClicked
public void mouseClicked(java.awt.event.MouseEvent e)
- Specified by:
mouseClicked
in interfacejava.awt.event.MouseListener
-
mouseDragged
public void mouseDragged(java.awt.event.MouseEvent e)
- Specified by:
mouseDragged
in interfacejava.awt.event.MouseMotionListener
-
mouseEntered
public void mouseEntered(java.awt.event.MouseEvent e)
- Specified by:
mouseEntered
in interfacejava.awt.event.MouseListener
-
mouseExited
public void mouseExited(java.awt.event.MouseEvent e)
- Specified by:
mouseExited
in interfacejava.awt.event.MouseListener
-
mouseMoved
public void mouseMoved(java.awt.event.MouseEvent e)
- Specified by:
mouseMoved
in interfacejava.awt.event.MouseMotionListener
-
mousePressed
public void mousePressed(java.awt.event.MouseEvent e)
- Specified by:
mousePressed
in interfacejava.awt.event.MouseListener
-
mouseReleased
public void mouseReleased(java.awt.event.MouseEvent e)
- Specified by:
mouseReleased
in interfacejava.awt.event.MouseListener
-
paintComponent
protected void paintComponent(java.awt.Graphics g)
Paints this component.- Overrides:
paintComponent
in classjavax.swing.JComponent
- Parameters:
g
- The graphics context.
-
paintWrappedLineNumbers
private void paintWrappedLineNumbers(java.awt.Graphics g, java.awt.Rectangle visibleRect)
Paints line numbers for text areas with line wrap enabled.- Parameters:
g
- The graphics context.visibleRect
- The visible rectangle of these line numbers.
-
removeNotify
public void removeNotify()
Called when this component is removed from the view hierarchy.- Overrides:
removeNotify
in classAbstractGutterComponent
-
repaintLine
private void repaintLine(int line)
Repaints a single line in this list.- Parameters:
line
- The line to repaint.
-
setCurrentLineNumberColor
public void setCurrentLineNumberColor(java.awt.Color color)
Sets the color to use when painting the current line's line number.- Parameters:
color
- The color to use. If this isnull
, the current line's line number will be painted just like any other.- See Also:
getCurrentLineNumberColor()
-
setFont
public void setFont(java.awt.Font font)
Overridden to ensure line number cell sizes are updated with the font size change.- Overrides:
setFont
in classjavax.swing.JComponent
- Parameters:
font
- The new font to use for line numbers.
-
setLineNumberingStartIndex
public void setLineNumberingStartIndex(int index)
Sets the starting line's line number. The default value is1
. Applications can call this method to change this value if they are displaying a subset of lines in a file, for example.- Parameters:
index
- The new index.- See Also:
getLineNumberingStartIndex()
-
setLineNumberFormatter
public void setLineNumberFormatter(LineNumberFormatter formatter)
Sets a custom line number formatter. Can be called when other number formats are needed like hindu-arabic numerals.- Parameters:
formatter
- The new line number formatter- See Also:
getLineNumberFormatter()
-
setTextArea
public void setTextArea(RTextArea textArea)
Sets the text area being displayed.- Overrides:
setTextArea
in classAbstractGutterComponent
- Parameters:
textArea
- The text area.
-
updateCellHeights
private void updateCellHeights()
Changes the height of the cells in the JList so that they are as tall as font. This function should be called whenever the user changes the Font oftextArea
.
-
updateCellWidths
void updateCellWidths()
Changes the width of the cells in the JList so you can see every digit of each.
-
-