Package org.fife.ui.rsyntaxtextarea
Class SyntaxView
- java.lang.Object
-
- javax.swing.text.View
-
- org.fife.ui.rsyntaxtextarea.SyntaxView
-
- All Implemented Interfaces:
SwingConstants
,TabExpander
,TokenOrientedView
public class SyntaxView extends View implements TabExpander, TokenOrientedView
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
.
-
-
Field Summary
-
Fields inherited from class javax.swing.text.View
BadBreakWeight, ExcellentBreakWeight, ForcedBreakWeight, GoodBreakWeight, X_AXIS, Y_AXIS
-
Fields inherited from interface javax.swing.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
Constructors Constructor Description SyntaxView(Element elem)
Constructs a newSyntaxView
wrapped around an element.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
changedUpdate(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 void
damageLineRange(int line0, int line1, Shape a, Component host)
Repaint the given line range.int
getNextVisualPositionFrom(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.float
getPreferredSpan(int axis)
Determines the preferred span for this view along an axis.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(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 Rectangle
lineToRect(Shape a, int line)
Determine the rectangle that represents the given line.Shape
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.Shape
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.float
nextTabStop(float x, int tabOffset)
Returns the next tab stop position after a given reference position.void
paint(Graphics g, Shape a)
Actually paints the text area.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.void
setSize(float width, float height)
protected void
updateDamage(DocumentEvent changes, Shape a, ViewFactory f)
Repaint the region of change covered by the given document event.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.int
yForLine(Rectangle alloc, int line)
int
yForLineContaining(Rectangle alloc, int offs)
-
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
-
-
-
-
Constructor Detail
-
SyntaxView
public SyntaxView(Element elem)
Constructs a newSyntaxView
wrapped around an element.- Parameters:
elem
- The element representing the text to display.
-
-
Method Detail
-
changedUpdate
public void changedUpdate(DocumentEvent changes, Shape a, ViewFactory f)
Gives notification from the document that attributes were changed in a location that this view is responsible for.- Overrides:
changedUpdate
in 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:
View.changedUpdate(javax.swing.event.DocumentEvent, java.awt.Shape, javax.swing.text.ViewFactory)
-
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 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 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 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
-
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(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 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
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 intoline
- The line number to find the region of. This must be a valid line number in the model.
-
modelToView
public Shape modelToView(int pos, Shape a, Position.Bias b) throws BadLocationException
Provides a mapping from the document model coordinate space to the coordinate space of the view mapped to it.- Specified by:
modelToView
in 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:
View.modelToView(int, java.awt.Shape, javax.swing.text.Position.Bias)
-
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 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 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;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:
BadLocationException
- if the given position does not represent a valid location in the associated documentIllegalArgumentException
- 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 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
public void paint(Graphics g, Shape a)
Actually paints the text area. Only lines that have been damaged are repainted.
-
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 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
-
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 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
public int yForLine(Rectangle alloc, int line) throws BadLocationException
- Throws:
BadLocationException
-
yForLineContaining
public int yForLineContaining(Rectangle alloc, int offs) throws BadLocationException
- Throws:
BadLocationException
-
-