Package snap.text

Class TextBox

java.lang.Object
snap.text.TextBox

public class TextBox extends Object
This class manages a TextDoc to be rendered and edited in a bounding area.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected double
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    Constructor.
    Creates a new TextBox initialized with the given String and no attributes.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    addChars(CharSequence theChars, TextStyle theStyle, int anIndex)
    Adds characters with attributes to this text at given index.
    protected void
    addLinesForCharRange(int aLineIndex, int aStartCharIndex, int aEndCharIndex)
    Removes the lines from given char index to given char index.
    char
    charAt(int anIndex)
    Returns the char value at the specified index.
    protected TextBoxLine
    createTextBoxLine(TextLine aTextLine, int startCharIndex, int aLineIndex)
    Create and return TextBoxLines for given TextLine, start char index and line index.
    protected TextDoc
    Creates the default TextDoc.
    double
    Returns the y for alignment.
    Returns the Y alignment.
    Returns the current bounds.
    Returns the bounds path.
    int
    getCharIndexForXY(double anX, double aY)
    Returns the character index for the given x/y point.
    int
    Returns the end char in TextDoc.
    double
    Returns the font scale of the text box.
    double
    Returns the height.
    getLine(int anIndex)
    Returns the individual text line in this text.
    int
    Returns the number of lines in this text.
    getLineForCharIndex(int anIndex)
    Returns the TextLine at the given char index.
    getLineForY(double aY)
    Returns the line for the given y value.
    Returns the last line.
    Returns the longest line.
    Returns the list of lines.
    protected double
    getMaxHitX(double aY, double aH)
    Returns the max x value that doesn't hit right border for given y/height.
    double
    Returns the max X.
    double
    Returns the max Y.
    protected double
    getMinHitX(double aY, double aH, double anIndent)
    Returns the min x value that doesn't hit left border for given y/height and indent.
    getPathForCharRange(int aStartCharIndex, int aEndCharIndex)
    Returns a path for two char indexes - it will be a a simple box with extensions for first/last lines.
    double
    getPrefHeight(double aW)
    Returns the preferred height.
    double
    getPrefWidth(double aH)
    Returns the preferred width.
    int
    Returns the start char in TextDoc.
    Returns the string for the text.
    Returns the TextDoc.
    int
    Returns the number of characters in the text.
    getTokenForCharIndex(int anIndex)
    Returns the token at given index.
    Returns underlined runs for text box.
    double
    Returns the width.
    double
    Returns the X location.
    double
    Returns the Y location.
    protected boolean
    isHitRight(double aX, double aY, double aH)
    Returns whether given x location and run hit right border.
    boolean
    Returns whether layout tries to hyphenate wrapped words.
    boolean
    Returns whether text is linked to another text (and shouldn't add lines below bottom border).
    boolean
    Returns whether text supports multiple styles.
    boolean
    Returns whether this text box couldn't fit all text.
    boolean
    Returns whether text box contains an underlined run.
    boolean
    Returns whether to wrap lines that overrun bounds.
    int
    Returns the number of chars currently in text box.
    void
    paint(Painter aPntr)
    Paint TextBox to given painter.
    void
    removeChars(int aStart, int anEnd)
    Removes characters in given range.
    protected void
    removeLinesForCharRange(int startCharIndex, int endCharIndex)
    Removes the lines from given char index to given char index, extended to cover entire TextDoc.TextLines.
    void
    replaceChars(CharSequence theChars, TextStyle theStyle, int aStart, int anEnd)
    Replaces chars in given range, with given String, using the given attributes.
    void
    Scales font sizes of all text in TextBox to fit in bounds by finding/setting FontScale.
    void
    Sets the Y alignment.
    void
    setBounds(double aX, double aY, double aW, double aH)
    Sets the rect location and size.
    void
    setBounds(Rect aRect)
    Sets the rect location and size.
    void
    Sets the bounds path.
    void
    setFontScale(double aValue)
    Sets the font scale of the text box.
    void
    setHeight(double aValue)
    Sets the width.
    void
    setHyphenate(boolean aValue)
    Sets whether layout tries to hyphenate wrapped words.
    void
    setLinked(boolean aValue)
    Returns whether text is linked to another text (and shouldn't add lines below bottom border).
    protected void
    Updates all lines.
    void
    setRichText(boolean aValue)
    Sets whether text supports multiple styles.
    void
    setStartCharIndex(int charIndex)
    Sets the start char in TextDoc.
    void
    setString(String aString)
    Sets the text to the given string.
    void
    setTextDoc(TextDoc aTextDoc)
    Sets the TextDoc.
    protected void
    setUpdateBounds(int aStart, int aEnd)
    Sets the update bounds (in characters from start and from end).
    void
    setWidth(double aValue)
    Sets the width.
    void
    setWrapLines(boolean aValue)
    Sets whether to wrap lines that overrun bounds.
    void
    setX(double anX)
    Sets the X location.
    void
    setY(double aY)
    Sets the Y location.
    protected void
    textDocChangedChars(int aStart, int aEnd)
    Called when chars changed in TextDoc to track range in box and text to be synchronized.
    protected void
    Updates lines for TextDoc changes.
    Standard toString implementation.
    protected void
    Updates text box.
    protected void
    updateLines(int aStartCharIndex, int textBoxEndCharIndex, int textDocEndCharIndex)
    Updates lines for given char start and an old/new char end.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Field Details

    • _fontScale

      protected double _fontScale
  • Constructor Details

    • TextBox

      public TextBox()
      Constructor.
    • TextBox

      public TextBox(CharSequence theChars)
      Creates a new TextBox initialized with the given String and no attributes.
  • Method Details

    • getTextDoc

      public TextDoc getTextDoc()
      Returns the TextDoc.
    • setTextDoc

      public void setTextDoc(TextDoc aTextDoc)
      Sets the TextDoc.
    • createTextDoc

      protected TextDoc createTextDoc()
      Creates the default TextDoc.
    • isRichText

      public boolean isRichText()
      Returns whether text supports multiple styles.
    • setRichText

      public void setRichText(boolean aValue)
      Sets whether text supports multiple styles.
    • 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.
    • getAlignY

      public VPos getAlignY()
      Returns the Y alignment.
    • setAlignY

      public void setAlignY(VPos aPos)
      Sets the Y alignment.
    • getAlignedY

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

      public boolean isWrapLines()
      Returns whether to wrap lines that overrun bounds.
    • setWrapLines

      public void setWrapLines(boolean aValue)
      Sets whether to wrap lines that overrun bounds.
    • isHyphenate

      public boolean isHyphenate()
      Returns whether layout tries to hyphenate wrapped words.
    • setHyphenate

      public void setHyphenate(boolean aValue)
      Sets whether layout tries to hyphenate wrapped words.
    • isLinked

      public boolean isLinked()
      Returns whether text is linked to another text (and shouldn't add lines below bottom border).
    • setLinked

      public void setLinked(boolean aValue)
      Returns whether text is linked to another text (and shouldn't add lines below bottom border).
    • getStartCharIndex

      public int getStartCharIndex()
      Returns the start char in TextDoc.
    • setStartCharIndex

      public void setStartCharIndex(int charIndex)
      Sets the start char in TextDoc.
    • getEndCharIndex

      public int getEndCharIndex()
      Returns the end char in TextDoc.
    • getFontScale

      public double getFontScale()
      Returns the font scale of the text box.
    • setFontScale

      public void setFontScale(double aValue)
      Sets the font scale of the text box.
    • getBoundsPath

      public Shape getBoundsPath()
      Returns the bounds path.
    • setBoundsPath

      public void setBoundsPath(Shape aPath)
      Sets the bounds path.
    • length

      public int length()
      Returns the number of chars currently in text box.
    • charAt

      public char charAt(int anIndex)
      Returns the char value at the specified index.
    • getTextDocLength

      public int getTextDocLength()
      Returns the number of characters in the text.
    • getString

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

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

      public void addChars(CharSequence theChars, TextStyle theStyle, int anIndex)
      Adds characters with attributes to this text at given index.
    • removeChars

      public void removeChars(int aStart, int anEnd)
      Removes characters in given range.
    • replaceChars

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

      public int getLineCount()
      Returns the number of lines in this text.
    • getLine

      public TextBoxLine getLine(int anIndex)
      Returns the individual text line in this text.
    • getLines

      public List<TextBoxLine> getLines()
      Returns the list of lines.
    • getLineForCharIndex

      public TextBoxLine getLineForCharIndex(int anIndex)
      Returns the TextLine at the given char index.
    • getLineLast

      public TextBoxLine getLineLast()
      Returns the last line.
    • getLineLongest

      public TextBoxLine getLineLongest()
      Returns the longest line.
    • textDocDidPropChange

      protected void textDocDidPropChange(PropChange aPC)
      Updates lines for TextDoc changes.
    • textDocChangedChars

      protected void textDocChangedChars(int aStart, int aEnd)
      Called when chars changed in TextDoc to track range in box and text to be synchronized.
    • setNeedsUpdateAll

      protected void setNeedsUpdateAll()
      Updates all lines.
    • setUpdateBounds

      protected void setUpdateBounds(int aStart, int aEnd)
      Sets the update bounds (in characters from start and from end).
    • update

      protected void update()
      Updates text box.
    • updateLines

      protected void updateLines(int aStartCharIndex, int textBoxEndCharIndex, int textDocEndCharIndex)
      Updates lines for given char start and an old/new char end.
    • addLinesForCharRange

      protected void addLinesForCharRange(int aLineIndex, int aStartCharIndex, int aEndCharIndex)
      Removes the lines from given char index to given char index.
    • createTextBoxLine

      protected TextBoxLine createTextBoxLine(TextLine aTextLine, int startCharIndex, int aLineIndex)
      Create and return TextBoxLines for given TextLine, start char index and line index.
    • removeLinesForCharRange

      protected void removeLinesForCharRange(int startCharIndex, int endCharIndex)
      Removes the lines from given char index to given char index, extended to cover entire TextDoc.TextLines.
    • isHitRight

      protected boolean isHitRight(double aX, double aY, double aH)
      Returns whether given x location and run hit right border.
    • getMinHitX

      protected double getMinHitX(double aY, double aH, double anIndent)
      Returns the min x value that doesn't hit left border for given y/height and indent.
    • getMaxHitX

      protected double getMaxHitX(double aY, double aH)
      Returns the max x value that doesn't hit right border for given y/height.
    • getTokenForCharIndex

      public TextBoxToken getTokenForCharIndex(int anIndex)
      Returns the token at given index.
    • isUnderlined

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

      public List<TextBoxRun> getUnderlineRuns(Rect aRect)
      Returns underlined runs for text box.
    • getLineForY

      public TextBoxLine 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.
    • getPathForCharRange

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

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

      public double getPrefWidth(double aH)
      Returns the preferred width.
    • getPrefHeight

      public double getPrefHeight(double aW)
      Returns the preferred height.
    • scaleTextToFit

      public void scaleTextToFit()
      Scales font sizes of all text in TextBox to fit in bounds by finding/setting FontScale.
    • isTextOutOfBounds

      public boolean isTextOutOfBounds()
      Returns whether this text box couldn't fit all text.
    • toString

      public String toString()
      Standard toString implementation.
      Overrides:
      toString in class Object