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.

HSSFComment.java 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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.hssf.usermodel;
  16. import org.apache.poi.hssf.record.NoteRecord;
  17. import org.apache.poi.hssf.record.TextObjectRecord;
  18. import org.apache.poi.ss.usermodel.Comment;
  19. import org.apache.poi.ss.usermodel.RichTextString;
  20. /**
  21. * Represents a cell comment - a sticky note associated with a cell.
  22. *
  23. * @author Yegor Kozlov
  24. */
  25. public class HSSFComment extends HSSFTextbox implements Comment {
  26. /*
  27. * TODO - make HSSFComment more consistent when created vs read from file.
  28. * Currently HSSFComment has two main forms (corresponding to the two constructors). There
  29. * are certain operations that only work on comment objects in one of the forms (e.g. deleting
  30. * comments).
  31. * POI is also deficient in its management of RowRecord fields firstCol and lastCol. Those
  32. * fields are supposed to take comments into account, but POI does not do this yet (feb 2009).
  33. * It seems like HSSFRow should manage a collection of local HSSFComments
  34. */
  35. private boolean _visible;
  36. private int _row;
  37. private int _col;
  38. private String _author;
  39. private NoteRecord _note;
  40. private TextObjectRecord _txo;
  41. /**
  42. * Construct a new comment with the given parent and anchor.
  43. *
  44. * @param parent
  45. * @param anchor defines position of this anchor in the sheet
  46. */
  47. public HSSFComment(HSSFShape parent, HSSFAnchor anchor) {
  48. super(parent, anchor);
  49. setShapeType(OBJECT_TYPE_COMMENT);
  50. //default color for comments
  51. _fillColor = 0x08000050;
  52. //by default comments are hidden
  53. _visible = false;
  54. _author = "";
  55. }
  56. protected HSSFComment(NoteRecord note, TextObjectRecord txo) {
  57. this((HSSFShape) null, (HSSFAnchor) null);
  58. _txo = txo;
  59. _note = note;
  60. }
  61. /**
  62. * Returns whether this comment is visible.
  63. *
  64. * @param visible <code>true</code> if the comment is visible, <code>false</code> otherwise
  65. */
  66. public void setVisible(boolean visible){
  67. if(_note != null) {
  68. _note.setFlags(visible ? NoteRecord.NOTE_VISIBLE : NoteRecord.NOTE_HIDDEN);
  69. }
  70. _visible = visible;
  71. }
  72. /**
  73. * Sets whether this comment is visible.
  74. *
  75. * @return <code>true</code> if the comment is visible, <code>false</code> otherwise
  76. */
  77. public boolean isVisible() {
  78. return _visible;
  79. }
  80. /**
  81. * Return the row of the cell that contains the comment
  82. *
  83. * @return the 0-based row of the cell that contains the comment
  84. */
  85. public int getRow() {
  86. return _row;
  87. }
  88. /**
  89. * Set the row of the cell that contains the comment
  90. *
  91. * @param row the 0-based row of the cell that contains the comment
  92. */
  93. public void setRow(int row) {
  94. if(_note != null) {
  95. _note.setRow(row);
  96. }
  97. _row = row;
  98. }
  99. /**
  100. * Return the column of the cell that contains the comment
  101. *
  102. * @return the 0-based column of the cell that contains the comment
  103. */
  104. public int getColumn(){
  105. return _col;
  106. }
  107. /**
  108. * Set the column of the cell that contains the comment
  109. *
  110. * @param col the 0-based column of the cell that contains the comment
  111. */
  112. public void setColumn(int col) {
  113. if(_note != null) {
  114. _note.setColumn(col);
  115. }
  116. _col = col;
  117. }
  118. /**
  119. * @deprecated (Nov 2009) use {@link HSSFComment#setColumn(int)} }
  120. */
  121. @Deprecated
  122. public void setColumn(short col) {
  123. setColumn((int)col);
  124. }
  125. /**
  126. * Name of the original comment author
  127. *
  128. * @return the name of the original author of the comment
  129. */
  130. public String getAuthor() {
  131. return _author;
  132. }
  133. /**
  134. * Name of the original comment author
  135. *
  136. * @param author the name of the original author of the comment
  137. */
  138. public void setAuthor(String author){
  139. if(_note != null) _note.setAuthor(author);
  140. this._author = author;
  141. }
  142. /**
  143. * Sets the rich text string used by this comment.
  144. *
  145. * @param string Sets the rich text string used by this object.
  146. */
  147. public void setString(RichTextString string) {
  148. HSSFRichTextString hstring = (HSSFRichTextString) string;
  149. //if font is not set we must set the default one
  150. if (hstring.numFormattingRuns() == 0) hstring.applyFont((short)0);
  151. if (_txo != null) {
  152. _txo.setStr(hstring);
  153. }
  154. super.setString(string);
  155. }
  156. /**
  157. * Returns the underlying Note record
  158. */
  159. protected NoteRecord getNoteRecord() {
  160. return _note;
  161. }
  162. /**
  163. * Returns the underlying Text record
  164. */
  165. protected TextObjectRecord getTextObjectRecord() {
  166. return _txo;
  167. }
  168. }