2011-08-11 04:38:19 -04:00
|
|
|
/*
|
|
|
|
* ====================================================================
|
|
|
|
* 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;
|
|
|
|
|
2015-02-21 05:56:03 -05:00
|
|
|
import java.awt.geom.Rectangle2D;
|
|
|
|
import java.util.Iterator;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.regex.Pattern;
|
|
|
|
|
2015-03-19 19:44:23 -04:00
|
|
|
import org.apache.poi.openxml4j.opc.*;
|
|
|
|
import org.apache.poi.sl.usermodel.PlaceableShape;
|
|
|
|
import org.apache.poi.sl.usermodel.ShapeGroup;
|
2015-04-27 16:13:43 -04:00
|
|
|
import org.apache.poi.util.*;
|
2011-08-11 04:38:19 -04:00
|
|
|
import org.apache.xmlbeans.XmlObject;
|
2015-03-07 18:35:40 -05:00
|
|
|
import org.openxmlformats.schemas.drawingml.x2006.main.*;
|
2015-03-19 19:44:23 -04:00
|
|
|
import org.openxmlformats.schemas.presentationml.x2006.main.*;
|
2011-08-11 04:38:19 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Represents a group shape that consists of many shapes grouped together.
|
|
|
|
*
|
|
|
|
* @author Yegor Kozlov
|
|
|
|
*/
|
|
|
|
@Beta
|
2015-03-19 19:44:23 -04:00
|
|
|
public class XSLFGroupShape extends XSLFShape implements XSLFShapeContainer, ShapeGroup<XSLFShape> {
|
2015-04-27 16:13:43 -04:00
|
|
|
private static POILogger _logger = POILogFactory.getLogger(XSLFGroupShape.class);
|
|
|
|
|
2011-08-11 04:38:19 -04:00
|
|
|
private final List<XSLFShape> _shapes;
|
2015-03-07 18:35:40 -05:00
|
|
|
private final CTGroupShapeProperties _grpSpPr;
|
2011-08-11 04:38:19 -04:00
|
|
|
private XSLFDrawing _drawing;
|
|
|
|
|
2015-03-07 18:35:40 -05:00
|
|
|
protected XSLFGroupShape(CTGroupShape shape, XSLFSheet sheet){
|
|
|
|
super(shape,sheet);
|
|
|
|
_shapes = sheet.buildShapes(shape);
|
|
|
|
_grpSpPr = shape.getGrpSpPr();
|
2011-08-11 04:38:19 -04:00
|
|
|
}
|
|
|
|
|
2015-03-07 18:35:40 -05:00
|
|
|
protected CTGroupShapeProperties getGrpSpPr() {
|
|
|
|
return _grpSpPr;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected CTGroupTransform2D getSafeXfrm() {
|
|
|
|
CTGroupTransform2D xfrm = getXfrm();
|
|
|
|
return (xfrm == null ? getGrpSpPr().addNewXfrm() : xfrm);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected CTGroupTransform2D getXfrm() {
|
|
|
|
return getGrpSpPr().getXfrm();
|
2011-08-11 04:38:19 -04:00
|
|
|
}
|
|
|
|
|
2011-11-07 04:12:16 -05:00
|
|
|
@Override
|
2011-08-11 04:38:19 -04:00
|
|
|
public Rectangle2D getAnchor(){
|
2015-03-07 18:35:40 -05:00
|
|
|
CTGroupTransform2D xfrm = getXfrm();
|
2011-08-11 04:38:19 -04:00
|
|
|
CTPoint2D off = xfrm.getOff();
|
|
|
|
long x = off.getX();
|
|
|
|
long y = off.getY();
|
|
|
|
CTPositiveSize2D ext = xfrm.getExt();
|
|
|
|
long cx = ext.getCx();
|
|
|
|
long cy = ext.getCy();
|
|
|
|
return new Rectangle2D.Double(
|
|
|
|
Units.toPoints(x), Units.toPoints(y),
|
|
|
|
Units.toPoints(cx), Units.toPoints(cy));
|
|
|
|
}
|
|
|
|
|
2011-11-07 04:12:16 -05:00
|
|
|
@Override
|
2011-08-11 04:38:19 -04:00
|
|
|
public void setAnchor(Rectangle2D anchor){
|
2015-03-07 18:35:40 -05:00
|
|
|
CTGroupTransform2D xfrm = getSafeXfrm();
|
2011-08-11 04:38:19 -04:00
|
|
|
CTPoint2D off = xfrm.isSetOff() ? xfrm.getOff() : xfrm.addNewOff();
|
|
|
|
long x = Units.toEMU(anchor.getX());
|
|
|
|
long y = Units.toEMU(anchor.getY());
|
|
|
|
off.setX(x);
|
|
|
|
off.setY(y);
|
|
|
|
CTPositiveSize2D ext = xfrm.isSetExt() ? xfrm.getExt() : xfrm.addNewExt();
|
|
|
|
long cx = Units.toEMU(anchor.getWidth());
|
|
|
|
long cy = Units.toEMU(anchor.getHeight());
|
|
|
|
ext.setCx(cx);
|
|
|
|
ext.setCy(cy);
|
|
|
|
}
|
|
|
|
|
2011-11-07 04:12:16 -05:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @return the coordinates of the child extents rectangle
|
|
|
|
* used for calculations of grouping, scaling, and rotation
|
|
|
|
* behavior of shapes placed within a group.
|
|
|
|
*/
|
2011-08-11 04:38:19 -04:00
|
|
|
public Rectangle2D getInteriorAnchor(){
|
2015-03-07 18:35:40 -05:00
|
|
|
CTGroupTransform2D xfrm = getXfrm();
|
2011-08-11 04:38:19 -04:00
|
|
|
CTPoint2D off = xfrm.getChOff();
|
|
|
|
long x = off.getX();
|
|
|
|
long y = off.getY();
|
|
|
|
CTPositiveSize2D ext = xfrm.getChExt();
|
|
|
|
long cx = ext.getCx();
|
|
|
|
long cy = ext.getCy();
|
|
|
|
return new Rectangle2D.Double(
|
|
|
|
Units.toPoints(x), Units.toPoints(y),
|
|
|
|
Units.toPoints(cx), Units.toPoints(cy));
|
|
|
|
}
|
|
|
|
|
2011-11-07 04:12:16 -05:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param anchor the coordinates of the child extents rectangle
|
|
|
|
* used for calculations of grouping, scaling, and rotation
|
|
|
|
* behavior of shapes placed within a group.
|
|
|
|
*/
|
2011-08-11 04:38:19 -04:00
|
|
|
public void setInteriorAnchor(Rectangle2D anchor){
|
2015-03-07 18:35:40 -05:00
|
|
|
CTGroupTransform2D xfrm = getSafeXfrm();
|
2011-08-11 04:38:19 -04:00
|
|
|
CTPoint2D off = xfrm.isSetChOff() ? xfrm.getChOff() : xfrm.addNewChOff();
|
|
|
|
long x = Units.toEMU(anchor.getX());
|
|
|
|
long y = Units.toEMU(anchor.getY());
|
|
|
|
off.setX(x);
|
|
|
|
off.setY(y);
|
|
|
|
CTPositiveSize2D ext = xfrm.isSetChExt() ? xfrm.getChExt() : xfrm.addNewChExt();
|
|
|
|
long cx = Units.toEMU(anchor.getWidth());
|
|
|
|
long cy = Units.toEMU(anchor.getHeight());
|
|
|
|
ext.setCx(cx);
|
|
|
|
ext.setCy(cy);
|
|
|
|
}
|
|
|
|
|
2011-11-07 04:12:16 -05:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @return child shapes contained witin this group
|
|
|
|
*/
|
2015-04-27 16:13:43 -04:00
|
|
|
@Override
|
|
|
|
public List<XSLFShape> getShapes(){
|
|
|
|
return _shapes;
|
2011-08-11 04:38:19 -04:00
|
|
|
}
|
|
|
|
|
2012-01-14 06:10:46 -05:00
|
|
|
/**
|
|
|
|
* Returns an iterator over the shapes in this sheet
|
|
|
|
*
|
|
|
|
* @return an iterator over the shapes in this sheet
|
|
|
|
*/
|
|
|
|
public Iterator<XSLFShape> iterator(){
|
|
|
|
return _shapes.iterator();
|
|
|
|
}
|
|
|
|
|
2011-11-07 04:12:16 -05:00
|
|
|
/**
|
|
|
|
* Remove the specified shape from this group
|
|
|
|
*/
|
2011-08-11 04:38:19 -04:00
|
|
|
public boolean removeShape(XSLFShape xShape) {
|
|
|
|
XmlObject obj = xShape.getXmlObject();
|
2015-03-07 18:35:40 -05:00
|
|
|
CTGroupShape grpSp = (CTGroupShape)getXmlObject();
|
2011-08-11 04:38:19 -04:00
|
|
|
if(obj instanceof CTShape){
|
2015-03-07 18:35:40 -05:00
|
|
|
grpSp.getSpList().remove(obj);
|
2011-08-11 04:38:19 -04:00
|
|
|
} else if (obj instanceof CTGroupShape){
|
2015-03-07 18:35:40 -05:00
|
|
|
grpSp.getGrpSpList().remove(obj);
|
2011-08-11 04:38:19 -04:00
|
|
|
} else if (obj instanceof CTConnector){
|
2015-03-07 18:35:40 -05:00
|
|
|
grpSp.getCxnSpList().remove(obj);
|
2011-08-11 04:38:19 -04:00
|
|
|
} else {
|
|
|
|
throw new IllegalArgumentException("Unsupported shape: " + xShape);
|
|
|
|
}
|
|
|
|
return _shapes.remove(xShape);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param shapeId 1-based shapeId
|
|
|
|
*/
|
|
|
|
static CTGroupShape prototype(int shapeId) {
|
|
|
|
CTGroupShape ct = CTGroupShape.Factory.newInstance();
|
|
|
|
CTGroupShapeNonVisual nvSpPr = ct.addNewNvGrpSpPr();
|
|
|
|
CTNonVisualDrawingProps cnv = nvSpPr.addNewCNvPr();
|
|
|
|
cnv.setName("Group " + shapeId);
|
|
|
|
cnv.setId(shapeId + 1);
|
|
|
|
|
|
|
|
nvSpPr.addNewCNvGrpSpPr();
|
|
|
|
nvSpPr.addNewNvPr();
|
|
|
|
ct.addNewGrpSpPr();
|
|
|
|
return ct;
|
|
|
|
}
|
|
|
|
|
|
|
|
// shape factory methods
|
|
|
|
private XSLFDrawing getDrawing(){
|
|
|
|
if(_drawing == null) {
|
2015-03-07 18:35:40 -05:00
|
|
|
_drawing = new XSLFDrawing(getSheet(), (CTGroupShape)getXmlObject());
|
2011-08-11 04:38:19 -04:00
|
|
|
}
|
|
|
|
return _drawing;
|
|
|
|
}
|
|
|
|
|
|
|
|
public XSLFAutoShape createAutoShape(){
|
|
|
|
XSLFAutoShape sh = getDrawing().createAutoShape();
|
|
|
|
_shapes.add(sh);
|
2015-02-21 05:56:03 -05:00
|
|
|
sh.setParent(this);
|
2011-08-11 04:38:19 -04:00
|
|
|
return sh;
|
|
|
|
}
|
|
|
|
|
|
|
|
public XSLFFreeformShape createFreeform(){
|
|
|
|
XSLFFreeformShape sh = getDrawing().createFreeform();
|
|
|
|
_shapes.add(sh);
|
2015-02-21 05:56:03 -05:00
|
|
|
sh.setParent(this);
|
2011-08-11 04:38:19 -04:00
|
|
|
return sh;
|
|
|
|
}
|
|
|
|
|
|
|
|
public XSLFTextBox createTextBox(){
|
|
|
|
XSLFTextBox sh = getDrawing().createTextBox();
|
|
|
|
_shapes.add(sh);
|
2015-02-21 05:56:03 -05:00
|
|
|
sh.setParent(this);
|
2011-08-11 04:38:19 -04:00
|
|
|
return sh;
|
|
|
|
}
|
|
|
|
|
|
|
|
public XSLFConnectorShape createConnector(){
|
|
|
|
XSLFConnectorShape sh = getDrawing().createConnector();
|
|
|
|
_shapes.add(sh);
|
2015-02-21 05:56:03 -05:00
|
|
|
sh.setParent(this);
|
2011-08-11 04:38:19 -04:00
|
|
|
return sh;
|
|
|
|
}
|
|
|
|
|
|
|
|
public XSLFGroupShape createGroup(){
|
|
|
|
XSLFGroupShape sh = getDrawing().createGroup();
|
|
|
|
_shapes.add(sh);
|
2015-02-21 05:56:03 -05:00
|
|
|
sh.setParent(this);
|
2011-08-11 04:38:19 -04:00
|
|
|
return sh;
|
|
|
|
}
|
|
|
|
|
|
|
|
public XSLFPictureShape createPicture(int pictureIndex){
|
|
|
|
|
2015-03-07 18:35:40 -05:00
|
|
|
List<PackagePart> pics = getSheet().getPackagePart().getPackage()
|
2011-11-19 07:28:33 -05:00
|
|
|
.getPartsByName(Pattern.compile("/ppt/media/image" + (pictureIndex + 1) + ".*?"));
|
2011-08-11 04:38:19 -04:00
|
|
|
|
2011-11-19 07:28:33 -05:00
|
|
|
if(pics.size() == 0) {
|
|
|
|
throw new IllegalArgumentException("Picture with index=" + pictureIndex + " was not found");
|
|
|
|
}
|
|
|
|
|
|
|
|
PackagePart pic = pics.get(0);
|
2011-08-11 04:38:19 -04:00
|
|
|
|
2015-03-07 18:35:40 -05:00
|
|
|
PackageRelationship rel = getSheet().getPackagePart().addRelationship(
|
2011-08-11 04:38:19 -04:00
|
|
|
pic.getPartName(), TargetMode.INTERNAL, XSLFRelation.IMAGES.getRelation());
|
|
|
|
|
|
|
|
XSLFPictureShape sh = getDrawing().createPicture(rel.getId());
|
|
|
|
sh.resize();
|
|
|
|
_shapes.add(sh);
|
2015-02-21 05:56:03 -05:00
|
|
|
sh.setParent(this);
|
2011-08-11 04:38:19 -04:00
|
|
|
return sh;
|
|
|
|
}
|
|
|
|
|
2015-04-27 16:13:43 -04:00
|
|
|
public XSLFTable createTable(){
|
|
|
|
XSLFTable sh = getDrawing().createTable();
|
|
|
|
_shapes.add(sh);
|
|
|
|
sh.setParent(this);
|
|
|
|
return sh;
|
|
|
|
}
|
|
|
|
|
2011-11-07 04:12:16 -05:00
|
|
|
@Override
|
2011-10-21 09:17:33 -04:00
|
|
|
public void setFlipHorizontal(boolean flip){
|
2015-03-07 18:35:40 -05:00
|
|
|
getSafeXfrm().setFlipH(flip);
|
2011-10-21 09:17:33 -04:00
|
|
|
}
|
|
|
|
|
2011-11-07 04:12:16 -05:00
|
|
|
@Override
|
2011-10-21 09:17:33 -04:00
|
|
|
public void setFlipVertical(boolean flip){
|
2015-03-07 18:35:40 -05:00
|
|
|
getSafeXfrm().setFlipV(flip);
|
2011-10-21 09:17:33 -04:00
|
|
|
}
|
2011-11-07 04:12:16 -05:00
|
|
|
|
|
|
|
@Override
|
2011-10-21 09:17:33 -04:00
|
|
|
public boolean getFlipHorizontal(){
|
2015-03-07 18:35:40 -05:00
|
|
|
CTGroupTransform2D xfrm = getXfrm();
|
|
|
|
return (xfrm == null || !xfrm.isSetFlipH()) ? false : xfrm.getFlipH();
|
2011-10-21 09:17:33 -04:00
|
|
|
}
|
|
|
|
|
2011-11-07 04:12:16 -05:00
|
|
|
@Override
|
2011-10-21 09:17:33 -04:00
|
|
|
public boolean getFlipVertical(){
|
2015-03-07 18:35:40 -05:00
|
|
|
CTGroupTransform2D xfrm = getXfrm();
|
|
|
|
return (xfrm == null || !xfrm.isSetFlipV()) ? false : xfrm.getFlipV();
|
2011-10-21 09:17:33 -04:00
|
|
|
}
|
|
|
|
|
2011-11-07 04:12:16 -05:00
|
|
|
@Override
|
2011-10-21 09:17:33 -04:00
|
|
|
public void setRotation(double theta){
|
2015-03-07 18:35:40 -05:00
|
|
|
getSafeXfrm().setRot((int) (theta * 60000));
|
2011-10-21 09:17:33 -04:00
|
|
|
}
|
|
|
|
|
2011-11-07 04:12:16 -05:00
|
|
|
@Override
|
2011-10-21 09:17:33 -04:00
|
|
|
public double getRotation(){
|
2015-03-07 18:35:40 -05:00
|
|
|
CTGroupTransform2D xfrm = getXfrm();
|
|
|
|
return (xfrm == null || !xfrm.isSetRot()) ? 0 : (xfrm.getRot() / 60000.d);
|
2011-10-21 09:17:33 -04:00
|
|
|
}
|
|
|
|
|
2011-12-28 07:32:07 -05:00
|
|
|
@Override
|
|
|
|
void copy(XSLFShape src){
|
|
|
|
XSLFGroupShape gr = (XSLFGroupShape)src;
|
2015-04-27 16:13:43 -04:00
|
|
|
|
|
|
|
// clear shapes
|
|
|
|
clear();
|
|
|
|
|
2011-12-28 07:32:07 -05:00
|
|
|
// recursively update each shape
|
2015-04-27 16:13:43 -04:00
|
|
|
for(XSLFShape shape : gr.getShapes()) {
|
|
|
|
XSLFShape newShape = null;
|
|
|
|
if (shape instanceof XSLFTextBox) {
|
|
|
|
newShape = createTextBox();
|
|
|
|
} else if (shape instanceof XSLFAutoShape) {
|
|
|
|
newShape = createAutoShape();
|
|
|
|
} else if (shape instanceof XSLFConnectorShape) {
|
|
|
|
newShape = createConnector();
|
|
|
|
} else if (shape instanceof XSLFFreeformShape) {
|
|
|
|
newShape = createFreeform();
|
|
|
|
} else if (shape instanceof XSLFPictureShape) {
|
|
|
|
XSLFPictureShape p = (XSLFPictureShape)shape;
|
|
|
|
XSLFPictureData pd = p.getPictureData();
|
|
|
|
int picId = getSheet().getSlideShow().addPicture(pd.getData(), pd.getPictureType());
|
|
|
|
newShape = createPicture(picId);
|
|
|
|
} else if (shape instanceof XSLFGroupShape) {
|
|
|
|
newShape = createGroup();
|
|
|
|
} else if (shape instanceof XSLFTable) {
|
|
|
|
newShape = createTable();
|
|
|
|
} else {
|
|
|
|
_logger.log(POILogger.WARN, "copying of class "+shape.getClass()+" not supported.");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
newShape.copy(shape);
|
2011-12-28 07:32:07 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-14 06:10:46 -05:00
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-21 05:56:03 -05:00
|
|
|
public void addShape(XSLFShape shape) {
|
|
|
|
throw new UnsupportedOperationException(
|
|
|
|
"Adding a shape from a different container is not supported -"
|
2015-03-07 18:35:40 -05:00
|
|
|
+ " create it from scratch with XSLFGroupShape.create* methods");
|
2015-02-21 05:56:03 -05:00
|
|
|
}
|
2011-08-11 04:38:19 -04:00
|
|
|
}
|