org.fife.ui.rsyntaxtextarea
Class RSyntaxUtilities

java.lang.Object
  extended by org.fife.ui.rsyntaxtextarea.RSyntaxUtilities
All Implemented Interfaces:
SwingConstants

public class RSyntaxUtilities
extends Object
implements SwingConstants

Utility methods used by RSyntaxTextArea and its associated classes.

Version:
0.2
Author:
Robert Futrell

Field Summary
 
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
RSyntaxUtilities()
           
 
Method Summary
static String getLeadingWhitespace(String text)
          Returns the leading whitespace of a string.
static Rectangle getLineWidthUpTo(RSyntaxTextArea textArea, Segment s, int p0, int p1, TabExpander e, Rectangle rect, int x0)
          Returns the bounding box (in the current view) of a specified position in the model.
static int getMatchingBracketPosition(RSyntaxTextArea textArea)
           
static int getNextVisualPositionFrom(int pos, Position.Bias b, Shape a, int direction, Position.Bias[] biasRet, View view)
          Provides a way to determine the next visually represented model location at which one might place a caret.
static int getPositionAbove(RSyntaxTextArea c, int offs, float x, TabExpander e)
          Determines the position in the model that is closest to the given view location in the row above.
static int getPositionBelow(RSyntaxTextArea c, int offs, float x, TabExpander e)
          Determines the position in the model that is closest to the given view location in the row below.
static Token getTokenAtOffset(Token tokenList, int offset)
          Returns the token at the specified index, or null if the given offset isn't in this token list's range.
static float getTokenListWidth(Token tokenList, RSyntaxTextArea textArea, TabExpander e)
          Determines the width of the given token list taking tabs into consideration.
static float getTokenListWidth(Token tokenList, RSyntaxTextArea textArea, TabExpander e, float x0)
          Determines the width of the given token list taking tabs into consideration.
static float getTokenListWidthUpTo(Token tokenList, RSyntaxTextArea textArea, TabExpander e, float x0, int upTo)
          Determines the width of the given token list taking tabs into consideration and only up to the given index in the document (exclusive).
static int getWordEnd(RSyntaxTextArea textArea, int offs)
          Returns the end of the word at the given offset.
static int getWordStart(RSyntaxTextArea textArea, int offs)
          Returns the start of the word at the given offset.
static boolean isBracket(char ch)
          Returns whether or not this character is a "bracket" to be matched by such programming languages as C, C++, and Java.
static boolean isDigit(char ch)
          Returns whether or not a character is a digit (0-9).
static boolean isHexCharacter(char ch)
          Returns whether or not this character is a hex character.
static boolean isJavaOperator(char ch)
          Returns whether a character is a Java operator.
static boolean isLetter(char ch)
          Returns whether a character is a US-ASCII letter (A-Z or a-z).
static boolean isLetterOrDigit(char ch)
          Returns whether or not a character is a US-ASCII letter or a digit.
static boolean isWhitespace(char ch)
          Returns whether or not a character is a whitespace character (either a space ' ' or tab '\t').
static float makeTokenListStartAt(Token tokenList, int pos, TabExpander e, RSyntaxTextArea textArea, float x0)
          Modifies the passed-in token list to start at the specified offset.
static char toLowerCase(char ch)
          If the character is an upper-case US-ASCII letter, it returns the lower-case version of that letter; otherwise, it just returns the character.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

RSyntaxUtilities

public RSyntaxUtilities()
Method Detail

getLeadingWhitespace

public static String getLeadingWhitespace(String text)
Returns the leading whitespace of a string.

Parameters:
text - The String to check.
Returns:
The leading whitespace.

getLineWidthUpTo

public static Rectangle getLineWidthUpTo(RSyntaxTextArea textArea,
                                         Segment s,
                                         int p0,
                                         int p1,
                                         TabExpander e,
                                         Rectangle rect,
                                         int x0)
                                  throws BadLocationException
Returns the bounding box (in the current view) of a specified position in the model. This method is designed for line-wrapped views to use, as it allows you to specify a "starting position" in the line, from which the x-value is assumed to be zero. The idea is that you specify the first character in a physical line as p0, as this is the character where the x-pixel value is 0.

Parameters:
textArea - The text area containing the text.
s - A segment in which to load the line. This is passed in so we don't have to reallocate a new Segment for each call.
p0 - The starting position in the physical line in the document.
p1 - The position for which to get the bounding box in the view.
e - How to expand tabs.
rect - The rectangle whose x- and width-values are changed to represent the bounding box of p1. This is reused to keep from needlessly reallocating Rectangles.
x0 - The x-coordinate (pixel) marking the left-hand border of the text. This is useful if the text area has a border, for example.
Returns:
The bounding box in the view of the character p1.
Throws:
BadLocationException - If p0 or p1 is not a valid location in the specified text area's document.
IllegalArgumentException - If p0 and p1 are not on the same line.

getMatchingBracketPosition

public static int getMatchingBracketPosition(RSyntaxTextArea textArea)

getNextVisualPositionFrom

public static int getNextVisualPositionFrom(int pos,
                                            Position.Bias b,
                                            Shape a,
                                            int direction,
                                            Position.Bias[] biasRet,
                                            View view)
                                     throws BadLocationException
Provides a way to determine the next visually represented model location at which 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.

NOTE: You should only call this method if the passed-in javax.swing.text.View is an instance of TokenOrientedView and javax.swing.text.TabExpander; otherwise, a ClassCastException could be thrown.

Parameters:
pos - the position to convert >= 0
a - the allocated region in which to render
direction - the direction from the current position that can be thought of as the arrow keys typically found on a keyboard. This will be one of the following values:
  • SwingConstants.WEST
  • SwingConstants.EAST
  • SwingConstants.NORTH
  • SwingConstants.SOUTH
Returns:
the location within the model that best represents the next location visual position
Throws:
BadLocationException
IllegalArgumentException - if direction doesn't have one of the legal values above

getPositionAbove

public static final int getPositionAbove(RSyntaxTextArea c,
                                         int offs,
                                         float x,
                                         TabExpander e)
                                  throws BadLocationException
Determines the position in the model that is closest to the given view location in the row above. The component given must have a size to compute the result. If the component doesn't have a size a value of -1 will be returned.

Parameters:
c - the editor
offs - the offset in the document >= 0
x - the X coordinate >= 0
Returns:
the position >= 0 if the request can be computed, otherwise a value of -1 will be returned.
Throws:
BadLocationException - if the offset is out of range

getPositionBelow

public static final int getPositionBelow(RSyntaxTextArea c,
                                         int offs,
                                         float x,
                                         TabExpander e)
                                  throws BadLocationException
Determines the position in the model that is closest to the given view location in the row below. The component given must have a size to compute the result. If the component doesn't have a size a value of -1 will be returned.

Parameters:
c - the editor
offs - the offset in the document >= 0
x - the X coordinate >= 0
Returns:
the position >= 0 if the request can be computed, otherwise a value of -1 will be returned.
Throws:
BadLocationException - if the offset is out of range

getTokenAtOffset

public static final Token getTokenAtOffset(Token tokenList,
                                           int offset)
Returns the token at the specified index, or null if the given offset isn't in this token list's range.
Note that this method does NOT check to see if tokenList is null; callers should check for themselves.

Parameters:
tokenList - The list of tokens in which to search.
offset - The offset at which to get the token.
Returns:
The token at offset, or null if none of the tokens are at that offset.

getWordEnd

public static int getWordEnd(RSyntaxTextArea textArea,
                             int offs)
                      throws BadLocationException
Returns the end of the word at the given offset.

Parameters:
textArea - The text area.
offs - The offset into the text area's content.
Returns:
The end offset of the word.
Throws:
BadLocationException - If offs is invalid.
See Also:
getWordStart(RSyntaxTextArea, int)

getWordStart

public static int getWordStart(RSyntaxTextArea textArea,
                               int offs)
                        throws BadLocationException
Returns the start of the word at the given offset.

Parameters:
textArea - The text area.
offs - The offset into the text area's content.
Returns:
The start offset of the word.
Throws:
BadLocationException - If offs is invalid.
See Also:
getWordEnd(RSyntaxTextArea, int)

getTokenListWidth

public static final float getTokenListWidth(Token tokenList,
                                            RSyntaxTextArea textArea,
                                            TabExpander e)
Determines the width of the given token list taking tabs into consideration. This is implemented in a 1.1 style coordinate system where ints are used and 72dpi is assumed.

This method also assumes that the passed-in token list begins at x-pixel 0 in the view (for tab purposes).

Parameters:
tokenList - The tokenList list representing the text.
textArea - The text area in which this token list resides.
e - The tab expander. This value cannot be null.
Returns:
The width of the token list, in pixels.

getTokenListWidth

public static final float getTokenListWidth(Token tokenList,
                                            RSyntaxTextArea textArea,
                                            TabExpander e,
                                            float x0)
Determines the width of the given token list taking tabs into consideration. This is implemented in a 1.1 style coordinate system where ints are used and 72dpi is assumed.

Parameters:
tokenList - The token list list representing the text.
textArea - The text area in which this token list resides.
e - The tab expander. This value cannot be null.
x0 - The x-pixel coordinate of the start of the token list.
Returns:
The width of the token list, in pixels.
See Also:
getTokenListWidthUpTo(org.fife.ui.rsyntaxtextarea.Token, org.fife.ui.rsyntaxtextarea.RSyntaxTextArea, javax.swing.text.TabExpander, float, int)

getTokenListWidthUpTo

public static final float getTokenListWidthUpTo(Token tokenList,
                                                RSyntaxTextArea textArea,
                                                TabExpander e,
                                                float x0,
                                                int upTo)
Determines the width of the given token list taking tabs into consideration and only up to the given index in the document (exclusive).

Parameters:
tokenList - The token list representing the text.
textArea - The text area in which this token list resides.
e - The tab expander. This value cannot be null.
x0 - The x-pixel coordinate of the start of the token list.
upTo - The document position at which you want to stop, exclusive. If this position is before the starting position of the token list, a width of 0 will be returned; similarly, if this position comes after the entire token list, the width of the entire token list is returned.
Returns:
The width of the token list, in pixels, up to, but not including, the character at position upTo.
See Also:
getTokenListWidth(org.fife.ui.rsyntaxtextarea.Token, org.fife.ui.rsyntaxtextarea.RSyntaxTextArea, javax.swing.text.TabExpander)

isBracket

public static final boolean isBracket(char ch)
Returns whether or not this character is a "bracket" to be matched by such programming languages as C, C++, and Java.

Parameters:
ch - The character to check.
Returns:
Whether or not the character is a "bracket" - one of '(', ')', '[', ']', '{', and '}'.

isDigit

public static final boolean isDigit(char ch)
Returns whether or not a character is a digit (0-9).

Parameters:
ch - The character to check.
Returns:
Whether or not the character is a digit.

isHexCharacter

public static final boolean isHexCharacter(char ch)
Returns whether or not this character is a hex character. This method accepts both upper- and lower-case letters a-f.

Parameters:
ch - The character to check.
Returns:
Whether or not the character is a hex character 0-9, a-f, or A-F.

isJavaOperator

public static final boolean isJavaOperator(char ch)
Returns whether a character is a Java operator. Note that C and C++ operators are the same as Java operators.

Parameters:
ch - The character to check.
Returns:
Whether or not the character is a Java operator.

isLetter

public static final boolean isLetter(char ch)
Returns whether a character is a US-ASCII letter (A-Z or a-z).

Parameters:
ch - The character to check.
Returns:
Whether or not the character is a US-ASCII letter.

isLetterOrDigit

public static final boolean isLetterOrDigit(char ch)
Returns whether or not a character is a US-ASCII letter or a digit.

Parameters:
ch - The character to check.
Returns:
Whether or not the character is a US-ASCII letter or a digit.

isWhitespace

public static final boolean isWhitespace(char ch)
Returns whether or not a character is a whitespace character (either a space ' ' or tab '\t'). This checks for the Unicode character values 0x0020 and 0x0009.

Parameters:
ch - The character to check.
Returns:
Whether or not the character is a whitespace character.

makeTokenListStartAt

public static float makeTokenListStartAt(Token tokenList,
                                         int pos,
                                         TabExpander e,
                                         RSyntaxTextArea textArea,
                                         float x0)
Modifies the passed-in token list to start at the specified offset. For example, if the token list covered positions 20-60 in the document (inclusive) like so:
   [token1] -> [token2] -> [token3] -> [token4]
   20     30   31     40   41     50   51     60
 
and you used this method to make the token list start at position 44, then the token list would be modified to be the following:
   [part-of-old-token3] -> [token4]
   44                 50   51     60
 
Tokens that come before the specified position are forever lost, and the token containing that position is made to begin at that position if necessary. All token types remain the same as they were originally.

This method can be useful if you are only interested in part of a token list (i.e., the line it represents), but you don't want to modify the token list yourself.

Parameters:
tokenList - The list to make start at the specified position. This parameter is modified.
pos - The position at which the new token list is to start. If this position is not in the passed-in token list, returned token list will either be null or the unpaintable token(s) at the end of the passed-in token list.
e - How to expand tabs.
textArea - The text area from which the token list came.
x0 - The initial x-pixel position of the old token list.
Returns:
The width, in pixels, of the part of the token list "removed from the front." This way, you know the x-offset of the "new" token list.

toLowerCase

public static final char toLowerCase(char ch)
If the character is an upper-case US-ASCII letter, it returns the lower-case version of that letter; otherwise, it just returns the character.

Parameters:
ch - The character to lower-case (if it is a US-ASCII upper-case character).
Returns:
The lower-case version of the character.