Class 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
      • Nested classes/interfaces inherited from class java.awt.Container

        java.awt.Container.AccessibleAWTContainer
      • Nested classes/interfaces inherited from class java.awt.Component

        java.awt.Component.AccessibleAWTComponent, java.awt.Component.BaselineResizeBehavior, java.awt.Component.BltBufferStrategy, java.awt.Component.FlipBufferStrategy
    • Constructor Summary

      Constructors 
      Constructor Description
      LineNumberList​(RTextArea textArea)
      Constructs a new LineNumberList using default values for line number color (gray) and highlighting the current line.
      LineNumberList​(RTextArea textArea, java.awt.Color numberColor)
      Constructs a new LineNumberList.
      LineNumberList​(RTextArea textArea, java.awt.Color numberColor, java.awt.Color currentLineNumberColor)
      Constructs a new LineNumberList.
    • 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 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
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • 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
      • textAreaInsets

        private java.awt.Insets textAreaInsets
        Used in paintComponent(Graphics) to prevent reallocation on each paint.
      • visibleRect

        private java.awt.Rectangle visibleRect
        Used in paintComponent(Graphics) to prevent reallocation on each paint.
      • lineNumberingStartIndex

        private int lineNumberingStartIndex
        The index at which line numbering should start. The default value is 1, 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 new LineNumberList 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 new LineNumberList.
        Parameters:
        textArea - The text component for which line numbers will be displayed.
        numberColor - The color to use for the line numbers. If this is null, gray will be used.
      • LineNumberList

        public LineNumberList​(RTextArea textArea,
                              java.awt.Color numberColor,
                              java.awt.Color currentLineNumberColor)
        Constructs a new LineNumberList.
        Parameters:
        textArea - The text component for which line numbers will be displayed.
        numberColor - The color to use for the line numbers. If this is null, gray will be used.
        currentLineNumberColor - The color to use for the current line number. If this is null, 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 class AbstractGutterComponent
      • 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 is 1.
        Returns:
        The index
        See Also:
        setLineNumberingStartIndex(int)
      • getPreferredSize

        public java.awt.Dimension getPreferredSize()
        Overrides:
        getPreferredSize in class javax.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 class AbstractGutterComponent
        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 class AbstractGutterComponent
      • mouseClicked

        public void mouseClicked​(java.awt.event.MouseEvent e)
        Specified by:
        mouseClicked in interface java.awt.event.MouseListener
      • mouseDragged

        public void mouseDragged​(java.awt.event.MouseEvent e)
        Specified by:
        mouseDragged in interface java.awt.event.MouseMotionListener
      • mouseEntered

        public void mouseEntered​(java.awt.event.MouseEvent e)
        Specified by:
        mouseEntered in interface java.awt.event.MouseListener
      • mouseExited

        public void mouseExited​(java.awt.event.MouseEvent e)
        Specified by:
        mouseExited in interface java.awt.event.MouseListener
      • mouseMoved

        public void mouseMoved​(java.awt.event.MouseEvent e)
        Specified by:
        mouseMoved in interface java.awt.event.MouseMotionListener
      • mousePressed

        public void mousePressed​(java.awt.event.MouseEvent e)
        Specified by:
        mousePressed in interface java.awt.event.MouseListener
      • mouseReleased

        public void mouseReleased​(java.awt.event.MouseEvent e)
        Specified by:
        mouseReleased in interface java.awt.event.MouseListener
      • paintComponent

        protected void paintComponent​(java.awt.Graphics g)
        Paints this component.
        Overrides:
        paintComponent in class javax.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.
      • 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 is null, 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 class javax.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 is 1. 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()
      • 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 of textArea.
      • updateCellWidths

        void updateCellWidths()
        Changes the width of the cells in the JList so you can see every digit of each.