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 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 org.apache.poi.ss.usermodel.Comment;
  17. import org.apache.poi.ss.usermodel.RichTextString;
  18. import org.apache.poi.ss.util.CellReference;
  19. import org.apache.poi.xssf.model.CommentsTable;
  20. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComment;
  21. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRst;
  22. import schemasMicrosoftComVml.CTShape;
  23. import java.math.BigInteger;
  24. public class XSSFComment implements Comment {
  25. private final CTComment _comment;
  26. private final CommentsTable _comments;
  27. private final CTShape _vmlShape;
  28. /**
  29. * cached reference to the string with the comment text
  30. */
  31. private XSSFRichTextString _str;
  32. /**
  33. * Creates a new XSSFComment, associated with a given
  34. * low level comment object.
  35. */
  36. public XSSFComment(CommentsTable comments, CTComment comment, CTShape vmlShape) {
  37. _comment = comment;
  38. _comments = comments;
  39. _vmlShape = vmlShape;
  40. }
  41. /**
  42. *
  43. * @return Name of the original comment author. Default value is blank.
  44. */
  45. public String getAuthor() {
  46. return _comments.getAuthor((int) _comment.getAuthorId());
  47. }
  48. /**
  49. * Name of the original comment author. Default value is blank.
  50. *
  51. * @param author the name of the original author of the comment
  52. */
  53. public void setAuthor(String author) {
  54. _comment.setAuthorId(
  55. _comments.findAuthor(author)
  56. );
  57. }
  58. /**
  59. * @return the 0-based column of the cell that the comment is associated with.
  60. */
  61. public int getColumn() {
  62. return new CellReference(_comment.getRef()).getCol();
  63. }
  64. /**
  65. * @return the 0-based row index of the cell that the comment is associated with.
  66. */
  67. public int getRow() {
  68. return new CellReference(_comment.getRef()).getRow();
  69. }
  70. /**
  71. * @return whether the comment is visible
  72. */
  73. public boolean isVisible() {
  74. boolean visible = false;
  75. if(_vmlShape != null){
  76. String style = _vmlShape.getStyle();
  77. visible = style != null && style.indexOf("visibility:visible") != -1;
  78. }
  79. return visible;
  80. }
  81. /**
  82. * @param visible whether the comment is visible
  83. */
  84. public void setVisible(boolean visible) {
  85. if(_vmlShape != null){
  86. String style;
  87. if(visible) style = "position:absolute;visibility:visible";
  88. else style = "position:absolute;visibility:hidden";
  89. _vmlShape.setStyle(style);
  90. }
  91. }
  92. /**
  93. * Set the column of the cell that contains the comment
  94. *
  95. * @param col the 0-based column of the cell that contains the comment
  96. */
  97. public void setColumn(int col) {
  98. String oldRef = _comment.getRef();
  99. CellReference ref = new CellReference(getRow(), col);
  100. _comment.setRef(ref.formatAsString());
  101. _comments.referenceUpdated(oldRef, _comment);
  102. if(_vmlShape != null) {
  103. _vmlShape.getClientDataArray(0).setColumnArray(
  104. new BigInteger[] { new BigInteger(String.valueOf(col)) }
  105. );
  106. // There is a very odd xmlbeans bug when changing the column
  107. // arrays which can lead to corrupt pointer
  108. // This call seems to fix them again... See bug #50795
  109. _vmlShape.getClientDataList().toString();
  110. }
  111. }
  112. /**
  113. * Set the row of the cell that contains the comment
  114. *
  115. * @param row the 0-based row of the cell that contains the comment
  116. */
  117. public void setRow(int row) {
  118. String oldRef = _comment.getRef();
  119. String newRef =
  120. (new CellReference(row, getColumn())).formatAsString();
  121. _comment.setRef(newRef);
  122. _comments.referenceUpdated(oldRef, _comment);
  123. if(_vmlShape != null) _vmlShape.getClientDataArray(0).setRowArray(0, new BigInteger(String.valueOf(row)));
  124. }
  125. /**
  126. * @return the rich text string of the comment
  127. */
  128. public XSSFRichTextString getString() {
  129. if(_str == null) {
  130. CTRst rst = _comment.getText();
  131. if(rst != null) _str = new XSSFRichTextString(_comment.getText());
  132. }
  133. return _str;
  134. }
  135. /**
  136. * Sets the rich text string used by this comment.
  137. *
  138. * @param string the XSSFRichTextString used by this object.
  139. */
  140. public void setString(RichTextString string) {
  141. if(!(string instanceof XSSFRichTextString)){
  142. throw new IllegalArgumentException("Only XSSFRichTextString argument is supported");
  143. }
  144. _str = (XSSFRichTextString)string;
  145. _comment.setText(_str.getCTRst());
  146. }
  147. public void setString(String string) {
  148. setString(new XSSFRichTextString(string));
  149. }
  150. /**
  151. * @return the xml bean holding this comment's properties
  152. */
  153. protected CTComment getCTComment(){
  154. return _comment;
  155. }
  156. protected CTShape getCTShape(){
  157. return _vmlShape;
  158. }
  159. }