]> source.dussan.org Git - poi.git/commitdiff
fixed auto filters(added class HSSFCombobox)
authorEvgeniy Berlog <berlog@apache.org>
Wed, 11 Jul 2012 22:22:24 +0000 (22:22 +0000)
committerEvgeniy Berlog <berlog@apache.org>
Wed, 11 Jul 2012 22:22:24 +0000 (22:22 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/branches/gsoc2012@1360445 13f79535-47bb-0310-9956-ffa450edef68

src/java/org/apache/poi/hssf/usermodel/HSSFCombobox.java [new file with mode: 0644]
src/java/org/apache/poi/hssf/usermodel/HSSFPatriarch.java
src/java/org/apache/poi/hssf/usermodel/HSSFPicture.java
src/java/org/apache/poi/hssf/usermodel/HSSFShapeFactory.java
src/java/org/apache/poi/hssf/usermodel/HSSFSimpleShape.java
src/testcases/org/apache/poi/hssf/model/TestDrawingShapes.java
src/testcases/org/apache/poi/hssf/model/TestHSSFAnchor.java
src/testcases/org/apache/poi/hssf/usermodel/HSSFTestHelper.java

diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFCombobox.java b/src/java/org/apache/poi/hssf/usermodel/HSSFCombobox.java
new file mode 100644 (file)
index 0000000..4b13278
--- /dev/null
@@ -0,0 +1,77 @@
+package org.apache.poi.hssf.usermodel;\r
+\r
+import org.apache.poi.ddf.*;\r
+import org.apache.poi.hssf.record.*;\r
+\r
+/**\r
+ * @author Evgeniy Berlog\r
+ * @date 12.07.12\r
+ */\r
+public class HSSFCombobox extends HSSFSimpleShape {\r
+\r
+    public HSSFCombobox(EscherContainerRecord spContainer, ObjRecord objRecord) {\r
+        super(spContainer, objRecord);\r
+    }\r
+\r
+    public HSSFCombobox(HSSFShape parent, HSSFAnchor anchor) {\r
+        super(parent, anchor);\r
+        super.setShapeType(OBJECT_TYPE_COMBO_BOX);\r
+    }\r
+\r
+    @Override\r
+    protected EscherContainerRecord createSpContainer() {\r
+        EscherContainerRecord spContainer = new EscherContainerRecord();\r
+        EscherSpRecord sp = new EscherSpRecord();\r
+        EscherOptRecord opt = new EscherOptRecord();\r
+        EscherClientDataRecord clientData = new EscherClientDataRecord();\r
+\r
+        spContainer.setRecordId(EscherContainerRecord.SP_CONTAINER);\r
+        spContainer.setOptions((short) 0x000F);\r
+        sp.setRecordId(EscherSpRecord.RECORD_ID);\r
+        sp.setOptions((short) ((EscherAggregate.ST_HOSTCONTROL << 4) | 0x2));\r
+\r
+        sp.setFlags(EscherSpRecord.FLAG_HAVEANCHOR | EscherSpRecord.FLAG_HASSHAPETYPE);\r
+        opt.setRecordId(EscherOptRecord.RECORD_ID);\r
+        opt.addEscherProperty(new EscherBoolProperty(EscherProperties.PROTECTION__LOCKAGAINSTGROUPING, 17039620));\r
+        opt.addEscherProperty(new EscherBoolProperty(EscherProperties.TEXT__SIZE_TEXT_TO_FIT_SHAPE, 0x00080008));\r
+        opt.addEscherProperty(new EscherBoolProperty(EscherProperties.LINESTYLE__NOLINEDRAWDASH, 0x00080000));\r
+        opt.addEscherProperty(new EscherSimpleProperty(EscherProperties.GROUPSHAPE__PRINT, 0x00020000));\r
+\r
+        HSSFClientAnchor userAnchor = (HSSFClientAnchor) getAnchor();\r
+        userAnchor.setAnchorType(1);\r
+        EscherRecord anchor = userAnchor.getEscherAnchor();\r
+        clientData.setRecordId(EscherClientDataRecord.RECORD_ID);\r
+        clientData.setOptions((short) 0x0000);\r
+\r
+        spContainer.addChildRecord(sp);\r
+        spContainer.addChildRecord(opt);\r
+        spContainer.addChildRecord(anchor);\r
+        spContainer.addChildRecord(clientData);\r
+\r
+        return spContainer;\r
+    }\r
+\r
+    @Override\r
+    protected ObjRecord createObjRecord() {\r
+        ObjRecord obj = new ObjRecord();\r
+        CommonObjectDataSubRecord c = new CommonObjectDataSubRecord();\r
+        c.setObjectType(HSSFSimpleShape.OBJECT_TYPE_COMBO_BOX);\r
+        c.setLocked(true);\r
+        c.setPrintable(false);\r
+        c.setAutofill(true);\r
+        c.setAutoline(false);\r
+        FtCblsSubRecord f = new FtCblsSubRecord();\r
+        LbsDataSubRecord l = LbsDataSubRecord.newAutoFilterInstance();\r
+        EndSubRecord e = new EndSubRecord();\r
+        obj.addSubRecord(c);\r
+        obj.addSubRecord(f);\r
+        obj.addSubRecord(l);\r
+        obj.addSubRecord(e);\r
+        return obj;\r
+    }\r
+\r
+    @Override\r
+    public void setShapeType(int shapeType) {\r
+        throw new IllegalStateException("Shape type can not be changed in "+this.getClass().getSimpleName());\r
+    }\r
+}\r
index aa8603635bfb8c6d87f9e13b28ccbd7f177b7a34..fbe3f56b681897bbce1bfe18423818025cfec55b 100644 (file)
@@ -97,7 +97,6 @@ public final class HSSFPatriarch implements HSSFShapeContainer, Drawing {
     public HSSFShapeGroup createGroup(HSSFClientAnchor anchor)
     {
         HSSFShapeGroup group = new HSSFShapeGroup(null, anchor);
-        group.anchor = anchor;
         addShape(group);
         onCreate(group);
         return group;
@@ -114,7 +113,6 @@ public final class HSSFPatriarch implements HSSFShapeContainer, Drawing {
     public HSSFSimpleShape createSimpleShape(HSSFClientAnchor anchor)
     {
         HSSFSimpleShape shape = new HSSFSimpleShape(null, anchor);
-        shape.anchor = anchor;
         addShape(shape);
         //open existing file
         onCreate(shape);
@@ -139,7 +137,6 @@ public final class HSSFPatriarch implements HSSFShapeContainer, Drawing {
     {
         HSSFPicture shape = new HSSFPicture(null, anchor);
         shape.setPictureIndex( pictureIndex );
-        shape.anchor = anchor;
         addShape(shape);
         //open existing file
         onCreate(shape);
@@ -169,7 +166,6 @@ public final class HSSFPatriarch implements HSSFShapeContainer, Drawing {
     public HSSFPolygon createPolygon(HSSFClientAnchor anchor)
     {
         HSSFPolygon shape = new HSSFPolygon(null, anchor);
-        shape.anchor = anchor;
         addShape(shape);
         onCreate(shape);
         return shape;
@@ -185,7 +181,6 @@ public final class HSSFPatriarch implements HSSFShapeContainer, Drawing {
     public HSSFTextbox createTextbox(HSSFClientAnchor anchor)
     {
         HSSFTextbox shape = new HSSFTextbox(null, anchor);
-        shape.anchor = anchor;
         addShape(shape);
         onCreate(shape);
         return shape;
@@ -201,7 +196,6 @@ public final class HSSFPatriarch implements HSSFShapeContainer, Drawing {
    public HSSFComment createComment(HSSFAnchor anchor)
     {
         HSSFComment shape = new HSSFComment(null, anchor);
-        shape.anchor = anchor;
         addShape(shape);
         onCreate(shape);
         return shape;
@@ -214,10 +208,9 @@ public final class HSSFPatriarch implements HSSFShapeContainer, Drawing {
      */
      HSSFSimpleShape createComboBox(HSSFAnchor anchor)
      {
-         HSSFSimpleShape shape = new HSSFSimpleShape(null, anchor);
-         shape.setShapeType(HSSFSimpleShape.OBJECT_TYPE_COMBO_BOX);
-         shape.anchor = anchor;
+         HSSFCombobox shape = new HSSFCombobox(null, anchor);
          addShape(shape);
+         onCreate(shape);
          return shape;
      }
 
index 9eff50dd3ed785eaa7f273ca507f0960972027b7..6e5d98cf5565254ff7ab06a474f81b20b3986f61 100644 (file)
@@ -65,7 +65,7 @@ public final class HSSFPicture extends HSSFSimpleShape implements Picture {
     public HSSFPicture( HSSFShape parent, HSSFAnchor anchor )
     {
         super( parent, anchor );
-        setShapeType(OBJECT_TYPE_PICTURE);
+        super.setShapeType(OBJECT_TYPE_PICTURE);
     }
 
     public int getPictureIndex()
@@ -280,4 +280,9 @@ public final class HSSFPicture extends HSSFSimpleShape implements Picture {
             System.out.println("Unsupported encoding: UTF-16LE");
         }
     }
+
+    @Override
+    public void setShapeType(int shapeType) {
+        throw new IllegalStateException("Shape type can not be changed in "+this.getClass().getSimpleName());
+    }
 }
index eab30fcdaabd5b92149dab2ceba3ca26e4f26bb0..4bf927b2a0ff40ae242f30193f1f85bacc9bfb1d 100644 (file)
@@ -122,6 +122,9 @@ public class HSSFShapeFactory {
                 case CommonObjectDataSubRecord.OBJECT_TYPE_LINE:\r
                     shape = new HSSFSimpleShape(container, objRecord);\r
                     break;\r
+                case CommonObjectDataSubRecord.OBJECT_TYPE_COMBO_BOX:\r
+                    shape = new HSSFCombobox(container, objRecord);\r
+                    break;\r
                 case CommonObjectDataSubRecord.OBJECT_TYPE_MICROSOFT_OFFICE_DRAWING:\r
                     EscherOptRecord optRecord = container.getChildById(EscherOptRecord.RECORD_ID);\r
                     EscherProperty property = optRecord.lookup(EscherProperties.GEOMETRY__VERTICES);\r
@@ -143,19 +146,6 @@ public class HSSFShapeFactory {
             if (null != shape){\r
                 out.addShape(shape);\r
             }\r
-//            if (null != objRecord){\r
-//                HSSFShape shape = shapeCreator.createNewShape(spRecord.getShapeType(), container, objRecord);\r
-//                out.addShape(shape);\r
-//            }\r
-//            if (null != txtRecord){\r
-//                //TODO resolve textbox\r
-////                TextboxShape shape = new TextboxShape(container, txtRecord);\r
-////                out.a\r
-//            }\r
-////\r
-////            //TODO decide what shape to create based on ObjRecord / EscherSpRecord\r
-////            HSSFShape shape = new HSSFUnknownShape(container, objRecord);\r
-////            out.addShape(shape);\r
         }\r
     }\r
 }\r
index ac4697ac14978b1b2be415242a3564920e82fbe0..28153d2d45bb6165fc937269702e525552554a12 100644 (file)
@@ -70,11 +70,12 @@ public class HSSFSimpleShape extends HSSFShape
 
     public HSSFSimpleShape(EscherContainerRecord spContainer, ObjRecord objRecord, TextObjectRecord _textObjectRecord) {
         super(spContainer, objRecord);
-        this._textObjectRecord = _textObjectRecord;
+        this._textObjectRecord = _textObjectRecord == null ? createTextObjRecord() : _textObjectRecord;
     }
 
     public HSSFSimpleShape(EscherContainerRecord spContainer, ObjRecord objRecord) {
         super(spContainer, objRecord);
+        this._textObjectRecord = createTextObjRecord();
     }
 
     public HSSFSimpleShape( HSSFShape parent, HSSFAnchor anchor)
@@ -159,6 +160,10 @@ public class HSSFSimpleShape extends HSSFShape
      * @param string Sets the rich text string used by this object.
      */
     public void setString(RichTextString string) {
+        //TODO add other shape types which can not contain text
+        if (getShapeType() == 0 || getShapeType() == OBJECT_TYPE_LINE){
+            throw new IllegalStateException("Cannot set text for shape type: "+getShapeType());
+        }
         HSSFRichTextString rtr = (HSSFRichTextString) string;
         // If font is not set we must set the default one
         if (rtr.numFormattingRuns() == 0) rtr.applyFont((short) 0);
index 06564e94c48f208ceca26413a3b03c7f2143430d..7d5bfaee6d1bc2da8e7f0639288027685dbcc32d 100644 (file)
@@ -10,6 +10,7 @@ import org.apache.poi.hssf.usermodel.*;
 import org.apache.poi.util.HexDump;\r
 \r
 import java.io.IOException;\r
+import java.util.Arrays;\r
 \r
 import static junit.framework.Assert.assertEquals;\r
 \r
@@ -341,4 +342,59 @@ public class TestDrawingShapes extends TestCase {
         EscherDgRecord dgRecord = (EscherDgRecord) aggregate.getEscherRecord(0).getChild(0);\r
         assertEquals(dgRecord.getNumShapes(), 1);\r
     }\r
+\r
+    public void testTextForSimpleShape(){\r
+        HSSFWorkbook wb = new HSSFWorkbook();\r
+        HSSFSheet sheet = wb.createSheet();\r
+        HSSFPatriarch patriarch = sheet.createDrawingPatriarch();\r
+\r
+        HSSFSimpleShape shape = patriarch.createSimpleShape(new HSSFClientAnchor());\r
+        shape.setShapeType(HSSFSimpleShape.OBJECT_TYPE_RECTANGLE);\r
+\r
+        EscherAggregate agg = HSSFTestHelper.getEscherAggregate(patriarch);\r
+        assertNull(shape.getEscherContainer().getChildById(EscherTextboxRecord.RECORD_ID));\r
+        assertEquals(agg.getShapeToObjMapping().size(), 1);\r
+\r
+        wb = HSSFTestDataSamples.writeOutAndReadBack(wb);\r
+        sheet = wb.getSheetAt(0);\r
+        patriarch = sheet.getDrawingPatriarch();\r
+\r
+        shape = (HSSFSimpleShape) patriarch.getChildren().get(0);\r
+\r
+        agg = HSSFTestHelper.getEscherAggregate(patriarch);\r
+        assertNull(shape.getEscherContainer().getChildById(EscherTextboxRecord.RECORD_ID));\r
+        assertEquals(agg.getShapeToObjMapping().size(), 1);\r
+\r
+        shape.setString(new HSSFRichTextString("string1"));\r
+        assertEquals(shape.getString().getString(), "string1");\r
+\r
+        assertNotNull(shape.getEscherContainer().getChildById(EscherTextboxRecord.RECORD_ID));\r
+        assertEquals(agg.getShapeToObjMapping().size(), 2);\r
+\r
+        wb = HSSFTestDataSamples.writeOutAndReadBack(wb);\r
+\r
+        wb = HSSFTestDataSamples.writeOutAndReadBack(wb);\r
+        sheet = wb.getSheetAt(0);\r
+        patriarch = sheet.getDrawingPatriarch();\r
+\r
+        shape = (HSSFSimpleShape) patriarch.getChildren().get(0);\r
+\r
+        assertNotNull(shape.getTextObjectRecord());\r
+        assertEquals(shape.getString().getString(), "string1");\r
+        assertNotNull(shape.getEscherContainer().getChildById(EscherTextboxRecord.RECORD_ID));\r
+        assertEquals(agg.getShapeToObjMapping().size(), 2);\r
+    }\r
+\r
+    public void testComboboxRecords(){\r
+        HSSFWorkbook wb = new HSSFWorkbook();\r
+        HSSFSheet sheet = wb.createSheet();\r
+        HSSFPatriarch patriarch = sheet.createDrawingPatriarch();\r
+\r
+        HSSFCombobox combobox = new HSSFCombobox(null, new HSSFClientAnchor());\r
+        HSSFTestHelper.setShapeId(combobox, 1024);\r
+        ComboboxShape comboboxShape = new ComboboxShape(combobox, 1024);\r
+\r
+        assertTrue(Arrays.equals(comboboxShape.getSpContainer().serialize(), combobox.getEscherContainer().serialize()));\r
+        assertTrue(Arrays.equals(comboboxShape.getObjRecord().serialize(), combobox.getObjRecord().serialize()));\r
+    }\r
 }\r
index 9134332834e0f0c7ba8280c638cfd9c8775d3573..7d42b46375fc2ef00050698ea0f6c39077bf37d8 100644 (file)
@@ -54,11 +54,10 @@ public class TestHSSFAnchor extends TestCase {
         HSSFPatriarch drawing = sheet.getDrawingPatriarch();\r
 \r
         HSSFSimpleShape rectangle = (HSSFSimpleShape) drawing.getChildren().get(0);\r
-        rectangle.setShapeType(HSSFSimpleShape.OBJECT_TYPE_RECTANGLE);\r
 \r
         assertEquals(rectangle.getEscherContainer().getChild(0).getRecordId(), EscherSpRecord.RECORD_ID);\r
         assertEquals(rectangle.getEscherContainer().getChild(1).getRecordId(), EscherOptRecord.RECORD_ID);\r
-        assertEquals("  " + HexDump.toHex(rectangle.getEscherContainer().getChild(2).getRecordId()) + "    ", rectangle.getEscherContainer().getChild(2).getRecordId(), EscherClientAnchorRecord.RECORD_ID);\r
+        assertEquals(rectangle.getEscherContainer().getChild(2).getRecordId(), EscherClientAnchorRecord.RECORD_ID);\r
         assertEquals(rectangle.getEscherContainer().getChild(3).getRecordId(), EscherClientDataRecord.RECORD_ID);\r
 \r
         rectangle.setAnchor(new HSSFClientAnchor());\r
index d5606362c84b55e1de1f679954130172738941b2..50ff1dc50c97843dc64c8751af39ddd06cae2fa2 100644 (file)
@@ -93,11 +93,11 @@ public class HSSFTestHelper {
             method.setAccessible(true);
             method.invoke(new EscherAggregate(new MockDrawingManager()), shape, escherParent, shapeToObj);
         } catch (NoSuchMethodException e) {
-            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
+            e.printStackTrace();
         } catch (InvocationTargetException e) {
-            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
+            e.printStackTrace();
         } catch (IllegalAccessException e) {
-            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
+            e.printStackTrace();
         }
     }
 
@@ -110,9 +110,13 @@ public class HSSFTestHelper {
         } catch (IllegalAccessException e) {
             e.printStackTrace();
         } catch (NoSuchMethodException e) {
-            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
+            e.printStackTrace();
         } catch (InvocationTargetException e) {
-            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
+            e.printStackTrace();
         }
     }
+
+    public static void setShapeId(HSSFShape shape, int id){
+        shape.setShapeId(id);
+    }
 }