* Every HSSFComment shape has a link to a NoteRecord from the tailRec collection.
*/
public Map<Integer, NoteRecord> getTailRecords() {
- return tailRec;
+ return Collections.unmodifiableMap(tailRec);
}
/**
package org.apache.poi.hssf.usermodel;
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.Iterator;
-import java.util.List;
+import java.util.*;
import org.apache.poi.ddf.*;
import org.apache.poi.hssf.model.DrawingManager2;
import org.apache.poi.hssf.record.EscherAggregate;
+import org.apache.poi.hssf.record.NoteRecord;
+import org.apache.poi.hssf.util.CellReference;
import org.apache.poi.ss.usermodel.Chart;
+import org.apache.poi.util.POILogFactory;
+import org.apache.poi.util.POILogger;
import org.apache.poi.util.StringUtil;
import org.apache.poi.util.Internal;
import org.apache.poi.ss.usermodel.Drawing;
* @author Glen Stampoultzis (glens at apache.org)
*/
public final class HSSFPatriarch implements HSSFShapeContainer, Drawing {
+ private static POILogger log = POILogFactory.getLogger(HSSFPatriarch.class);
private final List<HSSFShape> _shapes = new ArrayList<HSSFShape>();
private final EscherSpgrRecord _spgrRecord;
* Creates the patriarch.
*
* @param sheet the sheet this patriarch is stored in.
+ * @param boundAggregate -low level representation of all binary data inside sheet
*/
HSSFPatriarch(HSSFSheet sheet, EscherAggregate boundAggregate) {
_sheet = sheet;
return newPatriarch;
}
+ /**
+ * check if any shapes contain wrong data
+ * At now(13.08.2010) check if patriarch contains 2 or more comments with same coordinates
+ */
+ protected void preSerialize(){
+ Map<Integer, NoteRecord> tailRecords = _boundAggregate.getTailRecords();
+ /**
+ * contains coordinates of comments we iterate over
+ */
+ Set<String> coordinates = new HashSet<String>(tailRecords.size());
+ for(NoteRecord rec : tailRecords.values()){
+ String noteRef = new CellReference(rec.getRow(),
+ rec.getColumn()).formatAsString(); // A1-style notation
+ if(coordinates.contains(noteRef )){
+ throw new IllegalStateException("found multiple cell comments for cell " + noteRef );
+ } else {
+ coordinates.add(noteRef);
+ }
+ }
+ }
+
/**
* @param shape to be removed
* @return true of shape is removed
*
* @param anchor the client anchor describes how this group is attached
* to the sheet.
+ * @param pictureIndex - pointer to the byte array saved inside workbook in escher bse record
* @return the newly created shape.
*/
public HSSFPicture createPicture(HSSFClientAnchor anchor, int pictureIndex) {
/**
* Returns the aggregate escher record we're bound to
+ * @return - low level representation of sheet drawing data
*/
protected EscherAggregate _getBoundAggregate() {
return _boundAggregate;
for (int i = 0; i < spgrChildren.size(); i++) {
EscherContainerRecord spContainer = spgrChildren.get(i);
- if (i == 0) {
- continue;
- } else {
+ if (i != 0) {
HSSFShapeFactory.createShapeTree(spContainer, _boundAggregate, this, _sheet.getWorkbook().getRootDirectory());
}
}
{
if(height == -1){
row.setHeight((short)(0xFF | 0x8000));
+ row.setBadFontHeight(false);
} else {
row.setBadFontHeight(true);
row.setHeight(height);
return sheet;
}
+ /**
+ * check whether the data of sheet can be serialized
+ */
+ protected void preSerialize(){
+ if (_patriarch != null){
+ _patriarch.preSerialize();
+ }
+ }
+
/**
* Return the parent workbook
*
HSSFRow row = new HSSFRow(_workbook, this, rownum);
// new rows inherit default height from the sheet
row.setHeight(getDefaultRowHeight());
+ row.getRowRecord().setBadFontHeight(false);
addRow(row, true);
return row;
// serialization is about to occur.
for (int i = 0; i < nSheets; i++) {
sheets[i].getSheet().preSerialize();
+ sheets[i].preSerialize();
}
int totalsize = workbook.getSize();
int idx = wb.addPicture(new byte[]{1,2,3}, HSSFWorkbook.PICTURE_TYPE_PNG);\r
\r
HSSFComment comment = patriarch.createCellComment(new HSSFClientAnchor());\r
+ comment.setColumn(5);\r
comment.setString(new HSSFRichTextString("comment1"));\r
comment = patriarch.createCellComment(new HSSFClientAnchor(0,0,100,100,(short)0,0,(short)10,10));\r
+ comment.setRow(5);\r
comment.setString(new HSSFRichTextString("comment2"));\r
comment.setBackgroundImage(idx);\r
assertEquals(comment.getBackgroundImageId(), idx);\r
assertEquals(comment.getShapeId(), 2024);\r
assertEquals(comment.getNoteRecord().getShapeId(), 1000);\r
}\r
+ \r
+ public void testAttemptToSave2CommentsWithSameCoordinates(){\r
+ Object err = null;\r
+ \r
+ HSSFWorkbook wb = new HSSFWorkbook();\r
+ HSSFSheet sh = wb.createSheet();\r
+ HSSFPatriarch patriarch = sh.createDrawingPatriarch();\r
+ patriarch.createCellComment(new HSSFClientAnchor());\r
+ patriarch.createCellComment(new HSSFClientAnchor());\r
+ \r
+ try{\r
+ HSSFTestDataSamples.writeOutAndReadBack(wb);\r
+ } catch (IllegalStateException e){\r
+ err = 1;\r
+ assertEquals(e.getMessage(), "found multiple cell comments for cell $A$1");\r
+ }\r
+ assertNotNull(err);\r
+ }\r
}\r
assertEquals(2, row.getFirstCellNum());
assertEquals(6, row.getLastCellNum());
}
+
+ public void testRowHeight(){
+ HSSFWorkbook workbook = new HSSFWorkbook();
+ HSSFSheet sheet = workbook.createSheet();
+ HSSFRow row = sheet.createRow(0);
+
+ assertEquals(row.getHeight(), sheet.getDefaultRowHeight());
+ assertEquals(row.getRowRecord().getBadFontHeight(), false);
+
+ row.setHeight((short) 123);
+ assertEquals(row.getHeight(), 123);
+ assertEquals(row.getRowRecord().getBadFontHeight(), true);
+
+ row.setHeight((short) -1);
+ assertEquals(row.getHeight(), sheet.getDefaultRowHeight());
+ assertEquals(row.getRowRecord().getBadFontHeight(), false);
+ }
}