Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /* ====================================================================
     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.hslf.model;
 
 import java.util.List;
 
Represents a group of shapes.

Author(s):
Yegor Kozlov
 
 public class ShapeGroup extends Shape{

    
Create a new ShapeGroup. This constructor is used when a new shape is created.
 
     public ShapeGroup(){
         this(nullnull);
          = createSpContainer(false);
     }

    
Create a ShapeGroup object and initilize it from the supplied Record container.

Parameters:
escherRecord EscherSpContainer container which holds information about this shape
parent the parent of the shape
 
     protected ShapeGroup(EscherContainerRecord escherRecordShape parent){
         super(escherRecordparent);
     }

    

Returns:
the shapes contained in this group container
 
     public Shape[] getShapes() {
     	// Out escher container record should contain several
         //  SpContainers, the first of which is the group shape itself
 
         // Don't include the first SpContainer, it is always NotPrimitive
         if (iter.hasNext()) {
         	iter.next();
         }
         List<ShapeshapeList = new ArrayList<Shape>();
         while (iter.hasNext()) {
         	EscherRecord r = iter.next();
         	if(r instanceof EscherContainerRecord) {
         		// Create the Shape for it
         		EscherContainerRecord container = (EscherContainerRecord)r;
         		Shape shape = ShapeFactory.createShape(containerthis);
                 shape.setSheet(getSheet());
         		shapeList.addshape );
         	} else {
         		// Should we do anything special with these non
         		//  Container records?
         		.log(."Shape contained non container escher record, was " + r.getClass().getName());
         	}
         }
 
         // Put the shapes into an array, and return
         Shape[] shapes = shapeList.toArray(new Shape[shapeList.size()]);
         return shapes;
     }

    
Sets the anchor (the bounding box rectangle) of this shape. All coordinates should be expressed in Master units (576 dpi).

Parameters:
anchor new anchor
    public void setAnchor(java.awt.Rectangle anchor){
        //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*/));
        clientAnchor.setCol1((short)(anchor.x*/));
        clientAnchor.setDx1((short)((anchor.width + anchor.x)*/));
        clientAnchor.setRow1((short)((anchor.height + anchor.y)*/));
        spgr.setRectX1(anchor.x*/);
        spgr.setRectY1(anchor.y*/);
        spgr.setRectX2((anchor.x + anchor.width)*/);
        spgr.setRectY2((anchor.y + anchor.height)*/);
    }

    
Sets the coordinate space of this group. All children are constrained to these coordinates.

Parameters:
anchor the coordinate space of this group
    public void setCoordinates(Rectangle2D anchor){
        int x1 = (int)Math.round(anchor.getX()*/);
        int y1 = (int)Math.round(anchor.getY()*/);
        int x2 = (int)Math.round((anchor.getX() + anchor.getWidth())*/);
        int y2 = (int)Math.round((anchor.getY() + anchor.getHeight())*/);
        spgr.setRectX1(x1);
        spgr.setRectY1(y1);
        spgr.setRectX2(x2);
        spgr.setRectY2(y2);
    }

    
Gets the coordinate space of this group. All children are constrained to these coordinates.

Returns:
the coordinate space of this group
    public Rectangle2D getCoordinates(){
        Rectangle2D.Float anchor = new Rectangle2D.Float();
        anchor.x = (float)spgr.getRectX1()*/;
        anchor.y = (float)spgr.getRectY1()*/;
        anchor.width = (float)(spgr.getRectX2() - spgr.getRectX1())*/;
        anchor.height = (float)(spgr.getRectY2() - spgr.getRectY1())*/;
        return anchor;
    }

    
Create a new ShapeGroup and create an instance of EscherSpgrContainer which represents a group of shapes
    protected EscherContainerRecord createSpContainer(boolean isChild) {
        EscherContainerRecord spgr = new EscherContainerRecord();
        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.setOptions((short)15);
        EscherSpgrRecord spg = new EscherSpgrRecord();
        spg.setOptions((short)1);
        spcont.addChildRecord(spg);
        EscherSpRecord sp = new EscherSpRecord();
        short type = (. << 4) + 2;
        sp.setOptions(type);
        spcont.addChildRecord(sp);
        EscherClientAnchorRecord anchor = new EscherClientAnchorRecord();
        spcont.addChildRecord(anchor);
        spgr.addChildRecord(spcont);
        return spgr;
    }

    
Add a shape to this group.

Parameters:
shape - the Shape to add
    public void addShape(Shape shape){
        Sheet sheet = getSheet();
        shape.setSheet(sheet);
        shape.setShapeId(sheet.allocateShapeId());
        shape.afterInsert(sheet);
    }

    
Moves this ShapeGroup to the specified location.

Parameters:
x the x coordinate of the top left corner of the shape in new location
y the y coordinate of the top left corner of the shape in new location
    public void moveTo(int xint y){
        java.awt.Rectangle anchor = getAnchor();
        int dx = x - anchor.x;
        int dy = y - anchor.y;
        anchor.translate(dxdy);
        setAnchor(anchor);
        Shape[] shape = getShapes();
        for (int i = 0; i < shape.lengthi++) {
            java.awt.Rectangle chanchor = shape[i].getAnchor();
            chanchor.translate(dxdy);
            shape[i].setAnchor(chanchor);
        }
    }

    
Returns the anchor (the bounding box rectangle) of this shape group. All coordinates are expressed in points (72 dpi).

Returns:
the anchor of this shape group
    public Rectangle2D getAnchor2D(){
        Rectangle2D.Float anchor = new Rectangle2D.Float();
        if(clientAnchor == null){
            .log(."EscherClientAnchorRecord was not found for shape group. Searching for EscherChildAnchorRecord.");
            anchor = new Rectangle2D.Float(
                (float)rec.getDx1()*/,
                (float)rec.getDy1()*/,
                (float)(rec.getDx2()-rec.getDx1())*/,
                (float)(rec.getDy2()-rec.getDy1())*/
            );
        } else {
            anchor.x = (float)clientAnchor.getCol1()*/;
            anchor.y = (float)clientAnchor.getFlag()*/;
            anchor.width = (float)(clientAnchor.getDx1() - clientAnchor.getCol1())*/ ;
            anchor.height = (float)(clientAnchor.getRow1() - clientAnchor.getFlag())*/;
        }
        return anchor;
    }

    
Return type of the shape. In most cases shape group type is org.apache.poi.sl.usermodel.ShapeTypes.NotPrimitive

Returns:
type of the shape.
    public int getShapeType(){
        EscherContainerRecord groupInfoContainer = (EscherContainerRecord).getChild(0);
        EscherSpRecord spRecord = groupInfoContainer.getChildById(.);
        return spRecord.getOptions() >> 4;
    }

    
Returns null - shape groups can't have hyperlinks

Returns:
null.
     public Hyperlink getHyperlink(){
        return null;
    }
    public void draw(Graphics2D graphics){
        AffineTransform at = graphics.getTransform();
        Shape[] sh = getShapes();
        for (int i = 0; i < sh.lengthi++) {
            sh[i].draw(graphics);
        }
        graphics.setTransform(at);
    }
New to GrepCode? Check out our FAQ X