org.fife.ui.rsyntaxtextarea
Class DefaultToken

java.lang.Object
  extended by org.fife.ui.rsyntaxtextarea.Token
      extended by org.fife.ui.rsyntaxtextarea.DefaultToken
Direct Known Subclasses:
VisibleWhitespaceToken

public class DefaultToken
extends Token

The default token used in the org.fife.ui.rsyntaxtextarea syntax package. This token type paints itself as you would expect, and properly accounts for rendering hints (anti-aliasing and fractional font metrics).

The current implementation paints as follows:

This method allows for rendering hints to be honored, since all possible characters are painted in a group. However, adjacent tokens will not have their "touching" characters rendered with rendering hints.

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.5
Author:
Robert Futrell
See Also:
Token, VisibleWhitespaceToken

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
DefaultToken()
          Creates a "null token."
DefaultToken(char[] line, int beg, int end, int startOffset, int type)
          Constructor.
DefaultToken(Segment line, int beg, int end, int startOffset, int type)
          Constructor.
 
Method Summary
 int getListOffset(RSyntaxTextArea textArea, 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, TabExpander e, float x0)
          Returns the width of a specified number of characters in this token.
 Rectangle listOffsetToView(RSyntaxTextArea textArea, TabExpander e, int pos, int x0, Rectangle rect)
          Returns the bounding box for the specified document location.
 float paint(Graphics2D g, float x, float y, RSyntaxTextArea host, TabExpander e, float clipStart)
          Paints this token.
 
Methods inherited from class org.fife.ui.rsyntaxtextarea.Token
appendHTMLRepresentation, containsPosition, copyFrom, documentToToken, getHTMLRepresentation, getLastNonCommentNonWhitespaceToken, getLastPaintableToken, getLexeme, getNextToken, getOffsetBeforeX, getWidth, is, isComment, 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

DefaultToken

public DefaultToken()
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 "multi-line token."


DefaultToken

public DefaultToken(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.

DefaultToken

public DefaultToken(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,
                         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,
                          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.

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 Rectangle listOffsetToView(RSyntaxTextArea textArea,
                                  TabExpander e,
                                  int pos,
                                  int x0,
                                  Rectangle rect)
Returns the bounding box for the specified document location. The location must be in the specified token list.

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 float paint(Graphics2D g,
                   float x,
                   float y,
                   RSyntaxTextArea host,
                   TabExpander e,
                   float clipStart)
Paints this token.

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 when this token is "to the left" of the clip rectangle.
Returns:
The x-coordinate representing the end of the painted text.