<changes>
<release version="3.9-beta1" date="2012-??-??">
+ <action dev="poi-developers" type="fix">53568 - Set shapes anchors in XSSF when reading from existing drawings</action>
<action dev="poi-developers" type="add">HSSFOptimiser will now also tidy away un-used cell styles, in addition to duplicate styles</action>
<action dev="poi-developers" type="fix">53493 - Fixed memory and temporary file leak in SXSSF </action>
<action dev="poi-developers" type="fix">53780 - Fixed memory and temporary file leak in SXSSF </action>
if (o == null || !(o instanceof XSSFClientAnchor)) return false;
XSSFClientAnchor anchor = (XSSFClientAnchor) o;
- return cell1.toString().equals(anchor.getFrom().toString()) &&
- cell2.toString().equals(anchor.getTo().toString()) ;
+ return getDx1() == anchor.getDx1() &&
+ getDx2() == anchor.getDx2() &&
+ getDy1() == anchor.getDy1() &&
+ getDy2() == anchor.getDy2() &&
+ getCol1() == anchor.getCol1() &&
+ getCol2() == anchor.getCol2() &&
+ getRow1() == anchor.getRow1() &&
+ getRow2() == anchor.getRow2() ;
}
import org.apache.poi.ss.usermodel.Drawing;
import org.apache.poi.util.Internal;
import org.apache.poi.xssf.model.CommentsTable;
+import org.apache.xmlbeans.XmlCursor;
import org.apache.xmlbeans.XmlException;
import org.apache.xmlbeans.XmlObject;
import org.apache.xmlbeans.XmlOptions;
public List<XSSFShape> getShapes(){
List<XSSFShape> lst = new ArrayList<XSSFShape>();
for(XmlObject obj : drawing.selectPath("./*/*")) {
- if(obj instanceof CTPicture) lst.add(new XSSFPicture(this, (CTPicture)obj)) ;
- else if(obj instanceof CTConnector) lst.add(new XSSFConnector(this, (CTConnector)obj)) ;
- else if(obj instanceof CTShape) lst.add(new XSSFSimpleShape(this, (CTShape)obj)) ;
- else if(obj instanceof CTGraphicalObjectFrame) lst.add(new XSSFGraphicFrame(this, (CTGraphicalObjectFrame)obj)) ;
- else if(obj instanceof CTGroupShape) lst.add(new XSSFShapeGroup(this, (CTGroupShape)obj)) ;
+ XSSFShape shape = null;
+ if(obj instanceof CTPicture) shape = new XSSFPicture(this, (CTPicture)obj) ;
+ else if(obj instanceof CTConnector) shape = new XSSFConnector(this, (CTConnector)obj) ;
+ else if(obj instanceof CTShape) shape = new XSSFSimpleShape(this, (CTShape)obj) ;
+ else if(obj instanceof CTGraphicalObjectFrame) shape = new XSSFGraphicFrame(this, (CTGraphicalObjectFrame)obj) ;
+ else if(obj instanceof CTGroupShape) shape = new XSSFShapeGroup(this, (CTGroupShape)obj) ;
+
+ if(shape != null){
+ shape.anchor = getAnchorFromParent(obj);
+ lst.add(shape);
+ }
}
return lst;
}
+
+
+ private XSSFAnchor getAnchorFromParent(XmlObject obj){
+ XSSFAnchor anchor = null;
+
+ XmlObject parentXbean = null;
+ XmlCursor cursor = obj.newCursor();
+ if(cursor.toParent()) parentXbean = cursor.getObject();
+ cursor.dispose();
+ if(parentXbean != null){
+ if (parentXbean instanceof CTTwoCellAnchor) {
+ CTTwoCellAnchor ct = (CTTwoCellAnchor)parentXbean;
+ anchor = new XSSFClientAnchor(ct.getFrom(), ct.getTo());
+ } else if (parentXbean instanceof CTOneCellAnchor) {
+ CTOneCellAnchor ct = (CTOneCellAnchor)parentXbean;
+ anchor = new XSSFClientAnchor(ct.getFrom(), CTMarker.Factory.newInstance());
+ }
+ }
+ return anchor;
+ }
+
}
assertTrue(shapes.get(4) instanceof XSSFSimpleShape);
assertTrue(shapes.get(5) instanceof XSSFPicture);
+ for(XSSFShape sh : shapes) assertNotNull(sh.getAnchor());
+
}
public void testNew() throws Exception {
rPr.getSolidFill().getSrgbClr().getVal()));
}
+
+ /**
+ * test that anchor is not null when reading shapes from existing drawings
+ */
+ public void testReadAnchors(){
+ XSSFWorkbook wb = new XSSFWorkbook();
+ XSSFSheet sheet = wb.createSheet();
+ XSSFDrawing drawing = sheet.createDrawingPatriarch();
+
+ XSSFClientAnchor anchor1 = new XSSFClientAnchor(0, 0, 0, 0, 2, 2, 3, 4);
+ XSSFShape shape1 = drawing.createTextbox(anchor1);
+
+ XSSFClientAnchor anchor2 = new XSSFClientAnchor(0, 0, 0, 0, 2, 2, 3, 5);
+ XSSFShape shape2 = drawing.createTextbox(anchor2);
+
+ int pictureIndex= wb.addPicture(new byte[]{}, XSSFWorkbook.PICTURE_TYPE_PNG);
+ XSSFClientAnchor anchor3 = new XSSFClientAnchor(0, 0, 0, 0, 2, 2, 3, 6);
+ XSSFShape shape3 = drawing.createPicture(anchor3, pictureIndex);
+
+ wb = XSSFTestDataSamples.writeOutAndReadBack(wb);
+ sheet = wb.getSheetAt(0);
+ drawing = sheet.createDrawingPatriarch();
+ List<XSSFShape> shapes = drawing.getShapes();
+ assertEquals(shapes.get(0).getAnchor(), anchor1);
+ assertEquals(shapes.get(1).getAnchor(), anchor2);
+ assertEquals(shapes.get(2).getAnchor(), anchor3);
+
+
+ }
}