com.rolemodelsoft.drawlet.util
Class BasicStringComposer

java.lang.Object
  |
  +--com.rolemodelsoft.drawlet.util.BasicStringRenderer
        |
        +--com.rolemodelsoft.drawlet.util.BasicStringComposer

public class BasicStringComposer
extends BasicStringRenderer

This provides basic default functionality for StringRenderers that provide wrapping. The first version doesn't handle all possibilities, but works for most purposes. NOTE: it is possible that individual line widths could be greater than the maxWidth as widths include calculation of ending whitespace in case someone wants to do something with them. However, no non-END_ABSORB characters should show up past the maxWidth.


Field Summary
protected  int goodWidth
          This keeps track of the last known x coordinate that we know to to be good (i.e.
protected  int maxHeight
          The maximum height to compose.
protected  int maxWidth
          The maximum width to allow non-END_ABSORBing (whitespace) characters before wrapping.
 
Fields inherited from class com.rolemodelsoft.drawlet.util.BasicStringRenderer
ALL_VERTICAL, begin, begins, end, ends, height, last, manuals, metrics, next, NO_INDEX, specials, startX, startY, string, width, widths
 
Constructor Summary
BasicStringComposer(java.lang.String string)
          Answer an instance prepared to render a String.
BasicStringComposer(java.lang.String string, java.awt.Font font)
          Answer an instance prepared to render a string using a particular font
BasicStringComposer(java.lang.String string, java.awt.Font font, int maxWidth)
          Answer an instance prepared to render a string using a particular font, wrapping after a given width.
BasicStringComposer(java.lang.String string, java.awt.Font font, int maxWidth, int maxHeight)
          Answer an instance prepared to render a string using a particular font, wrapping after a given width, and ignoring text past a given height.
BasicStringComposer(java.lang.String string, int maxWidth)
          Answer an instance prepared to render a string, wrapping after a given width.
BasicStringComposer(java.lang.String string, int maxWidth, int maxHeight)
          Answer an instance prepared to render a string, wrapping after a given width, and ignoring text past a given height.
 
Method Summary
protected  void addLine()
          Record the current settings as the end of a line.
protected  void beginNextLine()
          Set up to compose the next line.
protected  void closeLastLine()
          Make the last/pending line into a "real one".
protected  void compose()
          Compose the text in such a way as to produce all lines necessary to display text properly.
protected  void compose(int yBegin, int yEnd)
          Compose the text in such a way as to produce the lines necessary to display text between the given vertical coordinates.
protected  void composeVerticalArea(int yBegin, int yEnd)
          Compose the text in such a way as to produce the lines necessary to display text between the given vertical coordinates.
protected  int defaultMaxHeight()
          Answer the default/initial value for maxHeight.
protected  int defaultMaxWidth()
          Answer the default/initial value for maxWidth.
protected  int[] defaultSpecials()
          Answer the default/initial value for the array where characters can be marked as special.
protected  boolean handleSpecial(java.lang.String string, int index)
          Handles the special character at index during composition.
protected  boolean isFullyComposed()
          Answer whether the string has been fully composed.
 void paint(java.awt.Graphics g)
          Paints the string.
 java.lang.String toString()
          Returns the String representation of the receiver's values.
 
Methods inherited from class com.rolemodelsoft.drawlet.util.BasicStringRenderer
absorbEnd, adjustWidthForSpecial, areAllAbsorbed, compose, composeAll, composeIfNecessary, defaultFont, defaultMetrics, defaultStartX, defaultStartY, getBaseline, getFont, getLineHeight, getMetrics, getRawStringLines, getSpecialFlags, getSpecialFlags, getString, getStringHeight, getStringLines, getStringWidth, handleManually, indexOfSpecial, newLine, nextTabStop, paint, paintSpecial, paintTextBetween, reset, scanToSpecial, setFont, setString, verifyMetrics
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

maxWidth

protected int maxWidth
The maximum width to allow non-END_ABSORBing (whitespace) characters before wrapping.

maxHeight

protected int maxHeight
The maximum height to compose. ALL_VERTICAL means continue to end NOTE: If this is set to something other than the default no characters beyond those needed to get to that height will be taken into consideration.

goodWidth

protected int goodWidth
This keeps track of the last known x coordinate that we know to to be good (i.e. not past the maxWidth) for the last character we know we can display on the current line being composed.
Constructor Detail

BasicStringComposer

public BasicStringComposer(java.lang.String string)
Answer an instance prepared to render a String.
Parameters:
string - the string to render

BasicStringComposer

public BasicStringComposer(java.lang.String string,
                           int maxWidth)
Answer an instance prepared to render a string, wrapping after a given width.
Parameters:
string - the string to render
maxWidth - the width at which to wrap text to a new line

BasicStringComposer

public BasicStringComposer(java.lang.String string,
                           int maxWidth,
                           int maxHeight)
Answer an instance prepared to render a string, wrapping after a given width, and ignoring text past a given height.
Parameters:
string - the string to render
maxWidth - the width at which to wrap text to a new line
maxHeight - the height at which to ignore other text

BasicStringComposer

public BasicStringComposer(java.lang.String string,
                           java.awt.Font font)
Answer an instance prepared to render a string using a particular font
Parameters:
string - the string to render
font - the font to use when rendering

BasicStringComposer

public BasicStringComposer(java.lang.String string,
                           java.awt.Font font,
                           int maxWidth)
Answer an instance prepared to render a string using a particular font, wrapping after a given width.
Parameters:
string - the string to render
font - the font to use when rendering
maxWidth - the width at which to wrap text to a new line

BasicStringComposer

public BasicStringComposer(java.lang.String string,
                           java.awt.Font font,
                           int maxWidth,
                           int maxHeight)
Answer an instance prepared to render a string using a particular font, wrapping after a given width, and ignoring text past a given height.
Parameters:
string - the string to render
font - the font to use when rendering
maxWidth - the width at which to wrap text to a new line
maxHeight - the height at which to ignore other text
Method Detail

addLine

protected void addLine()
Record the current settings as the end of a line. Leave the running counters alone so others may use them before preparing to start the next line. Used during composition.
Overrides:
addLine in class BasicStringRenderer
See Also:
beginNextLine

beginNextLine

protected void beginNextLine()
Set up to compose the next line. Reset the running counters and buffers which may be needed. Used during composition. Often after addLine().
Overrides:
beginNextLine in class BasicStringRenderer
See Also:
addLine()

closeLastLine

protected void closeLastLine()
Make the last/pending line into a "real one".
Overrides:
closeLastLine in class BasicStringRenderer

compose

protected void compose()
Compose the text in such a way as to produce all lines necessary to display text properly.
Overrides:
compose in class BasicStringRenderer

compose

protected void compose(int yBegin,
                       int yEnd)
Compose the text in such a way as to produce the lines necessary to display text between the given vertical coordinates.
Overrides:
compose in class BasicStringRenderer
Parameters:
yBegin - the vertical point at which we need to begin composing.
yEnd - the vertical point at which we'll stop composing.

composeVerticalArea

protected void composeVerticalArea(int yBegin,
                                   int yEnd)
Compose the text in such a way as to produce the lines necessary to display text between the given vertical coordinates. However, don't assume that the last character scanned is the end of a real line. It is just the end of the current line as known so far.
Parameters:
yBegin - the vertical point at which we need to begin composing.
yEnd - the vertical point at which we'll stop composing.

defaultMaxHeight

protected int defaultMaxHeight()
Answer the default/initial value for maxHeight.
Returns:
an integer representing the default/initial maxHeight.

defaultMaxWidth

protected int defaultMaxWidth()
Answer the default/initial value for maxWidth.
Returns:
an integer representing the default/initial maxWidth.

defaultSpecials

protected int[] defaultSpecials()
Answer the default/initial value for the array where characters can be marked as special. By default, we assume ASCII. Subclasses can easily override this but may wish to address special characters in a more compact way (perhaps as a different implementer of the base interface).
Overrides:
defaultSpecials in class BasicStringRenderer
Returns:
an array of ints.

handleSpecial

protected boolean handleSpecial(java.lang.String string,
                                int index)
Handles the special character at index during composition. Answer true if a new line is necessary.
Overrides:
handleSpecial in class BasicStringRenderer
Parameters:
string - the string in which to find the special character
index - index of special character
Returns:
boolean whether or not a new line is necessary.

isFullyComposed

protected boolean isFullyComposed()
Answer whether the string has been fully composed.
Overrides:
isFullyComposed in class BasicStringRenderer
Returns:
a boolean value of true if the line is fully composed; false otherwise.

paint

public void paint(java.awt.Graphics g)
Paints the string.
Overrides:
paint in class BasicStringRenderer
Parameters:
g - the specified Graphics window.

toString

public java.lang.String toString()
Returns the String representation of the receiver's values.
Overrides:
toString in class BasicStringRenderer
Returns:
a String representing the receiver's values.