JHotDraw 7.5.1

org.jhotdraw.draw
Class TextAreaFigure

java.lang.Object
  extended by org.jhotdraw.beans.AbstractBean
      extended by org.jhotdraw.draw.AbstractFigure
          extended by org.jhotdraw.draw.AbstractAttributedFigure
              extended by org.jhotdraw.draw.AbstractAttributedDecoratedFigure
                  extended by org.jhotdraw.draw.TextAreaFigure
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable, DecoratedFigure, Figure, TextHolderFigure, DOMStorable

public class TextAreaFigure
extends AbstractAttributedDecoratedFigure
implements TextHolderFigure

A TextHolderFigure which holds multiple lines of text in a rectangular area.

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 Rectangle2D.Double, ie: FillColor, PenColor for the border, FontSize, FontStyle, and FontName, as well as four additional attributes LeftMargin, RightMargin, TopMargin, and TabSize.

A DrawingEditor should provide the TextAreaCreationTool to create a TextAreaFigure.

FIXME - TextAreaFigure should not draw a rectangle on its own but rather rely on a decorator. We probably need a DecoratorConnector for this and we need a way to specify the inner bounds of the decorator. We also need a way to center the text of the TextAreaFigure verticaly and horizontaly.

Version:
$Id: TextAreaFigure.java 626 2010-01-20 08:56:21Z rawcoder $
Author:
Eduardo Francos - InContext (original version), Werner Randelshofer (this derived version)
See Also:
Serialized Form

Field Summary
protected  java.awt.geom.Rectangle2D.Double bounds
           
protected  boolean editable
           
 
Fields inherited from class org.jhotdraw.draw.AbstractFigure
changingDepth, listenerList
 
Fields inherited from class org.jhotdraw.beans.AbstractBean
propertySupport
 
Fields inherited from interface org.jhotdraw.draw.Figure
CONNECTABLE_PROPERTY, REMOVABLE_PROPERTY, SELECTABLE_PROPERTY, TRANSFORMABLE_PROPERTY
 
Constructor Summary
TextAreaFigure()
          Creates a new instance.
TextAreaFigure(java.lang.String text)
           
 
Method Summary
 TextAreaFigure clone()
          Returns a clone of the figure, with clones of all aggregated figures, such as children and decorators.
 java.util.Collection<Handle> createHandles(int detailLevel)
          Creates handles used to manipulate the figure.
protected  void drawFill(java.awt.Graphics2D g)
          This method is called by method draw() to draw the fill area of the figure.
protected  void drawStroke(java.awt.Graphics2D g)
          This method is called by method draw() to draw the text of the figure .
protected  void drawText(java.awt.Graphics2D g)
           
 boolean figureContains(java.awt.geom.Point2D.Double p)
           
 double getBaseline()
          Gets the baseline of the first line of text, relative to the upper left corner of the figure bounds.
 java.awt.geom.Rectangle2D.Double getBounds()
          Returns the untransformed logical bounds of the figure as a Rectangle.
 java.awt.Color getFillColor()
          Returns the fill color to be used by a text editor for editing this Figure.
 java.awt.Font getFont()
          Returns the font to be used by a text editor for editing this Figure.
 float getFontSize()
          Gets the font size of the text held by the TextHolderFigure.
 Insets2D.Double getInsets()
          Returns the insets used to draw text.
 TextHolderFigure getLabelFor()
          Sometimes we want to use a TextHolderFigure as a label for another TextHolderFigure.
 Dimension2DDouble getPreferredTextSize(double maxWidth)
          Returns the preferred text size of the TextAreaFigure.
 int getTabSize()
          Gets the number of characters used to expand tabs.
 java.lang.String getText()
          Gets the text shown by the text figure.
 java.awt.Color getTextColor()
          Returns the text color to be used by a text editor for editing this Figure.
 int getTextColumns()
          Gets the number of columns to be overlaid when the figure is edited.
 Tool getTool(java.awt.geom.Point2D.Double p)
          Returns a specialized tool for the given coordinate.
 java.lang.Object getTransformRestoreData()
          Gets data which can be used to restore the transformation of the figure without loss of precision, after a transform has been applied to it.
 void invalidate()
          Invalidates cached data of the Figure.
 boolean isEditable()
          Returns true if the text of the TextHolderFigure can be edited.
 boolean isTextOverflow()
          Returns true, if the text does not fit into the bounds of the Figure.
 void read(DOMInput in)
           
protected  void readBounds(DOMInput in)
           
 void restoreTransformTo(java.lang.Object geometry)
          Restores the transform of the figure to a previously stored state.
 void setBounds(java.awt.geom.Point2D.Double anchor, java.awt.geom.Point2D.Double lead)
          Sets the logical and untransformed bounds of the figure.
 void setEditable(boolean b)
           
 void setFontSize(float size)
          Sets the font size of the text held by the TextHolderFigure.
 void setText(java.lang.String newText)
          Sets the text shown by the text figure.
 void transform(java.awt.geom.AffineTransform tx)
          Transforms the shape of the Figure.
 void write(DOMOutput out)
           
protected  void writeBounds(DOMOutput out)
           
 
Methods inherited from class org.jhotdraw.draw.AbstractAttributedDecoratedFigure
contains, draw, drawDecorator, drawFigure, getDecorator, getDrawingArea, getFigureDrawingArea, readDecorator, setDecorator, updateDecoratorBounds, writeDecorator
 
Methods inherited from class org.jhotdraw.draw.AbstractAttributedFigure
applyAttributesTo, get, getAttributeKey, getAttributes, getAttributesRestoreData, getStroke, getStrokeMiterLimitFactor, hasAttribute, isAttributeEnabled, readAttributes, removeAttribute, restoreAttributesTo, set, setAttributeEnabled, setAttributes, writeAttributes
 
Methods inherited from class org.jhotdraw.draw.AbstractFigure
addFigureListener, addNotify, basicClone, changed, findCompatibleConnector, findConnector, findFigureInside, fireAreaInvalidated, fireAreaInvalidated, fireAreaInvalidated, fireAttributeChanged, fireFigureAdded, fireFigureChanged, fireFigureChanged, fireFigureChanged, fireFigureHandlesChanged, fireFigureRemoved, fireFigureRequestRemove, fireUndoableEditHappened, getActions, getChangingDepth, getConnectors, getCursor, getDecomposition, getDrawing, getEndPoint, getFontRenderContext, getLayer, getLock, getPreferredSize, getStartPoint, getToolTipText, handleDrop, handleMouseClick, includes, isChanging, isConnectable, isRemovable, isSelectable, isTransformable, isVisible, remap, removeFigureListener, removeNotify, requestRemove, setBounds, setConnectable, setRemovable, setSelectable, setTransformable, setVisible, toString, validate, willChange
 
Methods inherited from class org.jhotdraw.beans.AbstractBean
addPropertyChangeListener, addPropertyChangeListener, firePropertyChange, firePropertyChange, firePropertyChange, getPropertyChangeListeners, removePropertyChangeListener, removePropertyChangeListener
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface org.jhotdraw.draw.Figure
addFigureListener, addNotify, addPropertyChangeListener, changed, contains, draw, findCompatibleConnector, findConnector, findFigureInside, get, getActions, getAttributes, getAttributesRestoreData, getConnectors, getCursor, getDecomposition, getDrawingArea, getEndPoint, getLayer, getPreferredSize, getStartPoint, getToolTipText, handleDrop, handleMouseClick, includes, isConnectable, isRemovable, isSelectable, isTransformable, isVisible, remap, removeFigureListener, removeNotify, removePropertyChangeListener, requestRemove, restoreAttributesTo, set, willChange
 

Field Detail

bounds

protected java.awt.geom.Rectangle2D.Double bounds

editable

protected boolean editable
Constructor Detail

TextAreaFigure

public TextAreaFigure()
Creates a new instance.


TextAreaFigure

public TextAreaFigure(java.lang.String text)
Method Detail

drawText

protected void drawText(java.awt.Graphics2D g)
Overrides:
drawText in class AbstractAttributedFigure

drawFill

protected void drawFill(java.awt.Graphics2D g)
Description copied from class: AbstractAttributedFigure
This method is called by method draw() to draw the fill area of the figure. AbstractAttributedFigure configures the Graphics2D object with the FILL_COLOR attribute before calling this method. If the FILL_COLOR attribute is null, this method is not called.

Specified by:
drawFill in class AbstractAttributedFigure

drawStroke

protected void drawStroke(java.awt.Graphics2D g)
Description copied from class: AbstractAttributedFigure
This method is called by method draw() to draw the text of the figure . AbstractAttributedFigure configures the Graphics2D object with the TEXT_COLOR attribute before calling this method. If the TEXT_COLOR attribute is null, this method is not called.

Specified by:
drawStroke in class AbstractAttributedFigure

setBounds

public void setBounds(java.awt.geom.Point2D.Double anchor,
                      java.awt.geom.Point2D.Double lead)
Description copied from interface: Figure
Sets the logical and untransformed bounds of the figure.

This is used by Tool's which create a new Figure and by Tool's which connect a Figure to another Figure.

This is a basic operation which does not fire events. Use the following code sequence, if you need event firing:

 figure.willChange();
 figure.setBounds(...);
 figure.changed();
 

Specified by:
setBounds in interface Figure
Overrides:
setBounds in class AbstractFigure
Parameters:
anchor - the start point of the bounds
lead - the end point of the bounds
See Also:
Figure.getBounds()

transform

public void transform(java.awt.geom.AffineTransform tx)
Description copied from interface: Figure
Transforms the shape of the Figure. Transformations using double precision arithmethics are inherently lossy operations. Therefore it is recommended to use getTransformRestoreData() restoreTransformTo() to provide lossless undo/redo functionality.

This is a basic operation which does not fire events. Use the following code sequence, if you need event firing:

 figure.willChange();
 figure.transform(...);
 figure.changed();
 

Specified by:
transform in interface Figure
Parameters:
tx - The transformation.
See Also:
Figure.getTransformRestoreData(), Figure.restoreTransformTo(java.lang.Object)

figureContains

public boolean figureContains(java.awt.geom.Point2D.Double p)
Specified by:
figureContains in class AbstractAttributedDecoratedFigure

getBounds

public java.awt.geom.Rectangle2D.Double getBounds()
Description copied from interface: Figure
Returns the untransformed logical bounds of the figure as a Rectangle.

The bounds are used by Handle objects for adjusting the figure and for aligning the figure on a grid.

Specified by:
getBounds in interface Figure

restoreTransformTo

public void restoreTransformTo(java.lang.Object geometry)
Description copied from interface: Figure
Restores the transform of the figure to a previously stored state.

Specified by:
restoreTransformTo in interface Figure

getTransformRestoreData

public java.lang.Object getTransformRestoreData()
Description copied from interface: Figure
Gets data which can be used to restore the transformation of the figure without loss of precision, after a transform has been applied to it.

Specified by:
getTransformRestoreData in interface Figure
See Also:
Figure.transform(AffineTransform)

getText

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

Specified by:
getText in interface TextHolderFigure

getInsets

public Insets2D.Double getInsets()
Returns the insets used to draw text.

Specified by:
getInsets in interface TextHolderFigure

getBaseline

public double getBaseline()
Description copied from interface: TextHolderFigure
Gets the baseline of the first line of text, relative to the upper left corner of the figure bounds.

Specified by:
getBaseline in interface TextHolderFigure

getTabSize

public int getTabSize()
Description copied from interface: TextHolderFigure
Gets the number of characters used to expand tabs.

Specified by:
getTabSize in interface TextHolderFigure

setText

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

Specified by:
setText in interface TextHolderFigure

getTextColumns

public int getTextColumns()
Description copied from interface: TextHolderFigure
Gets the number of columns to be overlaid when the figure is edited.

Specified by:
getTextColumns in interface TextHolderFigure

getFont

public java.awt.Font getFont()
Description copied from interface: TextHolderFigure
Returns the font to be used by a text editor for editing this Figure.

Specified by:
getFont in interface TextHolderFigure

getTextColor

public java.awt.Color getTextColor()
Description copied from interface: TextHolderFigure
Returns the text color to be used by a text editor for editing this Figure.

Specified by:
getTextColor in interface TextHolderFigure

getFillColor

public java.awt.Color getFillColor()
Description copied from interface: TextHolderFigure
Returns the fill color to be used by a text editor for editing this Figure.

Specified by:
getFillColor in interface TextHolderFigure

setFontSize

public void setFontSize(float size)
Description copied from interface: TextHolderFigure
Sets the font size of the text held by the TextHolderFigure.

Specified by:
setFontSize in interface TextHolderFigure

getFontSize

public float getFontSize()
Description copied from interface: TextHolderFigure
Gets the font size of the text held by the TextHolderFigure.

Specified by:
getFontSize in interface TextHolderFigure

isEditable

public boolean isEditable()
Description copied from interface: TextHolderFigure
Returns true if the text of the TextHolderFigure can be edited.

Specified by:
isEditable in interface TextHolderFigure

setEditable

public void setEditable(boolean b)

getTool

public Tool getTool(java.awt.geom.Point2D.Double p)
Returns a specialized tool for the given coordinate.

Returns null, if no specialized tool is available.

Specified by:
getTool in interface Figure
Overrides:
getTool in class AbstractFigure

getLabelFor

public TextHolderFigure getLabelFor()
Description copied from interface: TextHolderFigure
Sometimes we want to use a TextHolderFigure as a label for another TextHolderFigure. Returns the TextHolderFigure that should be really used.

Specified by:
getLabelFor in interface TextHolderFigure

clone

public TextAreaFigure clone()
Description copied from interface: Figure
Returns a clone of the figure, with clones of all aggregated figures, such as children and decorators. The cloned figure does not clone the list of FigureListeners from its original.

Specified by:
clone in interface Figure
Overrides:
clone in class AbstractAttributedDecoratedFigure

createHandles

public java.util.Collection<Handle> createHandles(int detailLevel)
Description copied from interface: Figure
Creates handles used to manipulate the figure.

Specified by:
createHandles in interface Figure
Overrides:
createHandles in class AbstractFigure
Parameters:
detailLevel - The detail level of the handles. Usually this is 0 for bounding box handles and 1 for point handles. The value -1 is used by the SelectAreaTracker and the HandleTracker to highlight figures, over which the mouse pointer is hovering.
Returns:
a Collection of handles
See Also:
Handle

readBounds

protected void readBounds(DOMInput in)
                   throws java.io.IOException
Throws:
java.io.IOException

writeBounds

protected void writeBounds(DOMOutput out)
                    throws java.io.IOException
Throws:
java.io.IOException

read

public void read(DOMInput in)
          throws java.io.IOException
Specified by:
read in interface DOMStorable
Overrides:
read in class AbstractAttributedDecoratedFigure
Throws:
java.io.IOException

write

public void write(DOMOutput out)
           throws java.io.IOException
Specified by:
write in interface DOMStorable
Overrides:
write in class AbstractAttributedDecoratedFigure
Throws:
java.io.IOException

invalidate

public void invalidate()
Description copied from class: AbstractFigure
Invalidates cached data of the Figure. This method must execute fast, because it can be called very often.

Overrides:
invalidate in class AbstractFigure

isTextOverflow

public boolean isTextOverflow()
Description copied from interface: TextHolderFigure
Returns true, if the text does not fit into the bounds of the Figure.

Specified by:
isTextOverflow in interface TextHolderFigure

getPreferredTextSize

public Dimension2DDouble getPreferredTextSize(double maxWidth)
Returns the preferred text size of the TextAreaFigure.

If you want to use this method to determine the bounds of the TextAreaFigure, you need to add the insets of the TextAreaFigure to the size.

Parameters:
maxWidth - the maximal width to use. Specify Double.MAX_VALUE if you want the width to be unlimited.
Returns:
width and height needed to lay out the text.

Copyright 1996-2010 (c) by the authors and contributors of the JHotDraw project.
Some rights reserved.