Package org.fife.ui.rsyntaxtextarea
Class WrappedSyntaxView
- java.lang.Object
-
- javax.swing.text.View
-
- javax.swing.text.CompositeView
-
- javax.swing.text.BoxView
-
- org.fife.ui.rsyntaxtextarea.WrappedSyntaxView
-
- All Implemented Interfaces:
javax.swing.SwingConstants,javax.swing.text.TabExpander,RSTAView
public class WrappedSyntaxView extends javax.swing.text.BoxView implements javax.swing.text.TabExpander, RSTAView
The view used byRSyntaxTextAreawhen word wrap is enabled.- Version:
- 0.2
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) classWrappedSyntaxView.WrappedLineSimple view of a line that wraps if it doesn't fit within the horizontal space allocated.
-
Field Summary
Fields Modifier and Type Field Description private javax.swing.text.SegmentdrawSegThis is reused to keep from allocating/deallocating.private RSyntaxTextAreahostCached for each paint() call so each drawView() call has access to it.private TokenImpllineCountTempTokenprivate java.awt.FontMetricsmetricsprivate static intMIN_WIDTHThe width of this view cannot be below this amount, as if the width is ever 0 (really a bug), we'll go into an infinite loop.private javax.swing.text.SegmentsThis is reused to keep from allocating/deallocating.private inttabBaseprivate inttabSizeprivate java.awt.RectangletempRectAnother variable initialized once to keep from allocating/deallocating.private TokenImpltempToken
-
Constructor Summary
Constructors Constructor Description WrappedSyntaxView(javax.swing.text.Element elem)Creates a new WrappedSyntaxView.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected intcalculateBreakPosition(int p0, Token tokenList, float x0)This is called by the nested wrapped line views to determine the break location.voidchangedUpdate(javax.swing.event.DocumentEvent e, 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.private voidchildAllocation2(int line, int y, java.awt.Rectangle alloc)Sets the allocation rectangle for a given line's view, but sets the y value to the passed-in value.protected voiddrawView(TokenPainter painter, java.awt.Graphics2D g, java.awt.Rectangle r, javax.swing.text.View view, int fontHeight, int y, int line)Draws a single view (i.e., a line of text for a wrapped view), wrapping the text onto multiple lines if necessary.protected voiddrawViewWithSelection(TokenPainter painter, java.awt.Graphics2D g, java.awt.Rectangle r, javax.swing.text.View view, int fontHeight, int y, int selStart, int selEnd)Draws a single view (i.e., a line of text for a wrapped view), wrapping the text onto multiple lines if necessary.java.awt.ShapegetChildAllocation(int index, java.awt.Shape a)Fetches the allocation for the given child view.java.awt.ShapegetChildAllocationImpl(int line, java.awt.Shape a)Fetches the allocation for the given child view to render into.floatgetMaximumSpan(int axis)Determines the maximum span for this view along an axis.floatgetMinimumSpan(int axis)Determines the minimum span for this view along an axis.floatgetPreferredSpan(int axis)Determines the preferred span for this view along an axis.protected intgetTabSize()Returns the tab size set for the document, defaulting to 5.protected javax.swing.text.ViewgetViewAtPoint(int x, int y, java.awt.Rectangle alloc)Overridden to allow for folded regions.voidinsertUpdate(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 voidloadChildren(javax.swing.text.ViewFactory f)Loads all of the children to initialize the view.java.awt.ShapemodelToView(int pos, java.awt.Shape a, javax.swing.text.Position.Bias b)java.awt.ShapemodelToView(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.floatnextTabStop(float x, int tabOffset)Returns the next tab stop position after a given reference position.voidpaint(java.awt.Graphics g, java.awt.Shape a)Paints the word-wrapped text.voidremoveUpdate(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.private voidsetSegment(int p0, int p1, javax.swing.text.Document document, javax.swing.text.Segment seg)Makes aSegmentpoint to the text in our document between the given positions.voidsetSize(float width, float height)Sets the size of the view.private voidsetWidthChangePending(boolean widthChangePending)(package private) voidupdateChildren(javax.swing.event.DocumentEvent e, java.awt.Shape a)Update the child views in response to a document event.(package private) voidupdateMetrics()intviewToModel(float x, float y, java.awt.Shape a, javax.swing.text.Position.Bias[] bias)intyForLine(java.awt.Rectangle alloc, int line)Returns the y-coordinate of the specified line.intyForLineContaining(java.awt.Rectangle alloc, int offs)Returns the y-coordinate of the line containing a specified offset.-
Methods inherited from class javax.swing.text.BoxView
baselineLayout, baselineRequirements, calculateMajorAxisRequirements, calculateMinorAxisRequirements, childAllocation, flipEastAndWestAtEnds, forwardUpdate, getAlignment, getAxis, getHeight, getOffset, getResizeWeight, getSpan, getWidth, isAfter, isAllocationValid, isBefore, isLayoutValid, layout, layoutChanged, layoutMajorAxis, layoutMinorAxis, paintChild, preferenceChanged, replace, setAxis
-
Methods inherited from class javax.swing.text.CompositeView
getBottomInset, getInsideAllocation, getLeftInset, getNextEastWestVisualPositionFrom, getNextNorthSouthVisualPositionFrom, getNextVisualPositionFrom, getRightInset, getTopInset, getView, getViewAtPosition, getViewCount, getViewIndex, getViewIndexAtPosition, setInsets, setParagraphInsets, setParent
-
Methods inherited from class javax.swing.text.View
append, breakView, createFragment, forwardUpdateToView, getAttributes, getBreakWeight, getContainer, getDocument, getElement, getEndOffset, getGraphics, getParent, getStartOffset, getToolTipText, getViewFactory, getViewIndex, insert, isVisible, modelToView, remove, removeAll, updateChildren, updateLayout, viewToModel
-
-
-
-
Field Detail
-
tabBase
private int tabBase
-
tabSize
private int tabSize
-
s
private javax.swing.text.Segment s
This is reused to keep from allocating/deallocating.
-
drawSeg
private javax.swing.text.Segment drawSeg
This is reused to keep from allocating/deallocating.
-
tempRect
private java.awt.Rectangle tempRect
Another variable initialized once to keep from allocating/deallocating.
-
host
private RSyntaxTextArea host
Cached for each paint() call so each drawView() call has access to it.
-
metrics
private java.awt.FontMetrics metrics
-
tempToken
private TokenImpl tempToken
-
lineCountTempToken
private TokenImpl lineCountTempToken
-
MIN_WIDTH
private static final int MIN_WIDTH
The width of this view cannot be below this amount, as if the width is ever 0 (really a bug), we'll go into an infinite loop.- See Also:
- Constant Field Values
-
-
Method Detail
-
calculateBreakPosition
protected int calculateBreakPosition(int p0, Token tokenList, float x0)This is called by the nested wrapped line views to determine the break location. This can be reimplemented to alter the breaking behavior. It will either break at word or character boundaries depending upon the break argument given at construction.
-
changedUpdate
public void changedUpdate(javax.swing.event.DocumentEvent e, 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:
changedUpdatein classjavax.swing.text.View- Parameters:
e- 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)
-
childAllocation2
private void childAllocation2(int line, int y, java.awt.Rectangle alloc)Sets the allocation rectangle for a given line's view, but sets the y value to the passed-in value. This should be used instead ofBoxView.childAllocation(int, Rectangle)since it allows you to account for hidden lines in collapsed fold regions.- Parameters:
line- The line to get the view allocation for.y- The y-offset to set for the allocation.alloc- The set allocation value.
-
drawView
protected void drawView(TokenPainter painter, java.awt.Graphics2D g, java.awt.Rectangle r, javax.swing.text.View view, int fontHeight, int y, int line)
Draws a single view (i.e., a line of text for a wrapped view), wrapping the text onto multiple lines if necessary.- Parameters:
painter- The painter to use to render tokens.g- The graphics context in which to paint.r- The rectangle in which to paint.view- TheViewto paint.fontHeight- The height of the font being used.y- The y-coordinate at which to begin painting.
-
drawViewWithSelection
protected void drawViewWithSelection(TokenPainter painter, java.awt.Graphics2D g, java.awt.Rectangle r, javax.swing.text.View view, int fontHeight, int y, int selStart, int selEnd)
Draws a single view (i.e., a line of text for a wrapped view), wrapping the text onto multiple lines if necessary. Any selected text is rendered with the editor's "selected text" color.- Parameters:
painter- The painter to use to render tokens.g- The graphics context in which to paint.r- The rectangle in which to paint.view- TheViewto paint.fontHeight- The height of the font being used.y- The y-coordinate at which to begin painting.selStart- The start of the selection.selEnd- The end of the selection.
-
getChildAllocation
public java.awt.Shape getChildAllocation(int index, java.awt.Shape a)Fetches the allocation for the given child view.Overridden to account for code folding.
- Overrides:
getChildAllocationin classjavax.swing.text.BoxView- Parameters:
index- The index of the child, >= 0 &&< getViewCount().a- The allocation to this view- Returns:
- The allocation to the child; or
nullifaisnull; ornullif the layout is invalid
-
getChildAllocationImpl
public java.awt.Shape getChildAllocationImpl(int line, java.awt.Shape a)Fetches the allocation for the given child view to render into.Overridden to account for lines hidden by collapsed folded regions.
- Parameters:
line- The index of the child, >= 0 &&< getViewCount()a- The allocation to this view- Returns:
- The allocation to the child
-
getMaximumSpan
public float getMaximumSpan(int axis)
Determines the maximum span for this view along an axis. This is implemented to provide the superclass behavior after first making sure that the current font metrics are cached (for the nested lines which use the metrics to determine the height of the potentially wrapped lines).- Overrides:
getMaximumSpanin classjavax.swing.text.BoxView- Parameters:
axis- may be either View.X_AXIS or View.Y_AXIS- Returns:
- the span the view would like to be rendered into. 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.
- See Also:
View.getMaximumSpan(int)
-
getMinimumSpan
public float getMinimumSpan(int axis)
Determines the minimum span for this view along an axis. This is implemented to provide the superclass behavior after first making sure that the current font metrics are cached (for the nested lines which use the metrics to determine the height of the potentially wrapped lines).- Overrides:
getMinimumSpanin classjavax.swing.text.BoxView- Parameters:
axis- may be either View.X_AXIS or View.Y_AXIS- Returns:
- the span the view would like to be rendered into. 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.
- See Also:
View.getMinimumSpan(int)
-
getPreferredSpan
public float getPreferredSpan(int axis)
Determines the preferred span for this view along an axis. This is implemented to provide the superclass behavior after first making sure that the current font metrics are cached (for the nested lines which use the metrics to determine the height of the potentially wrapped lines).- Overrides:
getPreferredSpanin classjavax.swing.text.BoxView- Parameters:
axis- may be either View.X_AXIS or View.Y_AXIS- Returns:
- the span the view would like to be rendered into. 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.
- See Also:
View.getPreferredSpan(int)
-
getTabSize
protected int getTabSize()
Returns the tab size set for the document, defaulting to 5.- Returns:
- the tab size
-
getViewAtPoint
protected javax.swing.text.View getViewAtPoint(int x, int y, java.awt.Rectangle alloc)Overridden to allow for folded regions.- Overrides:
getViewAtPointin classjavax.swing.text.BoxView
-
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. This is implemented to simply update the children.- Overrides:
insertUpdatein classjavax.swing.text.View- Parameters:
changes- The change information from the associated document.a- the current allocation of the viewf- the factory to use to rebuild if the view has children- See Also:
View.insertUpdate(javax.swing.event.DocumentEvent, java.awt.Shape, javax.swing.text.ViewFactory)
-
loadChildren
protected void loadChildren(javax.swing.text.ViewFactory f)
Loads all of the children to initialize the view. This is called by thesetParentmethod. Subclasses can re-implement this to initialize their child views in a different manner. The default implementation creates a child view for each child element.- Overrides:
loadChildrenin classjavax.swing.text.CompositeView- Parameters:
f- the view factory
-
modelToView
public java.awt.Shape modelToView(int pos, java.awt.Shape a, javax.swing.text.Position.Bias b) throws javax.swing.text.BadLocationException- Overrides:
modelToViewin classjavax.swing.text.BoxView- Throws:
javax.swing.text.BadLocationException
-
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.BadLocationExceptionProvides 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 classjavax.swing.text.CompositeView- 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:
javax.swing.text.BadLocationException- if the given position does not represent a valid location in the associated documentjava.lang.IllegalArgumentException- ifb0orb1are not one of the legalPosition.Biasvalues 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:
nextTabStopin 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)Paints the word-wrapped text.- Overrides:
paintin classjavax.swing.text.BoxView- Parameters:
g- The graphics context in which to paint.a- The shape (usually a rectangle) in which to paint.
-
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. This is implemented to simply update the children.- Overrides:
removeUpdatein classjavax.swing.text.View- Parameters:
changes- The change information from the associated document.a- the current allocation of the viewf- the factory to use to rebuild if the view has children- See Also:
View.removeUpdate(javax.swing.event.DocumentEvent, java.awt.Shape, javax.swing.text.ViewFactory)
-
setSegment
private void setSegment(int p0, int p1, javax.swing.text.Document document, javax.swing.text.Segment seg)Makes aSegmentpoint to the text in our document between the given positions. Note that the positions MUST be valid positions in the document.- Parameters:
p0- The first position in the document.p1- The second position in the document.document- The document from which you want to get the text.seg- The segment in which to load the text.
-
setSize
public void setSize(float width, float height)Sets the size of the view. This should cause layout of the view along the given axis, if it has any layout duties.- Overrides:
setSizein classjavax.swing.text.BoxView- Parameters:
width- the width >= 0height- the height >= 0
-
setWidthChangePending
private void setWidthChangePending(boolean widthChangePending)
-
updateChildren
void updateChildren(javax.swing.event.DocumentEvent e, java.awt.Shape a)Update the child views in response to a document event.
-
updateMetrics
final void updateMetrics()
-
viewToModel
public int viewToModel(float x, float y, java.awt.Shape a, javax.swing.text.Position.Bias[] bias)- Overrides:
viewToModelin classjavax.swing.text.BoxView
-
yForLine
public int yForLine(java.awt.Rectangle alloc, int line) throws javax.swing.text.BadLocationExceptionDescription 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.- Specified by:
yForLinein interfaceRSTAView- Parameters:
alloc- The area the text area can render into.line- The line number.- Returns:
- The y-coordinate of the top of the line, or
-1if this text area doesn't yet have a positive size or the line is hidden (i.e. from folding). - Throws:
javax.swing.text.BadLocationException- Iflineisn't a valid line number for this document.
-
yForLineContaining
public int yForLineContaining(java.awt.Rectangle alloc, int offs) throws javax.swing.text.BadLocationExceptionDescription 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:
javax.swing.text.BadLocationException- Ifoffsisn't a valid offset into the document.
-
-