javax.swing.text

Class Utilities

public class Utilities extends Object

A set of utilities to deal with text. This is used by several other classes inside this package.
Constructor Summary
Utilities()
Creates a new Utilities object.
Method Summary
static intdrawTabbedText(Segment s, int x, int y, Graphics g, TabExpander e, int startOffset)
Draws the given text segment.
static intgetBreakLocation(Segment s, FontMetrics metrics, int x0, int x, TabExpander e, int startOffset)
Determine where to break the text in the given Segment, attempting to find a word boundary.
static intgetNextWord(JTextComponent c, int offs)
Finds the start of the next word for the given offset.
static ElementgetParagraphElement(JTextComponent c, int offset)
Returns the paragraph element in the text component c at the specified location offset.
static intgetPositionAbove(JTextComponent c, int offset, int x)
Returns the document position that is closest above to the specified x coordinate in the row containing offset.
static intgetPositionBelow(JTextComponent c, int offset, int x)
Returns the document position that is closest below to the specified x coordinate in the row containing offset.
static intgetPreviousWord(JTextComponent c, int offs)
Finds the start of the previous word for the given offset.
static intgetRowEnd(JTextComponent c, int offs)
Get the model position of the end of the row that contains the specified model position.
static intgetRowStart(JTextComponent c, int offs)
Get the model position of the start of the row that contains the specified model position.
static intgetTabbedTextOffset(Segment s, FontMetrics fm, int x0, int x, TabExpander te, int p0, boolean round)
Provides a facility to map screen coordinates into a model location.
static intgetTabbedTextOffset(Segment s, FontMetrics fm, int x0, int x, TabExpander te, int p0)
Provides a facility to map screen coordinates into a model location.
static intgetTabbedTextWidth(Segment s, FontMetrics metrics, int x, TabExpander e, int startOffset)
Determines the width, that the given text s would take if it was printed with the given {@link java.awt.FontMetrics} on the specified screen position.
static intgetWordEnd(JTextComponent c, int offs)
Finds the end of a word for the given location.
static intgetWordStart(JTextComponent c, int offs)
Finds the start of a word for the given location.

Constructor Detail

Utilities

public Utilities()
Creates a new Utilities object.

Method Detail

drawTabbedText

public static final int drawTabbedText(Segment s, int x, int y, Graphics g, TabExpander e, int startOffset)
Draws the given text segment. Contained tabs and newline characters are taken into account. Tabs are expanded using the specified {@link TabExpander}. The X and Y coordinates denote the start of the baseline where the text should be drawn.

Parameters: s the text fragment to be drawn. x the x position for drawing. y the y position for drawing. g the {@link Graphics} context for drawing. e the {@link TabExpander} which specifies the Tab-expanding technique. startOffset starting offset in the text.

Returns: the x coordinate at the end of the drawn text.

getBreakLocation

public static final int getBreakLocation(Segment s, FontMetrics metrics, int x0, int x, TabExpander e, int startOffset)
Determine where to break the text in the given Segment, attempting to find a word boundary.

Parameters: s the Segment that holds the text metrics the font metrics used for calculating the break point x0 starting view location representing the start of the text x the target view location e the TabExpander used for expanding tabs (if this is null tabs are expanded to 1 space) startOffset the offset in the Document of the start of the text

Returns: the offset at which we should break the text

getNextWord

public static final int getNextWord(JTextComponent c, int offs)
Finds the start of the next word for the given offset.

Parameters: c the text component offs the offset in the document

Returns: the location in the model of the start of the next word.

Throws: BadLocationException if the offset is invalid.

getParagraphElement

public static final Element getParagraphElement(JTextComponent c, int offset)
Returns the paragraph element in the text component c at the specified location offset.

Parameters: c the text component offset the offset of the paragraph element to return

Returns: the paragraph element at offset

getPositionAbove

public static final int getPositionAbove(JTextComponent c, int offset, int x)
Returns the document position that is closest above to the specified x coordinate in the row containing offset.

Parameters: c the text component offset the offset x the x coordinate

Returns: the document position that is closest above to the specified x coordinate in the row containing offset

Throws: BadLocationException if offset is not a valid offset

getPositionBelow

public static final int getPositionBelow(JTextComponent c, int offset, int x)
Returns the document position that is closest below to the specified x coordinate in the row containing offset.

Parameters: c the text component offset the offset x the x coordinate

Returns: the document position that is closest above to the specified x coordinate in the row containing offset

Throws: BadLocationException if offset is not a valid offset

getPreviousWord

public static final int getPreviousWord(JTextComponent c, int offs)
Finds the start of the previous word for the given offset.

Parameters: c the text component offs the offset in the document

Returns: the location in the model of the start of the previous word.

Throws: BadLocationException if the offset is invalid.

getRowEnd

public static final int getRowEnd(JTextComponent c, int offs)
Get the model position of the end of the row that contains the specified model position. Return null if the given JTextComponent does not have a size.

Parameters: c the JTextComponent offs the model position

Returns: the model position of the end of the row containing the given offset

Throws: BadLocationException if the offset is invalid

getRowStart

public static final int getRowStart(JTextComponent c, int offs)
Get the model position of the start of the row that contains the specified model position. Return null if the given JTextComponent does not have a size.

Parameters: c the JTextComponent offs the model position

Returns: the model position of the start of the row containing the given offset

Throws: BadLocationException if the offset is invalid

getTabbedTextOffset

public static final int getTabbedTextOffset(Segment s, FontMetrics fm, int x0, int x, TabExpander te, int p0, boolean round)
Provides a facility to map screen coordinates into a model location. For a given text fragment and start location within this fragment, this method determines the model location so that the resulting fragment fits best into the span [x0, x]. The parameter round controls which model location is returned if the view coordinates are on a character: If round is true, then the result is rounded up to the next character, so that the resulting fragment is the smallest fragment that is larger than the specified span. If round is false, then the resulting fragment is the largest fragment that is smaller than the specified span.

Parameters: s the text segment fm the font metrics to use x0 the starting screen location x the target screen location at which the requested fragment should end te the tab expander to use; if this is null, TABs are expanded to one space character p0 the starting model location round if true round up to the next location, otherwise round down to the current location

Returns: the model location, so that the resulting fragment fits within the specified span

getTabbedTextOffset

public static final int getTabbedTextOffset(Segment s, FontMetrics fm, int x0, int x, TabExpander te, int p0)
Provides a facility to map screen coordinates into a model location. For a given text fragment and start location within this fragment, this method determines the model location so that the resulting fragment fits best into the span [x0, x]. This method rounds up to the next location, so that the resulting fragment will be the smallest fragment of the text, that is greater than the specified span.

Parameters: s the text segment fm the font metrics to use x0 the starting screen location x the target screen location at which the requested fragment should end te the tab expander to use; if this is null, TABs are expanded to one space character p0 the starting model location

Returns: the model location, so that the resulting fragment fits within the specified span

getTabbedTextWidth

public static final int getTabbedTextWidth(Segment s, FontMetrics metrics, int x, TabExpander e, int startOffset)
Determines the width, that the given text s would take if it was printed with the given {@link java.awt.FontMetrics} on the specified screen position.

Parameters: s the text fragment metrics the font metrics of the font to be used x the x coordinate of the point at which drawing should be done e the {@link TabExpander} to be used startOffset the index in s where to start

UNKNOWN: the width of the given text s. This takes tabs and newlines into account.

getWordEnd

public static final int getWordEnd(JTextComponent c, int offs)
Finds the end of a word for the given location.

Parameters: c the text component offs the offset location

Returns: the location of the word end

Throws: BadLocationException if the offset location is invalid

getWordStart

public static final int getWordStart(JTextComponent c, int offs)
Finds the start of a word for the given location.

Parameters: c the text component offs the offset location

Returns: the location of the word beginning

Throws: BadLocationException if the offset location is invalid