com.borland.primetime.editor
Class TextUtilities

java.lang.Object
  |
  +--com.borland.primetime.editor.TextUtilities

public class TextUtilities
extends java.lang.Object

A customized collection of methods to deal with various text related activities. We have to use these methods instead of the corresponding ones in javax.swing.text.Utilities because those don't work well for source code.


Field Summary
static java.lang.String DEBUGGER_END_DELIMITERS
          The characters in this string define the end of a token for the ripText methods, when called by the debugger.
static java.lang.String DEBUGGER_START_DELIMITERS
          The characters in this string define the start of a token for the ripText methods, when called by the debugger.
static java.lang.String STANDARD_DELIMITERS
          The characters in this string define the standard ends of a token for the ripText methods.
 
Constructor Summary
TextUtilities()
           
 
Method Summary
static int drawTabbedText(javax.swing.text.Segment s, int x, int y, java.awt.Graphics g, javax.swing.text.TabExpander e, int startOffset)
          Draws the given text, expanding any tabs that are contained using the given tab expansion technique.
static javax.swing.text.Element getLineElement(javax.swing.text.JTextComponent c, int offs)
          Determines the element to use for a paragraph/line.
static int getNextWord(javax.swing.text.JTextComponent c, int offs)
          Determines the start of the next word for the given location.
static int getPreviousWord(javax.swing.text.JTextComponent c, int offs)
          Determine the start of the prev word for the given location.
static int getTabbedTextPosition(char[] textArray, int tabSize, int offs)
          Determines the character offset of a position, converting all spaces to tabs.
static int getTabbedTextPosition(javax.swing.text.JTextComponent c, javax.swing.text.Element lineElement, int tabSize, int offs)
          Determines the character offset of a position, converting all spaces to tabs.
static int getTabbedTextPosition(java.lang.String text, int tabSize, int offs)
          Determines the character offset of a position, converting all spaces to tabs.
static int getTabbedTextWidth(javax.swing.text.Segment s, java.awt.FontMetrics metrics, int x, javax.swing.text.TabExpander e, int startOffset)
          Determines the width of the given segment of text taking tabs into consideration.
static int getTokenEnd(char[] characters, int startOffset, int endOffset, int offset, java.lang.String delimiters)
          Return the offset of the end of a token.
static int getTokenStart(char[] characters, int startOffset, int endOffset, int offset, java.lang.String delimiters)
          Return the offset of the start of a token.
static int getWordEnd(javax.swing.text.JTextComponent c, int offs)
          This method returns the location of the end of the current word, or, if offs is at the end of a word or between words, this method returns the location at the end of the next word.
static int getWordStart(javax.swing.text.JTextComponent c, int offs)
          Determines the start of a word for the given model location.
static boolean isBlankLine(char[] textLine)
          Determines whether or not a particular String consists entirely of whitespace.
static boolean isBlankLine(javax.swing.text.JTextComponent c, javax.swing.text.Element lineElement)
          Determines whether or not a particular element consists entirely of whitespace.
protected static boolean isSkipCharacter(char ch, boolean skipOnUnderline)
          This method is used to determine if a character can be skipped when scanning for word boundaries.
static java.lang.String ripTextAt(char[] characters, int startOffset, int endOffset, int offset, java.lang.String delimiters)
          Return the string at the location specified by offset.
static java.lang.String ripTextAt(char[] characters, int startOffset, int endOffset, int offset, java.lang.String startDelimiters, java.lang.String endDelimiters)
          Return the string at the location specified by offset.
static java.lang.String ripTextAt(EditorPane editor, int offset)
          Return the string at the location specified by offset.
static java.lang.String ripTextAt(EditorPane editor, int offset, java.lang.String delimiters)
          Return the string at the location specified by offset.
static java.lang.String ripTextAt(EditorPane editor, int offset, java.lang.String startDelimiters, java.lang.String endDelimiters)
          Return the string at the location specified by offset.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

STANDARD_DELIMITERS

public static final java.lang.String STANDARD_DELIMITERS
The characters in this string define the standard ends of a token for the ripText methods.

DEBUGGER_START_DELIMITERS

public static final java.lang.String DEBUGGER_START_DELIMITERS
The characters in this string define the start of a token for the ripText methods, when called by the debugger. (The debugger wants strings such as 'metrics.charWidth', while we usually want just 'charWidth'.)

DEBUGGER_END_DELIMITERS

public static final java.lang.String DEBUGGER_END_DELIMITERS
The characters in this string define the end of a token for the ripText methods, when called by the debugger. The only difference here is that a '.' character will end a token, when called by the debugger. That way, if the caret is in on the 'b' of a.b.c, we'll return "a.b".
Constructor Detail

TextUtilities

public TextUtilities()
Method Detail

getWordStart

public static final int getWordStart(javax.swing.text.JTextComponent c,
                                     int offs)
                              throws javax.swing.text.BadLocationException
Determines the start of a word for the given model location. If the offset is not currently on a word, it finds the start of the previous word.

This is mostly used for Emacs emulation. CUA usually uses getPreviousWord().

Parameters:
c - the editor
offs - the offset in the document >= 0
Throws:
javax.swing.text.BadLocationException - if the offset is out of range

getWordEnd

public static final int getWordEnd(javax.swing.text.JTextComponent c,
                                   int offs)
                            throws javax.swing.text.BadLocationException
This method returns the location of the end of the current word, or, if offs is at the end of a word or between words, this method returns the location at the end of the next word.

This is mostly used for Emacs emulation. CUA usually uses getNextWord().

Parameters:
c - The editor
offs - The offset in the document >= 0
Throws:
javax.swing.text.BadLocationException - if the offset is out of range.

getNextWord

public static final int getNextWord(javax.swing.text.JTextComponent c,
                                    int offs)
                             throws javax.swing.text.BadLocationException
Determines the start of the next word for the given location. Uses BreakIterator.getWordInstance() to actually get the words.
Parameters:
c - the editor
offs - the offset in the document >= 0
Throws:
javax.swing.text.BadLocationException - if the offset is out of range

getPreviousWord

public static final int getPreviousWord(javax.swing.text.JTextComponent c,
                                        int offs)
                                 throws javax.swing.text.BadLocationException
Determine the start of the prev word for the given location. Uses BreakIterator.getWordInstance() to actually get the words.
Parameters:
c - the editor
offs - the offset in the document >= 0
Throws:
javax.swing.text.BadLocationException - if the offset is out of range

getLineElement

public static final javax.swing.text.Element getLineElement(javax.swing.text.JTextComponent c,
                                                            int offs)
Determines the element to use for a paragraph/line.
Parameters:
c - the editor
offs - the starting offset in the document >= 0
Returns:
the element

isBlankLine

public static boolean isBlankLine(javax.swing.text.JTextComponent c,
                                  javax.swing.text.Element lineElement)
                           throws javax.swing.text.BadLocationException
Determines whether or not a particular element consists entirely of whitespace.
Parameters:
c - the editor
lineElement - the element.
Returns:
true if the line is blank.

isBlankLine

public static boolean isBlankLine(char[] textLine)
Determines whether or not a particular String consists entirely of whitespace.
Parameters:
textLine - The text to check.
Returns:
true if the line is blank.

getTabbedTextPosition

public static int getTabbedTextPosition(javax.swing.text.JTextComponent c,
                                        javax.swing.text.Element lineElement,
                                        int tabSize,
                                        int offs)
                                 throws javax.swing.text.BadLocationException
Determines the character offset of a position, converting all spaces to tabs.
Parameters:
c - the editor
lineElement - the element.
tabSize - the size of an individual tab
offs - The location to check. This offset is relative to the start of the line (i.e. a position at the start of the line would be 0).
Returns:
The location of the offs in the lineElement

getTabbedTextPosition

public static int getTabbedTextPosition(java.lang.String text,
                                        int tabSize,
                                        int offs)
Determines the character offset of a position, converting all spaces to tabs.
Parameters:
text - The text to check.
tabSize - the size of an individual tab
offs - The location to check. This offset is relative to the start of the line (i.e. a position at the start of the line would be 0).
Returns:
The location of the offs in the string

getTabbedTextPosition

public static int getTabbedTextPosition(char[] textArray,
                                        int tabSize,
                                        int offs)
Determines the character offset of a position, converting all spaces to tabs.
Parameters:
textArray - The text to check.
tabSize - the size of an individual tab
offs - The location to check. This offset is relative to the start of the line (i.e. a position at the start of the line would be 0).
Returns:
The location of the offs in the string

isSkipCharacter

protected static final boolean isSkipCharacter(char ch,
                                               boolean skipOnUnderline)
This method is used to determine if a character can be skipped when scanning for word boundaries.

Note that the underline character '_' is not a skip character for the JBuilder 2.0 editor, but it *is* a skip character for Emacs.

Returns:
True if the character is whitespace or a symbol.

drawTabbedText

public static final int drawTabbedText(javax.swing.text.Segment s,
                                       int x,
                                       int y,
                                       java.awt.Graphics g,
                                       javax.swing.text.TabExpander e,
                                       int startOffset)
Draws the given text, expanding any tabs that are contained using the given tab expansion technique. This particular implementation renders in a 1.1 style coordinate system where ints are used and 72dpi is assumed.

In this implementation, we assume that all of the text we're drawing is fixed pitch, thus we only need to calculate the character width once.

Parameters:
s - the source of the text
x - the X origin >= 0
y - the Y origin >= 0
g - the graphics context
e - how to expand the tabs. If this value is null, tabs will be expanded as a space character.
startOffset - starting offset of the text in the document >= 0

getTabbedTextWidth

public static final int getTabbedTextWidth(javax.swing.text.Segment s,
                                           java.awt.FontMetrics metrics,
                                           int x,
                                           javax.swing.text.TabExpander e,
                                           int startOffset)
Determines the width of the given segment of text taking tabs into consideration. This is implemented in a 1.1 style coordinate system where ints are used and 72dpi is assumed.

In this implementation, we assume that all of the text we're drawing is fixed pitch, thus we only need to calculate the character width once.

Parameters:
s - the source of the text
metrics - the font metrics to use for the calculation
x - the X origin >= 0
e - how to expand the tabs. If this value is null, tabs will be expanded as a space character.
startOffset - starting offset of the text in the document >= 0

ripTextAt

public static java.lang.String ripTextAt(EditorPane editor,
                                         int offset)
Return the string at the location specified by offset. The string at a particular location is defined as the text between delimiter characters on the left and right. In this method, the standard set of delimiters is used.
Parameters:
characters - The editor from which to rip a String.
offset - The location in the editor which contains the String of interest.
Returns:
The string.

ripTextAt

public static java.lang.String ripTextAt(EditorPane editor,
                                         int offset,
                                         java.lang.String delimiters)
Return the string at the location specified by offset. The string at a particular location is defined as the text between delimiter characters on the left and right.
Parameters:
characters - The editor from which to rip a String.
offset - The location in the editor which contains the String of interest.
delimiters - A String containing the delimiters that define the bounds of the String.
Returns:
The string.

ripTextAt

public static java.lang.String ripTextAt(EditorPane editor,
                                         int offset,
                                         java.lang.String startDelimiters,
                                         java.lang.String endDelimiters)
Return the string at the location specified by offset. The string at a particular location is defined as the text between delimiter characters on the left and right.
Parameters:
characters - The editor from which to rip a String.
offset - The location in the editor which contains the String of interest.
startDelimiters - A String containing the delimiters that define the start of the String.
endDelimiters - A String containing the delimiters that define the end of the String.
Returns:
The string.

ripTextAt

public static java.lang.String ripTextAt(char[] characters,
                                         int startOffset,
                                         int endOffset,
                                         int offset,
                                         java.lang.String delimiters)
Return the string at the location specified by offset. The string at a particular location is defined as the text between delimiter characters on the left and right.
Parameters:
characters - The array of characters from which to rip a String.
startOffset - The location of the start of the characters of interest in the array.
endOffset - The location of the end of the characters of interest in the array.
offset - The location in the array which contains the String of interest. IMPORTANT: This value is an offset from the startOffset, not an offset from the start of the array.
delimiters - A String containing the delimiters that define the bounds of the String.
Returns:
The string.

ripTextAt

public static java.lang.String ripTextAt(char[] characters,
                                         int startOffset,
                                         int endOffset,
                                         int offset,
                                         java.lang.String startDelimiters,
                                         java.lang.String endDelimiters)
Return the string at the location specified by offset. The string at a particular location is defined as the text between delimiter characters on the left and right.
Parameters:
characters - The array of characters from which to rip a String.
startOffset - The location of the start of the characters of interest in the array.
endOffset - The location of the end of the characters of interest in the array.
offset - The location in the array which contains the String of interest. IMPORTANT: This value is an offset from the startOffset, not an offset from the start of the array.
startDelimiters - A String containing the delimiters that define the start of the String.
endDelimiters - A String containing the delimiters that define the end of the String.
Returns:
The string.

getTokenStart

public static int getTokenStart(char[] characters,
                                int startOffset,
                                int endOffset,
                                int offset,
                                java.lang.String delimiters)
Return the offset of the start of a token. The start is defined as the start of the text string between the specified offset and the preceeding delimiter.
Parameters:
characters - The array of characters to check.
startOffset - The location of the start of the characters of interest in the array.
endOffset - The location of the end of the characters of interest in the array.
offset - The location in the array which contains the String of interest. IMPORTANT: This value is an offset from the startOffset, not an offset from the start of the array.
delimiters - A String containing the delimiters that define the bounds of the String.
Returns:
the start of the token.

getTokenEnd

public static int getTokenEnd(char[] characters,
                              int startOffset,
                              int endOffset,
                              int offset,
                              java.lang.String delimiters)
Return the offset of the end of a token. The end is defined as the end of the text string between the specified offset and the next delimiter.
Parameters:
characters - The array of characters to check.
startOffset - The location of the start of the characters of interest in the array.
endOffset - The location of the end of the characters of interest in the array.
offset - The location in the array which contains the String of interest. IMPORTANT: This value is an offset from the startOffset, not an offset from the start of the array.
delimiters - A String containing the delimiters that define the bounds of the String.
Returns:
the end of the token.