You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

XSSFComment.java 9.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.xssf.usermodel;
  16. import static org.apache.poi.util.Units.EMU_PER_PIXEL;
  17. import java.math.BigInteger;
  18. import org.apache.poi.ss.usermodel.ClientAnchor;
  19. import org.apache.poi.ss.usermodel.Comment;
  20. import org.apache.poi.ss.usermodel.RichTextString;
  21. import org.apache.poi.ss.util.CellAddress;
  22. import org.apache.poi.ss.util.CellReference;
  23. import org.apache.poi.xssf.model.Comments;
  24. import org.apache.poi.xssf.model.CommentsTable;
  25. import org.openxmlformats.schemas.officeDocument.x2006.sharedTypes.STTrueFalseBlank;
  26. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComment;
  27. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRst;
  28. import com.microsoft.schemas.office.excel.CTClientData;
  29. import com.microsoft.schemas.vml.CTShape;
  30. public class XSSFComment implements Comment {
  31. private final CTComment _comment;
  32. private final Comments _comments;
  33. private final CTShape _vmlShape;
  34. /**
  35. * cached reference to the string with the comment text
  36. */
  37. private XSSFRichTextString _str;
  38. /**
  39. * Creates a new XSSFComment, associated with a given
  40. * low level comment object.
  41. */
  42. public XSSFComment(CommentsTable comments, CTComment comment, CTShape vmlShape) {
  43. this((Comments)comments, comment, vmlShape);
  44. }
  45. /**
  46. * Creates a new XSSFComment, associated with a given
  47. * low level comment object.
  48. * @since POI 5.2.0
  49. */
  50. public XSSFComment(Comments comments, CTComment comment, CTShape vmlShape) {
  51. _comment = comment;
  52. _comments = comments;
  53. _vmlShape = vmlShape;
  54. // we potentially need to adjust the column/row information in the shape
  55. // the same way as we do in setRow()/setColumn()
  56. if(comment != null && vmlShape != null && vmlShape.sizeOfClientDataArray() > 0) {
  57. CellReference ref = new CellReference(comment.getRef());
  58. CTClientData clientData = vmlShape.getClientDataArray(0);
  59. clientData.setRowArray(0, new BigInteger(String.valueOf(ref.getRow())));
  60. clientData.setColumnArray(0, new BigInteger(String.valueOf(ref.getCol())));
  61. avoidXmlbeansCorruptPointer(vmlShape);
  62. }
  63. }
  64. /**
  65. *
  66. * @return Name of the original comment author. Default value is blank.
  67. */
  68. @Override
  69. public String getAuthor() {
  70. return _comments.getAuthor(_comment.getAuthorId());
  71. }
  72. /**
  73. * Name of the original comment author. Default value is blank.
  74. *
  75. * @param author the name of the original author of the comment
  76. */
  77. @Override
  78. public void setAuthor(String author) {
  79. _comment.setAuthorId(_comments.findAuthor(author));
  80. _comments.commentUpdated(this);
  81. }
  82. /**
  83. * @return the 0-based column of the cell that the comment is associated with.
  84. */
  85. @Override
  86. public int getColumn() {
  87. return getAddress().getColumn();
  88. }
  89. /**
  90. * @return the 0-based row index of the cell that the comment is associated with.
  91. */
  92. @Override
  93. public int getRow() {
  94. return getAddress().getRow();
  95. }
  96. /**
  97. * Returns whether this comment is visible.
  98. *
  99. * @return <code>true</code> if the comment is visible, <code>false</code> otherwise
  100. */
  101. @Override
  102. public boolean isVisible() {
  103. boolean visible = false;
  104. if(_vmlShape != null) {
  105. if (_vmlShape.sizeOfClientDataArray() > 0) {
  106. CTClientData clientData = _vmlShape.getClientDataArray(0);
  107. visible = clientData != null && clientData.sizeOfVisibleArray() > 0;
  108. } else {
  109. String style = _vmlShape.getStyle();
  110. visible = style != null && style.contains("visibility:visible");
  111. }
  112. }
  113. return visible;
  114. }
  115. /**
  116. * Sets whether this comment is visible.
  117. *
  118. * @param visible <code>true</code> if the comment is visible, <code>false</code> otherwise
  119. */
  120. @Override
  121. public void setVisible(boolean visible) {
  122. if(_vmlShape != null) {
  123. if (visible) {
  124. _vmlShape.setStyle("position:absolute");
  125. CTClientData clientData = _vmlShape.getClientDataArray(0);
  126. if (clientData != null && clientData.sizeOfVisibleArray() == 0) {
  127. clientData.addVisible(STTrueFalseBlank.X);
  128. }
  129. } else {
  130. _vmlShape.setStyle("position:absolute;visibility:hidden");
  131. CTClientData clientData = _vmlShape.getClientDataArray(0);
  132. if (clientData != null && clientData.sizeOfVisibleArray() > 0) {
  133. clientData.removeVisible(0);
  134. }
  135. }
  136. }
  137. _comments.commentUpdated(this);
  138. }
  139. @Override
  140. public CellAddress getAddress() {
  141. return new CellAddress(_comment.getRef());
  142. }
  143. @Override
  144. public void setAddress(int row, int col) {
  145. setAddress(new CellAddress(row, col));
  146. }
  147. @Override
  148. public void setAddress(CellAddress address) {
  149. CellAddress oldRef = new CellAddress(_comment.getRef());
  150. if (address.equals(oldRef)) {
  151. // nothing to do
  152. return;
  153. }
  154. _comment.setRef(address.formatAsString());
  155. _comments.referenceUpdated(oldRef, this);
  156. if (_vmlShape != null) {
  157. CTClientData clientData = _vmlShape.getClientDataArray(0);
  158. clientData.setRowArray(0, new BigInteger(String.valueOf(address.getRow())));
  159. clientData.setColumnArray(0, new BigInteger(String.valueOf(address.getColumn())));
  160. avoidXmlbeansCorruptPointer(_vmlShape);
  161. }
  162. _comments.commentUpdated(this);
  163. }
  164. /**
  165. * Set the column of the cell that contains the comment
  166. *
  167. * If changing both row and column, use {@link #setAddress}.
  168. *
  169. * @param col the 0-based column of the cell that contains the comment
  170. */
  171. @Override
  172. public void setColumn(int col) {
  173. setAddress(getRow(), col);
  174. }
  175. /**
  176. * Set the row of the cell that contains the comment
  177. *
  178. * If changing both row and column, use {@link #setAddress}.
  179. *
  180. * @param row the 0-based row of the cell that contains the comment
  181. */
  182. @Override
  183. public void setRow(int row) {
  184. setAddress(row, getColumn());
  185. }
  186. /**
  187. * @return the rich text string of the comment
  188. */
  189. @Override
  190. public XSSFRichTextString getString() {
  191. if(_str == null) {
  192. CTRst rst = _comment.getText();
  193. if(rst != null) _str = new XSSFRichTextString(_comment.getText());
  194. }
  195. return _str;
  196. }
  197. /**
  198. * Sets the rich text string used by this comment.
  199. *
  200. * @param string the XSSFRichTextString used by this object.
  201. */
  202. @Override
  203. public void setString(RichTextString string) {
  204. if(!(string instanceof XSSFRichTextString)){
  205. throw new IllegalArgumentException("Only XSSFRichTextString argument is supported");
  206. }
  207. _str = (XSSFRichTextString)string;
  208. _comment.setText(_str.getCTRst());
  209. _comments.commentUpdated(this);
  210. }
  211. public void setString(String string) {
  212. setString(new XSSFRichTextString(string));
  213. }
  214. @Override
  215. public ClientAnchor getClientAnchor() {
  216. if(_vmlShape == null) {
  217. return null;
  218. }
  219. String position = _vmlShape.getClientDataArray(0).getAnchorArray(0);
  220. int[] pos = new int[8];
  221. int i = 0;
  222. for (String s : position.split(",")) {
  223. pos[i++] = Integer.parseInt(s.trim());
  224. }
  225. return new XSSFClientAnchor(pos[1]*EMU_PER_PIXEL, pos[3]*EMU_PER_PIXEL, pos[5]*EMU_PER_PIXEL, pos[7]*EMU_PER_PIXEL, pos[0], pos[2], pos[4], pos[6]);
  226. }
  227. /**
  228. * @return the xml bean holding this comment's properties
  229. * @since POI 5.2.0 (was protected before POI 5.2.0)
  230. */
  231. public CTComment getCTComment(){
  232. return _comment;
  233. }
  234. protected CTShape getCTShape(){
  235. return _vmlShape;
  236. }
  237. @Override
  238. public boolean equals(Object obj) {
  239. if (!(obj instanceof XSSFComment)) {
  240. return false;
  241. }
  242. XSSFComment other = (XSSFComment) obj;
  243. return ((getCTComment() == other.getCTComment()) &&
  244. (getCTShape() == other.getCTShape()));
  245. }
  246. @Override
  247. public int hashCode() {
  248. return ((getRow()*17) + getColumn())*31;
  249. }
  250. private static void avoidXmlbeansCorruptPointer(CTShape vmlShape) {
  251. // There is a very odd xmlbeans bug when changing the row
  252. // arrays which can lead to corrupt pointer
  253. // This call seems to fix them again... See bug #50795
  254. //noinspection ResultOfMethodCallIgnored
  255. vmlShape.getClientDataList().toString();
  256. }
  257. }