You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@poi.apache.org by be...@apache.org on 2012/07/12 00:22:24 UTC

svn commit: r1360445 - in /poi/branches/gsoc2012/src: java/org/apache/poi/hssf/usermodel/ testcases/org/apache/poi/hssf/model/ testcases/org/apache/poi/hssf/usermodel/

Author: berlog
Date: Wed Jul 11 22:22:24 2012
New Revision: 1360445

URL: http://svn.apache.org/viewvc?rev=1360445&view=rev
Log:
fixed auto filters(added class HSSFCombobox)

Added:
    poi/branches/gsoc2012/src/java/org/apache/poi/hssf/usermodel/HSSFCombobox.java
Modified:
    poi/branches/gsoc2012/src/java/org/apache/poi/hssf/usermodel/HSSFPatriarch.java
    poi/branches/gsoc2012/src/java/org/apache/poi/hssf/usermodel/HSSFPicture.java
    poi/branches/gsoc2012/src/java/org/apache/poi/hssf/usermodel/HSSFShapeFactory.java
    poi/branches/gsoc2012/src/java/org/apache/poi/hssf/usermodel/HSSFSimpleShape.java
    poi/branches/gsoc2012/src/testcases/org/apache/poi/hssf/model/TestDrawingShapes.java
    poi/branches/gsoc2012/src/testcases/org/apache/poi/hssf/model/TestHSSFAnchor.java
    poi/branches/gsoc2012/src/testcases/org/apache/poi/hssf/usermodel/HSSFTestHelper.java

Added: poi/branches/gsoc2012/src/java/org/apache/poi/hssf/usermodel/HSSFCombobox.java
URL: http://svn.apache.org/viewvc/poi/branches/gsoc2012/src/java/org/apache/poi/hssf/usermodel/HSSFCombobox.java?rev=1360445&view=auto
==============================================================================
--- poi/branches/gsoc2012/src/java/org/apache/poi/hssf/usermodel/HSSFCombobox.java (added)
+++ poi/branches/gsoc2012/src/java/org/apache/poi/hssf/usermodel/HSSFCombobox.java Wed Jul 11 22:22:24 2012
@@ -0,0 +1,77 @@
+package org.apache.poi.hssf.usermodel;
+
+import org.apache.poi.ddf.*;
+import org.apache.poi.hssf.record.*;
+
+/**
+ * @author Evgeniy Berlog
+ * @date 12.07.12
+ */
+public class HSSFCombobox extends HSSFSimpleShape {
+
+    public HSSFCombobox(EscherContainerRecord spContainer, ObjRecord objRecord) {
+        super(spContainer, objRecord);
+    }
+
+    public HSSFCombobox(HSSFShape parent, HSSFAnchor anchor) {
+        super(parent, anchor);
+        super.setShapeType(OBJECT_TYPE_COMBO_BOX);
+    }
+
+    @Override
+    protected EscherContainerRecord createSpContainer() {
+        EscherContainerRecord spContainer = new EscherContainerRecord();
+        EscherSpRecord sp = new EscherSpRecord();
+        EscherOptRecord opt = new EscherOptRecord();
+        EscherClientDataRecord clientData = new EscherClientDataRecord();
+
+        spContainer.setRecordId(EscherContainerRecord.SP_CONTAINER);
+        spContainer.setOptions((short) 0x000F);
+        sp.setRecordId(EscherSpRecord.RECORD_ID);
+        sp.setOptions((short) ((EscherAggregate.ST_HOSTCONTROL << 4) | 0x2));
+
+        sp.setFlags(EscherSpRecord.FLAG_HAVEANCHOR | EscherSpRecord.FLAG_HASSHAPETYPE);
+        opt.setRecordId(EscherOptRecord.RECORD_ID);
+        opt.addEscherProperty(new EscherBoolProperty(EscherProperties.PROTECTION__LOCKAGAINSTGROUPING, 17039620));
+        opt.addEscherProperty(new EscherBoolProperty(EscherProperties.TEXT__SIZE_TEXT_TO_FIT_SHAPE, 0x00080008));
+        opt.addEscherProperty(new EscherBoolProperty(EscherProperties.LINESTYLE__NOLINEDRAWDASH, 0x00080000));
+        opt.addEscherProperty(new EscherSimpleProperty(EscherProperties.GROUPSHAPE__PRINT, 0x00020000));
+
+        HSSFClientAnchor userAnchor = (HSSFClientAnchor) getAnchor();
+        userAnchor.setAnchorType(1);
+        EscherRecord anchor = userAnchor.getEscherAnchor();
+        clientData.setRecordId(EscherClientDataRecord.RECORD_ID);
+        clientData.setOptions((short) 0x0000);
+
+        spContainer.addChildRecord(sp);
+        spContainer.addChildRecord(opt);
+        spContainer.addChildRecord(anchor);
+        spContainer.addChildRecord(clientData);
+
+        return spContainer;
+    }
+
+    @Override
+    protected ObjRecord createObjRecord() {
+        ObjRecord obj = new ObjRecord();
+        CommonObjectDataSubRecord c = new CommonObjectDataSubRecord();
+        c.setObjectType(HSSFSimpleShape.OBJECT_TYPE_COMBO_BOX);
+        c.setLocked(true);
+        c.setPrintable(false);
+        c.setAutofill(true);
+        c.setAutoline(false);
+        FtCblsSubRecord f = new FtCblsSubRecord();
+        LbsDataSubRecord l = LbsDataSubRecord.newAutoFilterInstance();
+        EndSubRecord e = new EndSubRecord();
+        obj.addSubRecord(c);
+        obj.addSubRecord(f);
+        obj.addSubRecord(l);
+        obj.addSubRecord(e);
+        return obj;
+    }
+
+    @Override
+    public void setShapeType(int shapeType) {
+        throw new IllegalStateException("Shape type can not be changed in "+this.getClass().getSimpleName());
+    }
+}

Modified: poi/branches/gsoc2012/src/java/org/apache/poi/hssf/usermodel/HSSFPatriarch.java
URL: http://svn.apache.org/viewvc/poi/branches/gsoc2012/src/java/org/apache/poi/hssf/usermodel/HSSFPatriarch.java?rev=1360445&r1=1360444&r2=1360445&view=diff
==============================================================================
--- poi/branches/gsoc2012/src/java/org/apache/poi/hssf/usermodel/HSSFPatriarch.java (original)
+++ poi/branches/gsoc2012/src/java/org/apache/poi/hssf/usermodel/HSSFPatriarch.java Wed Jul 11 22:22:24 2012
@@ -97,7 +97,6 @@ public final class HSSFPatriarch impleme
     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 impleme
     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 impleme
     {
         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 impleme
     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 impleme
     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 impleme
    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 impleme
      */
      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;
      }
 

Modified: poi/branches/gsoc2012/src/java/org/apache/poi/hssf/usermodel/HSSFPicture.java
URL: http://svn.apache.org/viewvc/poi/branches/gsoc2012/src/java/org/apache/poi/hssf/usermodel/HSSFPicture.java?rev=1360445&r1=1360444&r2=1360445&view=diff
==============================================================================
--- poi/branches/gsoc2012/src/java/org/apache/poi/hssf/usermodel/HSSFPicture.java (original)
+++ poi/branches/gsoc2012/src/java/org/apache/poi/hssf/usermodel/HSSFPicture.java Wed Jul 11 22:22:24 2012
@@ -65,7 +65,7 @@ public final class HSSFPicture extends H
     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 H
             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());
+    }
 }

Modified: poi/branches/gsoc2012/src/java/org/apache/poi/hssf/usermodel/HSSFShapeFactory.java
URL: http://svn.apache.org/viewvc/poi/branches/gsoc2012/src/java/org/apache/poi/hssf/usermodel/HSSFShapeFactory.java?rev=1360445&r1=1360444&r2=1360445&view=diff
==============================================================================
--- poi/branches/gsoc2012/src/java/org/apache/poi/hssf/usermodel/HSSFShapeFactory.java (original)
+++ poi/branches/gsoc2012/src/java/org/apache/poi/hssf/usermodel/HSSFShapeFactory.java Wed Jul 11 22:22:24 2012
@@ -122,6 +122,9 @@ public class HSSFShapeFactory {
                 case CommonObjectDataSubRecord.OBJECT_TYPE_LINE:
                     shape = new HSSFSimpleShape(container, objRecord);
                     break;
+                case CommonObjectDataSubRecord.OBJECT_TYPE_COMBO_BOX:
+                    shape = new HSSFCombobox(container, objRecord);
+                    break;
                 case CommonObjectDataSubRecord.OBJECT_TYPE_MICROSOFT_OFFICE_DRAWING:
                     EscherOptRecord optRecord = container.getChildById(EscherOptRecord.RECORD_ID);
                     EscherProperty property = optRecord.lookup(EscherProperties.GEOMETRY__VERTICES);
@@ -143,19 +146,6 @@ public class HSSFShapeFactory {
             if (null != shape){
                 out.addShape(shape);
             }
-//            if (null != objRecord){
-//                HSSFShape shape = shapeCreator.createNewShape(spRecord.getShapeType(), container, objRecord);
-//                out.addShape(shape);
-//            }
-//            if (null != txtRecord){
-//                //TODO resolve textbox
-////                TextboxShape shape = new TextboxShape(container, txtRecord);
-////                out.a
-//            }
-////
-////            //TODO decide what shape to create based on ObjRecord / EscherSpRecord
-////            HSSFShape shape = new HSSFUnknownShape(container, objRecord);
-////            out.addShape(shape);
         }
     }
 }

Modified: poi/branches/gsoc2012/src/java/org/apache/poi/hssf/usermodel/HSSFSimpleShape.java
URL: http://svn.apache.org/viewvc/poi/branches/gsoc2012/src/java/org/apache/poi/hssf/usermodel/HSSFSimpleShape.java?rev=1360445&r1=1360444&r2=1360445&view=diff
==============================================================================
--- poi/branches/gsoc2012/src/java/org/apache/poi/hssf/usermodel/HSSFSimpleShape.java (original)
+++ poi/branches/gsoc2012/src/java/org/apache/poi/hssf/usermodel/HSSFSimpleShape.java Wed Jul 11 22:22:24 2012
@@ -70,11 +70,12 @@ public class HSSFSimpleShape extends HSS
 
     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 HSS
      * @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);

Modified: poi/branches/gsoc2012/src/testcases/org/apache/poi/hssf/model/TestDrawingShapes.java
URL: http://svn.apache.org/viewvc/poi/branches/gsoc2012/src/testcases/org/apache/poi/hssf/model/TestDrawingShapes.java?rev=1360445&r1=1360444&r2=1360445&view=diff
==============================================================================
--- poi/branches/gsoc2012/src/testcases/org/apache/poi/hssf/model/TestDrawingShapes.java (original)
+++ poi/branches/gsoc2012/src/testcases/org/apache/poi/hssf/model/TestDrawingShapes.java Wed Jul 11 22:22:24 2012
@@ -10,6 +10,7 @@ import org.apache.poi.hssf.usermodel.*;
 import org.apache.poi.util.HexDump;
 
 import java.io.IOException;
+import java.util.Arrays;
 
 import static junit.framework.Assert.assertEquals;
 
@@ -341,4 +342,59 @@ public class TestDrawingShapes extends T
         EscherDgRecord dgRecord = (EscherDgRecord) aggregate.getEscherRecord(0).getChild(0);
         assertEquals(dgRecord.getNumShapes(), 1);
     }
+
+    public void testTextForSimpleShape(){
+        HSSFWorkbook wb = new HSSFWorkbook();
+        HSSFSheet sheet = wb.createSheet();
+        HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
+
+        HSSFSimpleShape shape = patriarch.createSimpleShape(new HSSFClientAnchor());
+        shape.setShapeType(HSSFSimpleShape.OBJECT_TYPE_RECTANGLE);
+
+        EscherAggregate agg = HSSFTestHelper.getEscherAggregate(patriarch);
+        assertNull(shape.getEscherContainer().getChildById(EscherTextboxRecord.RECORD_ID));
+        assertEquals(agg.getShapeToObjMapping().size(), 1);
+
+        wb = HSSFTestDataSamples.writeOutAndReadBack(wb);
+        sheet = wb.getSheetAt(0);
+        patriarch = sheet.getDrawingPatriarch();
+
+        shape = (HSSFSimpleShape) patriarch.getChildren().get(0);
+
+        agg = HSSFTestHelper.getEscherAggregate(patriarch);
+        assertNull(shape.getEscherContainer().getChildById(EscherTextboxRecord.RECORD_ID));
+        assertEquals(agg.getShapeToObjMapping().size(), 1);
+
+        shape.setString(new HSSFRichTextString("string1"));
+        assertEquals(shape.getString().getString(), "string1");
+
+        assertNotNull(shape.getEscherContainer().getChildById(EscherTextboxRecord.RECORD_ID));
+        assertEquals(agg.getShapeToObjMapping().size(), 2);
+
+        wb = HSSFTestDataSamples.writeOutAndReadBack(wb);
+
+        wb = HSSFTestDataSamples.writeOutAndReadBack(wb);
+        sheet = wb.getSheetAt(0);
+        patriarch = sheet.getDrawingPatriarch();
+
+        shape = (HSSFSimpleShape) patriarch.getChildren().get(0);
+
+        assertNotNull(shape.getTextObjectRecord());
+        assertEquals(shape.getString().getString(), "string1");
+        assertNotNull(shape.getEscherContainer().getChildById(EscherTextboxRecord.RECORD_ID));
+        assertEquals(agg.getShapeToObjMapping().size(), 2);
+    }
+
+    public void testComboboxRecords(){
+        HSSFWorkbook wb = new HSSFWorkbook();
+        HSSFSheet sheet = wb.createSheet();
+        HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
+
+        HSSFCombobox combobox = new HSSFCombobox(null, new HSSFClientAnchor());
+        HSSFTestHelper.setShapeId(combobox, 1024);
+        ComboboxShape comboboxShape = new ComboboxShape(combobox, 1024);
+
+        assertTrue(Arrays.equals(comboboxShape.getSpContainer().serialize(), combobox.getEscherContainer().serialize()));
+        assertTrue(Arrays.equals(comboboxShape.getObjRecord().serialize(), combobox.getObjRecord().serialize()));
+    }
 }

Modified: poi/branches/gsoc2012/src/testcases/org/apache/poi/hssf/model/TestHSSFAnchor.java
URL: http://svn.apache.org/viewvc/poi/branches/gsoc2012/src/testcases/org/apache/poi/hssf/model/TestHSSFAnchor.java?rev=1360445&r1=1360444&r2=1360445&view=diff
==============================================================================
--- poi/branches/gsoc2012/src/testcases/org/apache/poi/hssf/model/TestHSSFAnchor.java (original)
+++ poi/branches/gsoc2012/src/testcases/org/apache/poi/hssf/model/TestHSSFAnchor.java Wed Jul 11 22:22:24 2012
@@ -54,11 +54,10 @@ public class TestHSSFAnchor extends Test
         HSSFPatriarch drawing = sheet.getDrawingPatriarch();
 
         HSSFSimpleShape rectangle = (HSSFSimpleShape) drawing.getChildren().get(0);
-        rectangle.setShapeType(HSSFSimpleShape.OBJECT_TYPE_RECTANGLE);
 
         assertEquals(rectangle.getEscherContainer().getChild(0).getRecordId(), EscherSpRecord.RECORD_ID);
         assertEquals(rectangle.getEscherContainer().getChild(1).getRecordId(), EscherOptRecord.RECORD_ID);
-        assertEquals("  " + HexDump.toHex(rectangle.getEscherContainer().getChild(2).getRecordId()) + "    ", rectangle.getEscherContainer().getChild(2).getRecordId(), EscherClientAnchorRecord.RECORD_ID);
+        assertEquals(rectangle.getEscherContainer().getChild(2).getRecordId(), EscherClientAnchorRecord.RECORD_ID);
         assertEquals(rectangle.getEscherContainer().getChild(3).getRecordId(), EscherClientDataRecord.RECORD_ID);
 
         rectangle.setAnchor(new HSSFClientAnchor());

Modified: poi/branches/gsoc2012/src/testcases/org/apache/poi/hssf/usermodel/HSSFTestHelper.java
URL: http://svn.apache.org/viewvc/poi/branches/gsoc2012/src/testcases/org/apache/poi/hssf/usermodel/HSSFTestHelper.java?rev=1360445&r1=1360444&r2=1360445&view=diff
==============================================================================
--- poi/branches/gsoc2012/src/testcases/org/apache/poi/hssf/usermodel/HSSFTestHelper.java (original)
+++ poi/branches/gsoc2012/src/testcases/org/apache/poi/hssf/usermodel/HSSFTestHelper.java Wed Jul 11 22:22:24 2012
@@ -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);
+    }
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org