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

java.lang.Object
  extended by edu.northwestern.at.wordhoard.model.text.TextLine
All Implemented Interfaces:
java.lang.Cloneable
Direct Known Subclasses:
BuildParagraph

public class TextLine
extends java.lang.Object
implements java.lang.Cloneable

A line of text.

A line is a list of style runs (TextRun objects) plus line attributes: the line justification (left, center, or right), indentation, numeric and string versions of the line number (if any), an optional stanza label if this is the first line of a stanza, and marginalia (if any).

Centering and right justification override indentation - the indentation attribute is used only with left justification.

Marginalia are short pieces of text that are rendered in the margin. They are of type Text, so they may contain multiple lines and multiple runs. They should not contain line numbers or nested marginalia (if they do, they are not rendered).

Lines must be finalized by calling the finalize method before they 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.

Lines have a "marked" attribute that is set and used by the client. Marked lines are drawn with a marker character in the left margin.


Field Summary
static byte CENTER
          Center justification.
static byte LEFT
          Left justification.
static byte RIGHT
          Right justification.
 
Constructor Summary
TextLine()
          Create a new plain line.
TextLine(byte justification, int indentation)
          Creates a new line.
TextLine(byte justification, int indentation, int number, java.lang.String label)
          Creates a new line.
TextLine(byte justification, int indentation, int number, java.lang.String label, java.lang.String stanzaLabel)
          Creates a new line.
TextLine(int indentation)
          Creates a new indented line.
TextLine(java.lang.String text, byte charset, byte size, boolean bold, boolean italic, byte justification, int indentation, int number, java.lang.String label)
          Creates a new line with a single run.
TextLine(java.lang.String text, byte charset, byte size, boolean bold, boolean italic, byte justification, int indentation, int number, java.lang.String label, java.lang.String stanzaLabel)
          Creates a new line with a single run.
TextLine(java.lang.String str, FontInfo fontInfo)
          Creates a new line with font information.
 
Method Summary
 void addIndentation(int extra)
          Adds extra indentation to the line.
 void appendRun(java.lang.String text, byte charset, byte size, boolean bold, boolean italic)
          Appends a run to a line.
 void appendRun(java.lang.String text, FontInfo fontInfo)
          Appends a run with font information to a line.
 void appendRun(TextRun run)
          Appends a run to a line.
 java.lang.Object clone()
          Clones the line.
 void computeVerticalPositioningInformation()
          Computes the line vertical positioning information.
 void computeWidth()
          Computes the line width.
(package private)  void draw(DrawingContext context, int x, int y, int selStartOffsetInLine, int selEndOffsetInLine)
          Draws the line.
 void finalize()
          Finalizes the line.
 int getAscent()
          Gets the ascent of the line in pixels.
 int getDescent()
          Gets the descent of the line in pixels.
 int getHeight()
          Gets the height of the line in pixels.
 int getIndentation()
          Gets the indentation.
 byte getJustification()
          Returns the line justification.
 java.lang.String getLabel()
          Gets the line number label.
 int getLeading()
          Gets the leading of the line in pixels.
 int getLength()
          Gets the length of the line in characters.
 Text getMarginalia()
          Gets the marginalia.
 int getNumber()
          Gets the integer line number.
 TextRun[] getRuns()
          Gets the style runs.
 java.lang.String getStanzaLabel()
          Gets the stanza label.
 java.lang.String getText()
          Gets the text of the line.
 int getWidth()
          Gets the width of the the line in pixels.
 boolean isEmpty()
          Returns true if the line is empty.
 boolean isMarked()
          Returns true if the line is marked.
 void readExternal(java.io.ObjectInput in)
          Reads the line from an object input stream (deserializes the object).
 void setIndentation(int indentation)
          Sets the indentation.
 void setJustification(byte justification)
          Sets the line justification.
 void setMarginalia(Text marginalia)
          Sets the marginalia.
 void setMarked(boolean marked)
          Sets the marked attribute.
 void setWidth(int width)
          Sets the width of the line in pixels.
(package private)  LocationAndCharset viewToModel(DrawingContext context, int lineIndex, int x)
          Converts a point to a location and character set.
 void writeExternal(java.io.ObjectOutput out)
          Writes the line 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

LEFT

public static final byte LEFT
Left justification.

See Also:
Constant Field Values

CENTER

public static final byte CENTER
Center justification.

See Also:
Constant Field Values

RIGHT

public static final byte RIGHT
Right justification.

See Also:
Constant Field Values
Constructor Detail

TextLine

public TextLine(byte justification,
                int indentation,
                int number,
                java.lang.String label,
                java.lang.String stanzaLabel)
Creates a new line.

The new line initially contains no runs or marginalia.

Parameters:
justification - Justification (LEFT, CENTER, or RIGHT).
indentation - Indentation in pixels.
number - Integer line number.
label - Line number label.
stanzaLabel - Stanza label.

TextLine

public TextLine(byte justification,
                int indentation,
                int number,
                java.lang.String label)
Creates a new line.

The new line initially contains no runs, marginalia, or stanza label.

Parameters:
justification - Justification (LEFT, CENTER, or RIGHT).
indentation - Indentation in pixels.
number - Integer line number.
label - Line number label.

TextLine

public TextLine(byte justification,
                int indentation)
Creates a new line.

The new line initially contains no runs, line number or label, stanza label, or marginalia.

Parameters:
justification - Justification (LEFT, CENTER, or RIGHT).
indentation - Indentation in pixels.

TextLine

public TextLine(int indentation)
Creates a new indented line.

The new line has no numbers, labels, or marginalia, and initially contains no runs.

Parameters:
indentation - Indentation in pixels.

TextLine

public TextLine()
Create a new plain line.

The new line has no numbers, labels, or marginalia, is left justified with no indentation, and initially contains no runs.


TextLine

public TextLine(java.lang.String text,
                byte charset,
                byte size,
                boolean bold,
                boolean italic,
                byte justification,
                int indentation,
                int number,
                java.lang.String label,
                java.lang.String stanzaLabel)
Creates a new line with a single run.

The new line has no stanza label or marginalia.

Parameters:
text - Text.
charset - Character set.
size - Font size.
bold - True if bold.
italic - True if italic.
justification - Justification (LEFT, CENTER, or RIGHT).
indentation - Indentation in pixels.
number - Integer line number.
label - Line number label.
stanzaLabel - Optional stanza label if first line of stanza, else null.

TextLine

public TextLine(java.lang.String text,
                byte charset,
                byte size,
                boolean bold,
                boolean italic,
                byte justification,
                int indentation,
                int number,
                java.lang.String label)
Creates a new line with a single run.

The new line has no stanza label or marginalia.

Parameters:
text - Text.
charset - Character set.
size - Font size.
bold - True if bold.
italic - True if italic.
justification - Justification (LEFT, CENTER, or RIGHT).
indentation - Indentation in pixels.
number - Integer line number.
label - Line number label.

TextLine

public TextLine(java.lang.String str,
                FontInfo fontInfo)
Creates a new line with font information.

The line contains a single plain run in the Roman character set, is left justified with no indentation, and has no line number, labels, or marginalia.

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

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

getMarginalia

public Text getMarginalia()
Gets the marginalia.

Returns:
The marginalia, or null if none.

setMarginalia

public void setMarginalia(Text marginalia)
Sets the marginalia.

Parameters:
marginalia - Marginalia.

appendRun

public void appendRun(TextRun run)
Appends a run to a line.

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

Parameters:
run - Text run.

appendRun

public void appendRun(java.lang.String text,
                      byte charset,
                      byte size,
                      boolean bold,
                      boolean italic)
Appends a run to a line.

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

Parameters:
text - Text.
charset - Character set.
size - Font size.
bold - True if bold.
italic - True if italic.

appendRun

public void appendRun(java.lang.String text,
                      FontInfo fontInfo)
Appends a run with font information to a line.

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

Parameters:
text - Text.
fontInfo - Font info.

finalize

public void finalize()
Finalizes the line.

This method must be called before the line can be serialized or used. If the line has already been finalized, this method does nothing.

Overrides:
finalize in class java.lang.Object

isEmpty

public boolean isEmpty()
Returns true if the line is empty.

This method may be called before or after finalization.

Returns:
True if line is empty.

getRuns

public TextRun[] getRuns()
Gets the style runs.

Returns:
The style runs, or null if the line has not yet been finalized.

getJustification

public byte getJustification()
Returns the line justification.

Returns:
Line justification (LEFT, CENTER, or RIGHT).

setJustification

public void setJustification(byte justification)
Sets the line justification.

Parameters:
justification - Line justification (LEFT, CENTER, or RIGHT).

getIndentation

public int getIndentation()
Gets the indentation.

Returns:
The indentation in pixels.

setIndentation

public void setIndentation(int indentation)
Sets the indentation.

Parameters:
indentation - Indentation in pixels.

addIndentation

public void addIndentation(int extra)
Adds extra indentation to the line.

Parameters:
extra - Extra indentation to add to line.

getNumber

public int getNumber()
Gets the integer line number.

Returns:
The integer line number.

getLabel

public java.lang.String getLabel()
Gets the line number label.

Returns:
The line number label, or null if this line has no line number.

getStanzaLabel

public java.lang.String getStanzaLabel()
Gets the stanza label.

Returns:
The stanza label, or null.

getText

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

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

Returns:
Text of the line.

getLength

public int getLength()
Gets the length of the line in characters.

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

Returns:
Length of the line in characters.

getWidth

public int getWidth()
Gets the width of the the line in pixels.

Returns:
The width of the line in pixels, or -1 if not yet set.

setWidth

public void setWidth(int width)
Sets the width of the line in pixels.

Parameters:
width - The width of the line in pixels.

getHeight

public int getHeight()
Gets the height of the line in pixels.

Returns:
The height of the line in pixels, or -1 if not yet set.

getLeading

public int getLeading()
Gets the leading of the line in pixels.

Returns:
The leading of the line in pixels, or -1 if not yet set.

getAscent

public int getAscent()
Gets the ascent of the line in pixels.

Returns:
The ascent of the line in pixels, or -1 if not yet set.

getDescent

public int getDescent()
Gets the descent of the line in pixels.

Returns:
The descent of the line in pixels, or -1 if not yet set.

computeWidth

public void computeWidth()
Computes the line width.

The line 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 the line vertical positioning information.

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

The font info must be set for all the runs in the line. The leading, ascent, and descent of the line are computed as the maximum of the corresponding values for all the runs in the line. The height of the line is computed as the sum of the line's leading, ascent, and descent.

The leading, ascent, and descent are further adjusted to guarentee enough room for any underline, overline, superscript, and/or subscript runs in the line.


isMarked

public boolean isMarked()
Returns true if the line is marked.

Returns:
True if line is marked.

setMarked

public void setMarked(boolean marked)
Sets the marked attribute.

Parameters:
marked - True if line is marked.

clone

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

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

The clone is a deep copy of the line.

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

writeExternal

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

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

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 line from an object input stream (deserializes the object).

The deserialized line is finalized.

Parameters:
in - Object input stream.
Throws:
java.io.IOException
java.lang.ClassNotFoundException

draw

void draw(DrawingContext context,
          int x,
          int y,
          int selStartOffsetInLine,
          int selEndOffsetInLine)
Draws the line.

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

Parameters:
context - Drawing context.
x - X coordinate of left edge of line.
y - Y coordinate of top of line.
selStartOffsetInLine - Selection start offset in line.
selEndOffsetInLine - Selection end offset in line.

viewToModel

LocationAndCharset viewToModel(DrawingContext context,
                               int lineIndex,
                               int x)
Converts a point to a location and character set.

Parameters:
context - Drawing context.
lineIndex - Index of line.
x - X coordinate of point in line.
Returns:
Location and character set.