Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

HSSFComment.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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 java.util.Objects;
  17. import org.apache.poi.ddf.DefaultEscherRecordFactory;
  18. import org.apache.poi.ddf.EscherBSERecord;
  19. import org.apache.poi.ddf.EscherContainerRecord;
  20. import org.apache.poi.ddf.EscherOptRecord;
  21. import org.apache.poi.ddf.EscherPropertyTypes;
  22. import org.apache.poi.ddf.EscherSimpleProperty;
  23. import org.apache.poi.hssf.record.CommonObjectDataSubRecord;
  24. import org.apache.poi.hssf.record.EndSubRecord;
  25. import org.apache.poi.hssf.record.NoteRecord;
  26. import org.apache.poi.hssf.record.NoteStructureSubRecord;
  27. import org.apache.poi.hssf.record.ObjRecord;
  28. import org.apache.poi.hssf.record.TextObjectRecord;
  29. import org.apache.poi.ss.usermodel.ClientAnchor;
  30. import org.apache.poi.ss.usermodel.Comment;
  31. import org.apache.poi.ss.util.CellAddress;
  32. /**
  33. * Represents a cell comment - a sticky note associated with a cell.
  34. */
  35. public class HSSFComment extends HSSFTextbox implements Comment {
  36. private static final int FILL_TYPE_SOLID = 0;
  37. private static final int FILL_TYPE_PICTURE = 3;
  38. private static final int GROUP_SHAPE_PROPERTY_DEFAULT_VALUE = 655362;
  39. private static final int GROUP_SHAPE_HIDDEN_MASK = 0x1000002;
  40. private static final int GROUP_SHAPE_NOT_HIDDEN_MASK = 0xFEFFFFFD;
  41. /*
  42. * TODO - make HSSFComment more consistent when created vs read from file.
  43. * Currently HSSFComment has two main forms (corresponding to the two constructors). There
  44. * are certain operations that only work on comment objects in one of the forms (e.g. deleting
  45. * comments).
  46. * POI is also deficient in its management of RowRecord fields firstCol and lastCol. Those
  47. * fields are supposed to take comments into account, but POI does not do this yet (feb 2009).
  48. * It seems like HSSFRow should manage a collection of local HSSFComments
  49. */
  50. private final NoteRecord _note;
  51. public HSSFComment(EscherContainerRecord spContainer, ObjRecord objRecord, TextObjectRecord textObjectRecord, NoteRecord note) {
  52. super(spContainer, objRecord, textObjectRecord);
  53. _note = note;
  54. }
  55. /**
  56. * Construct a new comment with the given parent and anchor.
  57. *
  58. * @param parent
  59. * @param anchor defines position of this anchor in the sheet
  60. */
  61. public HSSFComment(HSSFShape parent, HSSFAnchor anchor) {
  62. this(parent, anchor, createNoteRecord());
  63. }
  64. private HSSFComment(HSSFShape parent, HSSFAnchor anchor, NoteRecord note) {
  65. super(parent, anchor);
  66. _note = note;
  67. //default color for comments
  68. setFillColor(0x08000050);
  69. //by default comments are hidden
  70. setVisible(false);
  71. setAuthor("");
  72. CommonObjectDataSubRecord cod = (CommonObjectDataSubRecord) getObjRecord().getSubRecords().get(0);
  73. cod.setObjectType(CommonObjectDataSubRecord.OBJECT_TYPE_COMMENT);
  74. }
  75. protected HSSFComment(NoteRecord note, TextObjectRecord txo) {
  76. this(null, new HSSFClientAnchor(), note);
  77. }
  78. @Override
  79. void afterInsert(HSSFPatriarch patriarch) {
  80. super.afterInsert(patriarch);
  81. patriarch.getBoundAggregate().addTailRecord(getNoteRecord());
  82. }
  83. @Override
  84. protected EscherContainerRecord createSpContainer() {
  85. EscherContainerRecord spContainer = super.createSpContainer();
  86. EscherOptRecord opt = spContainer.getChildById(EscherOptRecord.RECORD_ID);
  87. opt.removeEscherProperty(EscherPropertyTypes.TEXT__TEXTLEFT);
  88. opt.removeEscherProperty(EscherPropertyTypes.TEXT__TEXTRIGHT);
  89. opt.removeEscherProperty(EscherPropertyTypes.TEXT__TEXTTOP);
  90. opt.removeEscherProperty(EscherPropertyTypes.TEXT__TEXTBOTTOM);
  91. opt.setEscherProperty(new EscherSimpleProperty(EscherPropertyTypes.GROUPSHAPE__FLAGS, false, false, GROUP_SHAPE_PROPERTY_DEFAULT_VALUE));
  92. return spContainer;
  93. }
  94. @Override
  95. protected ObjRecord createObjRecord() {
  96. ObjRecord obj = new ObjRecord();
  97. CommonObjectDataSubRecord c = new CommonObjectDataSubRecord();
  98. c.setObjectType(OBJECT_TYPE_COMMENT);
  99. c.setLocked(true);
  100. c.setPrintable(true);
  101. c.setAutofill(false);
  102. c.setAutoline(true);
  103. NoteStructureSubRecord u = new NoteStructureSubRecord();
  104. EndSubRecord e = new EndSubRecord();
  105. obj.addSubRecord(c);
  106. obj.addSubRecord(u);
  107. obj.addSubRecord(e);
  108. return obj;
  109. }
  110. private static NoteRecord createNoteRecord() {
  111. NoteRecord note = new NoteRecord();
  112. note.setFlags(NoteRecord.NOTE_HIDDEN);
  113. note.setAuthor("");
  114. return note;
  115. }
  116. @Override
  117. void setShapeId(int shapeId) {
  118. if(shapeId > 65535) {
  119. throw new IllegalArgumentException("Cannot add more than " + 65535 + " shapes");
  120. }
  121. super.setShapeId(shapeId);
  122. CommonObjectDataSubRecord cod = (CommonObjectDataSubRecord) getObjRecord().getSubRecords().get(0);
  123. cod.setObjectId(shapeId);
  124. _note.setShapeId(shapeId);
  125. }
  126. /**
  127. * Sets whether this comment is visible.
  128. *
  129. * @param visible <code>true</code> if the comment is visible, <code>false</code> otherwise
  130. */
  131. @Override
  132. public void setVisible(boolean visible) {
  133. _note.setFlags(visible ? NoteRecord.NOTE_VISIBLE : NoteRecord.NOTE_HIDDEN);
  134. setHidden(!visible);
  135. }
  136. /**
  137. * Returns whether this comment is visible.
  138. *
  139. * @return <code>true</code> if the comment is visible, <code>false</code> otherwise
  140. */
  141. @Override
  142. public boolean isVisible() {
  143. return _note.getFlags() == NoteRecord.NOTE_VISIBLE;
  144. }
  145. @Override
  146. public CellAddress getAddress() {
  147. return new CellAddress(getRow(), getColumn());
  148. }
  149. @Override
  150. public void setAddress(CellAddress address) {
  151. setRow(address.getRow());
  152. setColumn(address.getColumn());
  153. }
  154. @Override
  155. public void setAddress(int row, int col) {
  156. setRow(row);
  157. setColumn(col);
  158. }
  159. /**
  160. * Return the row of the cell that contains the comment
  161. *
  162. * @return the 0-based row of the cell that contains the comment
  163. */
  164. @Override
  165. public int getRow() {
  166. return _note.getRow();
  167. }
  168. /**
  169. * Set the row of the cell that contains the comment
  170. *
  171. * @param row the 0-based row of the cell that contains the comment
  172. */
  173. @Override
  174. public void setRow(int row) {
  175. _note.setRow(row);
  176. }
  177. /**
  178. * Return the column of the cell that contains the comment
  179. *
  180. * @return the 0-based column of the cell that contains the comment
  181. */
  182. @Override
  183. public int getColumn() {
  184. return _note.getColumn();
  185. }
  186. /**
  187. * Set the column of the cell that contains the comment
  188. *
  189. * @param col the 0-based column of the cell that contains the comment
  190. */
  191. @Override
  192. public void setColumn(int col) {
  193. _note.setColumn(col);
  194. }
  195. /**
  196. * Name of the original comment author
  197. *
  198. * @return the name of the original author of the comment
  199. */
  200. @Override
  201. public String getAuthor() {
  202. return _note.getAuthor();
  203. }
  204. /**
  205. * Name of the original comment author
  206. *
  207. * @param author the name of the original author of the comment
  208. */
  209. @Override
  210. public void setAuthor(String author) {
  211. if (_note != null) _note.setAuthor(author);
  212. }
  213. /**
  214. * Returns the underlying Note record
  215. */
  216. protected NoteRecord getNoteRecord() {
  217. return _note;
  218. }
  219. /**
  220. * Do we know which cell this comment belongs to?
  221. */
  222. public boolean hasPosition() {
  223. if (_note == null) return false;
  224. if (getColumn() < 0 || getRow() < 0) return false;
  225. return true;
  226. }
  227. @Override
  228. public ClientAnchor getClientAnchor() {
  229. HSSFAnchor ha = super.getAnchor();
  230. if (ha instanceof ClientAnchor) {
  231. return (ClientAnchor) ha;
  232. }
  233. throw new IllegalStateException("Anchor can not be changed in "
  234. + ClientAnchor.class.getSimpleName());
  235. }
  236. @Override
  237. public void setShapeType(int shapeType) {
  238. throw new IllegalStateException("Shape type can not be changed in "+this.getClass().getSimpleName());
  239. }
  240. @Override
  241. public void afterRemove(HSSFPatriarch patriarch){
  242. super.afterRemove(patriarch);
  243. patriarch.getBoundAggregate().removeTailRecord(getNoteRecord());
  244. }
  245. @Override
  246. protected HSSFShape cloneShape() {
  247. TextObjectRecord txo = (TextObjectRecord) getTextObjectRecord().cloneViaReserialise();
  248. EscherContainerRecord spContainer = new EscherContainerRecord();
  249. byte [] inSp = getEscherContainer().serialize();
  250. spContainer.fillFields(inSp, 0, new DefaultEscherRecordFactory());
  251. ObjRecord obj = (ObjRecord) getObjRecord().cloneViaReserialise();
  252. NoteRecord note = (NoteRecord) getNoteRecord().cloneViaReserialise();
  253. return new HSSFComment(spContainer, obj, txo, note);
  254. }
  255. public void setBackgroundImage(int pictureIndex){
  256. setPropertyValue(new EscherSimpleProperty( EscherPropertyTypes.FILL__PATTERNTEXTURE, false, true, pictureIndex));
  257. setPropertyValue(new EscherSimpleProperty( EscherPropertyTypes.FILL__FILLTYPE, false, false, FILL_TYPE_PICTURE));
  258. EscherBSERecord bse = getPatriarch().getSheet().getWorkbook().getWorkbook().getBSERecord(pictureIndex);
  259. bse.setRef(bse.getRef() + 1);
  260. }
  261. public void resetBackgroundImage(){
  262. EscherSimpleProperty property = getOptRecord().lookup(EscherPropertyTypes.FILL__PATTERNTEXTURE);
  263. if (null != property){
  264. EscherBSERecord bse = getPatriarch().getSheet().getWorkbook().getWorkbook().getBSERecord(property.getPropertyValue());
  265. bse.setRef(bse.getRef() - 1);
  266. getOptRecord().removeEscherProperty(EscherPropertyTypes.FILL__PATTERNTEXTURE);
  267. }
  268. setPropertyValue(new EscherSimpleProperty( EscherPropertyTypes.FILL__FILLTYPE, false, false, FILL_TYPE_SOLID));
  269. }
  270. public int getBackgroundImageId(){
  271. EscherSimpleProperty property = getOptRecord().lookup(EscherPropertyTypes.FILL__PATTERNTEXTURE);
  272. return property == null ? 0 : property.getPropertyValue();
  273. }
  274. private void setHidden(boolean value){
  275. EscherSimpleProperty property = getOptRecord().lookup(EscherPropertyTypes.GROUPSHAPE__FLAGS);
  276. // see http://msdn.microsoft.com/en-us/library/dd949807(v=office.12).aspx
  277. if (value){
  278. setPropertyValue(new EscherSimpleProperty(EscherPropertyTypes.GROUPSHAPE__FLAGS, false, false, property.getPropertyValue() | GROUP_SHAPE_HIDDEN_MASK));
  279. } else {
  280. setPropertyValue(new EscherSimpleProperty(EscherPropertyTypes.GROUPSHAPE__FLAGS, false, false, property.getPropertyValue() & GROUP_SHAPE_NOT_HIDDEN_MASK));
  281. }
  282. }
  283. @Override
  284. public boolean equals(Object obj) {
  285. if (!(obj instanceof HSSFComment)) {
  286. return false;
  287. }
  288. HSSFComment other = (HSSFComment) obj;
  289. return getNoteRecord().equals(other.getNoteRecord());
  290. }
  291. @Override
  292. public int hashCode() {
  293. return Objects.hash(getRow(),getColumn());
  294. }
  295. }