Class SyntaxView

  • All Implemented Interfaces:
    SwingConstants, TabExpander, TokenOrientedView

    public class SyntaxView
    extends View
    implements TabExpander, TokenOrientedView
    The javax.swing.text.View object used by RSyntaxTextArea when word wrap is disabled. It implements syntax highlighting for programming languages using the colors and font styles specified by the RSyntaxTextArea.

    You don't really have to do anything to use this class, as RSyntaxTextAreaUI automatically sets the text area's view to be an instance of this class if word wrap is disabled.

    The tokens that specify how to paint the syntax-highlighted text are gleaned from the text area's RSyntaxDocument.

    • Constructor Detail

      • SyntaxView

        public SyntaxView​(Element elem)
        Constructs a new SyntaxView wrapped around an element.
        Parameters:
        elem - The element representing the text to display.
    • Method Detail

      • damageLineRange

        protected void damageLineRange​(int line0,
                                       int line1,
                                       Shape a,
                                       Component host)
        Repaint the given line range.
        Parameters:
        line0 - The starting line number to repaint. This must be a valid line number in the model.
        line1 - The ending line number to repaint. This must be a valid line number in the model.
        a - The region allocated for the view to render into.
        host - The component hosting the view (used to call repaint).
      • getNextVisualPositionFrom

        public int getNextVisualPositionFrom​(int pos,
                                             Position.Bias b,
                                             Shape a,
                                             int direction,
                                             Position.Bias[] biasRet)
                                      throws BadLocationException
        Provides a way to determine the next visually represented model location that one might place a caret. Some views may not be visible, they might not be in the same order found in the model, or they just might not allow access to some of the locations in the model.
        Overrides:
        getNextVisualPositionFrom in class View
        Parameters:
        pos - the position to convert >= 0
        a - the allocated region to render into
        direction - the direction from the current position that can be thought of as the arrow keys typically found on a keyboard. This may be SwingConstants.WEST, SwingConstants.EAST, SwingConstants.NORTH, or SwingConstants.SOUTH.
        Returns:
        the location within the model that best represents the next location visual position.
        Throws:
        BadLocationException
        IllegalArgumentException - for an invalid direction
      • getPreferredSpan

        public float getPreferredSpan​(int axis)
        Determines the preferred span for this view along an axis.
        Specified by:
        getPreferredSpan in class View
        Parameters:
        axis - may be either View.X_AXIS or View.Y_AXIS
        Returns:
        the span the view would like to be rendered into >= 0. Typically the view is told to render into the span that is returned, although there is no guarantee. The parent may choose to resize or break the view.
        Throws:
        IllegalArgumentException - for an invalid axis
      • getTokenListForPhysicalLineAbove

        public Token getTokenListForPhysicalLineAbove​(int offset)
        Returns a token list for the physical line above the physical line containing the specified offset into the document. Note that for this plain (non-wrapped) view, this is simply the token list for the logical line above the line containing offset, since lines are not wrapped.
        Specified by:
        getTokenListForPhysicalLineAbove in interface TokenOrientedView
        Parameters:
        offset - The offset in question.
        Returns:
        A token list for the physical (and in this view, logical) line before this one. If offset is in the first line in the document, null is returned.
      • getTokenListForPhysicalLineBelow

        public Token getTokenListForPhysicalLineBelow​(int offset)
        Returns a token list for the physical line below the physical line containing the specified offset into the document. Note that for this plain (non-wrapped) view, this is simply the token list for the logical line below the line containing offset, since lines are not wrapped.
        Specified by:
        getTokenListForPhysicalLineBelow in interface TokenOrientedView
        Parameters:
        offset - The offset in question.
        Returns:
        A token list for the physical (and in this view, logical) line after this one. If offset is in the last physical line in the document, null is returned.
      • insertUpdate

        public void insertUpdate​(DocumentEvent changes,
                                 Shape a,
                                 ViewFactory f)
        Gives notification that something was inserted into the document in a location that this view is responsible for.
        Overrides:
        insertUpdate in class View
        Parameters:
        changes - The change information from the associated document.
        a - The current allocation of the view.
        f - The factory to use to rebuild if the view has children.
      • lineToRect

        protected Rectangle lineToRect​(Shape a,
                                       int line)
        Determine the rectangle that represents the given line.
        Parameters:
        a - The region allocated for the view to render into
        line - The line number to find the region of. This must be a valid line number in the model.
      • modelToView

        public Shape modelToView​(int p0,
                                 Position.Bias b0,
                                 int p1,
                                 Position.Bias b1,
                                 Shape a)
                          throws BadLocationException
        Provides a mapping, for a given region, from the document model coordinate space to the view coordinate space. The specified region is created as a union of the first and last character positions.

        This is implemented to subtract the width of the second character, as this view's modelToView actually returns the width of the character instead of "1" or "0" like the View implementations in javax.swing.text. Thus, if we don't override this method, the View implementation will return one character's width too much for its consumers (implementations of javax.swing.text.Highlighter).

        Overrides:
        modelToView in class View
        Parameters:
        p0 - the position of the first character (>=0)
        b0 - The bias of the first character position, toward the previous character or the next character represented by the offset, in case the position is a boundary of two views; b0 will have one of these values:
        • Position.Bias.Forward
        • Position.Bias.Backward
        p1 - the position of the last character (>=0)
        b1 - the bias for the second character position, defined one of the legal values shown above
        a - the area of the view, which encompasses the requested region
        Returns:
        the bounding box which is a union of the region specified by the first and last character positions
        Throws:
        BadLocationException - if the given position does not represent a valid location in the associated document
        IllegalArgumentException - if b0 or b1 are not one of the legal Position.Bias values listed above
        See Also:
        View.viewToModel(float, float, java.awt.Shape, javax.swing.text.Position.Bias[])
      • nextTabStop

        public float nextTabStop​(float x,
                                 int tabOffset)
        Returns the next tab stop position after a given reference position. This implementation does not support things like centering so it ignores the tabOffset argument.
        Specified by:
        nextTabStop in interface TabExpander
        Parameters:
        x - the current position >= 0
        tabOffset - the position within the text stream that the tab occurred at >= 0.
        Returns:
        the tab stop, measured in points >= 0
      • paint

        public void paint​(Graphics g,
                          Shape a)
        Actually paints the text area. Only lines that have been damaged are repainted.
        Specified by:
        paint in class View
        Parameters:
        g - The graphics context with which to paint.
        a - The allocated region in which to render.
      • removeUpdate

        public void removeUpdate​(DocumentEvent changes,
                                 Shape a,
                                 ViewFactory f)
        Gives notification that something was removed from the document in a location that this view is responsible for.
        Overrides:
        removeUpdate in class View
        Parameters:
        changes - the change information from the associated document
        a - the current allocation of the view
        f - the factory to use to rebuild if the view has children
      • setSize

        public void setSize​(float width,
                            float height)
        Overrides:
        setSize in class View
      • updateDamage

        protected void updateDamage​(DocumentEvent changes,
                                    Shape a,
                                    ViewFactory f)
        Repaint the region of change covered by the given document event. Damages the line that begins the range to cover the case when the insert/remove is only on one line. If lines are added or removed, damages the whole view. The longest line is checked to see if it has changed.
      • viewToModel

        public int viewToModel​(float fx,
                               float fy,
                               Shape a,
                               Position.Bias[] bias)
        Provides a mapping from the view coordinate space to the logical coordinate space of the model.
        Specified by:
        viewToModel in class View
        Parameters:
        fx - the X coordinate >= 0
        fy - the Y coordinate >= 0
        a - the allocated region to render into
        Returns:
        the location within the model that best represents the given point in the view >= 0