123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339 |
- /* ====================================================================
- Licensed to the Apache Software Foundation (ASF) under one or more
- contributor license agreements. See the NOTICE file distributed with
- this work for additional information regarding copyright ownership.
- The ASF licenses this file to You under the Apache License, Version 2.0
- (the "License"); you may not use this file except in compliance with
- the License. You may obtain a copy of the License at
-
- http://www.apache.org/licenses/LICENSE-2.0
-
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
- ==================================================================== */
- package org.apache.poi.hssf.usermodel;
-
- import java.util.Objects;
-
- import org.apache.poi.ddf.DefaultEscherRecordFactory;
- import org.apache.poi.ddf.EscherBSERecord;
- import org.apache.poi.ddf.EscherContainerRecord;
- import org.apache.poi.ddf.EscherOptRecord;
- import org.apache.poi.ddf.EscherPropertyTypes;
- import org.apache.poi.ddf.EscherSimpleProperty;
- import org.apache.poi.hssf.record.CommonObjectDataSubRecord;
- import org.apache.poi.hssf.record.EndSubRecord;
- import org.apache.poi.hssf.record.NoteRecord;
- import org.apache.poi.hssf.record.NoteStructureSubRecord;
- import org.apache.poi.hssf.record.ObjRecord;
- import org.apache.poi.hssf.record.TextObjectRecord;
- import org.apache.poi.ss.usermodel.ClientAnchor;
- import org.apache.poi.ss.usermodel.Comment;
- import org.apache.poi.ss.util.CellAddress;
-
- /**
- * Represents a cell comment - a sticky note associated with a cell.
- */
- public class HSSFComment extends HSSFTextbox implements Comment {
-
- private static final int FILL_TYPE_SOLID = 0;
- private static final int FILL_TYPE_PICTURE = 3;
-
- private static final int GROUP_SHAPE_PROPERTY_DEFAULT_VALUE = 655362;
- private static final int GROUP_SHAPE_HIDDEN_MASK = 0x1000002;
- private static final int GROUP_SHAPE_NOT_HIDDEN_MASK = 0xFEFFFFFD;
-
- /*
- * TODO - make HSSFComment more consistent when created vs read from file.
- * Currently HSSFComment has two main forms (corresponding to the two constructors). There
- * are certain operations that only work on comment objects in one of the forms (e.g. deleting
- * comments).
- * POI is also deficient in its management of RowRecord fields firstCol and lastCol. Those
- * fields are supposed to take comments into account, but POI does not do this yet (feb 2009).
- * It seems like HSSFRow should manage a collection of local HSSFComments
- */
-
- private final NoteRecord _note;
-
- public HSSFComment(EscherContainerRecord spContainer, ObjRecord objRecord, TextObjectRecord textObjectRecord, NoteRecord note) {
- super(spContainer, objRecord, textObjectRecord);
- _note = note;
- }
-
- /**
- * Construct a new comment with the given parent and anchor.
- *
- * @param parent
- * @param anchor defines position of this anchor in the sheet
- */
- public HSSFComment(HSSFShape parent, HSSFAnchor anchor) {
- this(parent, anchor, createNoteRecord());
- }
-
- private HSSFComment(HSSFShape parent, HSSFAnchor anchor, NoteRecord note) {
- super(parent, anchor);
- _note = note;
- //default color for comments
- setFillColor(0x08000050);
-
- //by default comments are hidden
- setVisible(false);
- setAuthor("");
- CommonObjectDataSubRecord cod = (CommonObjectDataSubRecord) getObjRecord().getSubRecords().get(0);
- cod.setObjectType(CommonObjectDataSubRecord.OBJECT_TYPE_COMMENT);
- }
-
- protected HSSFComment(NoteRecord note, TextObjectRecord txo) {
- this(null, new HSSFClientAnchor(), note);
- }
-
- @Override
- void afterInsert(HSSFPatriarch patriarch) {
- super.afterInsert(patriarch);
- patriarch.getBoundAggregate().addTailRecord(getNoteRecord());
- }
-
- @Override
- protected EscherContainerRecord createSpContainer() {
- EscherContainerRecord spContainer = super.createSpContainer();
- EscherOptRecord opt = spContainer.getChildById(EscherOptRecord.RECORD_ID);
- opt.removeEscherProperty(EscherPropertyTypes.TEXT__TEXTLEFT);
- opt.removeEscherProperty(EscherPropertyTypes.TEXT__TEXTRIGHT);
- opt.removeEscherProperty(EscherPropertyTypes.TEXT__TEXTTOP);
- opt.removeEscherProperty(EscherPropertyTypes.TEXT__TEXTBOTTOM);
- opt.setEscherProperty(new EscherSimpleProperty(EscherPropertyTypes.GROUPSHAPE__FLAGS, false, false, GROUP_SHAPE_PROPERTY_DEFAULT_VALUE));
- return spContainer;
- }
-
- @Override
- protected ObjRecord createObjRecord() {
- ObjRecord obj = new ObjRecord();
- CommonObjectDataSubRecord c = new CommonObjectDataSubRecord();
- c.setObjectType(OBJECT_TYPE_COMMENT);
- c.setLocked(true);
- c.setPrintable(true);
- c.setAutofill(false);
- c.setAutoline(true);
-
- NoteStructureSubRecord u = new NoteStructureSubRecord();
- EndSubRecord e = new EndSubRecord();
- obj.addSubRecord(c);
- obj.addSubRecord(u);
- obj.addSubRecord(e);
- return obj;
- }
-
- private static NoteRecord createNoteRecord() {
- NoteRecord note = new NoteRecord();
- note.setFlags(NoteRecord.NOTE_HIDDEN);
- note.setAuthor("");
- return note;
- }
-
- @Override
- void setShapeId(int shapeId) {
- if(shapeId > 65535) {
- throw new IllegalArgumentException("Cannot add more than " + 65535 + " shapes");
- }
- super.setShapeId(shapeId);
- CommonObjectDataSubRecord cod = (CommonObjectDataSubRecord) getObjRecord().getSubRecords().get(0);
- cod.setObjectId(shapeId);
- _note.setShapeId(shapeId);
- }
-
- /**
- * Sets whether this comment is visible.
- *
- * @param visible <code>true</code> if the comment is visible, <code>false</code> otherwise
- */
- @Override
- public void setVisible(boolean visible) {
- _note.setFlags(visible ? NoteRecord.NOTE_VISIBLE : NoteRecord.NOTE_HIDDEN);
- setHidden(!visible);
- }
-
- /**
- * Returns whether this comment is visible.
- *
- * @return <code>true</code> if the comment is visible, <code>false</code> otherwise
- */
- @Override
- public boolean isVisible() {
- return _note.getFlags() == NoteRecord.NOTE_VISIBLE;
- }
-
- @Override
- public CellAddress getAddress() {
- return new CellAddress(getRow(), getColumn());
- }
-
- @Override
- public void setAddress(CellAddress address) {
- setRow(address.getRow());
- setColumn(address.getColumn());
- }
-
- @Override
- public void setAddress(int row, int col) {
- setRow(row);
- setColumn(col);
- }
-
- /**
- * Return the row of the cell that contains the comment
- *
- * @return the 0-based row of the cell that contains the comment
- */
- @Override
- public int getRow() {
- return _note.getRow();
- }
-
- /**
- * Set the row of the cell that contains the comment
- *
- * @param row the 0-based row of the cell that contains the comment
- */
- @Override
- public void setRow(int row) {
- _note.setRow(row);
- }
-
- /**
- * Return the column of the cell that contains the comment
- *
- * @return the 0-based column of the cell that contains the comment
- */
- @Override
- public int getColumn() {
- return _note.getColumn();
- }
-
- /**
- * Set the column of the cell that contains the comment
- *
- * @param col the 0-based column of the cell that contains the comment
- */
- @Override
- public void setColumn(int col) {
- _note.setColumn(col);
- }
-
- /**
- * Name of the original comment author
- *
- * @return the name of the original author of the comment
- */
- @Override
- public String getAuthor() {
- return _note.getAuthor();
- }
-
- /**
- * Name of the original comment author
- *
- * @param author the name of the original author of the comment
- */
- @Override
- public void setAuthor(String author) {
- if (_note != null) _note.setAuthor(author);
- }
-
- /**
- * Returns the underlying Note record
- */
- protected NoteRecord getNoteRecord() {
- return _note;
- }
-
- /**
- * Do we know which cell this comment belongs to?
- */
- public boolean hasPosition() {
- if (_note == null) return false;
- if (getColumn() < 0 || getRow() < 0) return false;
- return true;
- }
-
- @Override
- public ClientAnchor getClientAnchor() {
- HSSFAnchor ha = super.getAnchor();
- if (ha instanceof ClientAnchor) {
- return (ClientAnchor) ha;
- }
-
- throw new IllegalStateException("Anchor can not be changed in "
- + ClientAnchor.class.getSimpleName());
- }
-
- @Override
- public void setShapeType(int shapeType) {
- throw new IllegalStateException("Shape type can not be changed in "+this.getClass().getSimpleName());
- }
-
- @Override
- public void afterRemove(HSSFPatriarch patriarch){
- super.afterRemove(patriarch);
- patriarch.getBoundAggregate().removeTailRecord(getNoteRecord());
- }
-
- @Override
- protected HSSFShape cloneShape() {
- TextObjectRecord txo = (TextObjectRecord) getTextObjectRecord().cloneViaReserialise();
- EscherContainerRecord spContainer = new EscherContainerRecord();
- byte [] inSp = getEscherContainer().serialize();
- spContainer.fillFields(inSp, 0, new DefaultEscherRecordFactory());
- ObjRecord obj = (ObjRecord) getObjRecord().cloneViaReserialise();
- NoteRecord note = (NoteRecord) getNoteRecord().cloneViaReserialise();
- return new HSSFComment(spContainer, obj, txo, note);
- }
-
- public void setBackgroundImage(int pictureIndex){
- setPropertyValue(new EscherSimpleProperty( EscherPropertyTypes.FILL__PATTERNTEXTURE, false, true, pictureIndex));
- setPropertyValue(new EscherSimpleProperty( EscherPropertyTypes.FILL__FILLTYPE, false, false, FILL_TYPE_PICTURE));
- EscherBSERecord bse = getPatriarch().getSheet().getWorkbook().getWorkbook().getBSERecord(pictureIndex);
- bse.setRef(bse.getRef() + 1);
- }
-
- public void resetBackgroundImage(){
- EscherSimpleProperty property = getOptRecord().lookup(EscherPropertyTypes.FILL__PATTERNTEXTURE);
- if (null != property){
- EscherBSERecord bse = getPatriarch().getSheet().getWorkbook().getWorkbook().getBSERecord(property.getPropertyValue());
- bse.setRef(bse.getRef() - 1);
- getOptRecord().removeEscherProperty(EscherPropertyTypes.FILL__PATTERNTEXTURE);
- }
- setPropertyValue(new EscherSimpleProperty( EscherPropertyTypes.FILL__FILLTYPE, false, false, FILL_TYPE_SOLID));
- }
-
- public int getBackgroundImageId(){
- EscherSimpleProperty property = getOptRecord().lookup(EscherPropertyTypes.FILL__PATTERNTEXTURE);
- return property == null ? 0 : property.getPropertyValue();
- }
-
- private void setHidden(boolean value){
- EscherSimpleProperty property = getOptRecord().lookup(EscherPropertyTypes.GROUPSHAPE__FLAGS);
- // see http://msdn.microsoft.com/en-us/library/dd949807(v=office.12).aspx
- if (value){
- setPropertyValue(new EscherSimpleProperty(EscherPropertyTypes.GROUPSHAPE__FLAGS, false, false, property.getPropertyValue() | GROUP_SHAPE_HIDDEN_MASK));
- } else {
- setPropertyValue(new EscherSimpleProperty(EscherPropertyTypes.GROUPSHAPE__FLAGS, false, false, property.getPropertyValue() & GROUP_SHAPE_NOT_HIDDEN_MASK));
- }
- }
-
- @Override
- public boolean equals(Object obj) {
- if (!(obj instanceof HSSFComment)) {
- return false;
- }
- HSSFComment other = (HSSFComment) obj;
- return getNoteRecord().equals(other.getNoteRecord());
- }
-
- @Override
- public int hashCode() {
- return Objects.hash(getRow(),getColumn());
- }
- }
|