FileDocCategorySizeDatePackage
HSSFRichTextString.javaAPI DocApache Poi 3.0.18748Mon Jan 01 12:39:36 GMT 2007org.apache.poi.hssf.usermodel

HSSFRichTextString

public class HSSFRichTextString extends Object implements Comparable
Rich text unicode string. These strings can have fonts applied to arbitary parts of the string.
author
Glen Stampoultzis (glens at apache.org)
author
Jason Height (jheight at apache.org)

Fields Summary
public static final short
NO_FONT
Place holder for indicating that NO_FONT has been applied here
private UnicodeString
string
private org.apache.poi.hssf.model.Workbook
book
private LabelSSTRecord
record
Constructors Summary
public HSSFRichTextString()


     
    
        this("");
    
public HSSFRichTextString(String string)

        if (string == null)
          string = "";
        this.string = new UnicodeString(string);
    
HSSFRichTextString(org.apache.poi.hssf.model.Workbook book, LabelSSTRecord record)

      setWorkbookReferences(book, record);
      
      this.string = book.getSSTString(record.getSSTIndex());
    
Methods Summary
private voidaddToSSTIfRequired()

      if (book != null) {
        int index = book.addSSTString(string);
        record.setSSTIndex(index);
        //The act of adding the string to the SST record may have meant that
        //a extsing string was returned for the index, so update our local version
        string = book.getSSTString(index);
      }
    
public voidapplyFont(short fontIndex)
Applies the specified font to the entire string.

param
fontIndex the font to apply.

        applyFont(0, string.getCharCount(), fontIndex);
    
public voidapplyFont(int startIndex, int endIndex, short fontIndex)
Applies a font to the specified characters of a string.

param
startIndex The start index to apply the font to (inclusive)
param
endIndex The end index to apply the font to (exclusive)
param
fontIndex The font to use.

        if (startIndex > endIndex)
            throw new IllegalArgumentException("Start index must be less than end index.");
        if (startIndex < 0 || endIndex > length())
            throw new IllegalArgumentException("Start and end index not in range.");
        if (startIndex == endIndex)
            return;

        //Need to check what the font is currently, so we can reapply it after
        //the range is completed
        short currentFont = NO_FONT;
        if (endIndex != length()) {
          currentFont = this.getFontAtIndex(startIndex);
        }

        //Need to clear the current formatting between the startIndex and endIndex
        string = cloneStringIfRequired();
        Iterator formatting = string.formatIterator();
        if (formatting != null) {
          while (formatting.hasNext()) {
            UnicodeString.FormatRun r = (UnicodeString.FormatRun)formatting.next();
            if ((r.getCharacterPos() >= startIndex) && (r.getCharacterPos() < endIndex))
              formatting.remove();
          }
        }


        string.addFormatRun(new UnicodeString.FormatRun((short)startIndex, fontIndex));
        if (endIndex != length())
          string.addFormatRun(new UnicodeString.FormatRun((short)endIndex, currentFont));
          
        addToSSTIfRequired();
    
public voidapplyFont(int startIndex, int endIndex, org.apache.poi.hssf.usermodel.HSSFFont font)
Applies a font to the specified characters of a string.

param
startIndex The start index to apply the font to (inclusive)
param
endIndex The end index to apply to font to (exclusive)
param
font The index of the font to use.

        applyFont(startIndex, endIndex, font.getIndex());
    
public voidapplyFont(org.apache.poi.hssf.usermodel.HSSFFont font)
Sets the font of the entire string.

param
font The font to use.

        applyFont(0, string.getCharCount(), font);
    
public voidclearFormatting()
Removes any formatting that may have been applied to the string.

      string = cloneStringIfRequired();
      string.clearFormatting();
      addToSSTIfRequired();
    
private org.apache.poi.hssf.record.UnicodeStringcloneStringIfRequired()
Called whenever the unicode string is modified. When it is modified we need to create a new SST index, so that other LabelSSTRecords will not be affected by changes tat we make to this string.

      if (book == null)
        return string;
      UnicodeString s = (UnicodeString)string.clone();
      return s;
    
public intcompareTo(java.lang.Object o)
Compares one rich text string to another.

       HSSFRichTextString r = (HSSFRichTextString)o;
       return string.compareTo(r.string);
    
public booleanequals(java.lang.Object o)

      if (o instanceof HSSFRichTextString) {
        return string.equals(((HSSFRichTextString)o).string);
      }
      return false;
    
    
public shortgetFontAtIndex(int index)
Returns the font in use at a particular index.

param
index The index.
return
The font that's currently being applied at that index or null if no font is being applied or the index is out of range.

      int size = string.getFormatRunCount();
      UnicodeString.FormatRun currentRun = null;
      for (int i=0;i<size;i++) {
        UnicodeString.FormatRun r = string.getFormatRun(i);
        if (r.getCharacterPos() > index)
          break;
        else currentRun = r;
      }
      if (currentRun == null)
        return NO_FONT;
      else return currentRun.getFontIndex();
    
public shortgetFontOfFormattingRun(int index)
Gets the font used in a particular formatting run.

param
index the index of the formatting run
return
the font number used.

      UnicodeString.FormatRun r = string.getFormatRun(index);
      return r.getFontIndex();
    
public intgetIndexOfFormattingRun(int index)
The index within the string to which the specified formatting run applies.

param
index the index of the formatting run
return
the index within the string.

        UnicodeString.FormatRun r = string.getFormatRun(index);
        return r.getCharacterPos();
    
public java.lang.StringgetString()
Returns the plain string representation.

        return string.getString();
    
org.apache.poi.hssf.record.UnicodeStringgetUnicodeString()
Used internally by the HSSFCell to get the internal string value

      return cloneStringIfRequired();
    
public intlength()

return
the number of characters in the font.

        return string.getCharCount();
    
public intnumFormattingRuns()

return
The number of formatting runs used. There will always be at least one of font NO_FONT.
see
#NO_FONT

        return string.getFormatRunCount();
    
voidsetUnicodeString(org.apache.poi.hssf.record.UnicodeString str)
Used internally by the HSSFCell to set the internal string value

      this.string = str;
    
voidsetWorkbookReferences(org.apache.poi.hssf.model.Workbook book, org.apache.poi.hssf.record.LabelSSTRecord record)
This must be called to setup the internal work book references whenever a RichTextString is added to a cell

      this.book = book;
      this.record = record;      
    
public java.lang.StringtoString()

return
the plain text representation of this string.

        return string.toString();