Package snap.text

Class TextBlock

All Implemented Interfaces:
CharSequence, Cloneable, PropChange.DoChange, CharSequenceX, XMLArchiver.Archivable
Direct Known Subclasses:
TextBox, TextDoc

public class TextBlock extends PropObject implements CharSequenceX, Cloneable, XMLArchiver.Archivable
This class is the basic text storage class, holding a list of TextLine.
  • Field Details

  • Constructor Details

    • TextBlock

      public TextBlock()
      Constructor.
    • TextBlock

      public TextBlock(boolean isRich)
      Constructor with option to make rich text.
  • Method Details

    • createLine

      protected TextLine createLine()
      Creates a new TextLine for use in this text.
    • isRichText

      public boolean isRichText()
      Whether this text supports multiple styles (font, color, etc.).
    • setRichText

      public void setRichText(boolean aValue)
      Sets whether text supports multiple styles.
    • getSourceText

      public TextBlock getSourceText()
      Returns the root text block.
    • length

      public int length()
      Returns the number of characters in the text.
      Specified by:
      length in interface CharSequence
    • charAt

      public char charAt(int anIndex)
      Returns the char value at the specified index.
      Specified by:
      charAt in interface CharSequence
    • subSequence

      public CharSequence subSequence(int aStart, int anEnd)
      Returns a new char sequence that is a subsequence of this sequence.
      Specified by:
      subSequence in interface CharSequence
    • getString

      public String getString()
      Returns the string for the text.
    • setString

      public void setString(String aString)
      Sets the text to the given string.
    • getDefaultTextStyle

      public TextStyle getDefaultTextStyle()
      Returns the default text style for text.
    • setDefaultTextStyle

      public void setDefaultTextStyle(TextStyle aStyle)
      Sets the default text style.
    • getDefaultLineStyle

      public TextLineStyle getDefaultLineStyle()
      Returns the default line style for text.
    • setDefaultLineStyle

      public void setDefaultLineStyle(TextLineStyle aLineStyle)
      Sets the default line style.
    • getDefaultFont

      public Font getDefaultFont()
      Returns the default font.
    • setDefaultFont

      public void setDefaultFont(Font aFont)
      Sets the default font.
    • getDefaultTextColor

      public Color getDefaultTextColor()
      Returns the default text color.
    • setDefaultTextColor

      public void setDefaultTextColor(Color aColor)
      Sets the default text color.
    • isTextModified

      public boolean isTextModified()
      Returns whether text is modified.
    • setTextModified

      public void setTextModified(boolean aValue)
      Sets whether text is modified.
    • addChars

      public void addChars(CharSequence theChars)
      Adds characters to this text.
    • addChars

      public void addChars(CharSequence theChars, int anIndex)
      Adds characters to this text at given index.
    • addCharsWithStyle

      public void addCharsWithStyle(CharSequence theChars, TextStyle theStyle)
      Adds characters with given style to this text.
    • addCharsWithStyle

      public void addCharsWithStyle(CharSequence theChars, TextStyle theStyle, int anIndex)
      Adds characters with given style to this text at given index.
    • addCharsToLine

      protected void addCharsToLine(CharSequence theChars, TextStyle theStyle, int charIndex, TextLine textLine, boolean charsHaveNewline)
      Adds a block of chars to line - each block is guaranteed to either have no newlines or end with newline.
    • moveLineCharsToNextLine

      protected void moveLineCharsToNextLine(TextLine textLine, int startCharIndex)
      Move line chars from given start char index to line end to next line.
    • removeChars

      public void removeChars(int aStartCharIndex, int anEndCharIndex)
      Removes characters in given range.
    • removeCharsFromLine

      protected void removeCharsFromLine(int startCharIndex, int endCharIndex, TextLine textLine)
      Remove chars from line.
    • replaceChars

      public void replaceChars(CharSequence theChars, int aStart, int anEnd)
      Replaces chars in given range, with given String, using the given attributes.
    • replaceChars

      public void replaceChars(CharSequence theChars, TextStyle theStyle, int aStart, int anEnd)
      Replaces chars in given range, with given String, using the given attributes.
    • splitLineAtIndex

      protected TextLine splitLineAtIndex(TextLine textLine, int anIndex)
      Splits given line at given character index and adds remainder to text and returns it.
    • joinLineWithNextLine

      protected void joinLineWithNextLine(TextLine textLine)
      Joins given line with next line.
    • addTextBlock

      public void addTextBlock(TextBlock textBlock, int anIndex)
      Adds given TextBlock to this text at given index.
    • setTextStyle

      public void setTextStyle(TextStyle textStyle, int aStart, int anEnd)
      Sets the given text style for given range.
    • setStyleValue

      public void setStyleValue(Object aValue)
      Sets a given style value to given value for a given range.
    • setStyleValue

      public void setStyleValue(Object aValue, int aStart, int aEnd)
      Sets a given style value to given value for a given range.
    • setStyleValue

      public void setStyleValue(String aKey, Object aValue, int aStart, int anEnd)
      Sets a given attribute to a given value for a given range.
    • setLineStyle

      public void setLineStyle(TextLineStyle aStyle, int aStart, int anEnd)
      Sets a given style to a given range.
    • setLineStyleValue

      public void setLineStyleValue(String aKey, Object aValue, int aStart, int anEnd)
      Sets a given style to a given range.
    • getLineCount

      public int getLineCount()
      Returns the number of block in this doc.
    • getLine

      public TextLine getLine(int anIndex)
      Returns the individual block in this doc.
    • getLines

      public List<TextLine> getLines()
      Returns the list of blocks.
    • addLine

      protected void addLine(TextLine aLine, int anIndex)
      Adds a block at given index.
    • removeLine

      protected void removeLine(int anIndex)
      Removes the block at given index.
    • getLineLongest

      public TextLine getLineLongest()
      Returns the longest line.
    • clear

      public void clear()
      Clears the text.
    • getLineForCharIndex

      public TextLine getLineForCharIndex(int charIndex)
      Returns the block at the given char index.
    • getLastLine

      public TextLine getLastLine()
      Returns the last text line (or null if none).
    • getRunForCharIndex

      public TextRun getRunForCharIndex(int charIndex)
      Returns the TextRun that contains the given char index.
    • getRunForCharRange

      public TextRun getRunForCharRange(int startIndex, int endIndex)
      Returns the TextRun for the given char range (usually just run for start, but can be next run if at boundary).
    • getRunIterForCharRange

      public TextRunIter getRunIterForCharRange(int startCharIndex, int endCharIndex)
      Returns a TextRunIter to easily traverse the runs for a given range of chars.
    • getTokenForCharIndex

      public TextToken getTokenForCharIndex(int charIndex)
      Returns the token at given char index.
    • getFontForCharIndex

      public Font getFontForCharIndex(int charIndex)
      Returns the Font for run at given character index.
    • getTextStyleForCharIndex

      public TextStyle getTextStyleForCharIndex(int charIndex)
      Returns the TextStyle for the run at the given character index.
    • getTextStyleForCharRange

      public TextStyle getTextStyleForCharRange(int startIndex, int endIndex)
      Returns the TextStyle for the run for given char range.
    • getLineStyleForCharIndex

      public TextLineStyle getLineStyleForCharIndex(int charIndex)
      Returns the TextLineStyle for the run at the given character index.
    • getLineForY

      public TextLine getLineForY(double aY)
      Returns the line for the given y value.
    • getCharIndexForXY

      public int getCharIndexForXY(double anX, double aY)
      Returns the character index for the given x/y point.
    • isUnderlined

      public boolean isUnderlined()
      Returns whether text contains an underlined run.
    • setUnderlined

      public void setUnderlined(boolean aFlag)
      Sets text to be underlined.
    • getAlignX

      public HPos getAlignX()
      Returns the horizontal alignment of the first paragraph of the text.
    • setAlignX

      public void setAlignX(HPos anAlignX)
      Sets the horizontal alignment of the text.
    • getStartCharIndex

      public int getStartCharIndex()
      Returns the start char index (always 0, unless this is SubText).
    • getEndCharIndex

      public int getEndCharIndex()
      Returns the end char in source TextBlock.
    • scaleFonts

      public void scaleFonts(double aScale)
      Scales all the fonts in text by given factor.
    • indexOf

      public int indexOf(String aStr, int aStart)
      Returns the index of given string.
    • getX

      public double getX()
      Returns the X location.
    • setX

      public void setX(double anX)
      Sets the X location.
    • getY

      public double getY()
      Returns the Y location.
    • setY

      public void setY(double aY)
      Sets the Y location.
    • getWidth

      public double getWidth()
      Returns the width.
    • setWidth

      public void setWidth(double aValue)
      Sets the width.
    • getHeight

      public double getHeight()
      Returns the height.
    • setHeight

      public void setHeight(double aValue)
      Sets the width.
    • getBounds

      public Rect getBounds()
      Returns the current bounds.
    • setBounds

      public void setBounds(Rect aRect)
      Sets the rect location and size.
    • setBounds

      public void setBounds(double aX, double aY, double aW, double aH)
      Sets the rect location and size.
    • getMaxX

      public double getMaxX()
      Returns the max X.
    • getMaxY

      public double getMaxY()
      Returns the max Y.
    • getAlignedY

      public double getAlignedY()
      Returns the y for alignment.
    • getPathForCharRange

      public Shape getPathForCharRange(int aStartCharIndex, int aEndCharIndex)
      Returns a path for two char indexes - it will be a simple box with extensions for first/last lines.
    • isPropChangeEnabled

      public boolean isPropChangeEnabled()
      Returns whether property change is enabled.
    • setPropChangeEnabled

      public void setPropChangeEnabled(boolean aValue)
      Sets whether property change is enabled.
    • paint

      public void paint(Painter aPntr)
      Paint TextBox to given painter.
    • paintLine

      public void paintLine(Painter aPntr, TextLine textLine, double lineY)
      Paint TextBox to given painter.
    • getPrefWidth

      public double getPrefWidth()
      Returns the width of text.
    • getPrefWidthForStartCharIndex

      public double getPrefWidthForStartCharIndex(int startCharIndex)
      Returns the width of text from given start char index.
    • getPrefHeight

      public double getPrefHeight()
      Returns the preferred height.
    • createTokensForTextLine

      protected TextToken[] createTokensForTextLine(TextLine aTextLine)
      Creates TextTokens for a TextLine.
    • resetLineYForLinesAfterIndex

      protected void resetLineYForLinesAfterIndex(int lineIndex)
      Resets Line Y positions for lines after given line index.
    • updateLines

      protected void updateLines(int anIndex)
      Updates Lines (Index, Start) from index line to text end.
    • updateLine

      protected void updateLine(TextLine textLine, int newIndex, int newStartCharIndex)
      Updates an individual line for new index and start char index.
    • getUnderlineRuns

      public TextRun[] getUnderlineRuns(Rect aRect)
      Returns underlined runs for text box.
    • activateUndo

      public void activateUndo()
      Called to activate undo.
    • getUndoer

      public Undoer getUndoer()
      Returns the text undoer.
    • undo

      public UndoSet undo()
      Called to undo the last text change.
    • redo

      public UndoSet redo()
      Called to redo the last text change.
    • undoerAddPropChange

      protected void undoerAddPropChange(PropChange anEvent)
      Adds a property change to undoer.
    • firePropChange

      protected void firePropChange(PropChange aPC)
      Override to register undo.
      Overrides:
      firePropChange in class PropObject
    • copyForRange

      public TextBlock copyForRange(int aStart, int aEnd)
      Returns a copy of this text for given char range.
    • clone

      public TextBlock clone()
      Standard clone implementation.
      Overrides:
      clone in class PropObject
    • toStringProps

      public String toStringProps()
      Standard toStringProps implementation.
      Overrides:
      toStringProps in class PropObject
    • toXML

      public XMLElement toXML(XMLArchiver anArchiver)
      XMLArchiver.Archivable archival.
      Specified by:
      toXML in interface XMLArchiver.Archivable
    • fromXML

      public Object fromXML(XMLArchiver anArchiver, XMLElement anElement)
      XMLArchiver.Archivable unarchival.
      Specified by:
      fromXML in interface XMLArchiver.Archivable