2004-04-09 09:05:39 -04:00
|
|
|
/* ====================================================================
|
2006-12-22 14:18:16 -05: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
|
2004-04-09 09:05:39 -04:00
|
|
|
|
|
|
|
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.
|
|
|
|
==================================================================== */
|
2004-08-23 04:52:54 -04:00
|
|
|
|
2004-04-09 07:45:38 -04:00
|
|
|
package org.apache.poi.hssf.usermodel;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Iterator;
|
|
|
|
import java.util.List;
|
|
|
|
|
2012-06-26 07:21:13 -04:00
|
|
|
import org.apache.poi.ddf.*;
|
|
|
|
import org.apache.poi.hssf.model.DrawingManager2;
|
2008-01-09 18:21:35 -05:00
|
|
|
import org.apache.poi.hssf.record.EscherAggregate;
|
2011-05-20 04:22:53 -04:00
|
|
|
import org.apache.poi.ss.usermodel.Chart;
|
2008-01-09 18:21:35 -05:00
|
|
|
import org.apache.poi.util.StringUtil;
|
2011-07-20 03:38:01 -04:00
|
|
|
import org.apache.poi.util.Internal;
|
2008-11-12 02:15:37 -05:00
|
|
|
import org.apache.poi.ss.usermodel.Drawing;
|
|
|
|
import org.apache.poi.ss.usermodel.ClientAnchor;
|
2008-01-09 18:21:35 -05:00
|
|
|
|
2004-04-09 07:45:38 -04:00
|
|
|
/**
|
|
|
|
* The patriarch is the toplevel container for shapes in a sheet. It does
|
|
|
|
* little other than act as a container for other shapes and groups.
|
|
|
|
*
|
|
|
|
* @author Glen Stampoultzis (glens at apache.org)
|
|
|
|
*/
|
2009-02-13 17:11:16 -05:00
|
|
|
public final class HSSFPatriarch implements HSSFShapeContainer, Drawing {
|
|
|
|
private final List<HSSFShape> _shapes = new ArrayList<HSSFShape>();
|
|
|
|
private int _x1 = 0;
|
|
|
|
private int _y1 = 0 ;
|
|
|
|
private int _x2 = 1023;
|
|
|
|
private int _y2 = 255;
|
2004-04-09 07:45:38 -04:00
|
|
|
|
2008-01-09 18:21:35 -05:00
|
|
|
/**
|
|
|
|
* The EscherAggregate we have been bound to.
|
|
|
|
* (This will handle writing us out into records,
|
|
|
|
* and building up our shapes from the records)
|
|
|
|
*/
|
2009-02-13 17:11:16 -05:00
|
|
|
private EscherAggregate _boundAggregate;
|
|
|
|
final HSSFSheet _sheet; // TODO make private
|
2008-01-09 18:21:35 -05:00
|
|
|
|
2004-04-09 07:45:38 -04:00
|
|
|
/**
|
|
|
|
* Creates the patriarch.
|
|
|
|
*
|
2008-01-09 18:21:35 -05:00
|
|
|
* @param sheet the sheet this patriarch is stored in.
|
2004-04-09 07:45:38 -04:00
|
|
|
*/
|
2009-02-13 17:11:16 -05:00
|
|
|
HSSFPatriarch(HSSFSheet sheet, EscherAggregate boundAggregate){
|
|
|
|
_sheet = sheet;
|
|
|
|
_boundAggregate = boundAggregate;
|
2004-04-09 07:45:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new group record stored under this patriarch.
|
|
|
|
*
|
|
|
|
* @param anchor the client anchor describes how this group is attached
|
|
|
|
* to the sheet.
|
|
|
|
* @return the newly created group.
|
|
|
|
*/
|
|
|
|
public HSSFShapeGroup createGroup(HSSFClientAnchor anchor)
|
|
|
|
{
|
|
|
|
HSSFShapeGroup group = new HSSFShapeGroup(null, anchor);
|
|
|
|
group.anchor = anchor;
|
2011-07-20 03:38:01 -04:00
|
|
|
addShape(group);
|
2012-07-01 05:38:08 -04:00
|
|
|
onCreate(group);
|
2004-04-09 07:45:38 -04:00
|
|
|
return group;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a simple shape. This includes such shapes as lines, rectangles,
|
|
|
|
* and ovals.
|
|
|
|
*
|
|
|
|
* @param anchor the client anchor describes how this group is attached
|
|
|
|
* to the sheet.
|
|
|
|
* @return the newly created shape.
|
|
|
|
*/
|
|
|
|
public HSSFSimpleShape createSimpleShape(HSSFClientAnchor anchor)
|
|
|
|
{
|
|
|
|
HSSFSimpleShape shape = new HSSFSimpleShape(null, anchor);
|
|
|
|
shape.anchor = anchor;
|
2011-07-20 03:38:01 -04:00
|
|
|
addShape(shape);
|
2012-06-26 07:21:13 -04:00
|
|
|
//open existing file
|
|
|
|
onCreate(shape);
|
2004-04-09 07:45:38 -04:00
|
|
|
return shape;
|
|
|
|
}
|
|
|
|
|
2005-05-01 07:26:18 -04:00
|
|
|
/**
|
|
|
|
* Creates a picture.
|
|
|
|
*
|
|
|
|
* @param anchor the client anchor describes how this group is attached
|
|
|
|
* to the sheet.
|
|
|
|
* @return the newly created shape.
|
|
|
|
*/
|
|
|
|
public HSSFPicture createPicture(HSSFClientAnchor anchor, int pictureIndex)
|
|
|
|
{
|
|
|
|
HSSFPicture shape = new HSSFPicture(null, anchor);
|
|
|
|
shape.setPictureIndex( pictureIndex );
|
|
|
|
shape.anchor = anchor;
|
2011-07-20 03:38:01 -04:00
|
|
|
addShape(shape);
|
2012-06-26 07:21:13 -04:00
|
|
|
//open existing file
|
|
|
|
onCreate(shape);
|
2011-07-20 03:38:01 -04:00
|
|
|
|
|
|
|
EscherBSERecord bse = _sheet.getWorkbook().getWorkbook().getBSERecord(pictureIndex);
|
|
|
|
bse.setRef(bse.getRef() + 1);
|
2005-05-01 07:26:18 -04:00
|
|
|
return shape;
|
|
|
|
}
|
2011-07-20 03:38:01 -04:00
|
|
|
|
2008-11-12 02:15:37 -05:00
|
|
|
public HSSFPicture createPicture(ClientAnchor anchor, int pictureIndex)
|
|
|
|
{
|
|
|
|
return createPicture((HSSFClientAnchor)anchor, pictureIndex);
|
|
|
|
}
|
2005-05-01 07:26:18 -04:00
|
|
|
|
2004-04-09 07:45:38 -04:00
|
|
|
/**
|
|
|
|
* Creates a polygon
|
|
|
|
*
|
|
|
|
* @param anchor the client anchor describes how this group is attached
|
|
|
|
* to the sheet.
|
|
|
|
* @return the newly created shape.
|
|
|
|
*/
|
|
|
|
public HSSFPolygon createPolygon(HSSFClientAnchor anchor)
|
|
|
|
{
|
|
|
|
HSSFPolygon shape = new HSSFPolygon(null, anchor);
|
|
|
|
shape.anchor = anchor;
|
2011-07-20 03:38:01 -04:00
|
|
|
addShape(shape);
|
2012-07-01 05:38:08 -04:00
|
|
|
onCreate(shape);
|
2004-04-09 07:45:38 -04:00
|
|
|
return shape;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructs a textbox under the patriarch.
|
|
|
|
*
|
|
|
|
* @param anchor the client anchor describes how this group is attached
|
|
|
|
* to the sheet.
|
|
|
|
* @return the newly created textbox.
|
|
|
|
*/
|
|
|
|
public HSSFTextbox createTextbox(HSSFClientAnchor anchor)
|
|
|
|
{
|
|
|
|
HSSFTextbox shape = new HSSFTextbox(null, anchor);
|
|
|
|
shape.anchor = anchor;
|
2011-07-20 03:38:01 -04:00
|
|
|
addShape(shape);
|
2012-06-26 07:21:13 -04:00
|
|
|
onCreate(shape);
|
2004-04-09 07:45:38 -04:00
|
|
|
return shape;
|
|
|
|
}
|
|
|
|
|
2007-01-01 16:02:22 -05:00
|
|
|
/**
|
|
|
|
* Constructs a cell comment.
|
|
|
|
*
|
|
|
|
* @param anchor the client anchor describes how this comment is attached
|
|
|
|
* to the sheet.
|
|
|
|
* @return the newly created comment.
|
|
|
|
*/
|
|
|
|
public HSSFComment createComment(HSSFAnchor anchor)
|
|
|
|
{
|
|
|
|
HSSFComment shape = new HSSFComment(null, anchor);
|
|
|
|
shape.anchor = anchor;
|
2011-07-20 03:38:01 -04:00
|
|
|
addShape(shape);
|
2012-06-28 06:56:55 -04:00
|
|
|
onCreate(shape);
|
2007-01-01 16:02:22 -05:00
|
|
|
return shape;
|
|
|
|
}
|
|
|
|
|
2010-08-08 07:11:38 -04:00
|
|
|
/**
|
|
|
|
* YK: used to create autofilters
|
|
|
|
*
|
2011-05-20 04:22:53 -04:00
|
|
|
* @see org.apache.poi.hssf.usermodel.HSSFSheet#setAutoFilter(org.apache.poi.ss.util.CellRangeAddress)
|
2010-08-08 07:11:38 -04:00
|
|
|
*/
|
|
|
|
HSSFSimpleShape createComboBox(HSSFAnchor anchor)
|
|
|
|
{
|
|
|
|
HSSFSimpleShape shape = new HSSFSimpleShape(null, anchor);
|
|
|
|
shape.setShapeType(HSSFSimpleShape.OBJECT_TYPE_COMBO_BOX);
|
|
|
|
shape.anchor = anchor;
|
2011-07-20 03:38:01 -04:00
|
|
|
addShape(shape);
|
2010-08-08 07:11:38 -04:00
|
|
|
return shape;
|
|
|
|
}
|
|
|
|
|
2009-11-27 15:47:39 -05:00
|
|
|
public HSSFComment createCellComment(ClientAnchor anchor) {
|
2009-11-27 12:39:17 -05:00
|
|
|
return createComment((HSSFAnchor)anchor);
|
|
|
|
}
|
|
|
|
|
2004-04-09 07:45:38 -04:00
|
|
|
/**
|
|
|
|
* Returns a list of all shapes contained by the patriarch.
|
|
|
|
*/
|
2009-02-13 17:11:16 -05:00
|
|
|
public List<HSSFShape> getChildren()
|
2004-04-09 07:45:38 -04:00
|
|
|
{
|
2009-02-13 17:11:16 -05:00
|
|
|
return _shapes;
|
2004-04-09 07:45:38 -04:00
|
|
|
}
|
|
|
|
|
2011-07-20 03:38:01 -04:00
|
|
|
/**
|
|
|
|
* add a shape to this drawing
|
|
|
|
*/
|
|
|
|
@Internal
|
|
|
|
public void addShape(HSSFShape shape){
|
|
|
|
shape._patriarch = this;
|
|
|
|
_shapes.add(shape);
|
|
|
|
}
|
|
|
|
|
2012-06-26 07:21:13 -04:00
|
|
|
private void onCreate(HSSFShape shape){
|
|
|
|
if(_boundAggregate.getPatriarch() == null){
|
|
|
|
EscherContainerRecord spgrContainer =
|
|
|
|
_boundAggregate.getEscherContainer().getChildContainers().get(0);
|
|
|
|
|
|
|
|
EscherContainerRecord spContainer = shape.getEscherContainer();
|
|
|
|
int shapeId = newShapeId();
|
|
|
|
shape.setShapeId(shapeId);
|
|
|
|
|
|
|
|
spgrContainer.addChildRecord(spContainer);
|
|
|
|
shape.afterInsert(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-04-09 07:45:38 -04:00
|
|
|
/**
|
|
|
|
* Total count of all children and their children's children.
|
|
|
|
*/
|
2009-02-13 17:11:16 -05:00
|
|
|
public int countOfAllChildren() {
|
|
|
|
int count = _shapes.size();
|
|
|
|
for (Iterator<HSSFShape> iterator = _shapes.iterator(); iterator.hasNext();) {
|
|
|
|
HSSFShape shape = iterator.next();
|
2004-04-09 07:45:38 -04:00
|
|
|
count += shape.countOfAllChildren();
|
|
|
|
}
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
/**
|
2009-02-13 17:11:16 -05:00
|
|
|
* Sets the coordinate space of this group. All children are constrained
|
2004-04-09 07:45:38 -04:00
|
|
|
* to these coordinates.
|
|
|
|
*/
|
2009-02-13 17:11:16 -05:00
|
|
|
public void setCoordinates(int x1, int y1, int x2, int y2){
|
|
|
|
_x1 = x1;
|
|
|
|
_y1 = y1;
|
|
|
|
_x2 = x2;
|
|
|
|
_y2 = y2;
|
2004-04-09 07:45:38 -04:00
|
|
|
}
|
2008-11-12 02:15:37 -05:00
|
|
|
|
2012-06-26 07:21:13 -04:00
|
|
|
int newShapeId() {
|
|
|
|
if (_boundAggregate.getEscherContainer() == null){
|
|
|
|
throw new IllegalStateException("We can use this method for only existing files");
|
|
|
|
}
|
|
|
|
DrawingManager2 dm = _boundAggregate.getDrawingManager();
|
|
|
|
EscherDgRecord dg =
|
|
|
|
_boundAggregate.getEscherContainer().getChildById(EscherDgRecord.RECORD_ID);
|
|
|
|
short drawingGroupId = dg.getDrawingGroupId();
|
|
|
|
return dm.allocateShapeId(drawingGroupId, dg);
|
|
|
|
}
|
|
|
|
|
2008-01-09 18:21:35 -05:00
|
|
|
/**
|
|
|
|
* Does this HSSFPatriarch contain a chart?
|
|
|
|
* (Technically a reference to a chart, since they
|
|
|
|
* get stored in a different block of records)
|
|
|
|
* FIXME - detect chart in all cases (only seems
|
|
|
|
* to work on some charts so far)
|
|
|
|
*/
|
|
|
|
public boolean containsChart() {
|
2008-04-05 09:07:22 -04:00
|
|
|
// TODO - support charts properly in usermodel
|
2008-11-12 02:15:37 -05:00
|
|
|
|
2008-04-05 09:07:22 -04:00
|
|
|
// We're looking for a EscherOptRecord
|
|
|
|
EscherOptRecord optRecord = (EscherOptRecord)
|
2009-02-13 17:11:16 -05:00
|
|
|
_boundAggregate.findFirstWithId(EscherOptRecord.RECORD_ID);
|
2008-04-05 09:07:22 -04:00
|
|
|
if(optRecord == null) {
|
|
|
|
// No opt record, can't have chart
|
|
|
|
return false;
|
|
|
|
}
|
2008-11-12 02:15:37 -05:00
|
|
|
|
2009-02-13 17:11:16 -05:00
|
|
|
for(Iterator<EscherProperty> it = optRecord.getEscherProperties().iterator(); it.hasNext();) {
|
|
|
|
EscherProperty prop = it.next();
|
2008-04-05 09:07:22 -04:00
|
|
|
if(prop.getPropertyNumber() == 896 && prop.isComplex()) {
|
|
|
|
EscherComplexProperty cp = (EscherComplexProperty)prop;
|
|
|
|
String str = StringUtil.getFromUnicodeLE(cp.getComplexData());
|
2009-02-13 17:11:16 -05:00
|
|
|
|
2008-04-05 09:07:22 -04:00
|
|
|
if(str.equals("Chart 1\0")) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-01-09 18:21:35 -05:00
|
|
|
|
2008-04-05 09:07:22 -04:00
|
|
|
return false;
|
2008-01-09 18:21:35 -05:00
|
|
|
}
|
2004-04-09 07:45:38 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The top left x coordinate of this group.
|
|
|
|
*/
|
|
|
|
public int getX1()
|
|
|
|
{
|
2009-02-13 17:11:16 -05:00
|
|
|
return _x1;
|
2004-04-09 07:45:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The top left y coordinate of this group.
|
|
|
|
*/
|
|
|
|
public int getY1()
|
|
|
|
{
|
2009-02-13 17:11:16 -05:00
|
|
|
return _y1;
|
2004-04-09 07:45:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The bottom right x coordinate of this group.
|
|
|
|
*/
|
|
|
|
public int getX2()
|
|
|
|
{
|
2009-02-13 17:11:16 -05:00
|
|
|
return _x2;
|
2004-04-09 07:45:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The bottom right y coordinate of this group.
|
|
|
|
*/
|
|
|
|
public int getY2()
|
|
|
|
{
|
2009-02-13 17:11:16 -05:00
|
|
|
return _y2;
|
2004-04-09 07:45:38 -04:00
|
|
|
}
|
|
|
|
|
2008-01-15 11:31:29 -05:00
|
|
|
/**
|
2009-11-27 15:47:39 -05:00
|
|
|
* Returns the aggregate escher record we're bound to
|
2008-01-15 11:31:29 -05:00
|
|
|
*/
|
|
|
|
protected EscherAggregate _getBoundAggregate() {
|
2009-02-13 17:11:16 -05:00
|
|
|
return _boundAggregate;
|
2008-01-15 11:31:29 -05:00
|
|
|
}
|
2011-05-20 04:22:53 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new client anchor and sets the top-left and bottom-right
|
|
|
|
* coordinates of the anchor.
|
|
|
|
*
|
|
|
|
* @param dx1 the x coordinate in EMU within the first cell.
|
|
|
|
* @param dy1 the y coordinate in EMU within the first cell.
|
|
|
|
* @param dx2 the x coordinate in EMU within the second cell.
|
|
|
|
* @param dy2 the y coordinate in EMU within the second cell.
|
|
|
|
* @param col1 the column (0 based) of the first cell.
|
|
|
|
* @param row1 the row (0 based) of the first cell.
|
|
|
|
* @param col2 the column (0 based) of the second cell.
|
|
|
|
* @param row2 the row (0 based) of the second cell.
|
|
|
|
* @return the newly created client anchor
|
|
|
|
*/
|
|
|
|
public HSSFClientAnchor createAnchor(int dx1, int dy1, int dx2, int dy2, int col1, int row1, int col2, int row2){
|
|
|
|
return new HSSFClientAnchor(dx1, dy1, dx2, dy2, (short)col1, row1, (short)col2, row2);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Chart createChart(ClientAnchor anchor) {
|
|
|
|
throw new RuntimeException("NotImplemented");
|
|
|
|
}
|
|
|
|
|
2012-06-08 13:47:37 -04:00
|
|
|
|
|
|
|
void buildShapeTree(){
|
|
|
|
EscherContainerRecord dgContainer = _boundAggregate.getEscherContainer();
|
|
|
|
EscherContainerRecord spgrConrainer = dgContainer.getChildContainers().get(0);
|
|
|
|
List<EscherContainerRecord> spgrChildren = spgrConrainer.getChildContainers();
|
|
|
|
|
|
|
|
for(int i = 0; i < spgrChildren.size(); i++){
|
|
|
|
EscherContainerRecord spContainer = spgrChildren.get(i);
|
|
|
|
if (i == 0){
|
|
|
|
EscherSpgrRecord spgr = (EscherSpgrRecord)spContainer.getChildById(EscherSpgrRecord.RECORD_ID);
|
|
|
|
setCoordinates(
|
|
|
|
spgr.getRectX1(), spgr.getRectY1(),
|
|
|
|
spgr.getRectX2(), spgr.getRectY2()
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
HSSFShapeFactory.createShapeTree(spContainer, _boundAggregate, this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2004-04-09 07:45:38 -04:00
|
|
|
}
|