com.arsdigita.toolbox.rebop
Class StyledText

java.lang.Object
  extended bycom.arsdigita.toolbox.rebop.AbstractDrawable
      extended bycom.arsdigita.toolbox.rebop.StyledText
All Implemented Interfaces:
GenericDrawable

public class StyledText
extends AbstractDrawable
implements GenericDrawable

Represents text that contains multiple fonts and colors. A simple word processor could use a single StyledText object for managing its contents.

Author:
Gavin Doughtie

Field Summary
static String versionId
           
 
Constructor Summary
StyledText()
           
 
Method Summary
 void addRun(FormattedText run)
          Adds a run of text that uses the same font and color.
 void draw(Graphics g, int x, int y, int width, int height)
          GenericDrawable implementation.
static void drawWrappedText(Graphics g, Font baseFont, Vector runs, Dimension size, int leftMargin, boolean measureOnly, Point startPoint, Dimension preferredDimension, Vector wrappedRuns)
          Draws and/or measures runs of styled text
 FormattedText getFormattedTextAt(int index)
           
 Dimension getPreferredSize(Graphics g, int width, int height)
           
 Vector getWrappedRuns()
           
 void setBaseFont(Font font)
          Sets the default font for all runs that don't specify a font
 void setFormattedTextAt(int index, FormattedText ft)
          Replaces an existing run with a new one
 void setRunColor(int runIndex, Color color)
          Changes the color of a specific run.
 void setWrappedRuns(Vector runs)
          if a caller is caching runs after they have been wrapped once, the pre-wrapped runs can be added here.
 String toString()
           
 
Methods inherited from class com.arsdigita.toolbox.rebop.AbstractDrawable
getXOffset, getYOffset, setXOffset, setYOffset
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface com.arsdigita.toolbox.rebop.GenericDrawable
getXOffset, getYOffset, setXOffset, setYOffset
 

Field Detail

versionId

public static final String versionId
See Also:
Constant Field Values
Constructor Detail

StyledText

public StyledText()
Method Detail

addRun

public void addRun(FormattedText run)
Adds a run of text that uses the same font and color. Runs are appended to an internal list of runs and are rendered in order.

Parameters:
run - Font, Color and String specification

setWrappedRuns

public void setWrappedRuns(Vector runs)
if a caller is caching runs after they have been wrapped once, the pre-wrapped runs can be added here. if a StyledText object has any wrapped runs, they will be drawn instead of re-wrapping.

Parameters:
runs - Vector of WrappedRun objects

getWrappedRuns

public Vector getWrappedRuns()
Returns:
Vector of WrappedRun objects

draw

public void draw(Graphics g,
                 int x,
                 int y,
                 int width,
                 int height)
GenericDrawable implementation. Draws wrapped runs, or wraps and draws all runs.

Specified by:
draw in interface GenericDrawable
Parameters:
g - Graphics to draw on
x - horizontal location to begin drawing
y - vertical location to begin drawing
width - width of area to draw in
height - height of area to draw in

setBaseFont

public void setBaseFont(Font font)
Sets the default font for all runs that don't specify a font

Parameters:
font - Font to draw in

getFormattedTextAt

public FormattedText getFormattedTextAt(int index)
Parameters:
index - index of a specific run to retrieve
Returns:
the retrieved run or null if no run exists at the specified index.

setFormattedTextAt

public void setFormattedTextAt(int index,
                               FormattedText ft)
Replaces an existing run with a new one

Parameters:
index - index of an existing run
ft - FormattedText to add at index

setRunColor

public void setRunColor(int runIndex,
                        Color color)
Changes the color of a specific run. Typically called by buttons or list items that want to change their text colors when selected.

Parameters:
color - new color for the run

toString

public String toString()
Returns:
the unformatted text of all runs as a single string

getPreferredSize

public Dimension getPreferredSize(Graphics g,
                                  int width,
                                  int height)
Parameters:
g - Graphics from which to calculate dimensions
width - width within which to calculate wrapped text
height - height of area for wrapped text
Returns:
a Dimension within which all text could be displayed

drawWrappedText

public static void drawWrappedText(Graphics g,
                                   Font baseFont,
                                   Vector runs,
                                   Dimension size,
                                   int leftMargin,
                                   boolean measureOnly,
                                   Point startPoint,
                                   Dimension preferredDimension,
                                   Vector wrappedRuns)
Draws and/or measures runs of styled text

Parameters:
g - Graphics context to draw the wrapped text upon.
baseFont - Font to use if none is specified in the run
runs - FormattedText runs to draw
size - text will be wrapped within the width of this Dimension. Text will not be drawn below the bottom of this size, but if the measureOnly flag is set, the max height WILL be calculated.
leftMargin - new lines after the first line will begin at this horizontal offset in the current coordinate system.
startPoint - The location in the current coordinate system to begin drawing the first line of text
preferredDimension - @out if all the text were to be drawn, this dimension would be required to hold it. Only valid if measureOnly is set.
measureOnly - if true, calculate preferredDimension but do not draw.
wrappedRuns - if non-null but empty, WrappedRuns as used to draw the StyledText will be returned in this parameter. If non-null AND non-empty, wrapped runs will be drawn and no further word wrapping or calculations will be performed.


Copyright (c) 2004 Red Hat, Inc. Corporation. All Rights Reserved. Generated at July 21 2004:2337 UTC