javax.swing.text
Class Utilities

java.lang.Object
  extended byjavax.swing.text.Utilities

public class Utilities
extends Object

A collection of methods to deal with various text related activities.

Author:
Timothy Prinzing

Constructor Summary
Utilities()
           
 
Method Summary
(package private) static int drawComposedText(AttributeSet attr, Graphics g, int x, int y, int p0, int p1)
          Draws the given composed text passed from an input method.
static int drawTabbedText(Segment s, int x, int y, Graphics g, TabExpander e, int startOffset)
          Draws the given text, expanding any tabs that are contained using the given tab expansion technique.
static int getBreakLocation(Segment s, FontMetrics metrics, int x0, int x, TabExpander e, int startOffset)
          Determine where to break the given text to fit within the the given span.
(package private) static int getNextVisualPositionFrom(View v, int pos, Position.Bias b, Shape alloc, int direction, Position.Bias[] biasRet)
          Provides a way to determine the next visually represented model location that one might place a caret.
static int getNextWord(JTextComponent c, int offs)
          Determines the start of the next word for the given location.
(package private) static int getNextWordInParagraph(JTextComponent c, Element line, int offs, boolean first)
          Finds the next word in the given elements text.
static Element getParagraphElement(JTextComponent c, int offs)
          Determines the element to use for a paragraph/line.
static int getPositionAbove(JTextComponent c, int offs, int x)
          Determines the position in the model that is closest to the given view location in the row above.
static int getPositionBelow(JTextComponent c, int offs, int x)
          Determines the position in the model that is closest to the given view location in the row below.
static int getPreviousWord(JTextComponent c, int offs)
          Determine the start of the prev word for the given location.
(package private) static int getPrevWordInParagraph(JTextComponent c, Element line, int offs)
          Finds the previous word in the given elements text.
static int getRowEnd(JTextComponent c, int offs)
          Determines the ending row model position of the row that contains the specified model position.
static int getRowStart(JTextComponent c, int offs)
          Determines the starting row model position of the row that contains the specified model position.
static int getTabbedTextOffset(Segment s, FontMetrics metrics, int x0, int x, TabExpander e, int startOffset)
          Determines the relative offset into the given text that best represents the given span in the view coordinate system.
static int getTabbedTextOffset(Segment s, FontMetrics metrics, int x0, int x, TabExpander e, int startOffset, boolean round)
           
static int getTabbedTextWidth(Segment s, FontMetrics metrics, int x, TabExpander e, int startOffset)
          Determines the width of the given segment of text taking tabs into consideration.
static int getWordEnd(JTextComponent c, int offs)
          Determines the end of a word for the given location.
static int getWordStart(JTextComponent c, int offs)
          Determines the start of a word for the given model location.
(package private) static boolean isComposedTextAttributeDefined(AttributeSet as)
           
(package private) static boolean isComposedTextElement(Document doc, int offset)
           
(package private) static boolean isComposedTextElement(Element elem)
           
(package private) static boolean isLeftToRight(Component c)
           
(package private) static void paintComposedText(Graphics g, Rectangle alloc, GlyphView v)
          Paints the composed text in a GlyphView
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Utilities

public Utilities()
Method Detail

drawTabbedText

public static final int drawTabbedText(Segment s,
                                       int x,
                                       int y,
                                       Graphics g,
                                       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.

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
Returns:
the X location at the end of the rendered text

getTabbedTextWidth

public static final int getTabbedTextWidth(Segment s,
                                           FontMetrics metrics,
                                           int x,
                                           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.

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
Returns:
the width of the text

getTabbedTextOffset

public static final int getTabbedTextOffset(Segment s,
                                            FontMetrics metrics,
                                            int x0,
                                            int x,
                                            TabExpander e,
                                            int startOffset)
Determines the relative offset into the given text that best represents the given span in the view coordinate system. This is implemented in a 1.1 style coordinate system where ints are used and 72dpi is assumed.

Parameters:
s - the source of the text
metrics - the font metrics to use for the calculation
x0 - the starting view location representing the start of the given text >= 0.
x - the target view location to translate to an offset into the text >= 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
Returns:
the offset into the text >= 0

getTabbedTextOffset

public static final int getTabbedTextOffset(Segment s,
                                            FontMetrics metrics,
                                            int x0,
                                            int x,
                                            TabExpander e,
                                            int startOffset,
                                            boolean round)

getBreakLocation

public static final int getBreakLocation(Segment s,
                                         FontMetrics metrics,
                                         int x0,
                                         int x,
                                         TabExpander e,
                                         int startOffset)
Determine where to break the given text to fit within the the given span. This tries to find a whitespace boundary.

Parameters:
s - the source of the text
metrics - the font metrics to use for the calculation
x0 - the starting view location representing the start of the given text.
x - the target view location to translate to an offset into the text.
e - how to expand the tabs. If this value is null, tabs will be expanded as a space character.
startOffset - starting offset in the document of the text
Returns:
the offset into the given text

getRowStart

public static final int getRowStart(JTextComponent c,
                                    int offs)
                             throws BadLocationException
Determines the starting row model position of the row that contains the specified model position. 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
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

getRowEnd

public static final int getRowEnd(JTextComponent c,
                                  int offs)
                           throws BadLocationException
Determines the ending row model position of the row that contains the specified model position. 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
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

getPositionAbove

public static final int getPositionAbove(JTextComponent c,
                                         int offs,
                                         int x)
                                  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(JTextComponent c,
                                         int offs,
                                         int x)
                                  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

getWordStart

public static final int getWordStart(JTextComponent c,
                                     int offs)
                              throws BadLocationException
Determines the start of a word for the given model location. Uses BreakIterator.getWordInstance() to actually get the words.

Parameters:
c - the editor
offs - the offset in the document >= 0
Returns:
the location in the model of the word start >= 0
Throws:
BadLocationException - if the offset is out of range

getWordEnd

public static final int getWordEnd(JTextComponent c,
                                   int offs)
                            throws BadLocationException
Determines the end of a word for the given location. Uses BreakIterator.getWordInstance() to actually get the words.

Parameters:
c - the editor
offs - the offset in the document >= 0
Returns:
the location in the model of the word end >= 0
Throws:
BadLocationException - if the offset is out of range

getNextWord

public static final int getNextWord(JTextComponent c,
                                    int offs)
                             throws 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
Returns:
the location in the model of the word start >= 0
Throws:
BadLocationException - if the offset is out of range

getNextWordInParagraph

static int getNextWordInParagraph(JTextComponent c,
                                  Element line,
                                  int offs,
                                  boolean first)
                           throws BadLocationException
Finds the next word in the given elements text. The first parameter allows searching multiple paragraphs where even the first offset is desired. Returns the offset of the next word, or BreakIterator.DONE if there are no more words in the element.

Throws:
BadLocationException

getPreviousWord

public static final int getPreviousWord(JTextComponent c,
                                        int offs)
                                 throws 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
Returns:
the location in the model of the word start >= 0
Throws:
BadLocationException - if the offset is out of range

getPrevWordInParagraph

static int getPrevWordInParagraph(JTextComponent c,
                                  Element line,
                                  int offs)
                           throws BadLocationException
Finds the previous word in the given elements text. The first parameter allows searching multiple paragraphs where even the first offset is desired. Returns the offset of the next word, or BreakIterator.DONE if there are no more words in the element.

Throws:
BadLocationException

getParagraphElement

public static final Element getParagraphElement(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

isComposedTextElement

static boolean isComposedTextElement(Document doc,
                                     int offset)

isComposedTextElement

static boolean isComposedTextElement(Element elem)

isComposedTextAttributeDefined

static boolean isComposedTextAttributeDefined(AttributeSet as)

drawComposedText

static int drawComposedText(AttributeSet attr,
                            Graphics g,
                            int x,
                            int y,
                            int p0,
                            int p1)
                     throws BadLocationException
Draws the given composed text passed from an input method.

Parameters:
attr - the attributes containing the composed text
g - the graphics context
x - the X origin
y - the Y origin
p0 - starting offset in the composed text to be rendered
p1 - ending offset in the composed text to be rendered
Returns:
the new insertion position
Throws:
BadLocationException

paintComposedText

static void paintComposedText(Graphics g,
                              Rectangle alloc,
                              GlyphView v)
Paints the composed text in a GlyphView


isLeftToRight

static boolean isLeftToRight(Component c)

getNextVisualPositionFrom

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

This implementation assumes the views are layed out in a logical manner. That is, that the view at index x + 1 is visually after the View at index x, and that the View at index x - 1 is visually before the View at x. There is support for reversing this behavior only if the passed in View is an instance of CompositeView. The CompositeView must then override the flipEastAndWestAtEnds method.

Parameters:
v - View to query
pos - the position to convert >= 0
direction - the direction from the current position that can be thought of as the arrow keys typically found on a keyboard; this may be one of the following:
  • SwingConstants.WEST
  • SwingConstants.EAST
  • SwingConstants.NORTH
  • SwingConstants.SOUTH
biasRet - an array contain the bias that was checked
Returns:
the location within the model that best represents the next location visual position
Throws:
BadLocationException
IllegalArgumentException - if direction is invalid