PageRenderTime 49ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/components/forks/poi/src/loci/poi/hssf/usermodel/HSSFRichTextString.java

http://github.com/openmicroscopy/bioformats
Java | 302 lines | 144 code | 35 blank | 123 comment | 27 complexity | fd86da49246c46065d6f11b412066aee MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, Apache-2.0, BSD-2-Clause, MPL-2.0-no-copyleft-exception
  1. /*
  2. * #%L
  3. * Fork of Apache Jakarta POI.
  4. * %%
  5. * Copyright (C) 2008 - 2013 Open Microscopy Environment:
  6. * - Board of Regents of the University of Wisconsin-Madison
  7. * - Glencoe Software, Inc.
  8. * - University of Dundee
  9. * %%
  10. * Licensed under the Apache License, Version 2.0 (the "License");
  11. * you may not use this file except in compliance with the License.
  12. * You may obtain a copy of the License at
  13. *
  14. * http://www.apache.org/licenses/LICENSE-2.0
  15. *
  16. * Unless required by applicable law or agreed to in writing, software
  17. * distributed under the License is distributed on an "AS IS" BASIS,
  18. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  19. * See the License for the specific language governing permissions and
  20. * limitations under the License.
  21. * #L%
  22. */
  23. /* ====================================================================
  24. Licensed to the Apache Software Foundation (ASF) under one or more
  25. contributor license agreements. See the NOTICE file distributed with
  26. this work for additional information regarding copyright ownership.
  27. The ASF licenses this file to You under the Apache License, Version 2.0
  28. (the "License"); you may not use this file except in compliance with
  29. the License. You may obtain a copy of the License at
  30. http://www.apache.org/licenses/LICENSE-2.0
  31. Unless required by applicable law or agreed to in writing, software
  32. distributed under the License is distributed on an "AS IS" BASIS,
  33. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  34. See the License for the specific language governing permissions and
  35. limitations under the License.
  36. ==================================================================== */
  37. package loci.poi.hssf.usermodel;
  38. import loci.poi.hssf.model.Workbook;
  39. import loci.poi.hssf.record.LabelSSTRecord;
  40. import loci.poi.hssf.record.UnicodeString;
  41. import java.util.Iterator;
  42. /**
  43. * Rich text unicode string. These strings can have fonts applied to
  44. * arbitary parts of the string.
  45. *
  46. * @author Glen Stampoultzis (glens at apache.org)
  47. * @author Jason Height (jheight at apache.org)
  48. */
  49. public class HSSFRichTextString
  50. implements Comparable
  51. {
  52. /** Place holder for indicating that NO_FONT has been applied here */
  53. public static final short NO_FONT = 0;
  54. private UnicodeString string;
  55. private Workbook book;
  56. private LabelSSTRecord record;
  57. public HSSFRichTextString()
  58. {
  59. this("");
  60. }
  61. public HSSFRichTextString( String string )
  62. {
  63. if (string == null)
  64. string = "";
  65. this.string = new UnicodeString(string);
  66. }
  67. HSSFRichTextString(Workbook book, LabelSSTRecord record) {
  68. setWorkbookReferences(book, record);
  69. this.string = book.getSSTString(record.getSSTIndex());
  70. }
  71. /** This must be called to setup the internal work book references whenever
  72. * a RichTextString is added to a cell
  73. */
  74. void setWorkbookReferences(Workbook book, LabelSSTRecord record) {
  75. this.book = book;
  76. this.record = record;
  77. }
  78. /** Called whenever the unicode string is modified. When it is modified
  79. * we need to create a new SST index, so that other LabelSSTRecords will not
  80. * be affected by changes tat we make to this string.
  81. */
  82. private UnicodeString cloneStringIfRequired() {
  83. if (book == null)
  84. return string;
  85. UnicodeString s = (UnicodeString)string.clone();
  86. return s;
  87. }
  88. private void addToSSTIfRequired() {
  89. if (book != null) {
  90. int index = book.addSSTString(string);
  91. record.setSSTIndex(index);
  92. //The act of adding the string to the SST record may have meant that
  93. //a extsing string was returned for the index, so update our local version
  94. string = book.getSSTString(index);
  95. }
  96. }
  97. /**
  98. * Applies a font to the specified characters of a string.
  99. *
  100. * @param startIndex The start index to apply the font to (inclusive)
  101. * @param endIndex The end index to apply the font to (exclusive)
  102. * @param fontIndex The font to use.
  103. */
  104. public void applyFont(int startIndex, int endIndex, short fontIndex)
  105. {
  106. if (startIndex > endIndex)
  107. throw new IllegalArgumentException("Start index must be less than end index.");
  108. if (startIndex < 0 || endIndex > length())
  109. throw new IllegalArgumentException("Start and end index not in range.");
  110. if (startIndex == endIndex)
  111. return;
  112. //Need to check what the font is currently, so we can reapply it after
  113. //the range is completed
  114. short currentFont = NO_FONT;
  115. if (endIndex != length()) {
  116. currentFont = this.getFontAtIndex(startIndex);
  117. }
  118. //Need to clear the current formatting between the startIndex and endIndex
  119. string = cloneStringIfRequired();
  120. Iterator formatting = string.formatIterator();
  121. if (formatting != null) {
  122. while (formatting.hasNext()) {
  123. UnicodeString.FormatRun r = (UnicodeString.FormatRun)formatting.next();
  124. if ((r.getCharacterPos() >= startIndex) && (r.getCharacterPos() < endIndex))
  125. formatting.remove();
  126. }
  127. }
  128. string.addFormatRun(new UnicodeString.FormatRun((short)startIndex, fontIndex));
  129. if (endIndex != length())
  130. string.addFormatRun(new UnicodeString.FormatRun((short)endIndex, currentFont));
  131. addToSSTIfRequired();
  132. }
  133. /**
  134. * Applies a font to the specified characters of a string.
  135. *
  136. * @param startIndex The start index to apply the font to (inclusive)
  137. * @param endIndex The end index to apply to font to (exclusive)
  138. * @param font The index of the font to use.
  139. */
  140. public void applyFont(int startIndex, int endIndex, HSSFFont font)
  141. {
  142. applyFont(startIndex, endIndex, font.getIndex());
  143. }
  144. /**
  145. * Sets the font of the entire string.
  146. * @param font The font to use.
  147. */
  148. public void applyFont(HSSFFont font)
  149. {
  150. applyFont(0, string.getCharCount(), font);
  151. }
  152. /**
  153. * Removes any formatting that may have been applied to the string.
  154. */
  155. public void clearFormatting() {
  156. string = cloneStringIfRequired();
  157. string.clearFormatting();
  158. addToSSTIfRequired();
  159. }
  160. /**
  161. * Returns the plain string representation.
  162. */
  163. public String getString()
  164. {
  165. return string.getString();
  166. }
  167. /** Used internally by the HSSFCell to get the internal string value*/
  168. UnicodeString getUnicodeString() {
  169. return cloneStringIfRequired();
  170. }
  171. /** Used internally by the HSSFCell to set the internal string value*/
  172. void setUnicodeString(UnicodeString str) {
  173. this.string = str;
  174. }
  175. /**
  176. * @return the number of characters in the font.
  177. */
  178. public int length()
  179. {
  180. return string.getCharCount();
  181. }
  182. /**
  183. * Returns the font in use at a particular index.
  184. *
  185. * @param index The index.
  186. * @return The font that's currently being applied at that
  187. * index or null if no font is being applied or the
  188. * index is out of range.
  189. */
  190. public short getFontAtIndex( int index )
  191. {
  192. int size = string.getFormatRunCount();
  193. UnicodeString.FormatRun currentRun = null;
  194. for (int i=0;i<size;i++) {
  195. UnicodeString.FormatRun r = string.getFormatRun(i);
  196. if (r.getCharacterPos() > index)
  197. break;
  198. else currentRun = r;
  199. }
  200. if (currentRun == null)
  201. return NO_FONT;
  202. else return currentRun.getFontIndex();
  203. }
  204. /**
  205. * @return The number of formatting runs used. There will always be at
  206. * least one of font NO_FONT.
  207. *
  208. * @see #NO_FONT
  209. */
  210. public int numFormattingRuns()
  211. {
  212. return string.getFormatRunCount();
  213. }
  214. /**
  215. * The index within the string to which the specified formatting run applies.
  216. * @param index the index of the formatting run
  217. * @return the index within the string.
  218. */
  219. public int getIndexOfFormattingRun(int index)
  220. {
  221. UnicodeString.FormatRun r = string.getFormatRun(index);
  222. return r.getCharacterPos();
  223. }
  224. /**
  225. * Gets the font used in a particular formatting run.
  226. *
  227. * @param index the index of the formatting run
  228. * @return the font number used.
  229. */
  230. public short getFontOfFormattingRun(int index)
  231. {
  232. UnicodeString.FormatRun r = string.getFormatRun(index);
  233. return r.getFontIndex();
  234. }
  235. /**
  236. * Compares one rich text string to another.
  237. */
  238. public int compareTo( Object o )
  239. {
  240. HSSFRichTextString r = (HSSFRichTextString)o;
  241. return string.compareTo(r.string);
  242. }
  243. public boolean equals(Object o) {
  244. if (o instanceof HSSFRichTextString) {
  245. return string.equals(((HSSFRichTextString)o).string);
  246. }
  247. return false;
  248. }
  249. /**
  250. * @return the plain text representation of this string.
  251. */
  252. public String toString()
  253. {
  254. return string.toString();
  255. }
  256. /**
  257. * Applies the specified font to the entire string.
  258. *
  259. * @param fontIndex the font to apply.
  260. */
  261. public void applyFont( short fontIndex )
  262. {
  263. applyFont(0, string.getCharCount(), fontIndex);
  264. }
  265. }