]> source.dussan.org Git - poi.git/commitdiff
added common interface for containers of shapes in XSLF
authorYegor Kozlov <yegor@apache.org>
Sat, 14 Jan 2012 11:10:46 +0000 (11:10 +0000)
committerYegor Kozlov <yegor@apache.org>
Sat, 14 Jan 2012 11:10:46 +0000 (11:10 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1231481 13f79535-47bb-0310-9956-ffa450edef68

src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFGroupShape.java
src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFShapeContainer.java [new file with mode: 0644]
src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFSheet.java
src/ooxml/testcases/org/apache/poi/xslf/usermodel/TestXSLFShapeContainer.java [new file with mode: 0644]

index 920cf8beaf256ec62e50be76a8e8b7b02f8791b5..2ad699b408cc1e7ca8c77006413865b5b7c36132 100644 (file)
@@ -25,7 +25,6 @@ import org.apache.poi.openxml4j.opc.TargetMode;
 import org.apache.poi.util.Beta;\r
 import org.apache.poi.util.Units;\r
 import org.apache.xmlbeans.XmlObject;\r
-import org.openxmlformats.schemas.drawingml.x2006.main.CTBlip;\r
 import org.openxmlformats.schemas.drawingml.x2006.main.CTGroupShapeProperties;\r
 import org.openxmlformats.schemas.drawingml.x2006.main.CTGroupTransform2D;\r
 import org.openxmlformats.schemas.drawingml.x2006.main.CTNonVisualDrawingProps;\r
@@ -34,12 +33,12 @@ import org.openxmlformats.schemas.drawingml.x2006.main.CTPositiveSize2D;
 import org.openxmlformats.schemas.presentationml.x2006.main.CTConnector;\r
 import org.openxmlformats.schemas.presentationml.x2006.main.CTGroupShape;\r
 import org.openxmlformats.schemas.presentationml.x2006.main.CTGroupShapeNonVisual;\r
-import org.openxmlformats.schemas.presentationml.x2006.main.CTPicture;\r
 import org.openxmlformats.schemas.presentationml.x2006.main.CTShape;\r
 \r
 import java.awt.Graphics2D;\r
 import java.awt.geom.AffineTransform;\r
 import java.awt.geom.Rectangle2D;\r
+import java.util.Iterator;\r
 import java.util.List;\r
 import java.util.regex.Pattern;\r
 \r
@@ -49,7 +48,7 @@ import java.util.regex.Pattern;
  * @author Yegor Kozlov\r
  */\r
 @Beta\r
-public class XSLFGroupShape extends XSLFShape {\r
+public class XSLFGroupShape extends XSLFShape implements XSLFShapeContainer {\r
     private final CTGroupShape _shape;\r
     private final XSLFSheet _sheet;\r
     private final List<XSLFShape> _shapes;\r
@@ -145,6 +144,15 @@ public class XSLFGroupShape extends XSLFShape {
         return _shapes.toArray(new XSLFShape[_shapes.size()]);\r
     }\r
 \r
+    /**\r
+     * Returns an iterator over the shapes in this sheet\r
+     *\r
+     * @return an iterator over the shapes in this sheet\r
+     */\r
+    public Iterator<XSLFShape> iterator(){\r
+        return _shapes.iterator();\r
+    }\r
+\r
     /**\r
      * Remove the specified shape from this group\r
      */\r
@@ -325,4 +333,14 @@ public class XSLFGroupShape extends XSLFShape {
         }\r
     }\r
 \r
+    /**\r
+     * Removes all of the elements from this container (optional operation).\r
+     * The container will be empty after this call returns.\r
+     */\r
+    public void clear() {\r
+        for(XSLFShape shape : getShapes()){\r
+            removeShape(shape);\r
+        }\r
+    }\r
+\r
 }
\ No newline at end of file
diff --git a/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFShapeContainer.java b/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFShapeContainer.java
new file mode 100644 (file)
index 0000000..bcb6afb
--- /dev/null
@@ -0,0 +1,87 @@
+/*
+ *  ====================================================================
+ *    Licensed to the Apache Software Foundation (ASF) under one or more
+ *    contributor license agreements.  See the NOTICE file distributed with
+ *    this work for additional information regarding copyright ownership.
+ *    The ASF licenses this file to You under the Apache License, Version 2.0
+ *    (the "License"); you may not use this file except in compliance with
+ *    the License.  You may obtain a copy of the License at
+ *
+ *        http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *    Unless required by applicable law or agreed to in writing, software
+ *    distributed under the License is distributed on an "AS IS" BASIS,
+ *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *    See the License for the specific language governing permissions and
+ *    limitations under the License.
+ * ====================================================================
+ */
+
+package org.apache.poi.xslf.usermodel;
+
+/**
+ * Common interface for shape containers, e.g. sheets or groups of shapes
+ */
+public interface XSLFShapeContainer extends Iterable<XSLFShape> {
+
+    /**
+     * create a new shape with a predefined geometry and add it to this shape container
+     */
+    XSLFAutoShape createAutoShape();
+
+    /**
+     * create a new shape with a custom geometry
+     */
+    XSLFFreeformShape createFreeform();
+
+    /**
+     * create a text box
+     */
+    XSLFTextBox createTextBox();
+
+    /**
+     *
+     * create a connector
+     */
+    XSLFConnectorShape createConnector();
+
+    /**
+     * create a group of shapes belonging to this container
+     */
+    XSLFGroupShape createGroup();
+
+    /**
+     * create a picture belonging to this container
+     *
+     * @param pictureIndex
+     * @return
+     */
+    XSLFPictureShape createPicture(int pictureIndex);
+
+    /**
+     * Returns an array containing all of the elements in this container in proper
+     * sequence (from first to last element).
+     *
+     * @return an array containing all of the elements in this container in proper
+     *         sequence
+     */
+    XSLFShape[] getShapes();
+
+    /**
+     * Removes the specified shape from this sheet, if it is present
+     * (optional operation).  If this sheet does not contain the element,
+     * it is unchanged.
+     *
+     * @param xShape shape to be removed from this sheet, if present
+     * @return <tt>true</tt> if this sheet contained the specified element
+     * @throws IllegalArgumentException if the type of the specified shape
+     *         is incompatible with this sheet (optional)
+     */
+    boolean removeShape(XSLFShape xShape) ;
+
+    /**
+     * Removes all of the elements from this container (optional operation).
+     * The container will be empty after this call returns.
+     */
+    void clear();
+}
index 0c64947b9c6f28cabd96600a4afd38079ac16959..63d3e8f37cc6b6675ff01c25fa223cc148b4ce0d 100644 (file)
@@ -48,7 +48,7 @@ import java.util.Map;
 import java.util.regex.Pattern;
 
 @Beta
-public abstract class XSLFSheet extends POIXMLDocumentPart implements Iterable<XSLFShape> {
+public abstract class XSLFSheet extends POIXMLDocumentPart implements XSLFShapeContainer {
     private XSLFCommonSlideData _commonSlideData;
     private XSLFDrawing _drawing;
     private List<XSLFShape> _shapes;
@@ -241,6 +241,16 @@ public abstract class XSLFSheet extends POIXMLDocumentPart implements Iterable<X
         return getShapeList().remove(xShape);
     }
 
+    /**
+     * Removes all of the elements from this container (optional operation).
+     * The container will be empty after this call returns.
+     */
+    public void clear() {
+        for(XSLFShape shape : getShapes()){
+            removeShape(shape);
+        }
+    }
+
     protected abstract String getRootElementName();
 
     protected CTGroupShape getSpTree(){
diff --git a/src/ooxml/testcases/org/apache/poi/xslf/usermodel/TestXSLFShapeContainer.java b/src/ooxml/testcases/org/apache/poi/xslf/usermodel/TestXSLFShapeContainer.java
new file mode 100644 (file)
index 0000000..8de25d3
--- /dev/null
@@ -0,0 +1,64 @@
+/*\r
+ *  ====================================================================\r
+ *    Licensed to the Apache Software Foundation (ASF) under one or more\r
+ *    contributor license agreements.  See the NOTICE file distributed with\r
+ *    this work for additional information regarding copyright ownership.\r
+ *    The ASF licenses this file to You under the Apache License, Version 2.0\r
+ *    (the "License"); you may not use this file except in compliance with\r
+ *    the License.  You may obtain a copy of the License at\r
+ *\r
+ *        http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ *    Unless required by applicable law or agreed to in writing, software\r
+ *    distributed under the License is distributed on an "AS IS" BASIS,\r
+ *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ *    See the License for the specific language governing permissions and\r
+ *    limitations under the License.\r
+ * ====================================================================\r
+ */\r
+package org.apache.poi.xslf.usermodel;\r
+\r
+import junit.framework.TestCase;\r
+import org.apache.poi.xslf.XSLFTestDataSamples;\r
+\r
+import java.awt.*;\r
+import java.awt.geom.Rectangle2D;\r
+\r
+/**\r
+ * test common operatrions on containers of shapes (sheets and groups of shapes)\r
+ *\r
+ * @author Yegor Kozlov\r
+ */\r
+public class TestXSLFShapeContainer extends TestCase {\r
+\r
+    public void verifyContainer(XSLFShapeContainer container) {\r
+        container.clear();\r
+        assertEquals(0, container.getShapes().length);\r
+\r
+        XSLFGroupShape shape1 = container.createGroup();\r
+        assertEquals(1, container.getShapes().length);\r
+\r
+        XSLFTextBox shape2 = container.createTextBox();\r
+        assertEquals(2, container.getShapes().length);\r
+\r
+        XSLFAutoShape shape3 = container.createAutoShape();\r
+        assertEquals(3, container.getShapes().length);\r
+\r
+        XSLFConnectorShape shape4 = container.createConnector();\r
+        assertEquals(4, container.getShapes().length);\r
+\r
+        container.clear();\r
+        assertEquals(0, container.getShapes().length);\r
+    }\r
+\r
+    public void testSheet() {\r
+        XMLSlideShow ppt = new XMLSlideShow();\r
+        XSLFSheet sheet = ppt.createSlide();\r
+        verifyContainer(sheet);\r
+\r
+\r
+        XSLFGroupShape group = sheet.createGroup();\r
+        verifyContainer(group);\r
+\r
+    }\r
+}
\ No newline at end of file