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;
|
|
|
|
|
2012-06-28 06:56:55 -04:00
|
|
|
import org.apache.poi.ddf.*;
|
2012-06-08 13:47:37 -04:00
|
|
|
import org.apache.poi.hssf.model.TextboxShape;
|
2012-07-01 05:38:08 -04:00
|
|
|
import org.apache.poi.hssf.record.*;
|
2012-06-08 13:47:37 -04:00
|
|
|
|
2004-04-09 07:45:38 -04:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Iterator;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A shape group may contain other shapes. It was no actual form on the
|
|
|
|
* sheet.
|
|
|
|
*
|
|
|
|
* @author Glen Stampoultzis (glens at apache.org)
|
|
|
|
*/
|
2012-07-01 05:38:08 -04:00
|
|
|
public class HSSFShapeGroup extends HSSFShape implements HSSFShapeContainer {
|
2009-07-18 12:49:56 -04:00
|
|
|
List<HSSFShape> shapes = new ArrayList<HSSFShape>();
|
2012-06-18 16:59:32 -04:00
|
|
|
private EscherSpgrRecord _spgrRecord;
|
2004-04-09 07:45:38 -04:00
|
|
|
|
2012-06-08 13:47:37 -04:00
|
|
|
public HSSFShapeGroup(EscherContainerRecord spgrContainer, ObjRecord objRecord) {
|
|
|
|
super(spgrContainer, objRecord);
|
|
|
|
|
|
|
|
// read internal and external coordinates from spgrContainer
|
|
|
|
EscherContainerRecord spContainer = spgrContainer.getChildContainers().get(0);
|
2012-06-18 16:59:32 -04:00
|
|
|
_spgrRecord = (EscherSpgrRecord) spContainer.getChild(0);
|
2012-07-01 05:38:08 -04:00
|
|
|
for (EscherRecord ch : spContainer.getChildRecords()) {
|
|
|
|
switch (ch.getRecordId()) {
|
2012-06-08 13:47:37 -04:00
|
|
|
case EscherSpgrRecord.RECORD_ID:
|
|
|
|
break;
|
|
|
|
case EscherClientAnchorRecord.RECORD_ID:
|
2012-07-01 05:38:08 -04:00
|
|
|
anchor = new HSSFClientAnchor((EscherClientAnchorRecord) ch);
|
2012-06-08 13:47:37 -04:00
|
|
|
break;
|
|
|
|
case EscherChildAnchorRecord.RECORD_ID:
|
2012-07-01 05:38:08 -04:00
|
|
|
anchor = new HSSFChildAnchor((EscherChildAnchorRecord) ch);
|
2012-06-08 13:47:37 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2004-04-09 07:45:38 -04:00
|
|
|
|
2012-07-01 05:38:08 -04:00
|
|
|
public HSSFShapeGroup(HSSFShape parent, HSSFAnchor anchor) {
|
|
|
|
super(parent, anchor);
|
2012-06-18 16:59:32 -04:00
|
|
|
_spgrRecord = new EscherSpgrRecord();
|
|
|
|
_spgrRecord.setRectX1(0);
|
|
|
|
_spgrRecord.setRectX2(1023);
|
|
|
|
_spgrRecord.setRectY1(0);
|
|
|
|
_spgrRecord.setRectY2(255);
|
2004-04-09 07:45:38 -04:00
|
|
|
}
|
|
|
|
|
2012-06-26 07:21:13 -04:00
|
|
|
@Override
|
|
|
|
protected EscherContainerRecord createSpContainer() {
|
2012-07-01 05:38:08 -04:00
|
|
|
EscherContainerRecord spgrContainer = new EscherContainerRecord();
|
2012-06-28 06:56:55 -04:00
|
|
|
EscherContainerRecord spContainer = new EscherContainerRecord();
|
2012-07-01 05:38:08 -04:00
|
|
|
EscherSpgrRecord spgr = new EscherSpgrRecord();
|
|
|
|
EscherSpRecord sp = new EscherSpRecord();
|
|
|
|
EscherOptRecord opt = new EscherOptRecord();
|
|
|
|
EscherRecord anchor;
|
|
|
|
EscherClientDataRecord clientData = new EscherClientDataRecord();
|
|
|
|
|
|
|
|
spgrContainer.setRecordId(EscherContainerRecord.SPGR_CONTAINER);
|
|
|
|
spgrContainer.setOptions((short) 0x000F);
|
|
|
|
spContainer.setRecordId(EscherContainerRecord.SP_CONTAINER);
|
|
|
|
spContainer.setOptions((short) 0x000F);
|
|
|
|
spgr.setRecordId(EscherSpgrRecord.RECORD_ID);
|
|
|
|
spgr.setOptions((short) 0x0001);
|
|
|
|
spgr.setRectX1(0);
|
|
|
|
spgr.setRectY1(0);
|
|
|
|
spgr.setRectX2(1023);
|
|
|
|
spgr.setRectY2(255);
|
|
|
|
sp.setRecordId(EscherSpRecord.RECORD_ID);
|
|
|
|
sp.setOptions((short) 0x0002);
|
|
|
|
if (getAnchor() instanceof HSSFClientAnchor) {
|
|
|
|
sp.setFlags(EscherSpRecord.FLAG_GROUP | EscherSpRecord.FLAG_HAVEANCHOR);
|
|
|
|
} else {
|
|
|
|
sp.setFlags(EscherSpRecord.FLAG_GROUP | EscherSpRecord.FLAG_HAVEANCHOR | EscherSpRecord.FLAG_CHILD);
|
|
|
|
}
|
|
|
|
opt.setRecordId(EscherOptRecord.RECORD_ID);
|
|
|
|
opt.setOptions((short) 0x0023);
|
|
|
|
opt.addEscherProperty(new EscherBoolProperty(EscherProperties.PROTECTION__LOCKAGAINSTGROUPING, 0x00040004));
|
|
|
|
opt.addEscherProperty(new EscherBoolProperty(EscherProperties.GROUPSHAPE__PRINT, 0x00080000));
|
|
|
|
|
|
|
|
anchor = getAnchor().getEscherAnchor();
|
|
|
|
clientData.setRecordId(EscherClientDataRecord.RECORD_ID);
|
|
|
|
clientData.setOptions((short) 0x0000);
|
|
|
|
|
|
|
|
spgrContainer.addChildRecord(spContainer);
|
|
|
|
spContainer.addChildRecord(spgr);
|
|
|
|
spContainer.addChildRecord(sp);
|
|
|
|
spContainer.addChildRecord(opt);
|
|
|
|
spContainer.addChildRecord(anchor);
|
|
|
|
spContainer.addChildRecord(clientData);
|
|
|
|
return spgrContainer;
|
2012-06-26 07:21:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected ObjRecord createObjRecord() {
|
2012-07-01 05:38:08 -04:00
|
|
|
ObjRecord obj = new ObjRecord();
|
|
|
|
CommonObjectDataSubRecord cmo = new CommonObjectDataSubRecord();
|
|
|
|
cmo.setObjectType(CommonObjectDataSubRecord.OBJECT_TYPE_GROUP);
|
|
|
|
cmo.setLocked(true);
|
|
|
|
cmo.setPrintable(true);
|
|
|
|
cmo.setAutofill(true);
|
|
|
|
cmo.setAutoline(true);
|
|
|
|
GroupMarkerSubRecord gmo = new GroupMarkerSubRecord();
|
|
|
|
EndSubRecord end = new EndSubRecord();
|
|
|
|
obj.addSubRecord(cmo);
|
|
|
|
obj.addSubRecord(gmo);
|
|
|
|
obj.addSubRecord(end);
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void onCreate(HSSFShape shape){
|
|
|
|
if(_patriarch != null && _patriarch._getBoundAggregate().getPatriarch() == null){
|
|
|
|
EscherContainerRecord spContainer = shape.getEscherContainer();
|
|
|
|
int shapeId = _patriarch.newShapeId();
|
|
|
|
shape.setShapeId(shapeId);
|
|
|
|
_escherContainer.addChildRecord(spContainer);
|
|
|
|
shape.afterInsert(_patriarch);
|
|
|
|
}
|
2012-06-26 07:21:13 -04:00
|
|
|
}
|
|
|
|
|
2004-04-09 07:45:38 -04:00
|
|
|
/**
|
|
|
|
* Create another group under this group.
|
2012-07-01 05:38:08 -04:00
|
|
|
*
|
|
|
|
* @param anchor the position of the new group.
|
|
|
|
* @return the group
|
2004-04-09 07:45:38 -04:00
|
|
|
*/
|
2012-07-01 05:38:08 -04:00
|
|
|
public HSSFShapeGroup createGroup(HSSFChildAnchor anchor) {
|
2004-04-09 07:45:38 -04:00
|
|
|
HSSFShapeGroup group = new HSSFShapeGroup(this, anchor);
|
2012-07-01 05:38:08 -04:00
|
|
|
group.parent = this;
|
2004-04-09 07:45:38 -04:00
|
|
|
group.anchor = anchor;
|
|
|
|
shapes.add(group);
|
2012-07-01 05:38:08 -04:00
|
|
|
onCreate(group);
|
2004-04-09 07:45:38 -04:00
|
|
|
return group;
|
|
|
|
}
|
|
|
|
|
2012-07-01 05:38:08 -04:00
|
|
|
public void addShape(HSSFShape shape) {
|
2011-09-12 06:19:50 -04:00
|
|
|
shape._patriarch = this._patriarch;
|
2012-07-01 05:38:08 -04:00
|
|
|
shape.parent = this;
|
2011-09-12 06:19:50 -04:00
|
|
|
shapes.add(shape);
|
|
|
|
}
|
|
|
|
|
2012-07-01 05:38:08 -04:00
|
|
|
public void addTextBox(TextboxShape textboxShape) {
|
2012-06-08 13:47:37 -04:00
|
|
|
// HSSFTextbox shape = new HSSFTextbox(this, textboxShape.geanchor);
|
|
|
|
// shapes.add(textboxShape);
|
|
|
|
}
|
|
|
|
|
2004-04-09 07:45:38 -04:00
|
|
|
/**
|
|
|
|
* Create a new simple shape under this group.
|
2012-07-01 05:38:08 -04:00
|
|
|
*
|
|
|
|
* @param anchor the position of the shape.
|
|
|
|
* @return the shape
|
2004-04-09 07:45:38 -04:00
|
|
|
*/
|
2012-07-01 05:38:08 -04:00
|
|
|
public HSSFSimpleShape createShape(HSSFChildAnchor anchor) {
|
2004-04-09 07:45:38 -04:00
|
|
|
HSSFSimpleShape shape = new HSSFSimpleShape(this, anchor);
|
2012-07-01 05:38:08 -04:00
|
|
|
shape.parent = this;
|
2004-04-09 07:45:38 -04:00
|
|
|
shape.anchor = anchor;
|
|
|
|
shapes.add(shape);
|
2012-07-01 05:38:08 -04:00
|
|
|
onCreate(shape);
|
2004-04-09 07:45:38 -04:00
|
|
|
return shape;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new textbox under this group.
|
2012-07-01 05:38:08 -04:00
|
|
|
*
|
|
|
|
* @param anchor the position of the shape.
|
|
|
|
* @return the textbox
|
2004-04-09 07:45:38 -04:00
|
|
|
*/
|
2012-07-01 05:38:08 -04:00
|
|
|
public HSSFTextbox createTextbox(HSSFChildAnchor anchor) {
|
2004-04-09 07:45:38 -04:00
|
|
|
HSSFTextbox shape = new HSSFTextbox(this, anchor);
|
2012-07-01 05:38:08 -04:00
|
|
|
shape.parent = this;
|
2004-04-09 07:45:38 -04:00
|
|
|
shape.anchor = anchor;
|
|
|
|
shapes.add(shape);
|
2012-07-01 05:38:08 -04:00
|
|
|
onCreate(shape);
|
2004-04-09 07:45:38 -04:00
|
|
|
return shape;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a polygon
|
|
|
|
*
|
2012-07-01 05:38:08 -04:00
|
|
|
* @param anchor the client anchor describes how this group is attached
|
|
|
|
* to the sheet.
|
|
|
|
* @return the newly created shape.
|
2004-04-09 07:45:38 -04:00
|
|
|
*/
|
2012-07-01 05:38:08 -04:00
|
|
|
public HSSFPolygon createPolygon(HSSFChildAnchor anchor) {
|
2004-04-09 07:45:38 -04:00
|
|
|
HSSFPolygon shape = new HSSFPolygon(this, anchor);
|
2012-07-01 05:38:08 -04:00
|
|
|
shape.parent = this;
|
2004-04-09 07:45:38 -04:00
|
|
|
shape.anchor = anchor;
|
|
|
|
shapes.add(shape);
|
2012-07-01 05:38:08 -04:00
|
|
|
onCreate(shape);
|
2004-04-09 07:45:38 -04:00
|
|
|
return shape;
|
|
|
|
}
|
|
|
|
|
2007-04-16 07:12:03 -04:00
|
|
|
/**
|
|
|
|
* Creates a picture.
|
|
|
|
*
|
2012-07-01 05:38:08 -04:00
|
|
|
* @param anchor the client anchor describes how this group is attached
|
|
|
|
* to the sheet.
|
|
|
|
* @return the newly created shape.
|
2007-04-16 07:12:03 -04:00
|
|
|
*/
|
2012-07-01 05:38:08 -04:00
|
|
|
public HSSFPicture createPicture(HSSFChildAnchor anchor, int pictureIndex) {
|
|
|
|
HSSFPicture shape = new HSSFPicture(this, anchor);
|
|
|
|
shape.parent = this;
|
|
|
|
shape.anchor = anchor;
|
|
|
|
shape.setPictureIndex(pictureIndex);
|
|
|
|
shapes.add(shape);
|
|
|
|
onCreate(shape);
|
|
|
|
return shape;
|
2007-04-16 07:12:03 -04:00
|
|
|
}
|
2012-07-01 05:38:08 -04:00
|
|
|
|
2004-04-09 07:45:38 -04:00
|
|
|
/**
|
|
|
|
* Return all children contained by this shape.
|
|
|
|
*/
|
2012-07-01 05:38:08 -04:00
|
|
|
public List<HSSFShape> getChildren() {
|
2004-04-09 07:45:38 -04:00
|
|
|
return shapes;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2008-01-15 11:31:29 -05:00
|
|
|
* Sets the coordinate space of this group. All children are constrained
|
2004-04-09 07:45:38 -04:00
|
|
|
* to these coordinates.
|
|
|
|
*/
|
2012-07-01 05:38:08 -04:00
|
|
|
public void setCoordinates(int x1, int y1, int x2, int y2) {
|
2012-06-18 16:59:32 -04:00
|
|
|
_spgrRecord.setRectX1(x1);
|
|
|
|
_spgrRecord.setRectX2(x2);
|
|
|
|
_spgrRecord.setRectY1(y1);
|
|
|
|
_spgrRecord.setRectY2(y2);
|
2004-04-09 07:45:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The top left x coordinate of this group.
|
|
|
|
*/
|
2012-07-01 05:38:08 -04:00
|
|
|
public int getX1() {
|
2012-06-18 16:59:32 -04:00
|
|
|
return _spgrRecord.getRectX1();
|
2004-04-09 07:45:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The top left y coordinate of this group.
|
|
|
|
*/
|
2012-07-01 05:38:08 -04:00
|
|
|
public int getY1() {
|
2012-06-18 16:59:32 -04:00
|
|
|
return _spgrRecord.getRectY1();
|
2004-04-09 07:45:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The bottom right x coordinate of this group.
|
|
|
|
*/
|
2012-07-01 05:38:08 -04:00
|
|
|
public int getX2() {
|
2012-06-18 16:59:32 -04:00
|
|
|
return _spgrRecord.getRectX2();
|
2004-04-09 07:45:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The bottom right y coordinate of this group.
|
|
|
|
*/
|
2012-07-01 05:38:08 -04:00
|
|
|
public int getY2() {
|
2012-06-18 16:59:32 -04:00
|
|
|
return _spgrRecord.getRectY2();
|
2004-04-09 07:45:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Count of all children and their childrens children.
|
|
|
|
*/
|
2012-07-01 05:38:08 -04:00
|
|
|
public int countOfAllChildren() {
|
2004-04-09 07:45:38 -04:00
|
|
|
int count = shapes.size();
|
2012-07-01 05:38:08 -04:00
|
|
|
for (Iterator iterator = shapes.iterator(); iterator.hasNext(); ) {
|
2004-04-09 07:45:38 -04:00
|
|
|
HSSFShape shape = (HSSFShape) iterator.next();
|
|
|
|
count += shape.countOfAllChildren();
|
|
|
|
}
|
|
|
|
return count;
|
|
|
|
}
|
2012-07-01 05:38:08 -04:00
|
|
|
|
|
|
|
@Override
|
|
|
|
void afterInsert(HSSFPatriarch patriarch){
|
|
|
|
EscherAggregate agg = patriarch._getBoundAggregate();
|
|
|
|
EscherContainerRecord containerRecord = _escherContainer.getChildById(EscherContainerRecord.SP_CONTAINER);
|
|
|
|
agg.associateShapeToObjRecord(containerRecord.getChildById(EscherClientDataRecord.RECORD_ID), getObjRecord());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
void setShapeId(int shapeId){
|
|
|
|
EscherContainerRecord containerRecord = _escherContainer.getChildById(EscherContainerRecord.SP_CONTAINER);
|
|
|
|
EscherSpRecord spRecord = containerRecord.getChildById(EscherSpRecord.RECORD_ID);
|
|
|
|
spRecord.setShapeId(shapeId);
|
|
|
|
CommonObjectDataSubRecord cod = (CommonObjectDataSubRecord) _objRecord.getSubRecords().get(0);
|
|
|
|
cod.setObjectId((short) (shapeId-1024));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
int getShapeId(){
|
|
|
|
EscherContainerRecord containerRecord = _escherContainer.getChildById(EscherContainerRecord.SP_CONTAINER);
|
|
|
|
return ((EscherSpRecord)containerRecord.getChildById(EscherSpRecord.RECORD_ID)).getShapeId();
|
|
|
|
}
|
2011-09-12 06:19:50 -04:00
|
|
|
}
|