Class SyntaxView
- All Implemented Interfaces:
SwingConstants, TabExpander, RSTAView, TokenOrientedView
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.
- Version:
- 0.3
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate intprivate intprivate intprivate FontThe default font used by the text area.private RSyntaxTextAreaCached for each paint() call so each drawLine() call has access to it.private intCached values to speed up the painting a tad.private ElementThe current longest line.private floatprivate FontMetricsFont metrics for the current font.private intprivate intprivate TokenImplTemporary token used when we need to "modify" tokens for rendering purposes.Fields inherited from class View
BadBreakWeight, ExcellentBreakWeight, ForcedBreakWeight, GoodBreakWeight, X_AXIS, Y_AXISFields inherited from interface SwingConstants
BOTTOM, CENTER, EAST, HORIZONTAL, LEADING, LEFT, NEXT, NORTH, NORTH_EAST, NORTH_WEST, PREVIOUS, RIGHT, SOUTH, SOUTH_EAST, SOUTH_WEST, TOP, TRAILING, VERTICAL, WEST -
Constructor Summary
ConstructorsConstructorDescriptionSyntaxView(Element elem) Constructs a newSyntaxViewwrapped around an element. -
Method Summary
Modifier and TypeMethodDescription(package private) voidIterate over the lines represented by the child elements of the element this view represents, looking for the line that is the longest.voidchangedUpdate(DocumentEvent changes, Shape a, ViewFactory f) Gives notification from the document that attributes were changed in a location that this view is responsible for.protected voiddamageLineRange(int line0, int line1, Shape a, Component host) Repaint the given line range.private floatdrawLine(TokenPainter painter, Token token, Graphics2D g, float x, float y, int line) Draws the passed-in text using syntax highlighting for the current language.private floatdrawLineWithSelection(TokenPainter painter, Token token, Graphics2D g, float x, float y, int selStart, int selEnd) Draws the passed-in text using syntax highlighting for the current language.private floatgetLineWidth(int lineNumber) Calculates the width of the line represented by the given element.intgetNextVisualPositionFrom(int pos, Position.Bias b, Shape a, int direction, Position.Bias[] biasRet) Provides a way to determine the next visually represented model location that one might place a caret.floatgetPreferredSpan(int axis) Determines the preferred span for this view along an axis.private intWorkaround for JTextComponents allowing the caret to be rendered entirely off-screen if the entire "previous" character fit entirely.private intReturns the tab size set for the document, defaulting to 5.getTokenListForPhysicalLineAbove(int offset) Returns a token list for the physical line above the physical line containing the specified offset into the document.getTokenListForPhysicalLineBelow(int offset) Returns a token list for the physical line below the physical line containing the specified offset into the document.voidinsertUpdate(DocumentEvent changes, Shape a, ViewFactory f) Gives notification that something was inserted into the document in a location that this view is responsible for.protected RectanglelineToRect(Shape a, int line) Determine the rectangle that represents the given line.modelToView(int pos, Shape a, Position.Bias b) Provides a mapping from the document model coordinate space to the coordinate space of the view mapped to it.modelToView(int p0, Position.Bias b0, int p1, Position.Bias b1, Shape a) Provides a mapping, for a given region, from the document model coordinate space to the view coordinate space.floatnextTabStop(float x, int tabOffset) Returns the next tab stop position after a given reference position.voidActually paints the text area.private booleanpossiblyUpdateLongLine(Element line, int lineNumber) If the passed-in line is longer than the current longest line, then the longest line is updated.voidremoveUpdate(DocumentEvent changes, Shape a, ViewFactory f) Gives notification that something was removed from the document in a location that this view is responsible for.voidsetSize(float width, float height) protected voidupdateDamage(DocumentEvent changes, Shape a, ViewFactory f) Repaint the region of change covered by the given document event.private voidChecks to see if the font metrics and longest line are up-to-date.intviewToModel(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.intReturns the y-coordinate of the specified line.intyForLineContaining(Rectangle alloc, int offs) Returns the y-coordinate of the line containing a specified offset.Methods inherited from class View
append, breakView, createFragment, forwardUpdate, forwardUpdateToView, getAlignment, getAttributes, getBreakWeight, getChildAllocation, getContainer, getDocument, getElement, getEndOffset, getGraphics, getMaximumSpan, getMinimumSpan, getParent, getResizeWeight, getStartOffset, getToolTipText, getView, getViewCount, getViewFactory, getViewIndex, getViewIndex, insert, isVisible, modelToView, preferenceChanged, remove, removeAll, replace, setParent, updateChildren, updateLayout, viewToModel
-
Field Details
-
font
The default font used by the text area. If this changes we need to recalculate the longest line. -
metrics
Font metrics for the current font. -
longLine
The current longest line. This is used to calculate the preferred width of the view. Since the calculation is potentially expensive, we try to avoid it by stashing which line is currently the longest. -
longLineWidth
private float longLineWidth -
tabSize
private int tabSize -
tabBase
private int tabBase -
host
Cached for each paint() call so each drawLine() call has access to it. -
lineHeight
private int lineHeightCached values to speed up the painting a tad. -
ascent
private int ascent -
clipStart
private int clipStart -
clipEnd
private int clipEnd -
tempToken
Temporary token used when we need to "modify" tokens for rendering purposes. Since tokens returned from RSyntaxDocuments are treated as immutable, we use this temporary token to do that work.
-
-
Constructor Details
-
SyntaxView
Constructs a newSyntaxViewwrapped around an element.- Parameters:
elem- The element representing the text to display.
-
-
Method Details
-
calculateLongestLine
void calculateLongestLine()Iterate over the lines represented by the child elements of the element this view represents, looking for the line that is the longest. The longLine variable is updated to represent the longest line contained. The font variable is updated to indicate the font used to calculate the longest line. -
changedUpdate
Gives notification from the document that attributes were changed in a location that this view is responsible for.- Overrides:
changedUpdatein classView- Parameters:
changes- the change information from the associated documenta- the current allocation of the viewf- the factory to use to rebuild if the view has children- See Also:
-
damageLineRange
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).
-
drawLine
Draws the passed-in text using syntax highlighting for the current language. It is assumed that the entire line is either not in a selected region, or painting with a selection-foreground color is turned off.- Parameters:
painter- The painter to render the tokens.token- The list of tokens to draw.g- The graphics context in which to draw.x- The x-coordinate at which to draw.y- The y-coordinate at which to draw.- Returns:
- The x-coordinate representing the end of the painted text.
-
drawLineWithSelection
private float drawLineWithSelection(TokenPainter painter, Token token, Graphics2D g, float x, float y, int selStart, int selEnd) Draws the passed-in text using syntax highlighting for the current language. Tokens are checked for being in a selected region, and are rendered appropriately if they are.- Parameters:
painter- The painter to render the tokens.token- The list of tokens to draw.g- The graphics context in which to draw.x- The x-coordinate at which to draw.y- The y-coordinate at which to draw.selStart- The start of the selection.selEnd- The end of the selection.- Returns:
- The x-coordinate representing the end of the painted text.
-
getLineWidth
private float getLineWidth(int lineNumber) Calculates the width of the line represented by the given element.- Parameters:
lineNumber- The line number of the specified line in the document.- Returns:
- The width of the line.
-
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 locations in the model.- Overrides:
getNextVisualPositionFromin classView- Parameters:
pos- the position to convert >= 0a- the allocated region to render intodirection- the direction from the current position that can be thought of as the arrow keys typically found on a keyboard. This may beSwingConstants.WEST,SwingConstants.EAST,SwingConstants.NORTH, orSwingConstants.SOUTH.- Returns:
- the location within the model that best represents the next location visual position.
- Throws:
BadLocationException- If the offset specified is invalid.IllegalArgumentException- for an invalid direction
-
getPreferredSpan
public float getPreferredSpan(int axis) Determines the preferred span for this view along an axis.- Specified by:
getPreferredSpanin classView- 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
-
getRhsCorrection
private int getRhsCorrection()Workaround for JTextComponents allowing the caret to be rendered entirely off-screen if the entire "previous" character fit entirely.- Returns:
- The amount of space to add to the x-axis preferred span.
-
getTabSize
private int getTabSize()Returns the tab size set for the document, defaulting to 5.- Returns:
- The tab size.
-
getTokenListForPhysicalLineAbove
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 containingoffset, since lines are not wrapped.- Specified by:
getTokenListForPhysicalLineAbovein interfaceTokenOrientedView- Parameters:
offset- The offset in question.- Returns:
- A token list for the physical (and in this view, logical) line
before this one. If
offsetis in the first line in the document,nullis returned.
-
getTokenListForPhysicalLineBelow
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 containingoffset, since lines are not wrapped.- Specified by:
getTokenListForPhysicalLineBelowin interfaceTokenOrientedView- Parameters:
offset- The offset in question.- Returns:
- A token list for the physical (and in this view, logical) line
after this one. If
offsetis in the last physical line in the document,nullis returned.
-
insertUpdate
Gives notification that something was inserted into the document in a location that this view is responsible for.- Overrides:
insertUpdatein classView- 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
-
modelToView
Provides a mapping from the document model coordinate space to the coordinate space of the view mapped to it.- Specified by:
modelToViewin classView- Parameters:
pos- the position to convert >= 0a- the allocated region to render into- Returns:
- the bounding box of the given position
- Throws:
BadLocationException- if the given position does not represent a valid location in the associated document- See Also:
-
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
modelToViewactually returns the width of the character instead of "1" or "0" like the View implementations injavax.swing.text. Thus, if we don't override this method, theViewimplementation will return one character's width too much for its consumers (implementations ofjavax.swing.text.Highlighter).- Overrides:
modelToViewin classView- 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;b0will 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 abovea- 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 documentIllegalArgumentException- ifb0orb1are not one of the legalPosition.Biasvalues listed above- See Also:
-
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:
nextTabStopin interfaceTabExpander- Parameters:
x- the current position >= 0tabOffset- the position within the text stream that the tab occurred at >= 0.- Returns:
- the tab stop, measured in points >= 0
-
paint
-
possiblyUpdateLongLine
If the passed-in line is longer than the current longest line, then the longest line is updated.- Parameters:
line- The line to test against the current longest.lineNumber- The line number of the passed-in line.- Returns:
trueiff the current longest line was updated.
-
removeUpdate
Gives notification that something was removed from the document in a location that this view is responsible for.- Overrides:
removeUpdatein classView- Parameters:
changes- the change information from the associated documenta- the current allocation of the viewf- the factory to use to rebuild if the view has children
-
setSize
-
updateDamage
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. -
updateMetrics
private void updateMetrics()Checks to see if the font metrics and longest line are up-to-date. -
viewToModel
Provides a mapping from the view coordinate space to the logical coordinate space of the model.- Specified by:
viewToModelin classView- Parameters:
fx- the X coordinate >= 0fy- the Y coordinate >= 0a- the allocated region to render into- Returns:
- the location within the model that best represents the given point in the view >= 0
-
yForLine
Description copied from interface:RSTAViewReturns the y-coordinate of the specified line.This method is quicker than using traditional
modelToView(int)calls, as the entire bounding box isn't computed. -
yForLineContaining
Description copied from interface:RSTAViewReturns the y-coordinate of the line containing a specified offset.This method is quicker than using traditional
modelToView(int)calls, as the entire bounding box isn't computed.- Specified by:
yForLineContainingin interfaceRSTAView- Parameters:
alloc- The area the text area can render into.offs- The offset info the document.- Returns:
- The y-coordinate of the top of the offset, or
-1if this text area doesn't yet have a positive size or the line is hidden (i.e. from folding). - Throws:
BadLocationException- Ifoffsisn't a valid offset into the document.
-