JHotDraw 6.0b1

org.jhotdraw.contrib
Class TextAreaFigure

java.lang.Object
  extended by org.jhotdraw.standard.AbstractFigure
      extended by org.jhotdraw.figures.AttributeFigure
          extended by org.jhotdraw.contrib.TextAreaFigure
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable, java.util.EventListener, Figure, FigureChangeListener, TextHolder, Storable
Direct Known Subclasses:
HTMLTextAreaFigure

public class TextAreaFigure
extends AttributeFigure
implements FigureChangeListener, TextHolder

A TextAreaFigure contains formatted text.
It automatically rearranges the text to fit its allocated display area, breaking the lines at word boundaries whenever possible.
The text can contain either LF or CRLF sequences to separate paragraphs, as well as tab characters for table like formatting and alignment.
Currently the tabs are distributed at regular intervals as determined by the TabSize property. Tabs align correctly with either fixed or variable fonts.
If, when resizing, the vertical size of the display box is not enough to display all the text, TextAreaFigure displays a dashed red line at the bottom of the figure to indicate there is hidden text.
TextAreFigure uses all standard attributes for the area rectangle, ie: FillColor, PenColor for the border, FontSize, FontStyle, and FontName, as well as four additional attributes LeftMargin, RightMargin, TopMargin, and TabSize.

See Also:
Serialized Form

Field Summary
protected  java.util.Hashtable attributesMap
          Map of attributes for the AttributedString used for the figure's text.
protected  java.awt.Font fFont
          The current font
protected  boolean fFontIsDirty
          True if the font has changed and font related calculations need to be remade
protected  float fFontWidth
          The width of the current font
protected  boolean fIsReadOnly
          True if the figure is read only
protected  OffsetLocator fLocator
          Description of the Field
protected  Figure fObservedFigure
          A connected figure
protected  java.util.List fParagraphs
          Paragraph cache resulting from splitting the text
protected  boolean fSizeIsDirty
          True if the sizing needs to be recalculated
protected  java.lang.String fText
          The text
protected  boolean fTextIsDirty
          True if the paragraph's cache needs to be reconstructed
 
Constructor Summary
TextAreaFigure()
          Constructor for the TextAreaFigure object
 
Method Summary
 boolean acceptsTyping()
          Tests whether the figure accepts typing.
 void basicDisplayBox(java.awt.Point origin, java.awt.Point corner)
          Sets the display box for the figure
protected  void basicMoveBy(int x, int y)
          Moves the figure the supplied offset
 void connect(Figure figure)
          Connects the figure to another figure
 java.awt.Font createFont()
          Creates the font from current attributes.
 void disconnect(Figure disconnectFigure)
          Disconnects a text holder from a connect figure.
 java.awt.Rectangle displayBox()
          Returns the current display box for the figure
 void draw(java.awt.Graphics g)
          Draws the figure.
 void drawBackground(java.awt.Graphics g)
          Draws the background for the figure.
 void drawFrame(java.awt.Graphics g)
          Draws the frame around the text
protected  float drawText(java.awt.Graphics g, java.awt.Rectangle displayBox)
          Formats and draws the text for the figure
 void figureChanged(FigureChangeEvent e)
          A connected figure has changed, update the figure's location
 void figureInvalidated(FigureChangeEvent e)
          Description of the Method
 void figureRemoved(FigureChangeEvent e)
          The figure is about to be removed from another composite figure
 void figureRequestRemove(FigureChangeEvent e)
          A request to remove the figure from another composite figure
 void figureRequestUpdate(FigureChangeEvent e)
          Sent when an update should happen.
 java.lang.Object getAttribute(java.lang.String name)
          Deprecated. use getAttribute(FigureAttributeConstant)
 java.awt.Font getFont()
          Gets the font.
protected  float getFontWidth()
          Gets the font width for the active font.
protected  java.lang.String getNextParagraph(java.lang.String text, java.awt.Point pos)
          Gets the next paragraph in the supplied string
Paragraphs are defined by a LF or CRLF sequence
Scanning starts from the next characters as given by the pos.y value
 Figure getRepresentingFigure()
          Usually, a TextHolders is implemented by a Figure subclass.
 java.lang.String getText()
          Gets the text of the figure
 java.awt.Color getTextColor()
          Gets the text color of a figure.
 TextHolder getTextHolder()
          Some figures have the ability to hold text.
 HandleEnumeration handles()
          Returns an iterator of standard sizing handles to manipulate the figure
 boolean isEmpty()
          Gets the empty attribute of the figure.
 boolean isFontDirty()
          Gets the fontDirty attribute of the TextAreaFigure object
 boolean isReadOnly()
           
 boolean isSizeDirty()
          Returns the current size dirty status
 boolean isTextDirty()
          Gets the textDirty attribute of the TextAreaFigure object
protected  void markFontDirty()
          Called whenever the something changes that requires font recomputing
protected  void markSizeDirty()
          Called whenever the something changes that requires size recomputing
protected  void markTextDirty()
          Called whenever the something changes that requires text recomputing
 void moveBy(int x, int y)
          Moves the figure the supplied offset
 int overlayColumns()
          Gets the number of columns to be overlaid when the figure is edited.
This method is mandatory by the TextHolder interface but is not used by the TextAreaFigure/TextAreaTool couple because the overlay always covers the text area display box
protected  void prepareText()
          Splits the text into paragraphs.
 void read(StorableInput dr)
          Reads the figure from StorableInput
protected  void readObject(java.io.ObjectInputStream s)
          Reads the figure from an object stream
 void setAttribute(java.lang.String name, java.lang.Object value)
          Deprecated. use setAttribute(FigureAttributeConstant, Object)
 void setFont(java.awt.Font newFont)
          Sets the font.
 void setFontDirty(boolean newFontIsDirty)
          Sets the fontDirty attribute of the TextAreaFigure object
 void setReadOnly(boolean newReadOnly)
           
 void setSizeDirty(boolean newSizeIsDirty)
          Called to set the dirty status of the size
 void setText(java.lang.String newText)
          Sets the text of the figure
protected  void setTextDirty(boolean newTextDirty)
          Sets the textDirty attribute of the TextAreaFigure object
 java.awt.Rectangle textDisplayBox()
          Returns the display box for the text
protected  void updateFontInfo()
          Retrieve all Font information needed
protected  void updateLocation()
          Updates the location relative to the connected figure.
 void write(StorableOutput dw)
          Writes the figure to StorableOutput
 
Methods inherited from class org.jhotdraw.figures.AttributeFigure
getAttribute, getDefaultAttribute, getDefaultAttribute, getFillColor, getFrameColor, initDefaultAttribute, setAttribute, setDefaultAttribute
 
Methods inherited from class org.jhotdraw.standard.AbstractFigure
addDependendFigure, addFigureChangeListener, addToContainer, canConnect, center, changed, clone, connectedTextLocator, connectionInsets, connectorAt, connectorVisibility, containsPoint, decompose, displayBox, displayBox, figures, findFigureInside, getDecoratedFigure, getDependendFigures, getZValue, includes, invalidate, invalidateRectangle, listener, release, removeDependendFigure, removeFigureChangeListener, removeFromContainer, setZValue, size, visit, willChange
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

fTextIsDirty

protected boolean fTextIsDirty
True if the paragraph's cache needs to be reconstructed


fSizeIsDirty

protected transient boolean fSizeIsDirty
True if the sizing needs to be recalculated


fParagraphs

protected java.util.List fParagraphs
Paragraph cache resulting from splitting the text


fText

protected java.lang.String fText
The text


fFont

protected java.awt.Font fFont
The current font


fFontIsDirty

protected boolean fFontIsDirty
True if the font has changed and font related calculations need to be remade


fFontWidth

protected float fFontWidth
The width of the current font


attributesMap

protected java.util.Hashtable attributesMap
Map of attributes for the AttributedString used for the figure's text. Currently it just uses one single attribute with the figure's current font.


fIsReadOnly

protected boolean fIsReadOnly
True if the figure is read only


fObservedFigure

protected Figure fObservedFigure
A connected figure


fLocator

protected OffsetLocator fLocator
Description of the Field

Constructor Detail

TextAreaFigure

public TextAreaFigure()
Constructor for the TextAreaFigure object

Method Detail

getText

public java.lang.String getText()
Gets the text of the figure

Specified by:
getText in interface TextHolder
Returns:
The text value

setText

public void setText(java.lang.String newText)
Sets the text of the figure

Specified by:
setText in interface TextHolder
Parameters:
newText - The new text value

textDisplayBox

public java.awt.Rectangle textDisplayBox()
Returns the display box for the text

Specified by:
textDisplayBox in interface TextHolder
Returns:
Description of the Return Value

createFont

public java.awt.Font createFont()
Creates the font from current attributes.

Returns:
Description of the Return Value

isReadOnly

public boolean isReadOnly()

setReadOnly

public void setReadOnly(boolean newReadOnly)

acceptsTyping

public boolean acceptsTyping()
Tests whether the figure accepts typing.

Specified by:
acceptsTyping in interface TextHolder
Returns:
Description of the Return Value

markTextDirty

protected void markTextDirty()
Called whenever the something changes that requires text recomputing


setTextDirty

protected void setTextDirty(boolean newTextDirty)
Sets the textDirty attribute of the TextAreaFigure object

Parameters:
newTextDirty - The new textDirty value

isTextDirty

public boolean isTextDirty()
Gets the textDirty attribute of the TextAreaFigure object

Returns:
The textDirty value

markSizeDirty

protected void markSizeDirty()
Called whenever the something changes that requires size recomputing


setSizeDirty

public void setSizeDirty(boolean newSizeIsDirty)
Called to set the dirty status of the size

Parameters:
newSizeIsDirty - The new sizeDirty value

isSizeDirty

public boolean isSizeDirty()
Returns the current size dirty status

Returns:
The sizeDirty value

getFont

public java.awt.Font getFont()
Gets the font.

Specified by:
getFont in interface TextHolder
Returns:
The font value

setFont

public void setFont(java.awt.Font newFont)
Sets the font.

Parameters:
newFont - The new font value

overlayColumns

public int overlayColumns()
Gets the number of columns to be overlaid when the figure is edited.
This method is mandatory by the TextHolder interface but is not used by the TextAreaFigure/TextAreaTool couple because the overlay always covers the text area display box

Specified by:
overlayColumns in interface TextHolder
Returns:
the number of overlay columns

basicDisplayBox

public void basicDisplayBox(java.awt.Point origin,
                            java.awt.Point corner)
Sets the display box for the figure

Specified by:
basicDisplayBox in interface Figure
Specified by:
basicDisplayBox in class AbstractFigure
Parameters:
origin - origin point
corner - corner point
See Also:
Figure

handles

public HandleEnumeration handles()
Returns an iterator of standard sizing handles to manipulate the figure

Specified by:
handles in interface Figure
Specified by:
handles in class AbstractFigure
Returns:
Description of the Return Value
See Also:
Handle

displayBox

public java.awt.Rectangle displayBox()
Returns the current display box for the figure

Specified by:
displayBox in interface Figure
Specified by:
displayBox in class AbstractFigure
Returns:
Description of the Return Value
See Also:
Figure.basicDisplayBox(java.awt.Point, java.awt.Point)

moveBy

public void moveBy(int x,
                   int y)
Moves the figure the supplied offset

Specified by:
moveBy in interface Figure
Overrides:
moveBy in class AbstractFigure
Parameters:
x - x displacement
y - y displacement

basicMoveBy

protected void basicMoveBy(int x,
                           int y)
Moves the figure the supplied offset

Specified by:
basicMoveBy in class AbstractFigure
Parameters:
x - x displacement
y - y displacement
See Also:
AbstractFigure.moveBy(int, int)

drawBackground

public void drawBackground(java.awt.Graphics g)
Draws the background for the figure. Called by the superclass with the colors set from the current attribute values

Overrides:
drawBackground in class AttributeFigure
Parameters:
g - The graphics to use for the drawing
See Also:
AttributeFigure.draw(java.awt.Graphics)

draw

public void draw(java.awt.Graphics g)
Draws the figure. Overriden so as to draw the text once everything else has been drawn

Specified by:
draw in interface Figure
Overrides:
draw in class AttributeFigure
Parameters:
g - The graphics to use for the drawing

drawFrame

public void drawFrame(java.awt.Graphics g)
Draws the frame around the text

Overrides:
drawFrame in class AttributeFigure
Parameters:
g - The graphics to use for the drawing
See Also:
AttributeFigure.draw(java.awt.Graphics)

drawText

protected float drawText(java.awt.Graphics g,
                         java.awt.Rectangle displayBox)
Formats and draws the text for the figure

Parameters:
g - the graphics for the drawing. It can be null when called just to compute the size
displayBox - the display box within which the text should be formatted and drawn
Returns:
Description of the Return Value

prepareText

protected void prepareText()
Splits the text into paragraphs. A paragraph is delimited by a LF or CRLF. If the paragraph is empty it returns a single space so the display logic has something to work with


getNextParagraph

protected java.lang.String getNextParagraph(java.lang.String text,
                                            java.awt.Point pos)
Gets the next paragraph in the supplied string
Paragraphs are defined by a LF or CRLF sequence
Scanning starts from the next characters as given by the pos.y value

Parameters:
text - the text to break into paragraphs
pos - a point where pos.x is the position of the first character of the paragraph in the string and pos.y is the last
Returns:
The text for the paragraph

getAttribute

public java.lang.Object getAttribute(java.lang.String name)
Deprecated. use getAttribute(FigureAttributeConstant)

A text area figure uses the "LeftMargin", "RightMargin", "TopMargin", "TabSize", "FontSize", "FontStyle", and "FontName" attributes

Specified by:
getAttribute in interface Figure
Overrides:
getAttribute in class AttributeFigure
Parameters:
name - the attribute's name
Returns:
the attribute value

setAttribute

public void setAttribute(java.lang.String name,
                         java.lang.Object value)
Deprecated. use setAttribute(FigureAttributeConstant, Object)

A text area figure uses the "LeftMargin", "RightMargin", "TopMargin", "TabSize", "FontSize", "FontStyle", and "FontName" attributes

Specified by:
setAttribute in interface Figure
Overrides:
setAttribute in class AttributeFigure
Parameters:
name - the new attribute name
value - the new attribute value

write

public void write(StorableOutput dw)
Writes the figure to StorableOutput

Specified by:
write in interface Storable
Overrides:
write in class AttributeFigure
Parameters:
dw - the output storable

read

public void read(StorableInput dr)
          throws java.io.IOException
Reads the figure from StorableInput

Specified by:
read in interface Storable
Overrides:
read in class AttributeFigure
Parameters:
dr - Description of the Parameter
Throws:
java.io.IOException - the inout storable

readObject

protected void readObject(java.io.ObjectInputStream s)
                   throws java.lang.ClassNotFoundException,
                          java.io.IOException
Reads the figure from an object stream

Parameters:
s - the input stream
Throws:
java.lang.ClassNotFoundException - thrown by called methods
java.io.IOException - thrown by called methods

connect

public void connect(Figure figure)
Connects the figure to another figure

Specified by:
connect in interface TextHolder
Parameters:
figure - the connecting figure

disconnect

public void disconnect(Figure disconnectFigure)
Disconnects a text holder from a connect figure.

Specified by:
disconnect in interface TextHolder
Parameters:
disconnectFigure - the disconnecting figure

figureInvalidated

public void figureInvalidated(FigureChangeEvent e)
Description of the Method

Specified by:
figureInvalidated in interface FigureChangeListener
Parameters:
e - Description of the Parameter

figureChanged

public void figureChanged(FigureChangeEvent e)
A connected figure has changed, update the figure's location

Specified by:
figureChanged in interface FigureChangeListener
Parameters:
e - Description of the Parameter

updateLocation

protected void updateLocation()
Updates the location relative to the connected figure. The TextAreaFigure is centered around the located point.


figureRemoved

public void figureRemoved(FigureChangeEvent e)
The figure is about to be removed from another composite figure

Specified by:
figureRemoved in interface FigureChangeListener
Parameters:
e - Description of the Parameter

figureRequestRemove

public void figureRequestRemove(FigureChangeEvent e)
A request to remove the figure from another composite figure

Specified by:
figureRequestRemove in interface FigureChangeListener
Parameters:
e - Description of the Parameter

figureRequestUpdate

public void figureRequestUpdate(FigureChangeEvent e)
Description copied from interface: FigureChangeListener
Sent when an update should happen.

Specified by:
figureRequestUpdate in interface FigureChangeListener
Parameters:
e - Description of the Parameter

getFontWidth

protected float getFontWidth()
Gets the font width for the active font. This is by convention the width of the 'W' character, the widest one

Returns:
The fontWidth value

updateFontInfo

protected void updateFontInfo()
Retrieve all Font information needed


getTextColor

public java.awt.Color getTextColor()
Gets the text color of a figure. This is a convenience method.

Returns:
The textColor value
See Also:
getAttribute(java.lang.String)

isEmpty

public boolean isEmpty()
Gets the empty attribute of the figure. True if there is no text

Specified by:
isEmpty in interface Figure
Overrides:
isEmpty in class AbstractFigure
Returns:
The empty value
See Also:
Figure.isEmpty()

markFontDirty

protected void markFontDirty()
Called whenever the something changes that requires font recomputing


isFontDirty

public boolean isFontDirty()
Gets the fontDirty attribute of the TextAreaFigure object

Returns:
The fontDirty value

setFontDirty

public void setFontDirty(boolean newFontIsDirty)
Sets the fontDirty attribute of the TextAreaFigure object

Parameters:
newFontIsDirty - The new fontDirty value

getRepresentingFigure

public Figure getRepresentingFigure()
Usually, a TextHolders is implemented by a Figure subclass. To avoid casting a TextHolder to a Figure this method can be used for polymorphism (in this case, let the (same) object appear to be of another type). Note, that the figure returned is not the figure to which the TextHolder is (and its representing figure) connected.

Specified by:
getRepresentingFigure in interface TextHolder
Returns:
figure responsible for representing the content of this TextHolder

getTextHolder

public TextHolder getTextHolder()
Description copied from interface: Figure
Some figures have the ability to hold text. This method returns the adjunctant TextHolder.

Specified by:
getTextHolder in interface Figure
Overrides:
getTextHolder in class AbstractFigure
Returns:
See Also:
Figure.getTextHolder()

JHotDraw 6.0b1