org.fife.ui.rsyntaxtextarea
Class VisibleWhitespaceToken

java.lang.Object
  extended by org.fife.ui.rsyntaxtextarea.Token
      extended by org.fife.ui.rsyntaxtextarea.VisibleWhitespaceToken

public class VisibleWhitespaceToken
extends Token

This token class paints spaces and tabs with special symbols so the user can see the whitespace in his document. Rendering hints are honored.

The current implementation paints as follows:

This means that rendering hints are applied to all groups of characters within a token, excluding whitespace and tabs.

A problem with this implementation is that FontMetrics.charsWidth() is still used to calculate the width of a group of chars painted. Thus, the group of characters will be painted with the rendering hints specified, but the following tab (or group of characters if the current group was the end of a token) will not necessarily be painted at the proper x-coordinate (as FontMetrics.charsWidth() returns an int and not a float). The way around this would be to calculate the token's width in such a way that a float is returned (Font.getStringBounds()?).

Version:
0.3
Author:
Robert Futrell
See Also:
Token, DefaultToken

Field Summary
 
Fields inherited from class org.fife.ui.rsyntaxtextarea.Token
COMMENT_DOCUMENTATION, COMMENT_EOL, COMMENT_MULTILINE, DATA_TYPE, ERROR_CHAR, ERROR_IDENTIFIER, ERROR_NUMBER_FORMAT, ERROR_STRING_DOUBLE, FUNCTION, IDENTIFIER, LITERAL_BACKQUOTE, LITERAL_BOOLEAN, LITERAL_CHAR, LITERAL_NUMBER_DECIMAL_INT, LITERAL_NUMBER_FLOAT, LITERAL_NUMBER_HEXADECIMAL, LITERAL_STRING_DOUBLE_QUOTE, MARKUP_TAG_ATTRIBUTE, MARKUP_TAG_DELIMITER, MARKUP_TAG_NAME, NULL, NUM_TOKEN_TYPES, offset, OPERATOR, PREPROCESSOR, RESERVED_WORD, SEPARATOR, text, textCount, textOffset, type, VARIABLE, WHITESPACE
 
Constructor Summary
VisibleWhitespaceToken()
          Creates a "null token."
VisibleWhitespaceToken(char[] line, int beg, int end, int startOffset, int type)
          Constructor.
VisibleWhitespaceToken(javax.swing.text.Segment line, int beg, int end, int startOffset, int type)
          Constructor.
 
Method Summary
 int getListOffset(RSyntaxTextArea textArea, javax.swing.text.TabExpander e, float x0, float x)
          Determines the offset into this token list (i.e., into the document) that covers pixel location x if the token list starts at pixel location x0.
 float getWidthUpTo(int numChars, RSyntaxTextArea textArea, javax.swing.text.TabExpander e, float x0)
          Returns the width of a specified number of characters in this token.
 java.awt.Rectangle listOffsetToView(RSyntaxTextArea textArea, javax.swing.text.TabExpander e, int pos, int x0, java.awt.Rectangle rect)
          Returns the bounding box for the specified document location.
 float paint(java.awt.Graphics2D g, float x, float y, RSyntaxTextArea host, javax.swing.text.TabExpander e, float clipStart)
          Paints this token, using special symbols for whitespace characters.
 
Methods inherited from class org.fife.ui.rsyntaxtextarea.Token
containsPosition, copyFrom, documentToToken, getHTMLRepresentation, getLastNonCommentNonWhitespaceToken, getLastPaintableToken, getLexeme, getNextToken, getOffsetBeforeX, getWidth, isHyperlink, isLeftCurly, isPaintable, isRightCurly, isSingleChar, isWhitespace, makeStartAt, moveOffset, paint, paintBackground, set, setHyperlink, setNextToken, tokenToDocument, toString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

VisibleWhitespaceToken

public VisibleWhitespaceToken()
Creates a "null token." The token itself is not null; rather, it signifies that it is the last token in a linked list of tokens and that it is not part of a "multiline token."


VisibleWhitespaceToken

public VisibleWhitespaceToken(javax.swing.text.Segment line,
                              int beg,
                              int end,
                              int startOffset,
                              int type)
Constructor.

Parameters:
line - The segment from which to get the token.
beg - The first character's position in line.
end - The last character's position in line.
startOffset - The offset into the document at which this token begins.
type - A token type listed as "generic" above.

VisibleWhitespaceToken

public VisibleWhitespaceToken(char[] line,
                              int beg,
                              int end,
                              int startOffset,
                              int type)
Constructor.

Parameters:
line - The segment from which to get the token.
beg - The first character's position in line.
end - The last character's position in line.
startOffset - The offset into the document at which this token begins.
type - A token type listed as "generic" above.
Method Detail

getListOffset

public int getListOffset(RSyntaxTextArea textArea,
                         javax.swing.text.TabExpander e,
                         float x0,
                         float x)
Determines the offset into this token list (i.e., into the document) that covers pixel location x if the token list starts at pixel location x0

. This method will return the document position "closest" to the x-coordinate (i.e., if they click on the "right-half" of the w in awe, the caret will be placed in between the w and e; similarly, clicking on the left-half places the caret between the a and w). This makes it useful for methods such as viewToModel found in javax.swing.text.View subclasses.

Specified by:
getListOffset in class Token
Parameters:
textArea - The text area from which the token list was derived.
e - How to expand tabs.
x0 - The pixel x-location that is the beginning of tokenList.
x - The pixel-position for which you want to get the corresponding offset.
Returns:
The position (in the document, NOT into the token list!) that covers the pixel location. If tokenList is null or has type Token.NULL, then -1

getWidthUpTo

public float getWidthUpTo(int numChars,
                          RSyntaxTextArea textArea,
                          javax.swing.text.TabExpander e,
                          float x0)
Returns the width of a specified number of characters in this token. For example, for the token "while", specifying a value of 3 here returns the width of the "whi" portion of the token.

This method is overridden to account for the case when "smooth fonts" are enabled; smooth fonts cause the token painting code to align things differently (since we "break" at spaces), so we need to reflect that behavior here as well.

Specified by:
getWidthUpTo in class Token
Parameters:
numChars - The number of characters for which to get the width.
textArea - The text area in which this token is being painted.
e - How to expand tabs. This value cannot be null.
x0 - The pixel-location at which this token begins. This is needed because of tabs.
Returns:
The width of the specified number of characters in this token.
See Also:
Token.getWidth(org.fife.ui.rsyntaxtextarea.RSyntaxTextArea, javax.swing.text.TabExpander, float)

listOffsetToView

public java.awt.Rectangle listOffsetToView(RSyntaxTextArea textArea,
                                           javax.swing.text.TabExpander e,
                                           int pos,
                                           int x0,
                                           java.awt.Rectangle rect)
Returns the bounding box for the specified document location. The location must be in the specified token list; if it isn't, null is returned.

Specified by:
listOffsetToView in class Token
Parameters:
textArea - The text area from which the token list was derived.
e - How to expand tabs.
pos - The position in the document for which to get the bounding box in the view.
x0 - The pixel x-location that is the beginning of tokenList.
rect - The rectangle in which we'll be returning the results. This object is reused to keep from frequent memory allocations.
Returns:
The bounding box for the specified position in the model.

paint

public final float paint(java.awt.Graphics2D g,
                         float x,
                         float y,
                         RSyntaxTextArea host,
                         javax.swing.text.TabExpander e,
                         float clipStart)
Paints this token, using special symbols for whitespace characters.

Specified by:
paint in class Token
Parameters:
g - The graphics context in which to paint.
x - The x-coordinate at which to paint.
y - The y-coordinate at which to paint.
host - The text area this token is in.
e - How to expand tabs.
clipStart - The left boundary of the clip rectangle in which we're painting. This optimizes painting by allowing us to not paint not paint when this token is "to the left" of the clip rectangle.
Returns:
The x-coordinate representing the end of the painted text.