Search in sources :

Example 6 with EscherChildAnchorRecord

use of org.apache.poi.ddf.EscherChildAnchorRecord in project poi by apache.

the class HSSFShape method setAnchor.

/**
     * Sets a particular anchor.  A top-level shape must have an anchor of
     * HSSFClientAnchor.  A child anchor must have an anchor of HSSFChildAnchor
     *
     * @param anchor the anchor to use.
     * @throws IllegalArgumentException when the wrong anchor is used for
     *                                  this particular shape.
     * @see HSSFChildAnchor
     * @see HSSFClientAnchor
     */
public void setAnchor(HSSFAnchor anchor) {
    int i = 0;
    int recordId = -1;
    if (parent == null) {
        if (anchor instanceof HSSFChildAnchor)
            throw new IllegalArgumentException("Must use client anchors for shapes directly attached to sheet.");
        EscherClientAnchorRecord anch = _escherContainer.getChildById(EscherClientAnchorRecord.RECORD_ID);
        if (null != anch) {
            for (i = 0; i < _escherContainer.getChildRecords().size(); i++) {
                if (_escherContainer.getChild(i).getRecordId() == EscherClientAnchorRecord.RECORD_ID) {
                    if (i != _escherContainer.getChildRecords().size() - 1) {
                        recordId = _escherContainer.getChild(i + 1).getRecordId();
                    }
                }
            }
            _escherContainer.removeChildRecord(anch);
        }
    } else {
        if (anchor instanceof HSSFClientAnchor)
            throw new IllegalArgumentException("Must use child anchors for shapes attached to groups.");
        EscherChildAnchorRecord anch = _escherContainer.getChildById(EscherChildAnchorRecord.RECORD_ID);
        if (null != anch) {
            for (i = 0; i < _escherContainer.getChildRecords().size(); i++) {
                if (_escherContainer.getChild(i).getRecordId() == EscherChildAnchorRecord.RECORD_ID) {
                    if (i != _escherContainer.getChildRecords().size() - 1) {
                        recordId = _escherContainer.getChild(i + 1).getRecordId();
                    }
                }
            }
            _escherContainer.removeChildRecord(anch);
        }
    }
    if (-1 == recordId) {
        _escherContainer.addChildRecord(anchor.getEscherAnchor());
    } else {
        _escherContainer.addChildBefore(anchor.getEscherAnchor(), recordId);
    }
    this.anchor = anchor;
}
Also used : EscherChildAnchorRecord(org.apache.poi.ddf.EscherChildAnchorRecord) EscherClientAnchorRecord(org.apache.poi.ddf.EscherClientAnchorRecord)

Aggregations

EscherChildAnchorRecord (org.apache.poi.ddf.EscherChildAnchorRecord)6 EscherClientAnchorRecord (org.apache.poi.ddf.EscherClientAnchorRecord)6 EscherSpRecord (org.apache.poi.ddf.EscherSpRecord)3 Rectangle2D (java.awt.geom.Rectangle2D)2 AbstractEscherOptRecord (org.apache.poi.ddf.AbstractEscherOptRecord)1 EscherContainerRecord (org.apache.poi.ddf.EscherContainerRecord)1 EscherOptRecord (org.apache.poi.ddf.EscherOptRecord)1 EscherRecord (org.apache.poi.ddf.EscherRecord)1 HSSFChildAnchor (org.apache.poi.hssf.usermodel.HSSFChildAnchor)1 HSSFClientAnchor (org.apache.poi.hssf.usermodel.HSSFClientAnchor)1 DrawPaint (org.apache.poi.sl.draw.DrawPaint)1 SolidPaint (org.apache.poi.sl.usermodel.PaintStyle.SolidPaint)1