edu.neu.ccs.gui
Class MultiLineTextPaintable

java.lang.Object
  extended byedu.neu.ccs.gui.AbstractPaintable
      extended byedu.neu.ccs.gui.PaintableSequenceComposite
          extended byedu.neu.ccs.gui.MultiLineTextPaintable
All Implemented Interfaces:
JPTConstants, Paintable, SupportsPropertyChange, SwingConstants

public class MultiLineTextPaintable
extends PaintableSequenceComposite

Class MultiLineTextPaintable provides the basic functionality of TextPaintable for text that contains newlines.

The class TextPaintable accepts text with newlines but does not treat them properly. The newlines are simply ignored and all text is placed on one line. There are deep technical reasons for this. The Java tools that we use to render the text and later to test for mouse hits is fundamentally a single-line technology. Since we have no other way to test for mouse hits, we must rely on this Java technology in TextPaintable.

In this class, we solve the problem of multi-line text by breaking the text into an array of single-line strings, using each line to create a TextPaintable, and then collecting these TextPaintable objects into an internal PaintableSequence. Here is a summary of the parameter options:

Depending on the constructor called, the parameters may be supplied or left to be the following defaults:

Of course, when the string is empty, nothing will be painted.

Since:
2.6.0
Version:
2.6.0
See Also:
TextPaintable

Field Summary
private  float anchorX
          The x anchor position.
private  float anchorY
          The y anchor position.
private  Paint fillpaint
          The fill paint.
private  Font font
          The font.
private  TextAnchor.Locator locator
          The text anchor locator.
static String SET_ANCHOR_LOCATOR
          Bound property name for set anchor locator.
static String SET_ANCHOR_POSITION
          Bound property name for set anchor position.
static String SET_FILL_PAINT
          Bound property name for set fill paint.
static String SET_FONT
          Bound property name for set font.
static String SET_STRING
          Bound property name for set string.
private  TextBounds.Strategy strategy
          The text bounds strategy.
private  String string
          The text string.
 
Fields inherited from class edu.neu.ccs.gui.PaintableSequenceComposite
 
Fields inherited from class edu.neu.ccs.gui.AbstractPaintable
SET_BACKGROUND_PAINT, SET_BACKGROUND_TILE
 
Fields inherited from interface edu.neu.ccs.gui.Paintable
REMOVE_PAINTABLE, SET_CLIPPING_SHAPE, SET_DEFAULT_BOUNDS2D, SET_DEFAULT_CENTER, SET_DEFAULT_ORIGINAL_BOUNDS2D, SET_DEFAULT_ORIGINAL_CENTER, SET_MUTATOR, SET_OPACITY, SET_PAINTABLE, SET_VISIBLE, SHIFT_PAINTABLE
 
Fields inherited from interface edu.neu.ccs.util.JPTConstants
ABOVE, ALIGNMENT, BELOW, BOTTOM_LEFT, BOTTOM_RIGHT, DEFAULT, FONT, INPUT_PROPERTIES, MANDATORY, OPTIONAL, ORIENTATION, TOP_LEFT, TOP_RIGHT, VALUE
 
Fields inherited from interface javax.swing.SwingConstants
BOTTOM, CENTER, EAST, HORIZONTAL, LEADING, LEFT, NEXT, NORTH, NORTH_EAST, NORTH_WEST, PREVIOUS, RIGHT, SOUTH, SOUTH_EAST, SOUTH_WEST, TOP, TRAILING, VERTICAL, WEST
 
Constructor Summary
MultiLineTextPaintable()
          The default constructor.
MultiLineTextPaintable(String string)
          The constructor that creates a MultiLineTextPaintable with the given string.
MultiLineTextPaintable(String string, float anchorX, float anchorY)
          The constructor that creates a MultiLineTextPaintable with the given string and anchor position.
MultiLineTextPaintable(String string, Font font)
          The constructor that creates a MultiLineTextPaintable with the given string and font.
MultiLineTextPaintable(String string, Font font, float anchorX, float anchorY)
          The constructor that creates a MultiLineTextPaintable with the given string, font, and anchor position.
MultiLineTextPaintable(String string, Font font, Paint fillpaint)
          The constructor that creates a MultiLineTextPaintable with the given string, font, fill paint.
MultiLineTextPaintable(String string, Font font, Paint fillpaint, float anchorX, float anchorY)
          The constructor that creates a MultiLineTextPaintable with the given string, font, fill paint, and anchor position.
MultiLineTextPaintable(String string, Font font, Paint fillpaint, TextAnchor.Locator locator, float anchorX, float anchorY)
          The constructor that creates a MultiLineTextPaintable with the given string, font, fill paint, bounds strategy, anchor locator, and anchor position.
MultiLineTextPaintable(String string, Font font, TextAnchor.Locator locator, float anchorX, float anchorY)
          The constructor that creates a MultiLineTextPaintable with the given string, font, anchor locator, and anchor position.
MultiLineTextPaintable(String string, TextAnchor.Locator locator, float anchorX, float anchorY)
          The constructor that creates a MultiLineTextPaintable with the given string, anchor locator, and anchor position.
 
Method Summary
 TextAnchor.Locator getAnchorLocator()
          Returns the TextAnchor.Locator for this MultiLineTextPaintable.
 float[] getAnchorPosition()
          Returns a copy of the anchor position for the first line this MultiLineTextPaintable as an array float[2].
 Paint getFillPaint()
          Returns the fill paint for this MultiLineTextPaintable.
 Font getFont()
          Returns the font for this MultiLineTextPaintable.
 String[] getLines()
          Returns the string for this MultiLineTextPaintable split into individual lines based on the newline character.
 XPoint2D getOriginalCenter()
          Returns a copy of the center of the paint region affected by the originalPaint method.
 String getString()
          Returns the string for this MultiLineTextPaintable.
protected  void initMLTP(String string, Font font, Paint fillpaint, TextAnchor.Locator locator, float anchorX, float anchorY)
          The helper method to carry out the work of the constructors.
protected  void makeMLTP()
          The helper method to build the MultiLineTextPaintable once the settings are established.
 void setAnchorLocator(TextAnchor.Locator locator)
          Sets the TextAnchor.Locator for this MultiLineTextPaintable.
 void setAnchorPosition(float[] position)
          Sets the anchor position for the first line of this MultiLineTextPaintable using an array float[2].
 void setAnchorPosition(float anchorX, float anchorY)
          Sets the anchor position for the first line of this MultiLineTextPaintable.
 void setFillPaint(Paint fillpaint)
          Sets the fill paint for this MultiLineTextPaintable.
 void setFont(Font font)
          Sets the font for this MultiLineTextPaintable.
 void setString(String string)
          Sets the string for this MultiLineTextPaintable.
 void shiftAnchorPosition(float dx, float dy)
          Shift the anchor position for each line of this MultiLineTextPaintable by the given dx and dy.
 
Methods inherited from class edu.neu.ccs.gui.PaintableSequenceComposite
clearBothInnerBackgrounds, clearInnerBackgroundPaint, clearInnerBackgroundTile, getActualBounds2D, getInnerBackgroundPaint, getInnerBackgroundTile, getPaintableSequence, originalContains, originalPaint, setInnerBackgroundPaint, setInnerBackgroundTile
 
Methods inherited from class edu.neu.ccs.gui.AbstractPaintable
addForwardingListener, addPostMutation, addPostMutation, addPreMutation, addPreMutation, addPropertyChangeListener, addPropertyChangeListener, addPropertyChangeListeners, addPropertyChangeListeners, affinetransform, applyOpacity, clearBackgroundPaint, clearBackgroundTile, clearBothBackgrounds, contains, contains, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getBackgroundPaint, getBackgroundTile, getBounds2D, getCenter, getCorner, getDefaultBounds2D, getDefaultCenter, getDefaultOriginalBounds2D, getDefaultOriginalCenter, getForwardingListener, getMutator, getMutatorInverse, getOpacity, getOriginalBounds2D, getPreparedGraphics2D, getPropertyChangeListeners, getPropertyChangeListeners, glide, glidereflect, hasListeners, hreflect, isVisible, lineartransform, makeSnapshot, move, move, moveCenterTo, moveCenterTo, moveCornerTo, moveCornerTo, mutate, mutate, originalContains, paint, paintAsTiles, paintAsTiles, paintAsTiles, paintAt, paintAt, paintAt, paintAt, possiblyContains, reflect, removeAndAddForwardingListener, removeForwardingListener, removePropertyChangeListener, removePropertyChangeListener, rotate, scale, scale, setBackgroundPaint, setBackgroundTile, setDefaultBounds2D, setDefaultCenter, setDefaultOriginalBounds2D, setDefaultOriginalCenter, setMutator, setMutator, setOpacity, setVisible, shear, vreflect
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

SET_STRING

public static final String SET_STRING
Bound property name for set string.

See Also:
Constant Field Values

SET_FONT

public static final String SET_FONT
Bound property name for set font.

See Also:
Constant Field Values

SET_FILL_PAINT

public static final String SET_FILL_PAINT
Bound property name for set fill paint.

See Also:
Constant Field Values

SET_ANCHOR_LOCATOR

public static final String SET_ANCHOR_LOCATOR
Bound property name for set anchor locator.

See Also:
Constant Field Values

SET_ANCHOR_POSITION

public static final String SET_ANCHOR_POSITION
Bound property name for set anchor position.

See Also:
Constant Field Values

string

private String string
The text string.


font

private Font font
The font.


fillpaint

private Paint fillpaint
The fill paint.


strategy

private TextBounds.Strategy strategy
The text bounds strategy.


locator

private TextAnchor.Locator locator
The text anchor locator.


anchorX

private float anchorX
The x anchor position.


anchorY

private float anchorY
The y anchor position.

Constructor Detail

MultiLineTextPaintable

public MultiLineTextPaintable()

The default constructor.

The following defaults are set:

All properties may be changed after construction.

With the default settings, the paintable will display nothing since its text string is empty.


MultiLineTextPaintable

public MultiLineTextPaintable(String string)

The constructor that creates a MultiLineTextPaintable with the given string.

Parameters:
string - the string to display on multiple lines

MultiLineTextPaintable

public MultiLineTextPaintable(String string,
                              float anchorX,
                              float anchorY)

The constructor that creates a MultiLineTextPaintable with the given string and anchor position.

Parameters:
string - the string to display on multiple lines
anchorX - the anchor x-coordinate for the first line
anchorY - the anchor y-coordinate for the first line

MultiLineTextPaintable

public MultiLineTextPaintable(String string,
                              TextAnchor.Locator locator,
                              float anchorX,
                              float anchorY)

The constructor that creates a MultiLineTextPaintable with the given string, anchor locator, and anchor position.

Parameters:
string - the string to display on multiple lines
locator - the locator for the anchor position
anchorX - the anchor x-coordinate for the first line
anchorY - the anchor y-coordinate for the first line

MultiLineTextPaintable

public MultiLineTextPaintable(String string,
                              Font font)

The constructor that creates a MultiLineTextPaintable with the given string and font.

Parameters:
string - the string to display on multiple lines
font - the font to use to display the string

MultiLineTextPaintable

public MultiLineTextPaintable(String string,
                              Font font,
                              float anchorX,
                              float anchorY)

The constructor that creates a MultiLineTextPaintable with the given string, font, and anchor position.

Parameters:
string - the string to display on multiple lines
font - the font to use to display the string
anchorX - the anchor x-coordinate for the first line
anchorY - the anchor y-coordinate for the first line

MultiLineTextPaintable

public MultiLineTextPaintable(String string,
                              Font font,
                              TextAnchor.Locator locator,
                              float anchorX,
                              float anchorY)

The constructor that creates a MultiLineTextPaintable with the given string, font, anchor locator, and anchor position.

Parameters:
string - the string to display on multiple lines
font - the font to use to display the string
locator - the locator for the anchor position
anchorX - the anchor x-coordinate for the first line
anchorY - the anchor y-coordinate for the first line

MultiLineTextPaintable

public MultiLineTextPaintable(String string,
                              Font font,
                              Paint fillpaint)

The constructor that creates a MultiLineTextPaintable with the given string, font, fill paint.

Parameters:
string - the string to display on multiple lines
font - the font to use to display the string
fillpaint - the paint to use to fill the string

MultiLineTextPaintable

public MultiLineTextPaintable(String string,
                              Font font,
                              Paint fillpaint,
                              float anchorX,
                              float anchorY)

The constructor that creates a MultiLineTextPaintable with the given string, font, fill paint, and anchor position.

Parameters:
string - the string to display on multiple lines
font - the font to use to display the string
fillpaint - the paint to use to fill the string
anchorX - the anchor x-coordinate for the first line
anchorY - the anchor y-coordinate for the first line

MultiLineTextPaintable

public MultiLineTextPaintable(String string,
                              Font font,
                              Paint fillpaint,
                              TextAnchor.Locator locator,
                              float anchorX,
                              float anchorY)

The constructor that creates a MultiLineTextPaintable with the given string, font, fill paint, bounds strategy, anchor locator, and anchor position.

Parameters:
string - the string to display on multiple lines
font - the font to use to display the string
fillpaint - the paint to use to fill the string
locator - the locator for the anchor position
anchorX - the anchor x-coordinate for the first line
anchorY - the anchor y-coordinate for the first line
Method Detail

initMLTP

protected void initMLTP(String string,
                        Font font,
                        Paint fillpaint,
                        TextAnchor.Locator locator,
                        float anchorX,
                        float anchorY)

The helper method to carry out the work of the constructors.

Parameters:
string - the string to display on multiple lines
font - the font to use to display the string
fillpaint - the paint to use to fill the string
locator - the locator for the anchor position
anchorX - the anchor x-coordinate for the first line
anchorY - the anchor y-coordinate for the first line

makeMLTP

protected void makeMLTP()

The helper method to build the MultiLineTextPaintable once the settings are established.


setString

public void setString(String string)

Sets the string for this MultiLineTextPaintable.

If the given string is null, it is set to the zero length string.

Does nothing if the string is equal to the current string.

Fires property change: SET_STRING.

Parameters:
string - the string to display on multiple lines

getString

public String getString()
Returns the string for this MultiLineTextPaintable.

Returns:
the string to display on multiple lines

getLines

public String[] getLines()
Returns the string for this MultiLineTextPaintable split into individual lines based on the newline character.


setFont

public void setFont(Font font)

Sets the font for this MultiLineTextPaintable.

If the given font is null then it is set to TextPaintable.getDefaultFont().

Does nothing if the font is equal to the current font.

Fires property change: SET_FONT.

Parameters:
font - the font to use to display the string

getFont

public Font getFont()
Returns the font for this MultiLineTextPaintable.

Returns:
the font to use to display the string

setFillPaint

public void setFillPaint(Paint fillpaint)

Sets the fill paint for this MultiLineTextPaintable.

If the given paint is null, it is set to Color.black.

Fires property change: SET_FILL_PAINT.

Parameters:
fillpaint - the paint to use to fill the string

getFillPaint

public Paint getFillPaint()
Returns the fill paint for this MultiLineTextPaintable.

Returns:
the paint to use to fill the string

setAnchorLocator

public void setAnchorLocator(TextAnchor.Locator locator)

Sets the TextAnchor.Locator for this MultiLineTextPaintable.

The following constants are provided in the class TextAnchor as possible options for the TextAnchor.Locator parameter:

If the given locator is null, it is set to TextAnchor.LEFT_ASCENTLINE.

Fires property change: SET_ANCHOR_LOCATOR.

Parameters:
locator - the locator for the anchor position

getAnchorLocator

public TextAnchor.Locator getAnchorLocator()

Returns the TextAnchor.Locator for this MultiLineTextPaintable.

Returns:
the locator for the anchor position

setAnchorPosition

public void setAnchorPosition(float anchorX,
                              float anchorY)

Sets the anchor position for the first line of this MultiLineTextPaintable.

The anchor position for each subsequent line is then adjusted appropriately.

Fires property change: SET_ANCHOR_POSITION.

Parameters:
anchorX - the anchor x-coordinate
anchorY - the anchor y-coordinate

setAnchorPosition

public void setAnchorPosition(float[] position)

Sets the anchor position for the first line of this MultiLineTextPaintable using an array float[2].

The anchor position for each subsequent line is then adjusted appropriately.

A null parameter is ignored.

Fires property change: SET_ANCHOR_POSITION.

Parameters:
position - the anchor position

shiftAnchorPosition

public void shiftAnchorPosition(float dx,
                                float dy)

Shift the anchor position for each line of this MultiLineTextPaintable by the given dx and dy.

Fires property change: SET_ANCHOR_POSITION.

Parameters:
dx - the shift in the anchor x position
dy - the shift in the anchor y position

getAnchorPosition

public float[] getAnchorPosition()
Returns a copy of the anchor position for the first line this MultiLineTextPaintable as an array float[2].

Returns:
the anchor position

getOriginalCenter

public XPoint2D getOriginalCenter()

Returns a copy of the center of the paint region affected by the originalPaint method.

If the user has set a default original position then that is returned. Otherwise, we define the original center to be the anchor position. This will usually not be the geometric center.

Specified by:
getOriginalCenter in interface Paintable
Overrides:
getOriginalCenter in class AbstractPaintable
Returns:
a copy of the center of the original paint region