FileDocCategorySizeDatePackage
ShapeGroup.javaAPI DocApache Poi 3.0.18607Thu May 31 18:45:28 BST 2007org.apache.poi.hslf.model

ShapeGroup

public class ShapeGroup extends Shape
Represents a group of shapes.
author
Yegor Kozlov

Fields Summary
Constructors Summary
public ShapeGroup()
Create a new ShapeGroup. This constructor is used when a new shape is created.

        this(null, null);
        _escherContainer = createSpContainer(false);
    
protected ShapeGroup(EscherContainerRecord escherRecord, Shape parent)
Create a ShapeGroup object and initilize it from the supplied Record container.

param
escherRecord EscherSpContainer container which holds information about this shape
param
parent the parent of the shape

        super(escherRecord, parent);
    
Methods Summary
public voidaddShape(org.apache.poi.hslf.model.Shape shape)
Add a shape to this group.

param
shape - the Shape to add

        _escherContainer.addChildRecord(shape.getSpContainer());

        Sheet sheet = getSheet();
        shape.setSheet(sheet);
        shape.afterInsert(sheet);

        if(shape instanceof TextBox) {
            TextBox tbox = (TextBox)shape;
            getSheet().getPPDrawing().addTextboxWrapper(tbox._txtbox);
        }
    
protected org.apache.poi.ddf.EscherContainerRecordcreateSpContainer(boolean isChild)
Create a new ShapeGroup and create an instance of EscherSpgrContainer which represents a group of shapes

        EscherContainerRecord spgr = new EscherContainerRecord();
        spgr.setRecordId(EscherContainerRecord.SPGR_CONTAINER);
        spgr.setOptions((short)15);

        //The group itself is a shape, and always appears as the first EscherSpContainer in the group container.
        EscherContainerRecord spcont = new EscherContainerRecord();
        spcont.setRecordId(EscherContainerRecord.SP_CONTAINER);
        spcont.setOptions((short)15);

        EscherSpgrRecord spg = new EscherSpgrRecord();
        spg.setOptions((short)1);
        spcont.addChildRecord(spg);

        EscherSpRecord sp = new EscherSpRecord();
        short type = (ShapeTypes.NotPrimitive << 4) + 2;
        sp.setOptions(type);
        sp.setFlags(EscherSpRecord.FLAG_HAVEANCHOR | EscherSpRecord.FLAG_GROUP);
        spcont.addChildRecord(sp);

        EscherClientAnchorRecord anchor = new EscherClientAnchorRecord();
        spcont.addChildRecord(anchor);

        spgr.addChildRecord(spcont);
        return spgr;
    
public java.awt.RectanglegetAnchor()
Returns the anchor (the bounding box rectangle) of this shape group. All coordinates are expressed in points (72 dpi).

return
the anchor of this shape group

        EscherContainerRecord groupInfoContainer = (EscherContainerRecord)_escherContainer.getChild(0);
        EscherSpgrRecord spgr = (EscherSpgrRecord)getEscherChild(groupInfoContainer, EscherSpgrRecord.RECORD_ID);
        java.awt.Rectangle anchor=null;

        anchor = new java.awt.Rectangle();
        anchor.x = spgr.getRectX1()*POINT_DPI/MASTER_DPI;
        anchor.y = spgr.getRectY1()*POINT_DPI/MASTER_DPI;
        anchor.width = (spgr.getRectX2() - spgr.getRectX1())*POINT_DPI/MASTER_DPI;
        anchor.height = (spgr.getRectY2() - spgr.getRectY1())*POINT_DPI/MASTER_DPI;
        return anchor;
    
public org.apache.poi.hslf.model.HyperlinkgetHyperlink()
Returns null - shape groups can't have hyperlinks

return
null.

        return null;
    
public intgetShapeType()
Return type of the shape. In most cases shape group type is {@link org.apache.poi.hslf.model.ShapeTypes#NotPrimitive}

return
type of the shape.

        EscherContainerRecord groupInfoContainer = (EscherContainerRecord)_escherContainer.getChild(0);
        EscherSpRecord spRecord = groupInfoContainer.getChildById(EscherSpRecord.RECORD_ID);
        return spRecord.getOptions() >> 4;
    
public org.apache.poi.hslf.model.Shape[]getShapes()

return
the shapes contained in this group container

    	// Out escher container record should contain serveral
        //  SpContainers, the first of which is the group shape itself
        List lst = _escherContainer.getChildRecords();

        ArrayList shapeList = new ArrayList();
        // Don't include the first SpContainer, it is always NotPrimitive
        for (int i = 1; i < lst.size(); i++){
        	EscherRecord r = (EscherRecord)lst.get(i);
        	if(r instanceof EscherContainerRecord) {
        		// Create the Shape for it
        		EscherContainerRecord container = (EscherContainerRecord)r;
        		Shape shape = ShapeFactory.createShape(container, this);
                shape.setSheet(getSheet());
        		shapeList.add( shape );
        	} else {
        		// Should we do anything special with these non
        		//  Container records?
        		logger.log(POILogger.ERROR, "Shape contained non container escher record, was " + r.getClass().getName());
        	}
        }
        
        // Put the shapes into an array, and return
        Shape[] shapes = (Shape[])shapeList.toArray(new Shape[shapeList.size()]);
        return shapes;
    
public voidmoveTo(int x, int y)
Moves this ShapeGroup to the specified location.

param
x the x coordinate of the top left corner of the shape in new location
param
y the y coordinate of the top left corner of the shape in new location

        java.awt.Rectangle anchor = getAnchor();
        int dx = x - anchor.x;
        int dy = y - anchor.y;
        anchor.translate(dx, dy);
        setAnchor(anchor);

        Shape[] shape = getShapes();
        for (int i = 0; i < shape.length; i++) {
            java.awt.Rectangle chanchor = shape[i].getAnchor();
            chanchor.translate(dx, dy);
            shape[i].setAnchor(chanchor);
        }
    
public voidsetAnchor(java.awt.Rectangle anchor)
Sets the anchor (the bounding box rectangle) of this shape. All coordinates should be expressed in Master units (576 dpi).

param
anchor new anchor


        EscherContainerRecord spContainer = (EscherContainerRecord)_escherContainer.getChildRecords().get(0);

        EscherClientAnchorRecord clientAnchor = (EscherClientAnchorRecord)getEscherChild(spContainer, EscherClientAnchorRecord.RECORD_ID);
        //hack. internal variable EscherClientAnchorRecord.shortRecord can be
        //initialized only in fillFields(). We need to set shortRecord=false;
        byte[] header = new byte[16];
        LittleEndian.putUShort(header, 0, 0);
        LittleEndian.putUShort(header, 2, 0);
        LittleEndian.putInt(header, 4, 8);
        clientAnchor.fillFields(header, 0, null);

        clientAnchor.setFlag((short)(anchor.y*MASTER_DPI/POINT_DPI));
        clientAnchor.setCol1((short)(anchor.x*MASTER_DPI/POINT_DPI));
        clientAnchor.setDx1((short)((anchor.width + anchor.x)*MASTER_DPI/POINT_DPI));
        clientAnchor.setRow1((short)((anchor.height + anchor.y)*MASTER_DPI/POINT_DPI));

        EscherSpgrRecord spgr = (EscherSpgrRecord)getEscherChild(spContainer, EscherSpgrRecord.RECORD_ID);

        spgr.setRectX1(anchor.x*MASTER_DPI/POINT_DPI);
        spgr.setRectY1(anchor.y*MASTER_DPI/POINT_DPI);
        spgr.setRectX2((anchor.x + anchor.width)*MASTER_DPI/POINT_DPI);
        spgr.setRectY2((anchor.y + anchor.height)*MASTER_DPI/POINT_DPI);