1 16 17 package org.apache.poi.hssf.usermodel; 18 19 import java.util.Map ; 20 import java.util.SortedMap ; 21 import java.util.TreeMap ; 22 23 29 public class HSSFRichTextString 30 implements Comparable 31 { 32 33 public static final short NO_FONT = -1; 34 35 String string; 36 SortedMap formattingRuns = new TreeMap (); 37 38 public HSSFRichTextString() 39 { 40 this(""); 41 } 42 43 public HSSFRichTextString( String string ) 44 { 45 this.string = string; 46 this.formattingRuns.put(new Integer (0), new Short (NO_FONT)); 47 } 48 49 56 public void applyFont(int startIndex, int endIndex, short fontIndex) 57 { 58 if (startIndex > endIndex) 59 throw new IllegalArgumentException ("Start index must be less than end index."); 60 if (startIndex < 0 || endIndex > length()) 61 throw new IllegalArgumentException ("Start and end index not in range."); 62 if (startIndex == endIndex) 63 return; 64 65 Integer from = new Integer (startIndex); 66 Integer to = new Integer (endIndex); 67 short fontAtIndex = NO_FONT; 68 if (endIndex != length()) 69 fontAtIndex = getFontAtIndex(endIndex); 70 formattingRuns.subMap(from, to).clear(); 71 formattingRuns.put(from, new Short (fontIndex)); 72 if (endIndex != length()) 73 { 74 if (fontIndex != fontAtIndex) 75 formattingRuns.put(to, new Short (fontAtIndex)); 76 } 77 } 78 79 86 public void applyFont(int startIndex, int endIndex, HSSFFont font) 87 { 88 applyFont(startIndex, endIndex, font.getIndex()); 89 } 90 91 95 public void applyFont(HSSFFont font) 96 { 97 applyFont(0, string.length(), font); 98 } 99 100 103 public String getString() 104 { 105 return string; 106 } 107 108 111 public int length() 112 { 113 return string.length(); 114 } 115 116 124 public short getFontAtIndex( int index ) 125 { 126 if (index < 0 || index >= string.length()) 127 throw new ArrayIndexOutOfBoundsException ("Font index " + index + " out of bounds of string"); 128 Integer key = new Integer (index + 1); 129 SortedMap head = formattingRuns.headMap(key); 130 if (head.isEmpty()) 131 throw new IllegalStateException ("Should not reach here. No font found."); 132 else 133 return ((Short ) head.get(head.lastKey())).shortValue(); 134 } 135 136 142 public int numFormattingRuns() 143 { 144 return formattingRuns.size(); 145 } 146 147 152 public int getIndexOfFormattingRun(int index) 153 { 154 Map.Entry [] runs = (Map.Entry []) formattingRuns.entrySet().toArray(new Map.Entry [formattingRuns.size()] ); 155 return ((Integer )runs[index].getKey()).intValue(); 156 } 157 158 164 public short getFontOfFormattingRun(int index) 165 { 166 Map.Entry [] runs = (Map.Entry []) formattingRuns.entrySet().toArray(new Map.Entry [formattingRuns.size()] ); 167 return ((Short )(runs[index].getValue())).shortValue(); 168 } 169 170 173 public int compareTo( Object o ) 174 { 175 return 0; } 177 178 181 public String toString() 182 { 183 return string; 184 } 185 186 191 public void applyFont( short fontIndex ) 192 { 193 applyFont(0, string.length(), fontIndex); 194 } 195 } 196 | Popular Tags |