Class RMXString

java.lang.Object
com.reportmill.base.RMObject
com.reportmill.text.RMXString
All Implemented Interfaces:
RMArchiver.Archiving, Cloneable

public class RMXString extends RMObject
An RMXString is like a String that lets you apply attributes, like fonts and colors, to character ranges. These character ranges with common attributes are represented internally as the inner class Run.

You might use it like this:

    RMXString xstring = new RMXString("Hello World", RMColor.red);
    xstring.addAttribute(RMFont.getFont("Arail Bold", 12), 0, 5);
    xstring.addAttribute(RMFont.getFont("Arial BoldItalic", 12), 6, xstring.length());
 

Advanced applications, that need to disect or render strings, might iterate over the runs like this:

    for(int i=0; i<xstring.getRunCount(); i++) {
      RMXString.Run run = xstring.getRun(i);
      graphics.setFont(run.getFont().awt());
      graphics.setColor(run.getColor().awt());
      graphics.drawString(xstring.substring(run.start(), run.end()));
    }
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static class 
    The Outline inner class represents the attributes of outlined text: strokeWidth and fillColor.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final String
     
    static final String
     
    static final String
     
    static final String
     
    static final String
     
    static final String
     
    static final String
     
    static final String
     
    static final String
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates an empty RMXString.
    RMXString(String aString)
    Creates an RMXString intialized with the given String and no attributes.
    RMXString(String aString, Object anAttr)
    Creates an RMXString initialized with the given String with all characters set to the given attribute (should be a common attribute type like RMFont, RMColor, RMFormat, etc.).
    RMXString(String aString, Object[] attrs)
    Creates an RMXString initialized with the given String with all characters set to the given attributes objects in object array (should be common attribute types like RMFont, RMColor, RMFormat, etc.).
    RMXString(String aString, Map attrs)
    Creates an RMXString initialized with the given String with all characters set to the given attributes.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    add(RMXString xString)
    Appends the given XString to the end of this String.
    void
    add(RMXString xString, int anIndex)
    Appends the given XString to this XString, at the given index.
    void
    add(String aString)
    Appends the given String to the end of this XString.
    void
    add(String aString, int anIndex)
    Appends the given String to this XString, at the given index.
    void
    add(String aString, Map attrs)
    Appends the given string to the end of this XString, with the given attributes.
    void
    add(String aString, Map attrs, int anIndex)
    Appends the given String to this XString, with the given attributes, at the given index.
    void
    Applies the given attribute to whole xstring, assuming it's a basic attr types (font, color, etc.).
    void
    addAttribute(Object attr, int start, int end)
    Applies the given attribute to the given character range, assuming it's a basic attr type (font, color, etc.).
    void
    addAttribute(String aKey, Object object)
    Adds a given attribute of given type to the whole string.
    void
    addAttribute(String aKey, Object attr, int start, int end)
    Adds a given attribute of given type to the given range of the string.
    void
    addAttribute(String aKey, Object attr, int start, int end, boolean ifAbsent)
    Adds a given attribute of given type to the given range of the string, only if specified type is missing.
    void
    addAttributes(Map attrs, int start, int end)
    Adds the attributes in the given map to the specified character range.
    void
    addAttributes(Map attrs, int start, int end, boolean ifAbsent)
    Adds the attributes in the given map to the specified character range.
    void
    Aligns the xstring center.
    void
    Aligns the xstring left.
    void
    Aligns the xstring right.
    blend(RMXString aString, float fraction)
    Returns a blended version of the receiver and the given string (0 is receiver, 1 is given string).
    char
    charAt(int anIndex)
    Returns the character at the given index.
    Returns a clone of the receiver.
    void
    Replaces any occurrance of consecutive newlines with a single newline.
    void
    delete(int start, int end)
    Deletes the chars in the given range.
    void
    Notifies string that something significant changed.
    boolean
    equals(Object anObj)
    Standard Object equals implementation.
    fromXML(RXArchiver anArchiver, RXElement anElement)
    XML unarchival.
    byte
    Returns the horizontal alignment of the first paragraph of the xstring.
    getAttributeAt(String aKey, int anIndex)
    Returns the value for the given attribute key at the given character index.
    getAttributes(int anIndex)
    Returns the attributes Map for the run at the given index.
    getAttributesAt(int anIndex)
    Returns the attributes Map for the run at the given character index.
    getColorAtIndex(int index)
    Returns the current color at the given character index.
    getFontAtIndex(int index)
    Returns the current font at the given character index.
    float
    Returns the value that should be multipied times all font sizes in this string (defaults to 1).
    getFormatAtIndex(int index)
    Returns the current format at the given character index.
    Returns the current paragraph at the given character index.
    Returns a version of this string that substitutes alternate fonts for any characters that cannot be displayed in their associated fonts (simply returns the receiver if all characters are valid).
    getRun(int anIndex)
    Returns the specific Run at the given index in this XString.
    getRunAt(int anIndex)
    Returns the XString run at the given index.
    getRunAtExactly(int anIndex)
    Returns an XString run for the given character index, potentially splitting an existing run to ensure that new run starts at the given index.
    int
    Returns the number of runs in this XString.
    int
    getRunIndexAt(int anIndex)
    Returns the XString run index for the given character index.
    int
    getRunIndexAtExactly(int anIndex)
    Returns an XString segement index for the given character index, potentially splitting an existing run to ensure that new run starts at the given index.
    Returns the last run in this XString (convenience).
    int
    Returns the version of this xstring.
    static RMXString
    Creates and returns an XString from a given HTML String.
    static RMXString
    Creates and returns an XString from a given RTF String.
    int
    indexOf(String aString)
    Returns the index within this string of the first occurrence of the given substring.
    int
    indexOf(String aString, int start)
    Returns the index within this string of first occurrence of given substring, starting at given index.
    Legacy archival.
    int
    Returns the number of characters associated with this RMXString.
    void
    removeAttribute(String aKey, int start, int end)
    Removes any attribute of the specified type from the specified range of the string.
    void
    replace(int start, int end, RMXString xString)
    Replaces the chars in given range, with given XString.
    void
    replace(int start, int end, String aString)
    Replaces chars in given range, with given String.
    void
    replace(int start, int end, String aString, Map attrs)
    Replaces chars in given range, with given String, using the given attributes.
    void
    replace(String aString)
    Replaces all chars of this XString with given String.
    rpgClone(ReportMill aReportMill, Object userInfo, RMShape aShape)
    Creates a clone of the receiver, with substitution performed on @-sign deliniated keys.
    separate(String aSeparator)
    Returns a List of XString substrings of this XString that are separated by the given separator String.
    void
    setAlign(int align)
    Sets the horizontal alignment of the xstring.
    void
    setFontScale(float aScale)
    Sets a value to that should be multiplied times all font sizes in this string.
    void
    setOutlined(boolean flag)
    Sets the xstring to be outlined.
    void
    setParagraph(RMParagraph ps, int start)
    Sets the paragraph at the given character index.
    void
    setParagraph(RMParagraph ps, int start, int end)
    Sets the paragraph for the given character index range.
    void
    setUnderlined(boolean flag)
    Sets the xstring to be underlined.
    Returns the plain String associated with this RMXString.
    substring(int index)
    Returns the substring from the given index to end of XString.
    substring(int start, int end)
    Returns the substring from the given start index to the given end index.
    Returns the String associated with this XString.
    Returns an XML style string representation of the attributes and string runs in this XString.
    toXML(RXArchiver anArchiver)
    XML archival.

    Methods inherited from class com.reportmill.base.RMObject

    copy, didChange, didUndo, getAnimAttribute, getClassNameShort, undoClone, undoCopy, undoEquals

    Methods inherited from class java.lang.Object

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

  • Constructor Details

    • RMXString

      public RMXString()
      Creates an empty RMXString.
    • RMXString

      public RMXString(String aString)
      Creates an RMXString intialized with the given String and no attributes.
    • RMXString

      public RMXString(String aString, Map attrs)
      Creates an RMXString initialized with the given String with all characters set to the given attributes.
    • RMXString

      public RMXString(String aString, Object anAttr)
      Creates an RMXString initialized with the given String with all characters set to the given attribute (should be a common attribute type like RMFont, RMColor, RMFormat, etc.).
    • RMXString

      public RMXString(String aString, Object[] attrs)
      Creates an RMXString initialized with the given String with all characters set to the given attributes objects in object array (should be common attribute types like RMFont, RMColor, RMFormat, etc.).
  • Method Details

    • string

      public String string()
      Returns the plain String associated with this RMXString.
    • length

      public int length()
      Returns the number of characters associated with this RMXString.
    • indexOf

      public int indexOf(String aString)
      Returns the index within this string of the first occurrence of the given substring.
    • indexOf

      public int indexOf(String aString, int start)
      Returns the index within this string of first occurrence of given substring, starting at given index.
    • charAt

      public char charAt(int anIndex)
      Returns the character at the given index.
    • getRunCount

      public int getRunCount()
      Returns the number of runs in this XString.
    • getRun

      public RMXStringRun getRun(int anIndex)
      Returns the specific Run at the given index in this XString.
    • getRunLast

      public RMXStringRun getRunLast()
      Returns the last run in this XString (convenience).
    • getAttributes

      public Map getAttributes(int anIndex)
      Returns the attributes Map for the run at the given index.
    • getAttributeAt

      public Object getAttributeAt(String aKey, int anIndex)
      Returns the value for the given attribute key at the given character index.
    • getAttributesAt

      public Map getAttributesAt(int anIndex)
      Returns the attributes Map for the run at the given character index.
    • addAttributes

      public void addAttributes(Map attrs, int start, int end)
      Adds the attributes in the given map to the specified character range.
    • addAttributes

      public void addAttributes(Map attrs, int start, int end, boolean ifAbsent)
      Adds the attributes in the given map to the specified character range. Provides an option to only apply each individual attribute if it isn't already specified for that character range.
    • addAttribute

      public void addAttribute(String aKey, Object object)
      Adds a given attribute of given type to the whole string.
    • addAttribute

      public void addAttribute(String aKey, Object attr, int start, int end)
      Adds a given attribute of given type to the given range of the string.
    • addAttribute

      public void addAttribute(String aKey, Object attr, int start, int end, boolean ifAbsent)
      Adds a given attribute of given type to the given range of the string, only if specified type is missing.
    • removeAttribute

      public void removeAttribute(String aKey, int start, int end)
      Removes any attribute of the specified type from the specified range of the string.
    • getFontAtIndex

      public RMFont getFontAtIndex(int index)
      Returns the current font at the given character index.
    • getColorAtIndex

      public RMColor getColorAtIndex(int index)
      Returns the current color at the given character index.
    • getFormatAtIndex

      public RMFormat getFormatAtIndex(int index)
      Returns the current format at the given character index.
    • getParagraphAtIndex

      public RMParagraph getParagraphAtIndex(int index)
      Returns the current paragraph at the given character index.
    • setParagraph

      public void setParagraph(RMParagraph ps, int start)
      Sets the paragraph at the given character index.
    • setParagraph

      public void setParagraph(RMParagraph ps, int start, int end)
      Sets the paragraph for the given character index range.
    • addAttribute

      public void addAttribute(Object attr)
      Applies the given attribute to whole xstring, assuming it's a basic attr types (font, color, etc.).
    • addAttribute

      public void addAttribute(Object attr, int start, int end)
      Applies the given attribute to the given character range, assuming it's a basic attr type (font, color, etc.).
    • getFontScale

      public float getFontScale()
      Returns the value that should be multipied times all font sizes in this string (defaults to 1).
    • setFontScale

      public void setFontScale(float aScale)
      Sets a value to that should be multiplied times all font sizes in this string.
    • add

      public void add(String aString)
      Appends the given String to the end of this XString.
    • add

      public void add(String aString, int anIndex)
      Appends the given String to this XString, at the given index.
    • add

      public void add(RMXString xString)
      Appends the given XString to the end of this String.
    • add

      public void add(RMXString xString, int anIndex)
      Appends the given XString to this XString, at the given index.
    • add

      public void add(String aString, Map attrs)
      Appends the given string to the end of this XString, with the given attributes.
    • add

      public void add(String aString, Map attrs, int anIndex)
      Appends the given String to this XString, with the given attributes, at the given index.
    • replace

      public void replace(String aString)
      Replaces all chars of this XString with given String.
    • replace

      public void replace(int start, int end, String aString)
      Replaces chars in given range, with given String.
    • replace

      public void replace(int start, int end, String aString, Map attrs)
      Replaces chars in given range, with given String, using the given attributes.
    • replace

      public void replace(int start, int end, RMXString xString)
      Replaces the chars in given range, with given XString.
    • delete

      public void delete(int start, int end)
      Deletes the chars in the given range.
    • substring

      public RMXString substring(int index)
      Returns the substring from the given index to end of XString.
    • substring

      public RMXString substring(int start, int end)
      Returns the substring from the given start index to the given end index.
    • separate

      public Vector separate(String aSeparator)
      Returns a List of XString substrings of this XString that are separated by the given separator String.
    • coalesceNewlines

      public void coalesceNewlines()
      Replaces any occurrance of consecutive newlines with a single newline.
    • getXStringForHTML

      public static RMXString getXStringForHTML(String html, RMFont aFont)
      Creates and returns an XString from a given HTML String.
    • getXStringForRTF

      public static RMXString getXStringForRTF(String rtf, RMFont aFont)
      Creates and returns an XString from a given RTF String.
    • getRunAt

      public RMXStringRun getRunAt(int anIndex)
      Returns the XString run at the given index.
    • getRunIndexAt

      public int getRunIndexAt(int anIndex)
      Returns the XString run index for the given character index.
    • getRunAtExactly

      public RMXStringRun getRunAtExactly(int anIndex)
      Returns an XString run for the given character index, potentially splitting an existing run to ensure that new run starts at the given index.
    • getRunIndexAtExactly

      public int getRunIndexAtExactly(int anIndex)
      Returns an XString segement index for the given character index, potentially splitting an existing run to ensure that new run starts at the given index.
    • didChange

      public void didChange()
      Notifies string that something significant changed. It's mostly for internal use (it's called automatically by most of XStrings modification methods).
    • setUnderlined

      public void setUnderlined(boolean flag)
      Sets the xstring to be underlined.
    • setOutlined

      public void setOutlined(boolean flag)
      Sets the xstring to be outlined.
    • getAlign

      public byte getAlign()
      Returns the horizontal alignment of the first paragraph of the xstring.
    • setAlign

      public void setAlign(int align)
      Sets the horizontal alignment of the xstring.
    • alignLeft

      public void alignLeft()
      Aligns the xstring left.
    • alignRight

      public void alignRight()
      Aligns the xstring right.
    • alignCenter

      public void alignCenter()
      Aligns the xstring center.
    • getVersion

      public int getVersion()
      Returns the version of this xstring.
    • getRepresentableString

      public RMXString getRepresentableString()
      Returns a version of this string that substitutes alternate fonts for any characters that cannot be displayed in their associated fonts (simply returns the receiver if all characters are valid).
    • clone

      public Object clone()
      Returns a clone of the receiver.
      Overrides:
      clone in class RMObject
    • equals

      public boolean equals(Object anObj)
      Standard Object equals implementation.
      Overrides:
      equals in class Object
    • rpgClone

      public RMXString rpgClone(ReportMill aReportMill, Object userInfo, RMShape aShape)
      Creates a clone of the receiver, with substitution performed on @-sign deliniated keys.
    • blend

      public RMXString blend(RMXString aString, float fraction)
      Returns a blended version of the receiver and the given string (0 is receiver, 1 is given string). The current implementation actually only blends text color.
    • initWithArchiver

      public Object initWithArchiver(RMArchiver anArchiver)
      Legacy archival.
      Specified by:
      initWithArchiver in interface RMArchiver.Archiving
      Overrides:
      initWithArchiver in class RMObject
    • toXML

      public RXElement toXML(RXArchiver anArchiver)
      XML archival.
    • fromXML

      public Object fromXML(RXArchiver anArchiver, RXElement anElement)
      XML unarchival.
    • toString

      public String toString()
      Returns the String associated with this XString.
      Overrides:
      toString in class Object
    • toStringXML

      public String toStringXML()
      Returns an XML style string representation of the attributes and string runs in this XString.