net.sf.vex.layout
Class TextBox

java.lang.Object
  |
  +--net.sf.vex.layout.AbstractBox
        |
        +--net.sf.vex.layout.TextBox
All Implemented Interfaces:
Box, InlineBox
Direct Known Subclasses:
DocumentTextBox, StaticTextBox

public abstract class TextBox
extends AbstractBox
implements InlineBox

An inline box containing text. The getText and splitAt methods are abstract and must be implemented by subclasses.


Nested Class Summary
 
Nested classes inherited from class net.sf.vex.layout.InlineBox
InlineBox.Pair
 
Constructor Summary
TextBox(Element element)
          Class constructor.
 
Method Summary
protected  void calculateSize(LayoutContext context)
          Causes the box to recalculate it size.
 boolean canBreakAtEnd()
          Returns true if a line break can occur after this box.
 int getBaseline()
          Returns the distance from the top of the inline box to the baseline.
 java.awt.Shape[] getCaretShapes(LayoutContext context, int offset)
          Returns shapes for the primary and secondary carets for the given offset.
 Element getElement()
          Returns the element that controls the styling for this text element.
abstract  java.lang.String getText()
          Return the text that comprises this text box.
static boolean isSplitChar(char c)
          Returns true if the given character is one where a linebreak should occur, e.g.
 void paint(LayoutContext context, int x, int y)
          Draws the box's content in the given Graphics context.
 InlineBox.Pair split(LayoutContext context, int maxWidth, boolean force)
          Splits this inline box into two.
abstract  InlineBox.Pair splitAt(LayoutContext context, int offset)
          Return a pair of boxes representing a split at the given offset.
 java.lang.String toString()
           
 
Methods inherited from class net.sf.vex.layout.AbstractBox
containsOffset, getChildren, getEndOffset, getHeight, getStartOffset, getWidth, getX, getY, hasContent, setHeight, setWidth, setX, setY, skipPaint, viewToModel
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface net.sf.vex.layout.Box
containsOffset, getChildren, getEndOffset, getHeight, getStartOffset, getWidth, getX, getY, hasContent, setX, setY, viewToModel
 

Constructor Detail

TextBox

public TextBox(Element element)
Class constructor.

Parameters:
element - Element containing the text. This is used for styling information.
Method Detail

calculateSize

protected void calculateSize(LayoutContext context)
Causes the box to recalculate it size. Subclasses should call this from their constructors after they are initialized.

Parameters:
context - LayoutContext used to calculate size.

canBreakAtEnd

public boolean canBreakAtEnd()
Description copied from interface: InlineBox
Returns true if a line break can occur after this box. For example, this would be true for text boxes that end with a space.

Specified by:
canBreakAtEnd in interface InlineBox
See Also:
InlineBox.canBreakAtEnd()

getBaseline

public int getBaseline()
Description copied from interface: InlineBox
Returns the distance from the top of the inline box to the baseline.

Specified by:
getBaseline in interface InlineBox
See Also:
InlineBox.getBaseline()

getCaretShapes

public java.awt.Shape[] getCaretShapes(LayoutContext context,
                                       int offset)
Description copied from interface: Box
Returns shapes for the primary and secondary carets for the given offset. A two-element array is returned, with the first entry being the primary caret and the second being the secondary caret. The second entry will be null if there is no secondary caret at the given offset. The position of the carets is relative to the top-left corner of the box.

Specified by:
getCaretShapes in interface Box
Overrides:
getCaretShapes in class AbstractBox
Parameters:
context - LayoutContext to be used
offset - offset for which to retrieve the caret
Returns:
a two-element array of Shape objects
See Also:
Box.getCaretShapes(net.sf.vex.layout.LayoutContext, int)

getElement

public Element getElement()
Returns the element that controls the styling for this text element.

Specified by:
getElement in interface Box
Overrides:
getElement in class AbstractBox
See Also:
Box.getElement()

getText

public abstract java.lang.String getText()
Return the text that comprises this text box. The actual text can come from the document content or from a static string.


isSplitChar

public static boolean isSplitChar(char c)
Returns true if the given character is one where a linebreak should occur, e.g. a space.

Parameters:
c - the character to test

paint

public void paint(LayoutContext context,
                  int x,
                  int y)
Description copied from interface: Box
Draws the box's content in the given Graphics context.

Specified by:
paint in interface Box
Overrides:
paint in class AbstractBox
Parameters:
context - LayoutContext containing the Graphics object into which the box should be painted
x - the x-offset at which the box should be painted
y - the y-offset at which the box should be painted
See Also:
Box.paint(net.sf.vex.layout.LayoutContext, int, int)

split

public InlineBox.Pair split(LayoutContext context,
                            int maxWidth,
                            boolean force)
Description copied from interface: InlineBox
Splits this inline box into two. If force is false, this method should find a natural split point (e.g. after a space) and return two boxes representing a split at that point. The width of the last box must not exceed maxWidth. If no such natural split exists, null should be returned as the left box and this returned as the right box.

If force is true, it means we are adding the first inline box to a line, therefore we must return something as the left box. In some cases, we may find a suboptimal split (e.g. between characters) that satisfies this. In other cases, this should be returned as the left box even though it exceeds maxWidth.

In both cases, the right box should contain null if this box fits into maxWidth

Specified by:
split in interface InlineBox
Parameters:
context - the layout context to be used.
maxWidth - Maximum width of the left part of the box.
force - if true, force a suboptimal split
See Also:
InlineBox.split(net.sf.vex.layout.LayoutContext, int, boolean)

splitAt

public abstract InlineBox.Pair splitAt(LayoutContext context,
                                       int offset)
Return a pair of boxes representing a split at the given offset. If split is zero, then the returned left box should be null. If the split is equal to the length of the text, then the right box should be null.

Parameters:
context - LayoutContext used to calculate the sizes of the resulting boxes.
offset - location of the split, relative to the start of the text box.
Returns:

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object
See Also:
Object.toString()