Class Gutter

All Implemented Interfaces:
ImageObserver, MenuContainer, Serializable, Accessible

public class Gutter extends JPanel
The gutter is the component on the left-hand side of the text area that displays optional information such as line numbers, fold regions, and icons (for bookmarks, debugging breakpoints, error markers, etc.).

Icons can be added on a per-line basis to visually mark syntax errors, lines with breakpoints set on them, etc. To add icons to the gutter, you must first call RTextScrollPane.setIconRowHeaderEnabled(boolean) on the parent scroll pane, to make the icon area visible. Then, you can add icons that track either lines in the document, or offsets, via addLineTrackingIcon(int, Icon) and addOffsetTrackingIcon(int, Icon), respectively. To remove an icon you've added, use removeTrackingIcon(GutterIconInfo).

In addition to support for arbitrary per-line icons, this component also has built-in support for displaying icons representing "bookmarks;" that is, lines a user can cycle through via F2 and Shift+F2. Bookmarked lines are toggled via Ctrl+F2. In order to enable bookmarking, you must first assign an icon to represent a bookmarked line, then actually enable the feature:

 Gutter gutter = scrollPane.getGutter();
 gutter.setBookmarkIcon(new ImageIcon("bookmark.png"));
 gutter.setBookmarkingEnabled(true);
 
Version:
1.0
See Also:
  • Field Details

    • DEFAULT_ACTIVE_LINE_RANGE_COLOR

      public static final Color DEFAULT_ACTIVE_LINE_RANGE_COLOR
      The color used to highlight active line ranges if none is specified.
    • textArea

      private RTextArea textArea
      The text area.
    • lineNumberList

      private LineNumberList lineNumberList
      Renders line numbers.
    • lineNumberColor

      private Color lineNumberColor
      The color used to render line numbers.
    • currentLineNumberColor

      private Color currentLineNumberColor
      The color used to render the currently active line's line number. If this is null, lineNumberColor is used.
    • lineNumberingStartIndex

      private int lineNumberingStartIndex
      The starting index for line numbers in the gutter.
    • lineNumberFormatter

      private LineNumberFormatter lineNumberFormatter
      Formats line numbers into a string to be displayed.
    • lineNumberFont

      private Font lineNumberFont
      The font used to render line numbers.
    • iconArea

      private IconRowHeader iconArea
      Renders bookmark icons, breakpoints, error icons, etc.
    • iconRowHeaderInheritsGutterBackground

      private boolean iconRowHeaderInheritsGutterBackground
      Whether the icon area inherits the gutter background (as opposed to painting with its own, default "panel" color).
    • spacingBetweenLineNumbersAndFoldIndicator

      private int spacingBetweenLineNumbersAndFoldIndicator
      Optional additional spacing between the line number component and the fold indicator component.
    • foldIndicator

      private FoldIndicator foldIndicator
      Shows lines that are code-foldable.
    • armed

      private boolean armed
      Whether this gutter, or any child components, are armed. Used internally for e.g. the fold indicator's appearance.
    • listener

      private transient Gutter.TextAreaListener listener
      Listens for events in our text area.
  • Constructor Details

    • Gutter

      public Gutter(RTextArea textArea)
      Constructor.
      Parameters:
      textArea - The parent text area.
  • Method Details

    • addLineTrackingIcon

      public GutterIconInfo addLineTrackingIcon(int line, Icon icon) throws BadLocationException
      Adds an icon that tracks an offset in the document, and is displayed adjacent to the line numbers. This is useful for marking things such as source code errors.
      Parameters:
      line - The line to track (zero-based).
      icon - The icon to display. This should be small (say 16x16).
      Returns:
      A tag for this icon. This can later be used in a call to removeTrackingIcon(GutterIconInfo) to remove this icon.
      Throws:
      BadLocationException - If offs is an invalid offset into the text area.
      See Also:
    • addLineTrackingIcon

      public GutterIconInfo addLineTrackingIcon(int line, Icon icon, String tip) throws BadLocationException
      Adds an icon that tracks an offset in the document, and is displayed adjacent to the line numbers. This is useful for marking things such as source code errors.
      Parameters:
      line - The line to track (zero-based).
      icon - The icon to display. This should be small (say 16x16).
      tip - An optional tool tip for the icon.
      Returns:
      A tag for this icon. This can later be used in a call to removeTrackingIcon(GutterIconInfo) to remove this icon.
      Throws:
      BadLocationException - If offs is an invalid offset into the text area.
      See Also:
    • addOffsetTrackingIcon

      public GutterIconInfo addOffsetTrackingIcon(int offs, Icon icon) throws BadLocationException
      Adds an icon that tracks an offset in the document, and is displayed adjacent to the line numbers. This is useful for marking things such as source code errors.
      Parameters:
      offs - The offset to track.
      icon - The icon to display. This should be small (say 16x16).
      Returns:
      A tag for this icon.
      Throws:
      BadLocationException - If offs is an invalid offset into the text area.
      See Also:
    • addOffsetTrackingIcon

      public GutterIconInfo addOffsetTrackingIcon(int offs, Icon icon, String tip) throws BadLocationException
      Adds an icon that tracks an offset in the document, and is displayed adjacent to the line numbers. This is useful for marking things such as source code errors.
      Parameters:
      offs - The offset to track.
      icon - The icon to display. This should be small (say 16x16).
      tip - An optional tool tip for the icon.
      Returns:
      A tag for this icon.
      Throws:
      BadLocationException - If offs is an invalid offset into the text area.
      See Also:
    • clearActiveLineRange

      private void clearActiveLineRange()
      Clears the active line range.
      See Also:
    • getActiveLineRangeColor

      public Color getActiveLineRangeColor()
      Returns the color used to paint the active line range, if any.
      Returns:
      The color.
      See Also:
    • getArmedFoldBackground

      public Color getArmedFoldBackground()
      Returns the background color used by the (default) fold icons when they are armed.
      Returns:
      The background color.
      See Also:
    • getBookmarkIcon

      public Icon getBookmarkIcon()
      Returns the icon to use for bookmarks.
      Returns:
      The icon to use for bookmarks. If this is null, bookmarking is effectively disabled.
      See Also:
    • getBookmarks

      public GutterIconInfo[] getBookmarks()
      Returns the bookmarks known to this gutter.
      Returns:
      The bookmarks. If there are no bookmarks, an empty array is returned.
      See Also:
    • getBorderColor

      public Color getBorderColor()
      Returns the color of the "border" line.
      Returns:
      The color.
      See Also:
    • getCurrentLineNumberColor

      public Color getCurrentLineNumberColor()
      The color used to render the currently active line's line number. If this is null, getLineNumberColor() is used.
      Returns:
      The color.
      See Also:
    • getExpandedFoldRenderStrategy

      public ExpandedFoldRenderStrategy getExpandedFoldRenderStrategy()
      Returns the strategy to use for rendering expanded folds.
      Returns:
      The strategy to use for rendering expanded folds.
      See Also:
    • getFoldBackground

      public Color getFoldBackground()
      Returns the background color used by the (default) fold icons.
      Returns:
      The background color.
      See Also:
    • getFoldIndicatorArmedForeground

      public Color getFoldIndicatorArmedForeground()
      Returns the foreground color of the fold indicator for armed folds.
      Returns:
      The foreground color of the fold indicator for armed folds.
      See Also:
    • getFoldIndicatorForeground

      public Color getFoldIndicatorForeground()
      Returns the foreground color of the fold indicator.
      Returns:
      The foreground color of the fold indicator.
      See Also:
    • getIconRowHeaderInheritsGutterBackground

      public boolean getIconRowHeaderInheritsGutterBackground()
      Returns whether the icon area inherits the gutter background (as opposed to painting with its own, default "panel" color, which is the default).
      Returns:
      Whether the gutter background is used in the icon row header.
      See Also:
    • getLineNumberColor

      public Color getLineNumberColor()
      Returns the color to use to paint line numbers.
      Returns:
      The color used when painting line numbers.
      See Also:
    • getLineNumberFont

      public Font getLineNumberFont()
      Returns the font used for line numbers.
      Returns:
      The font used for line numbers.
      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 LineNumberList.DEFAULT_LINE_NUMBER_FORMATTER
      Returns:
      The formatter.
      See Also:
    • getLineNumbersEnabled

      public boolean getLineNumbersEnabled()
      Returns true if the line numbers are enabled and visible.
      Returns:
      Whether line numbers are visible.
    • getShowCollapsedRegionToolTips

      public boolean getShowCollapsedRegionToolTips()
      Returns whether tool tips are displayed showing the contents of collapsed fold regions when the mouse hovers over a +/- icon.
      Returns:
      Whether these tool tips are displayed.
      See Also:
    • getSpacingBetweenLineNumbersAndFoldIndicator

      public int getSpacingBetweenLineNumbersAndFoldIndicator()
      Returns the additional spacing between the line number list and fold indicator. By default this is a small amount; if you want something larger, you can increase it. Note this value takes effect whether both line numbers and the fold indicator are enabled, so use it only when both are enabled.
      Returns:
      The additional spacing.
      See Also:
    • getTrackingIcons

      public GutterIconInfo[] getTrackingIcons(Point p) throws BadLocationException
      Returns the tracking icons at the specified view position.
      Parameters:
      p - The view position.
      Returns:
      The tracking icons at that position. If there are no tracking icons there, this will be an empty array.
      Throws:
      BadLocationException - If p is invalid.
    • isArmed

      public boolean isArmed()
      Returns whether this gutter is "armed", that is, any child components are armed. This is used by the internal API and should not be called.
      Returns:
      Whether the gutter is armed.
      See Also:
    • isFoldIndicatorEnabled

      public boolean isFoldIndicatorEnabled()
      Returns whether the fold indicator is enabled.
      Returns:
      Whether the fold indicator is enabled.
      See Also:
    • isBookmarkingEnabled

      public boolean isBookmarkingEnabled()
      Returns whether bookmarking is enabled.
      Returns:
      Whether bookmarking is enabled.
      See Also:
    • isIconRowHeaderEnabled

      public boolean isIconRowHeaderEnabled()
      Returns whether the icon row header is enabled.
      Returns:
      Whether the icon row header is enabled.
    • removeAllTrackingIcons

      public void removeAllTrackingIcons()
      Removes all tracking icons.
      See Also:
    • removeTrackingIcon

      public void removeTrackingIcon(GutterIconInfo tag)
      Removes the specified tracking icon.
      Parameters:
      tag - A tag for an icon in the gutter, as returned from either addLineTrackingIcon(int, Icon) or addOffsetTrackingIcon(int, Icon).
      See Also:
    • setActiveLineRangeColor

      public void setActiveLineRangeColor(Color color)
      Sets the color to use to render active line ranges.
      Parameters:
      color - The color to use. If this is null, then the default color is used.
      See Also:
    • setActiveLineRange

      private void setActiveLineRange(int startLine, int endLine)
      Highlights a range of lines in the icon area. This, of course, will only be visible if the icon area is visible.
      Parameters:
      startLine - The start of the line range.
      endLine - The end of the line range.
      See Also:
    • setArmed

      void setArmed(boolean armed)
      Toggles whether this gutter is "armed", that is, any child components are armed. This is used by the internal API and should not be called.
      Parameters:
      armed - Whether the gutter is armed.
      See Also:
    • setArmedFoldBackground

      public void setArmedFoldBackground(Color bg)
      Sets the background color used by the (default) fold icons when they are armed.
      Parameters:
      bg - The new background color. If this is null, then armed fold icons will not render with a special color.
      See Also:
    • setBookmarkIcon

      public void setBookmarkIcon(Icon icon)
      Sets the icon to use for bookmarks.
      Parameters:
      icon - The new bookmark icon. If this is null, bookmarking is effectively disabled.
      See Also:
    • setBookmarkingEnabled

      public void setBookmarkingEnabled(boolean enabled)
      Sets whether bookmarking is enabled. Note that a bookmarking icon must be set via setBookmarkIcon(Icon) before bookmarks are truly enabled.
      Parameters:
      enabled - Whether bookmarking is enabled.
      See Also:
    • setBorderColor

      public void setBorderColor(Color color)
      Sets the color for the "border" line.
      Parameters:
      color - The new color.
      See Also:
    • setComponentOrientation

      public void setComponentOrientation(ComponentOrientation o)
      Overrides:
      setComponentOrientation in class Component
    • setCurrentLineNumberColor

      public void setCurrentLineNumberColor(Color color)
      Sets the color used to render the currently active line's line number. If this is null, getLineNumberColor() is used.
      Parameters:
      color - The color to use.
      See Also:
    • setExpandedFoldRenderStrategy

      public void setExpandedFoldRenderStrategy(ExpandedFoldRenderStrategy strategy)
      Sets the strategy to use for rendering expanded folds.
      Parameters:
      strategy - The strategy to use. This cannot be null.
      See Also:
    • setFoldIcons

      public void setFoldIcons(FoldIndicatorIcon collapsedIcon, FoldIndicatorIcon expandedIcon)
      Sets the icons to use to represent collapsed and expanded folds. This method can be used for further customization after setting this component's general appearance via setFoldIndicatorStyle(FoldIndicatorStyle).
      Parameters:
      collapsedIcon - The collapsed fold icon. This cannot be null.
      expandedIcon - The expanded fold icon. This cannot be null.
      See Also:
    • setFoldIndicatorEnabled

      public void setFoldIndicatorEnabled(boolean enabled)
      Toggles whether the fold indicator is enabled.
      Parameters:
      enabled - Whether the fold indicator should be enabled.
      See Also:
    • setFoldIndicatorStyle

      public void setFoldIndicatorStyle(FoldIndicatorStyle style)
      Toggles the presentation of the fold region of this component. This method sets the icons used for fold regions to default values, amongst other configuration. To further customize those icons, see setFoldIcons(FoldIndicatorIcon, FoldIndicatorIcon).
      Parameters:
      style - The new presentation style.
      See Also:
    • setFoldBackground

      public void setFoldBackground(Color bg)
      Sets the background color used by the (default) fold icons.
      Parameters:
      bg - The new background color.
      See Also:
    • setFoldIndicatorArmedForeground

      public void setFoldIndicatorArmedForeground(Color fg)
      Sets the foreground color used by the fold indicator for armed folds.
      Parameters:
      fg - The new armed fold indicator foreground.
      See Also:
    • setFoldIndicatorForeground

      public void setFoldIndicatorForeground(Color fg)
      Sets the foreground color used by the fold indicator.
      Parameters:
      fg - The new fold indicator foreground.
      See Also:
    • setIconRowHeaderEnabled

      public void setIconRowHeaderEnabled(boolean enabled)
      Toggles whether the icon row header (used for breakpoints, bookmarks, etc.) is enabled.

      Most clients do not need to call this method directly. This is usually handled by `RTextScrollPane` directly. Calling this directly may require the caller to ensure this `gutter` is visible and sized properly in its parent container.

      Parameters:
      enabled - Whether the icon row header is enabled.
      See Also:
    • setIconRowHeaderInheritsGutterBackground

      public void setIconRowHeaderInheritsGutterBackground(boolean inherits)
      Sets whether the icon area inherits the gutter background (as opposed to painting with its own, default "panel" color, which is the default).
      Parameters:
      inherits - Whether the gutter background should be used in the icon row header. If this is false, a default, Look-and-feel-dependent color is used.
      See Also:
    • setLineNumberColor

      public void setLineNumberColor(Color color)
      Sets the color to use to paint line numbers.
      Parameters:
      color - The color to use when painting line numbers.
      See Also:
    • setLineNumberFont

      public void setLineNumberFont(Font font)
      Sets the font used for line numbers.
      Parameters:
      font - The font to use. This cannot be null.
      See Also:
    • 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:
    • setLineNumbersEnabled

      public void setLineNumbersEnabled(boolean enabled)
      Toggles whether line numbers are visible.

      Most clients do not need to call this method directly. This is usually handled by `RTextScrollPane` directly. Calling this directly may require the caller to ensure this `gutter` is visible and sized properly in its parent container.

      Parameters:
      enabled - Whether line numbers should be visible.
      See Also:
    • setShowCollapsedRegionToolTips

      public void setShowCollapsedRegionToolTips(boolean show)
      Toggles whether tool tips should be displayed showing the contents of collapsed fold regions when the mouse hovers over a +/- icon.
      Parameters:
      show - Whether to show these tool tips.
      See Also:
    • setSpacingBetweenLineNumbersAndFoldIndicator

      public void setSpacingBetweenLineNumbersAndFoldIndicator(int spacing)
      Sets additional spacing between the line number list and fold indicator. By default this is a small amount; if you want something larger, you can increase it. Note this value takes effect whether both line numbers and the fold indicator are enabled, so use it only when both are enabled.
      Parameters:
      spacing - The additional spacing. This should be >= 0.
      See Also:
    • setTextArea

      public void setTextArea(RTextArea textArea)
      Sets the text area being displayed. This will clear any tracking icons currently displayed.

      Most clients do not need to call this method directly. This is usually handled by `RTextScrollPane` directly.

      Parameters:
      textArea - The text area.
    • toggleBookmark

      public boolean toggleBookmark(int line) throws BadLocationException
      Programmatically toggles whether there is a bookmark for the specified line. If bookmarking is not enabled, this method does nothing.
      Parameters:
      line - The line.
      Returns:
      Whether a bookmark is now at the specified line.
      Throws:
      BadLocationException - If line is an invalid line number in the text area.
      See Also:
    • setBorder

      public void setBorder(Border border)
      Overrides:
      setBorder in class JComponent