Class SyntaxView
- java.lang.Object
-
- javax.swing.text.View
-
- org.fife.ui.rsyntaxtextarea.SyntaxView
-
- All Implemented Interfaces:
javax.swing.SwingConstants
,javax.swing.text.TabExpander
,RSTAView
,TokenOrientedView
public class SyntaxView extends javax.swing.text.View implements javax.swing.text.TabExpander, TokenOrientedView, RSTAView
Thejavax.swing.text.View
object used byRSyntaxTextArea
when word wrap is disabled. It implements syntax highlighting for programming languages using the colors and font styles specified by theRSyntaxTextArea
.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
Fields Modifier and Type Field Description private int
ascent
private int
clipEnd
private int
clipStart
private java.awt.Font
font
The default font used by the text area.private RSyntaxTextArea
host
Cached for each paint() call so each drawLine() call has access to it.private int
lineHeight
Cached values to speed up the painting a tad.private javax.swing.text.Element
longLine
The current longest line.private float
longLineWidth
private java.awt.FontMetrics
metrics
Font metrics for the current font.private int
tabBase
private int
tabSize
private TokenImpl
tempToken
Temporary token used when we need to "modify" tokens for rendering purposes.
-
Constructor Summary
Constructors Constructor Description SyntaxView(javax.swing.text.Element elem)
Constructs a newSyntaxView
wrapped around an element.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description (package private) 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.void
changedUpdate(javax.swing.event.DocumentEvent changes, java.awt.Shape a, javax.swing.text.ViewFactory f)
Gives notification from the document that attributes were changed in a location that this view is responsible for.protected void
damageLineRange(int line0, int line1, java.awt.Shape a, java.awt.Component host)
Repaint the given line range.private float
drawLine(TokenPainter painter, Token token, java.awt.Graphics2D g, float x, float y, int line)
Draws the passed-in text using syntax highlighting for the current language.private float
drawLineWithSelection(TokenPainter painter, Token token, java.awt.Graphics2D g, float x, float y, int selStart, int selEnd)
Draws the passed-in text using syntax highlighting for the current language.private float
getLineWidth(int lineNumber)
Calculates the width of the line represented by the given element.int
getNextVisualPositionFrom(int pos, javax.swing.text.Position.Bias b, java.awt.Shape a, int direction, javax.swing.text.Position.Bias[] biasRet)
Provides a way to determine the next visually represented model location that one might place a caret.float
getPreferredSpan(int axis)
Determines the preferred span for this view along an axis.private int
getRhsCorrection()
Workaround for JTextComponents allowing the caret to be rendered entirely off-screen if the entire "previous" character fit entirely.private int
getTabSize()
Returns the tab size set for the document, defaulting to 5.Token
getTokenListForPhysicalLineAbove(int offset)
Returns a token list for the physical line above the physical line containing the specified offset into the document.Token
getTokenListForPhysicalLineBelow(int offset)
Returns a token list for the physical line below the physical line containing the specified offset into the document.void
insertUpdate(javax.swing.event.DocumentEvent changes, java.awt.Shape a, javax.swing.text.ViewFactory f)
Gives notification that something was inserted into the document in a location that this view is responsible for.protected java.awt.Rectangle
lineToRect(java.awt.Shape a, int line)
Determine the rectangle that represents the given line.java.awt.Shape
modelToView(int pos, java.awt.Shape a, javax.swing.text.Position.Bias b)
Provides a mapping from the document model coordinate space to the coordinate space of the view mapped to it.java.awt.Shape
modelToView(int p0, javax.swing.text.Position.Bias b0, int p1, javax.swing.text.Position.Bias b1, java.awt.Shape a)
Provides a mapping, for a given region, from the document model coordinate space to the view coordinate space.float
nextTabStop(float x, int tabOffset)
Returns the next tab stop position after a given reference position.void
paint(java.awt.Graphics g, java.awt.Shape a)
Actually paints the text area.private boolean
possiblyUpdateLongLine(javax.swing.text.Element line, int lineNumber)
If the passed-in line is longer than the current longest line, then the longest line is updated.void
removeUpdate(javax.swing.event.DocumentEvent changes, java.awt.Shape a, javax.swing.text.ViewFactory f)
Gives notification that something was removed from the document in a location that this view is responsible for.void
setSize(float width, float height)
protected void
updateDamage(javax.swing.event.DocumentEvent changes, java.awt.Shape a, javax.swing.text.ViewFactory f)
Repaint the region of change covered by the given document event.private void
updateMetrics()
Checks to see if the font metrics and longest line are up-to-date.int
viewToModel(float fx, float fy, java.awt.Shape a, javax.swing.text.Position.Bias[] bias)
Provides a mapping from the view coordinate space to the logical coordinate space of the model.int
yForLine(java.awt.Rectangle alloc, int line)
Returns the y-coordinate of the specified line.int
yForLineContaining(java.awt.Rectangle alloc, int offs)
Returns the y-coordinate of the line containing a specified offset.-
Methods inherited from class javax.swing.text.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 Detail
-
font
private java.awt.Font font
The default font used by the text area. If this changes we need to recalculate the longest line.
-
metrics
private java.awt.FontMetrics metrics
Font metrics for the current font.
-
longLine
private javax.swing.text.Element 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
private RSyntaxTextArea host
Cached for each paint() call so each drawLine() call has access to it.
-
lineHeight
private int lineHeight
Cached values to speed up the painting a tad.
-
ascent
private int ascent
-
clipStart
private int clipStart
-
clipEnd
private int clipEnd
-
tempToken
private TokenImpl 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.
-
-
Method Detail
-
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
public void changedUpdate(javax.swing.event.DocumentEvent changes, java.awt.Shape a, javax.swing.text.ViewFactory f)
Gives notification from the document that attributes were changed in a location that this view is responsible for.- Overrides:
changedUpdate
in classjavax.swing.text.View
- 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:
View.changedUpdate(javax.swing.event.DocumentEvent, java.awt.Shape, javax.swing.text.ViewFactory)
-
damageLineRange
protected void damageLineRange(int line0, int line1, java.awt.Shape a, java.awt.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).
-
drawLine
private float drawLine(TokenPainter painter, Token token, java.awt.Graphics2D g, float x, float y, int line)
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, java.awt.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, javax.swing.text.Position.Bias b, java.awt.Shape a, int direction, javax.swing.text.Position.Bias[] biasRet) throws javax.swing.text.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:
getNextVisualPositionFrom
in classjavax.swing.text.View
- 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:
javax.swing.text.BadLocationException
- If the offset specified is invalid.java.lang.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 classjavax.swing.text.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:
java.lang.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
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 containingoffset
, since lines are not wrapped.- Specified by:
getTokenListForPhysicalLineAbove
in interfaceTokenOrientedView
- 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 containingoffset
, since lines are not wrapped.- Specified by:
getTokenListForPhysicalLineBelow
in interfaceTokenOrientedView
- 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(javax.swing.event.DocumentEvent changes, java.awt.Shape a, javax.swing.text.ViewFactory f)
Gives notification that something was inserted into the document in a location that this view is responsible for.- Overrides:
insertUpdate
in classjavax.swing.text.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 java.awt.Rectangle lineToRect(java.awt.Shape a, int line)
Determine the rectangle that represents the given line.- Parameters:
a
- The region allocated for the view to render intoline
- The line number to find the region of. This must be a valid line number in the model.
-
modelToView
public java.awt.Shape modelToView(int pos, java.awt.Shape a, javax.swing.text.Position.Bias b) throws javax.swing.text.BadLocationException
Provides a mapping from the document model coordinate space to the coordinate space of the view mapped to it.- Specified by:
modelToView
in classjavax.swing.text.View
- Parameters:
pos
- the position to convert >= 0a
- the allocated region to render into- Returns:
- the bounding box of the given position
- Throws:
javax.swing.text.BadLocationException
- if the given position does not represent a valid location in the associated document- See Also:
View.modelToView(int, java.awt.Shape, javax.swing.text.Position.Bias)
-
modelToView
public java.awt.Shape modelToView(int p0, javax.swing.text.Position.Bias b0, int p1, javax.swing.text.Position.Bias b1, java.awt.Shape a) throws javax.swing.text.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 injavax.swing.text
. Thus, if we don't override this method, theView
implementation will return one character's width too much for its consumers (implementations ofjavax.swing.text.Highlighter
).- Overrides:
modelToView
in classjavax.swing.text.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 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:
javax.swing.text.BadLocationException
- if the given position does not represent a valid location in the associated documentjava.lang.IllegalArgumentException
- ifb0
orb1
are not one of the legalPosition.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 interfacejavax.swing.text.TabExpander
- 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
public void paint(java.awt.Graphics g, java.awt.Shape a)
Actually paints the text area. Only lines that have been damaged are repainted.- Specified by:
paint
in classjavax.swing.text.View
- Parameters:
g
- The graphics context with which to paint.a
- The allocated region in which to render.
-
possiblyUpdateLongLine
private boolean possiblyUpdateLongLine(javax.swing.text.Element line, int lineNumber)
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:
true
iff the current longest line was updated.
-
removeUpdate
public void removeUpdate(javax.swing.event.DocumentEvent changes, java.awt.Shape a, javax.swing.text.ViewFactory f)
Gives notification that something was removed from the document in a location that this view is responsible for.- Overrides:
removeUpdate
in classjavax.swing.text.View
- 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
public void setSize(float width, float height)
- Overrides:
setSize
in classjavax.swing.text.View
-
updateDamage
protected void updateDamage(javax.swing.event.DocumentEvent changes, java.awt.Shape a, javax.swing.text.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.
-
updateMetrics
private void updateMetrics()
Checks to see if the font metrics and longest line are up-to-date.
-
viewToModel
public int viewToModel(float fx, float fy, java.awt.Shape a, javax.swing.text.Position.Bias[] bias)
Provides a mapping from the view coordinate space to the logical coordinate space of the model.- Specified by:
viewToModel
in classjavax.swing.text.View
- 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
public int yForLine(java.awt.Rectangle alloc, int line)
Description copied from interface:RSTAView
Returns 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
public int yForLineContaining(java.awt.Rectangle alloc, int offs) throws javax.swing.text.BadLocationException
Description copied from interface:RSTAView
Returns 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:
yForLineContaining
in 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
-1
if this text area doesn't yet have a positive size or the line is hidden (i.e. from folding). - Throws:
javax.swing.text.BadLocationException
- Ifoffs
isn't a valid offset into the document.
-
-