001: /* ====================================================================
002: Licensed to the Apache Software Foundation (ASF) under one or more
003: contributor license agreements. See the NOTICE file distributed with
004: this work for additional information regarding copyright ownership.
005: The ASF licenses this file to You under the Apache License, Version 2.0
006: (the "License"); you may not use this file except in compliance with
007: the License. You may obtain a copy of the License at
008:
009: http://www.apache.org/licenses/LICENSE-2.0
010:
011: Unless required by applicable law or agreed to in writing, software
012: distributed under the License is distributed on an "AS IS" BASIS,
013: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: See the License for the specific language governing permissions and
015: limitations under the License.
016: ==================================================================== */
017: package org.apache.poi.hssf.model;
018:
019: import org.apache.poi.hssf.record.*;
020: import org.apache.poi.hssf.usermodel.HSSFComment;
021: import org.apache.poi.hssf.usermodel.HSSFShape;
022: import org.apache.poi.util.LittleEndian;
023: import org.apache.poi.ddf.*;
024:
025: import java.util.List;
026: import java.util.Iterator;
027:
028: /**
029: * Represents a cell comment.
030: * This class converts highlevel model data from <code>HSSFComment</code>
031: * to low-level records.
032: *
033: * @author Yegor Kozlov
034: */
035: public class CommentShape extends TextboxShape {
036:
037: private NoteRecord note;
038:
039: /**
040: * Creates the low-level records for a comment.
041: *
042: * @param hssfShape The highlevel shape.
043: * @param shapeId The shape id to use for this shape.
044: */
045: public CommentShape(HSSFComment hssfShape, int shapeId) {
046: super (hssfShape, shapeId);
047:
048: note = createNoteRecord(hssfShape, shapeId);
049:
050: ObjRecord obj = getObjRecord();
051: List records = obj.getSubRecords();
052: int cmoIdx = 0;
053: for (int i = 0; i < records.size(); i++) {
054: Object r = records.get(i);
055:
056: if (r instanceof CommonObjectDataSubRecord) {
057: //modify autofill attribute inherited from <code>TextObjectRecord</code>
058: CommonObjectDataSubRecord cmo = (CommonObjectDataSubRecord) r;
059: cmo.setAutofill(false);
060: cmoIdx = i;
061: }
062: }
063: //add NoteStructure sub record
064: //we don't know it's format, for now the record data is empty
065: NoteStructureSubRecord u = new NoteStructureSubRecord();
066: obj.addSubRecord(cmoIdx + 1, u);
067: }
068:
069: /**
070: * Creates the low level <code>NoteRecord</code>
071: * which holds the comment attributes.
072: */
073: private NoteRecord createNoteRecord(HSSFComment shape, int shapeId) {
074: NoteRecord note = new NoteRecord();
075: note.setColumn(shape.getColumn());
076: note.setRow((short) shape.getRow());
077: note.setFlags(shape.isVisible() ? NoteRecord.NOTE_VISIBLE
078: : NoteRecord.NOTE_HIDDEN);
079: note.setShapeId((short) shapeId);
080: note.setAuthor(shape.getAuthor() == null ? "" : shape
081: .getAuthor());
082: return note;
083: }
084:
085: /**
086: * Sets standard escher options for a comment.
087: * This method is responsible for setting default background,
088: * shading and other comment properties.
089: *
090: * @param shape The highlevel shape.
091: * @param opt The escher records holding the proerties
092: * @return number of escher options added
093: */
094: protected int addStandardOptions(HSSFShape shape,
095: EscherOptRecord opt) {
096: super .addStandardOptions(shape, opt);
097:
098: //remove unnecessary properties inherited from TextboxShape
099: java.util.List props = opt.getEscherProperties();
100: for (Iterator iterator = props.iterator(); iterator.hasNext();) {
101: EscherProperty prop = (EscherProperty) iterator.next();
102: switch (prop.getId()) {
103: case EscherProperties.TEXT__TEXTLEFT:
104: case EscherProperties.TEXT__TEXTRIGHT:
105: case EscherProperties.TEXT__TEXTTOP:
106: case EscherProperties.TEXT__TEXTBOTTOM:
107: case EscherProperties.GROUPSHAPE__PRINT:
108: case EscherProperties.FILL__FILLBACKCOLOR:
109: case EscherProperties.LINESTYLE__COLOR:
110: iterator.remove();
111: break;
112: }
113: }
114:
115: HSSFComment comment = (HSSFComment) shape;
116: opt.addEscherProperty(new EscherSimpleProperty(
117: EscherProperties.GROUPSHAPE__PRINT,
118: comment.isVisible() ? 0x000A0000 : 0x000A0002));
119: opt
120: .addEscherProperty(new EscherSimpleProperty(
121: EscherProperties.SHADOWSTYLE__SHADOWOBSURED,
122: 0x00030003));
123: opt.addEscherProperty(new EscherSimpleProperty(
124: EscherProperties.SHADOWSTYLE__COLOR, 0x00000000));
125: opt.sortProperties();
126: return opt.getEscherProperties().size(); // # options added
127: }
128:
129: /**
130: * Return the <code>NoteRecord</code> holding the comment attributes
131: *
132: * @return <code>NoteRecord</code> holding the comment attributes
133: */
134: public NoteRecord getNoteRecord() {
135: return note;
136: }
137:
138: }
|