Class LineNumberList

All Implemented Interfaces:
MouseListener, MouseMotionListener, ImageObserver, MenuContainer, Serializable, EventListener, Accessible, MouseInputListener

public class LineNumberList extends AbstractGutterComponent implements MouseInputListener
Renders line numbers in the gutter.
Version:
1.0
See Also:
  • Field Details

    • 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 Map<?,?> aaHints
    • mouseDragStartOffset

      private int mouseDragStartOffset
    • l

      Listens for events from the current text area.
    • textAreaInsets

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

      private 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 Color currentLineNumberColor
      The color of current line number.
    • DEFAULT_LINE_NUMBER_COLOR

      public static final Color DEFAULT_LINE_NUMBER_COLOR
    • DEFAULT_LINE_NUMBER_FORMATTER

      public static final LineNumberFormatter DEFAULT_LINE_NUMBER_FORMATTER
  • Constructor Details

    • 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, 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, Color numberColor, 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 Details

    • 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 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:
    • getLineNumberingStartIndex

      public int getLineNumberingStartIndex()
      Returns the starting line's line number. The default value is 1.
      Returns:
      The index
      See Also:
    • getLineNumberFormatter

      public LineNumberFormatter getLineNumberFormatter()
      Returns the line number formatter. The default value is DEFAULT_LINE_NUMBER_FORMATTER
      Returns:
      The formatter
      See Also:
    • getPreferredSize

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

      public void mouseClicked(MouseEvent e)
      Specified by:
      mouseClicked in interface MouseListener
    • mouseDragged

      public void mouseDragged(MouseEvent e)
      Specified by:
      mouseDragged in interface MouseMotionListener
    • mouseEntered

      public void mouseEntered(MouseEvent e)
      Specified by:
      mouseEntered in interface MouseListener
    • mouseExited

      public void mouseExited(MouseEvent e)
      Specified by:
      mouseExited in interface MouseListener
    • mouseMoved

      public void mouseMoved(MouseEvent e)
      Specified by:
      mouseMoved in interface MouseMotionListener
    • mousePressed

      public void mousePressed(MouseEvent e)
      Specified by:
      mousePressed in interface MouseListener
    • mouseReleased

      public void mouseReleased(MouseEvent e)
      Specified by:
      mouseReleased in interface MouseListener
    • paintComponent

      protected void paintComponent(Graphics g)
      Paints this component.
      Overrides:
      paintComponent in class JComponent
      Parameters:
      g - The graphics context.
    • paintWrappedLineNumbers

      private void paintWrappedLineNumbers(Graphics g, 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 class AbstractGutterComponent
    • repaintLine

      private void repaintLine(int line)
      Repaints a single line in this list.
      Parameters:
      line - The line to repaint.
    • setCurrentLineNumberColor

      public void setCurrentLineNumberColor(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:
    • setFont

      public void setFont(Font font)
      Overridden to ensure line number cell sizes are updated with the font size change.
      Overrides:
      setFont in class 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:
    • 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:
    • setTextArea

      public void setTextArea(RTextArea textArea)
      Sets the text area being displayed.
      Overrides:
      setTextArea in class AbstractGutterComponent
      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 of textArea.
    • updateCellWidths

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