]> source.dussan.org Git - poi.git/commitdiff
properly set shapeId for new shapes
authorYegor Kozlov <yegor@apache.org>
Sat, 19 Apr 2008 11:16:53 +0000 (11:16 +0000)
committerYegor Kozlov <yegor@apache.org>
Sat, 19 Apr 2008 11:16:53 +0000 (11:16 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@649797 13f79535-47bb-0310-9956-ffa450edef68

src/scratchpad/src/org/apache/poi/hslf/model/Picture.java
src/scratchpad/src/org/apache/poi/hslf/model/Shape.java
src/scratchpad/src/org/apache/poi/hslf/model/Sheet.java
src/scratchpad/src/org/apache/poi/hslf/model/Table.java
src/scratchpad/src/org/apache/poi/hslf/model/TextShape.java
src/scratchpad/src/org/apache/poi/hslf/record/PPDrawingGroup.java
src/scratchpad/testcases/org/apache/poi/hslf/model/TestShapes.java

index 1e8dbcc253603ae8676552e757c9dc6c6178538d..ed642791f1db67475860de85e49b85b142899db1 100644 (file)
@@ -197,6 +197,7 @@ public class Picture extends SimpleShape {
      * By default set the orininal image size
      */
     protected void afterInsert(Sheet sh){
+        super.afterInsert(sh);
         java.awt.Rectangle anchor = getAnchor();
         if (anchor.equals(new java.awt.Rectangle())){
             setDefaultSize();
index 9ac61dbff17a54b6bb5c85203eaabc828230da85..2aed43f47d2f80453a04c1cb44bf47f10001c6d4 100644 (file)
@@ -18,10 +18,11 @@ package org.apache.poi.hslf.model;
 
 import org.apache.poi.ddf.*;
 import org.apache.poi.hslf.record.ColorSchemeAtom;
+import org.apache.poi.hslf.record.PPDrawing;
 import org.apache.poi.util.POILogger;
 import org.apache.poi.util.POILogFactory;
 
-import java.util.Iterator;
+import java.util.*;
 import java.awt.*;
 import java.awt.geom.Rectangle2D;
 
@@ -324,7 +325,58 @@ public abstract class Shape {
      * @param sh - owning shape
      */
     protected void afterInsert(Sheet sh){
+        PPDrawing ppdrawing = sh.getPPDrawing();
 
+        EscherContainerRecord dgContainer = (EscherContainerRecord) ppdrawing.getEscherRecords()[0];
+
+        EscherDgRecord dg = (EscherDgRecord) Shape.getEscherChild(dgContainer, EscherDgRecord.RECORD_ID);
+
+        int id = allocateShapeId(dg);
+        setShapeId(id);
+    }
+
+    /**
+     * Allocates new shape id for the new drawing group id.
+     *
+     * @param dg  EscherDgRecord of the sheet that owns the shape being created
+     *
+     * @return a new shape id.
+     */
+    protected int allocateShapeId(EscherDgRecord dg)
+    {
+        EscherDggRecord dgg = _sheet.getSlideShow().getDocumentRecord().getPPDrawingGroup().getEscherDggRecord();
+        if(dgg == null){
+            logger.log(POILogger.ERROR, "EscherDggRecord not found");
+            return 0;
+        }
+
+        dgg.setNumShapesSaved( dgg.getNumShapesSaved() + 1 );
+
+        // Add to existing cluster if space available
+        for (int i = 0; i < dgg.getFileIdClusters().length; i++)
+        {
+            EscherDggRecord.FileIdCluster c = dgg.getFileIdClusters()[i];
+            if (c.getDrawingGroupId() == dg.getDrawingGroupId() && c.getNumShapeIdsUsed() != 1024)
+            {
+                int result = c.getNumShapeIdsUsed() + (1024 * (i+1));
+                c.incrementShapeId();
+                dg.setNumShapes( dg.getNumShapes() + 1 );
+                dg.setLastMSOSPID( result );
+                if (result >= dgg.getShapeIdMax())
+                    dgg.setShapeIdMax( result + 1 );
+                return result;
+            }
+        }
+
+        // Create new cluster
+        dgg.addCluster( dg.getDrawingGroupId(), 0 );
+        dgg.getFileIdClusters()[dgg.getFileIdClusters().length-1].incrementShapeId();
+        dg.setNumShapes( dg.getNumShapes() + 1 );
+        int result = (1024 * dgg.getFileIdClusters().length);
+        dg.setLastMSOSPID( result );
+        if (result >= dgg.getShapeIdMax())
+            dgg.setShapeIdMax( result + 1 );
+        return result;
     }
 
     /**
@@ -353,6 +405,24 @@ public abstract class Shape {
         return new Color(tmp.getBlue(), tmp.getGreen(), tmp.getRed(), alpha);
     }
 
+    /**
+     * @return id for the shape.
+     */
+    public int getShapeId(){
+        EscherSpRecord spRecord = _escherContainer.getChildById(EscherSpRecord.RECORD_ID);
+        return spRecord == null ? 0 : spRecord.getShapeId();
+    }
+
+    /**
+     * Sets shape ID
+     *
+     * @param id of the shape
+     */
+    public void setShapeId(int id){
+        EscherSpRecord spRecord = _escherContainer.getChildById(EscherSpRecord.RECORD_ID);
+        if(spRecord != null) spRecord.setShapeId(id);
+    }
+
     /**
      * Fill properties of this shape
      *
index 2a1785bd12e3ac9c2f713049bfba089f8ed2d599..d9c8903d5bb3038ea927defe49aa78bb5218f78d 100644 (file)
@@ -247,14 +247,6 @@ public abstract class Sheet {
         EscherContainerRecord spgr = (EscherContainerRecord) Shape.getEscherChild(dgContainer, EscherContainerRecord.SPGR_CONTAINER);
         spgr.addChildRecord(shape.getSpContainer());
 
-        EscherDgRecord dg = (EscherDgRecord) Shape.getEscherChild(dgContainer, EscherDgRecord.RECORD_ID);
-        dg.setNumShapes(dg.getNumShapes() + 1);
-
-        int shapeId = dg.getLastMSOSPID()+1;
-        dg.setLastMSOSPID(shapeId);
-
-        EscherSpRecord sp = shape.getSpContainer().getChildById(EscherSpRecord.RECORD_ID);
-        if(sp != null) sp.setShapeId(shapeId);
         shape.setSheet(this);
         shape.afterInsert(this);
 
index dd4a6a33f047c091961765424e52411ffaa0666f..43dab58af129ef7f5bafe6354778ede79df27796 100755 (executable)
@@ -112,6 +112,8 @@ public class Table extends ShapeGroup {
     }\r
 \r
     protected void afterInsert(Sheet sh){\r
+        super.afterInsert(sh);\r
+\r
         EscherContainerRecord spCont = (EscherContainerRecord) getSpContainer().getChild(0);\r
         List lst = spCont.getChildRecords();\r
         EscherOptRecord opt = (EscherOptRecord)lst.get(lst.size()-2);\r
index 24c3126376b7a70d1878f98bb18700cb49998f0c..7249817bed3b342d5bca22f6e58649c9323189d4 100755 (executable)
@@ -184,6 +184,8 @@ public abstract class TextShape extends SimpleShape {
      * @param sh the sheet we are adding to\r
      */\r
     protected void afterInsert(Sheet sh){\r
+        super.afterInsert(sh);\r
+\r
         EscherTextboxWrapper _txtbox = getEscherTextboxWrapper();\r
         if(_txtbox != null){\r
             PPDrawing ppdrawing = sh.getPPDrawing();\r
index 8d25ce2140ded6d2edfea3cde25e8cbb6f2921a1..c7de1fccd7ad1bc8d2803e9e15d552d76b482902 100644 (file)
@@ -37,6 +37,8 @@ public class PPDrawingGroup extends RecordAtom {
 
     private byte[] _header;
     private EscherContainerRecord dggContainer;
+    //cached dgg
+    private EscherDggRecord dgg;
 
     protected PPDrawingGroup(byte[] source, int start, int len) {
         // Get the header
@@ -116,4 +118,17 @@ public class PPDrawingGroup extends RecordAtom {
     public EscherContainerRecord getDggContainer(){
         return dggContainer;
     }
+
+    public EscherDggRecord getEscherDggRecord(){
+        if(dgg == null){
+            for(Iterator it = dggContainer.getChildRecords().iterator(); it.hasNext();){
+                EscherRecord r = (EscherRecord) it.next();
+                if(r instanceof EscherDggRecord){
+                    dgg = (EscherDggRecord)r;
+                    break;
+                }
+            }
+        }
+        return dgg;
+    }
 }
index c2e081ee5caa407782fe441800a0f7ba6a040a99..511ef7f7453fc6d27317c0da4ae3f09610b05caf 100644 (file)
@@ -307,4 +307,22 @@ public class TestShapes extends TestCase {
         sl = ppt.getSlides()[0];
         assertEquals("expected 0 shaped in " + file, 0, sl.getShapes().length);
     }
+
+    public void testShapeId() throws IOException {
+        SlideShow ppt = new SlideShow();
+        Slide slide = ppt.createSlide();
+        Shape shape;
+
+        shape = new Line();
+        assertEquals(0, shape.getShapeId());
+        slide.addShape(shape);
+        assertTrue(shape.getShapeId() > 0);
+
+        int shapeId = shape.getShapeId();
+
+        shape = new Line();
+        assertEquals(0, shape.getShapeId());
+        slide.addShape(shape);
+        assertEquals(shapeId + 1, shape.getShapeId());
+    }
 }