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-11-21 10:54:01 -05:00
|
|
|
import java.awt.geom.Rectangle2D;
|
2015-03-07 18:35:40 -05:00
|
|
|
|
|
|
|
import javax.xml.namespace.QName;
|
|
|
|
|
2013-01-22 08:37:14 -05:00
|
|
|
import org.apache.poi.POIXMLException;
|
|
|
|
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
|
|
|
|
import org.apache.poi.openxml4j.opc.PackagePart;
|
|
|
|
import org.apache.poi.openxml4j.opc.PackageRelationship;
|
2016-01-15 19:11:01 -05:00
|
|
|
import org.apache.poi.sl.usermodel.GraphicalFrame;
|
2015-02-21 05:56:03 -05:00
|
|
|
import org.apache.poi.sl.usermodel.ShapeType;
|
2011-08-11 04:38:19 -04:00
|
|
|
import org.apache.poi.util.Beta;
|
2016-01-15 19:11:01 -05:00
|
|
|
import org.apache.poi.util.POILogFactory;
|
|
|
|
import org.apache.poi.util.POILogger;
|
2011-10-21 09:17:33 -04:00
|
|
|
import org.apache.poi.util.Units;
|
2013-01-22 08:37:14 -05:00
|
|
|
import org.apache.xmlbeans.XmlCursor;
|
2016-01-15 19:11:01 -05:00
|
|
|
import org.apache.xmlbeans.XmlException;
|
2013-01-22 08:37:14 -05:00
|
|
|
import org.apache.xmlbeans.XmlObject;
|
|
|
|
import org.openxmlformats.schemas.drawingml.x2006.main.CTGraphicalObjectData;
|
2011-10-21 09:17:33 -04:00
|
|
|
import org.openxmlformats.schemas.drawingml.x2006.main.CTPoint2D;
|
|
|
|
import org.openxmlformats.schemas.drawingml.x2006.main.CTPositiveSize2D;
|
|
|
|
import org.openxmlformats.schemas.drawingml.x2006.main.CTTransform2D;
|
2011-10-28 10:25:53 -04:00
|
|
|
import org.openxmlformats.schemas.presentationml.x2006.main.CTGraphicalObjectFrame;
|
2016-01-15 19:11:01 -05:00
|
|
|
import org.openxmlformats.schemas.presentationml.x2006.main.CTGroupShape;
|
2011-08-11 04:38:19 -04:00
|
|
|
|
|
|
|
@Beta
|
2016-01-15 19:11:01 -05:00
|
|
|
public class XSLFGraphicFrame extends XSLFShape implements GraphicalFrame<XSLFShape, XSLFTextParagraph> {
|
|
|
|
private static final POILogger LOG = POILogFactory.getLogger(XSLFGraphicFrame.class);
|
|
|
|
|
2011-08-11 04:38:19 -04:00
|
|
|
/*package*/ XSLFGraphicFrame(CTGraphicalObjectFrame shape, XSLFSheet sheet){
|
2015-03-07 18:35:40 -05:00
|
|
|
super(shape,sheet);
|
2011-08-11 04:38:19 -04:00
|
|
|
}
|
|
|
|
|
2015-02-21 05:56:03 -05:00
|
|
|
public ShapeType getShapeType(){
|
|
|
|
throw new UnsupportedOperationException();
|
2011-08-11 04:38:19 -04:00
|
|
|
}
|
|
|
|
|
2015-09-20 20:09:45 -04:00
|
|
|
@Override
|
2015-11-21 10:54:01 -05:00
|
|
|
public Rectangle2D getAnchor(){
|
2015-03-07 18:35:40 -05:00
|
|
|
CTTransform2D xfrm = ((CTGraphicalObjectFrame)getXmlObject()).getXfrm();
|
2011-10-21 09:17:33 -04:00
|
|
|
CTPoint2D off = xfrm.getOff();
|
2015-11-21 10:54:01 -05:00
|
|
|
double x = Units.toPoints(off.getX());
|
|
|
|
double y = Units.toPoints(off.getY());
|
2011-10-21 09:17:33 -04:00
|
|
|
CTPositiveSize2D ext = xfrm.getExt();
|
2015-11-21 10:54:01 -05:00
|
|
|
double cx = Units.toPoints(ext.getCx());
|
|
|
|
double cy = Units.toPoints(ext.getCy());
|
|
|
|
return new Rectangle2D.Double(x, y, cx, cy);
|
2011-08-11 04:38:19 -04:00
|
|
|
}
|
|
|
|
|
2015-11-21 10:54:01 -05:00
|
|
|
@Override
|
|
|
|
public void setAnchor(Rectangle2D anchor){
|
2015-03-07 18:35:40 -05:00
|
|
|
CTTransform2D xfrm = ((CTGraphicalObjectFrame)getXmlObject()).getXfrm();
|
2011-10-21 09:17:33 -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-08-11 04:38:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-08-17 06:18:31 -04:00
|
|
|
static XSLFGraphicFrame create(CTGraphicalObjectFrame shape, XSLFSheet sheet){
|
|
|
|
String uri = shape.getGraphic().getGraphicData().getUri();
|
|
|
|
if(XSLFTable.TABLE_URI.equals(uri)){
|
|
|
|
return new XSLFTable(shape, sheet);
|
|
|
|
} else {
|
|
|
|
return new XSLFGraphicFrame(shape, sheet);
|
|
|
|
}
|
2011-08-11 04:38:19 -04:00
|
|
|
}
|
|
|
|
|
2011-10-21 09:17:33 -04:00
|
|
|
/**
|
|
|
|
* Rotate this shape.
|
|
|
|
* <p>
|
|
|
|
* Positive angles are clockwise (i.e., towards the positive y axis);
|
|
|
|
* negative angles are counter-clockwise (i.e., towards the negative y axis).
|
|
|
|
* </p>
|
|
|
|
*
|
|
|
|
* @param theta the rotation angle in degrees.
|
|
|
|
*/
|
|
|
|
public void setRotation(double theta){
|
|
|
|
throw new IllegalArgumentException("Operation not supported");
|
|
|
|
}
|
2016-01-15 19:11:01 -05:00
|
|
|
|
2011-10-21 09:17:33 -04:00
|
|
|
/**
|
|
|
|
* Rotation angle in degrees
|
|
|
|
* <p>
|
|
|
|
* Positive angles are clockwise (i.e., towards the positive y axis);
|
|
|
|
* negative angles are counter-clockwise (i.e., towards the negative y axis).
|
|
|
|
* </p>
|
|
|
|
*
|
|
|
|
* @return rotation angle in degrees
|
|
|
|
*/
|
|
|
|
public double getRotation(){
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setFlipHorizontal(boolean flip){
|
|
|
|
throw new IllegalArgumentException("Operation not supported");
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setFlipVertical(boolean flip){
|
|
|
|
throw new IllegalArgumentException("Operation not supported");
|
|
|
|
}
|
2016-01-15 19:11:01 -05:00
|
|
|
|
2011-10-21 09:17:33 -04:00
|
|
|
/**
|
|
|
|
* Whether the shape is horizontally flipped
|
|
|
|
*
|
|
|
|
* @return whether the shape is horizontally flipped
|
|
|
|
*/
|
|
|
|
public boolean getFlipHorizontal(){
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean getFlipVertical(){
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-01-22 08:37:14 -05:00
|
|
|
@Override
|
|
|
|
void copy(XSLFShape sh){
|
|
|
|
super.copy(sh);
|
|
|
|
|
2015-03-07 18:35:40 -05:00
|
|
|
CTGraphicalObjectData data = ((CTGraphicalObjectFrame)getXmlObject()).getGraphic().getGraphicData();
|
2013-01-22 08:37:14 -05:00
|
|
|
String uri = data.getUri();
|
|
|
|
if(uri.equals("http://schemas.openxmlformats.org/drawingml/2006/diagram")){
|
|
|
|
copyDiagram(data, (XSLFGraphicFrame)sh);
|
|
|
|
} else {
|
|
|
|
// TODO support other types of objects
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO should be moved to a sub-class
|
|
|
|
private void copyDiagram(CTGraphicalObjectData objData, XSLFGraphicFrame srcShape){
|
|
|
|
String xpath = "declare namespace dgm='http://schemas.openxmlformats.org/drawingml/2006/diagram' $this//dgm:relIds";
|
|
|
|
XmlObject[] obj = objData.selectPath(xpath);
|
|
|
|
if(obj != null && obj.length == 1){
|
|
|
|
XmlCursor c = obj[0].newCursor();
|
|
|
|
|
|
|
|
XSLFSheet sheet = srcShape.getSheet();
|
|
|
|
try {
|
|
|
|
String dm = c.getAttributeText(new QName("http://schemas.openxmlformats.org/officeDocument/2006/relationships", "dm"));
|
|
|
|
PackageRelationship dmRel = sheet.getPackagePart().getRelationship(dm);
|
|
|
|
PackagePart dmPart = sheet.getPackagePart().getRelatedPart(dmRel);
|
2015-03-07 18:35:40 -05:00
|
|
|
getSheet().importPart(dmRel, dmPart);
|
2013-01-22 08:37:14 -05:00
|
|
|
|
|
|
|
String lo = c.getAttributeText(new QName("http://schemas.openxmlformats.org/officeDocument/2006/relationships", "lo"));
|
|
|
|
PackageRelationship loRel = sheet.getPackagePart().getRelationship(lo);
|
|
|
|
PackagePart loPart = sheet.getPackagePart().getRelatedPart(loRel);
|
2015-03-07 18:35:40 -05:00
|
|
|
getSheet().importPart(loRel, loPart);
|
2013-01-22 08:37:14 -05:00
|
|
|
|
|
|
|
String qs = c.getAttributeText(new QName("http://schemas.openxmlformats.org/officeDocument/2006/relationships", "qs"));
|
|
|
|
PackageRelationship qsRel = sheet.getPackagePart().getRelationship(qs);
|
|
|
|
PackagePart qsPart = sheet.getPackagePart().getRelatedPart(qsRel);
|
2015-03-07 18:35:40 -05:00
|
|
|
getSheet().importPart(qsRel, qsPart);
|
2013-01-22 08:37:14 -05:00
|
|
|
|
|
|
|
String cs = c.getAttributeText(new QName("http://schemas.openxmlformats.org/officeDocument/2006/relationships", "cs"));
|
|
|
|
PackageRelationship csRel = sheet.getPackagePart().getRelationship(cs);
|
|
|
|
PackagePart csPart = sheet.getPackagePart().getRelatedPart(csRel);
|
2015-03-07 18:35:40 -05:00
|
|
|
getSheet().importPart(csRel, csPart);
|
2013-01-22 08:37:14 -05:00
|
|
|
|
|
|
|
} catch (InvalidFormatException e){
|
|
|
|
throw new POIXMLException(e);
|
|
|
|
}
|
|
|
|
c.dispose();
|
|
|
|
}
|
|
|
|
}
|
2011-10-21 09:17:33 -04:00
|
|
|
|
2016-01-15 19:11:01 -05:00
|
|
|
@Override
|
|
|
|
public XSLFPictureShape getFallbackPicture() {
|
|
|
|
String xquery =
|
|
|
|
"declare namespace p='http://schemas.openxmlformats.org/presentationml/2006/main'; "
|
|
|
|
+ "declare namespace mc='http://schemas.openxmlformats.org/markup-compatibility/2006' "
|
|
|
|
+ ".//mc:Fallback/*/p:pic"
|
|
|
|
;
|
|
|
|
XmlObject xo = selectProperty(XmlObject.class, xquery);
|
|
|
|
if (xo == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
CTGroupShape gs;
|
|
|
|
try {
|
|
|
|
gs = CTGroupShape.Factory.parse(xo.newDomNode());
|
|
|
|
} catch (XmlException e) {
|
|
|
|
LOG.log(POILogger.WARN, "Can't parse fallback picture stream of graphical frame", e);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (gs.sizeOfPicArray() == 0) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return new XSLFPictureShape(gs.getPicArray(0), getSheet());
|
|
|
|
}
|
2011-08-11 04:38:19 -04:00
|
|
|
}
|