edu.northwestern.at.wordhoard.model.text
Class Text

java.lang.Object
  extended by edu.northwestern.at.wordhoard.model.text.Text
All Implemented Interfaces:
java.io.Externalizable, java.io.Serializable, java.lang.Cloneable
Direct Known Subclasses:
TranslatedText, WrappedText

public class Text
extends java.lang.Object
implements java.io.Externalizable, java.lang.Cloneable

Text.

Text consists of a list of lines (TextLine objects), plus attributes that specify whether or not the text includes line numbers or marginalia.

Text objects for work parts are created and made persistent in the object model by the build tools. They are stored as serialized objects in a type "mediumblob" column in the MySQL database. They are fetched as needed from the database and deserialized by Hibernate.

Some properties are used only during construction of the object, some are used only during presentation in the client, and some are used by both. Only the properties used by both are serialized and persisted.

The text must be finalized by calling the finalize method before it can be serialized or used. Some methods are intended for use during construction, and can only be called before finalization. Other methods are intended for use only after finalization.

See Also:
Serialized Form

Field Summary
(package private) static long serialVersionUID
          Serial version UID.
 
Constructor Summary
Text()
          Creates a new text object.
Text(boolean lineNumbers, boolean marginalia)
          Creates a new text object.
Text(java.lang.String str, FontInfo fontInfo)
          Creates a new text object with a single plain line and no line numbers or marginalia.
Text(TextLine line)
          Creates a new text object with a single line and no line numbers or marginalia.
 
Method Summary
 void appendBlankLine()
          Appends a blank line to the text.
 int appendLine(java.lang.String str, byte charset, int number, java.lang.String label)
          Appends a plain line to the text.
 void appendLine(TextLine line)
          Appends a line to the text.
 TextLocation baseToDerived(TextLocation loc)
          Converts a location from base to derived coordinates.
 TextRange baseToDerived(TextRange range)
          Converts a model range to view text coordinates.
 java.lang.Object clone()
          Clones the text.
 void computeLineWidths()
          Computes line widths.
 void computeVerticalPositioningInformation()
          Computes vertical positioning information.
 void copyLine(java.lang.String str, FontInfo fontInfo)
          Copies a line with font information.
 void copyLine(TextLine line)
          Copies a line to the text.
 TextLocation derivedToBase(TextLocation loc)
          Converts a location from derived to base coordinates.
 TextRange derivedToBase(TextRange range)
          Converts a range from derived to base coordinates.
 void draw(java.awt.Graphics2D g, int x, int y)
          Draws the text.
 void finalize()
          Finalizes the text.
 TextLine getLine(int lineIndex)
          Gets a spcific line.
 int getLineLocation(int lineIndex)
          Gets the location of a line.
 TextRange getLineLocation(TextLocation loc)
          Gets the location of a line.
 TextLine[] getLines()
          Gets the lines.
 int getNumLines()
          Gets the number of lines.
 java.lang.String getText(TextRange range)
          Gets a range of text.
 TextRange getWordLocation(TextLocation loc, byte charset)
          Gets the location of a word.
 boolean hasLineNumbers()
          Returns true if the text has line numbers.
 boolean hasMarginalia()
          Returns true if the text has marginalia.
 int initializeForDrawing(DrawingContext context)
          Initializes the text for drawing.
 boolean isMarked(int lineIndex)
          Returns true if a line is marked.
 void readExternal(java.io.ObjectInput in)
          Reads the text from an object input stream (deserializes the object).
 TextLocation roundLocation(TextLocation loc)
          Rounds a location to the nearest character of text.
 void setCollapseBlankLines(boolean collapseBlankLines)
          Sets the collapse blank lines option.
 void setExtraIndentation(int extraIndentation)
          Sets extra indentation.
 void setLineNumberInterval(int lineNumberInterval)
          Sets the line number interval.
 void setMarkers(TextLocation[] locations)
          Sets markers.
 void setSelection(TextRange selection)
          Sets the selection.
 LocationAndCharset viewToModel(java.awt.Point p)
          Converts a point to a location and character set.
 void writeExternal(java.io.ObjectOutput out)
          Writes the text to an object output stream (serializes the object).
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

serialVersionUID

static final long serialVersionUID
Serial version UID.

See Also:
Constant Field Values
Constructor Detail

Text

public Text(boolean lineNumbers,
            boolean marginalia)
Creates a new text object.

The new text object initially contains no lines.

Parameters:
lineNumbers - True if this text has line numbers.
marginalia - True if this text has marginalia.

Text

public Text()
Creates a new text object.

The new text object has no line numbers or marginalia and initially contains no lines.


Text

public Text(TextLine line)
Creates a new text object with a single line and no line numbers or marginalia.

The width and vertical positioning information are computed for the line, and the text is finalized and ready to be used.

Parameters:
line - Line.

Text

public Text(java.lang.String str,
            FontInfo fontInfo)
Creates a new text object with a single plain line and no line numbers or marginalia.

The width and vertical positioning information are computed for the line, and the text is finalized and ready to be used.

Parameters:
str - Text.
fontInfo - Font info.
Method Detail

appendLine

public void appendLine(TextLine line)
Appends a line to the text.

This method can only be used during construction, before the text is finalized. If it is called after finalization a null pointer exception is thrown.

Parameters:
line - The line.

appendLine

public int appendLine(java.lang.String str,
                      byte charset,
                      int number,
                      java.lang.String label)
Appends a plain line to the text.

This method can only be used during construction, before the text is finalized. If it is called after finalization a null pointer exception is thrown.

Parameters:
str - The text of the line.
charset - Character set.
number - Integer line number.
label - Line number label.
Returns:
Index of appended line.

appendBlankLine

public void appendBlankLine()
Appends a blank line to the text.

This method can only be used during construction, before the text is finalized. If it is called after finalization a null pointer exception is thrown.


copyLine

public void copyLine(TextLine line)
Copies a line to the text.

This method can only be used during construction, before the text is finalized. If it is called after finalization a null pointer exception is thrown.

The line is appended to the text unconditionally and without alteration. There is no check for collapsing blank lines, and no extra indentation is added.

Parameters:
line - The line.

copyLine

public void copyLine(java.lang.String str,
                     FontInfo fontInfo)
Copies a line with font information.

This method can only be used during construction, before the text is finalized. If it is called after finalization a null pointer exception is thrown.

The line is appended to the text unconditionally and without alteration. There is no check for collapsing blank lines, and no extra indentation is added.

The line is finalized, and width and vertical positioning information are computed for the line.

Parameters:
str - String.
fontInfo - Font info.

getNumLines

public int getNumLines()
Gets the number of lines.

This method can be used before or after finalization.

Returns:
The number of lines.

setCollapseBlankLines

public void setCollapseBlankLines(boolean collapseBlankLines)
Sets the collapse blank lines option.

Parameters:
collapseBlankLines - If true, multiple sequental blank lines are collapsed to a single blank line. This is the default.

setExtraIndentation

public void setExtraIndentation(int extraIndentation)
Sets extra indentation.

Parameters:
extraIndentation - Extra indentation for all subsequent lines.

finalize

public void finalize()
Finalizes the text.

Trims trailing blank lines and finalizes the text object and all of its lines. This method must be called before the text object can be serialized or used. If the text has already been finalized, this method does nothing.

Overrides:
finalize in class java.lang.Object

getLines

public TextLine[] getLines()
Gets the lines.

Returns:
The lines, or null if the text has not yet been finalized.

getLine

public TextLine getLine(int lineIndex)
Gets a spcific line.

Returns:
The requested line, or null if the text has not yet been finalized.

hasLineNumbers

public boolean hasLineNumbers()
Returns true if the text has line numbers.

Returns:
True if text has line numbers.

hasMarginalia

public boolean hasMarginalia()
Returns true if the text has marginalia.

Returns:
True if text has marginalia.

roundLocation

public TextLocation roundLocation(TextLocation loc)
Rounds a location to the nearest character of text.

The text must be finalized, or a null pointer exception is thrown.

Parameters:
loc - Location.
Returns:
Rounded location.

getWordLocation

public TextRange getWordLocation(TextLocation loc,
                                 byte charset)
Gets the location of a word.

The text must be finalized, or a null pointer exception is thrown.

Parameters:
loc - A location in the text.
charset - Character set.
Returns:
The location (range) of the word containing that location, in derived coordinates.

getLineLocation

public TextRange getLineLocation(TextLocation loc)
Gets the location of a line.

The text must be finalized, or a null pointer exception is thrown.

Parameters:
loc - A location in the text.
Returns:
The location (range) of the line containing that location, in derived coordinates.

getText

public java.lang.String getText(TextRange range)
Gets a range of text.

The text must be finalized, or a null pointer exception is thrown.

Parameters:
range - Range.
Returns:
Text contained in range, as a string.

computeLineWidths

public void computeLineWidths()
Computes line widths.

The text must be finalized, or a null pointer exception is thrown.

The font info must be set for all the runs in the line.


computeVerticalPositioningInformation

public void computeVerticalPositioningInformation()
Computes vertical positioning information.

The text must be finalized, or a null pointer exception is thrown.

The font info must be set for all the runs in the line.


derivedToBase

public TextLocation derivedToBase(TextLocation loc)
Converts a location from derived to base coordinates.

Subclasses which present derived views of model text (e.g., paragraph wapped views or views with translations) must override this method to convert a location in the derived coordinate system to the corresponding location in the base coordinate system.

This base class implementation does no translation. The location is returned unchanged.

See the package documentation for more details on derived and base coordinate systems.

Parameters:
loc - Text location in derived coordinates.
Returns:
Text location in base coordinates.

derivedToBase

public TextRange derivedToBase(TextRange range)
Converts a range from derived to base coordinates.

Parameters:
range - Text range in derived coordinates.
Returns:
Text range in base coordinates.

baseToDerived

public TextLocation baseToDerived(TextLocation loc)
Converts a location from base to derived coordinates.

Subclasses which present derived views of model text (e.g., paragraph wapped views or views with translations) must override this method to convert a location in the base coordinate system to the corresponding location in the derived coordinate system.

This base class implementation does no translation. The location is returned unchanged.

See the package documentation for more details on derived and base coordinate systems.

Parameters:
loc - Text location in base cooordinates.
Returns:
Text location in derived coordinates.

baseToDerived

public TextRange baseToDerived(TextRange range)
Converts a model range to view text coordinates.

Parameters:
range - Text range in model text cooordinates.
Returns:
Text range in view text coordinates.

clone

public java.lang.Object clone()
Clones the text.

The text must be finalized, or a null pointer exception is thrown.

The clone is a deep copy of the text.

Overrides:
clone in class java.lang.Object
Returns:
Deep clone of the text.

writeExternal

public void writeExternal(java.io.ObjectOutput out)
                   throws java.io.IOException
Writes the text to an object output stream (serializes the object).

The text must be finalized, or a null pointer exception is thrown.

Specified by:
writeExternal in interface java.io.Externalizable
Parameters:
out - Object output stream.
Throws:
java.io.IOException

readExternal

public void readExternal(java.io.ObjectInput in)
                  throws java.io.IOException,
                         java.lang.ClassNotFoundException
Reads the text from an object input stream (deserializes the object).
Specified by:
readExternal in interface java.io.Externalizable
Parameters:
in - Object input stream.
Throws:
java.io.IOException
java.lang.ClassNotFoundException

initializeForDrawing

public int initializeForDrawing(DrawingContext context)
Initializes the text for drawing.

The text must be finalized, the font info must be set for all the runs in the line, and each line's width and vertical positioning information must be computed.

Parameters:
context - Drawing context.
Returns:
Height of the text in pixels.

draw

public void draw(java.awt.Graphics2D g,
                 int x,
                 int y)
Draws the text.

The text must be initialized for drawing.

Drawing is done relative to the top and left pixel coordinates of the text in the graphics context. The top coordinate is the y coordinate of the top of the first line. The left coordinate is the x coordinate of the left edge of the text, not counting markers in the left margin. If the text contains markers, they are drawn to the left of the x coordinate, using an offset supplied by the drawing context.

Parameters:
g - Graphics context.
x - X coordinate of left edge of text.
y - Y coordinate of top of text.

setLineNumberInterval

public void setLineNumberInterval(int lineNumberInterval)
Sets the line number interval.

The line number interval controls the display of line numbers. 0 = no line numbers, 1 = every line numbered, n = every n'th line numbered, -1 = stanza numbers.

Parameters:
lineNumberInterval - Line number interval.

setSelection

public void setSelection(TextRange selection)
Sets the selection.

Parameters:
selection - The selection range, or null if none.

viewToModel

public LocationAndCharset viewToModel(java.awt.Point p)
Converts a point to a location and character set.

Parameters:
p - Point.
Returns:
Location and character set.

getLineLocation

public int getLineLocation(int lineIndex)
Gets the location of a line.

Parameters:
lineIndex - Line index.
Returns:
Y coordinate of top of line.

setMarkers

public void setMarkers(TextLocation[] locations)
Sets markers.

Parameters:
locations - Marker locations in base coordinates, or null to clear all markers.

isMarked

public boolean isMarked(int lineIndex)
Returns true if a line is marked.

Parameters:
lineIndex - Line index.
Returns:
True if line is marked.