123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338 |
- /* ====================================================================
- 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.hssf.usermodel;
-
- import java.util.ArrayList;
- import java.util.Iterator;
- import java.util.List;
-
- import org.apache.poi.ddf.EscherComplexProperty;
- import org.apache.poi.ddf.EscherContainerRecord;
- import org.apache.poi.ddf.EscherOptRecord;
- import org.apache.poi.ddf.EscherProperty;
- import org.apache.poi.ddf.EscherBSERecord;
- import org.apache.poi.ddf.EscherSpgrRecord;
- import org.apache.poi.hssf.record.EscherAggregate;
- import org.apache.poi.ss.usermodel.Chart;
- import org.apache.poi.util.StringUtil;
- import org.apache.poi.util.Internal;
- import org.apache.poi.ss.usermodel.Drawing;
- import org.apache.poi.ss.usermodel.ClientAnchor;
-
- /**
- * 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)
- */
- 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;
-
- /**
- * The EscherAggregate we have been bound to.
- * (This will handle writing us out into records,
- * and building up our shapes from the records)
- */
- private EscherAggregate _boundAggregate;
- final HSSFSheet _sheet; // TODO make private
-
- /**
- * Creates the patriarch.
- *
- * @param sheet the sheet this patriarch is stored in.
- */
- HSSFPatriarch(HSSFSheet sheet, EscherAggregate boundAggregate){
- _sheet = sheet;
- _boundAggregate = boundAggregate;
- }
-
- /**
- * 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;
- addShape(group);
- 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;
- addShape(shape);
- return shape;
- }
-
- /**
- * 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;
- addShape(shape);
-
- EscherBSERecord bse = _sheet.getWorkbook().getWorkbook().getBSERecord(pictureIndex);
- bse.setRef(bse.getRef() + 1);
- return shape;
- }
-
- public HSSFPicture createPicture(ClientAnchor anchor, int pictureIndex)
- {
- return createPicture((HSSFClientAnchor)anchor, pictureIndex);
- }
-
- /**
- * 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;
- addShape(shape);
- 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;
- addShape(shape);
- return shape;
- }
-
- /**
- * 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;
- addShape(shape);
- return shape;
- }
-
- /**
- * YK: used to create autofilters
- *
- * @see org.apache.poi.hssf.usermodel.HSSFSheet#setAutoFilter(org.apache.poi.ss.util.CellRangeAddress)
- */
- HSSFSimpleShape createComboBox(HSSFAnchor anchor)
- {
- HSSFSimpleShape shape = new HSSFSimpleShape(null, anchor);
- shape.setShapeType(HSSFSimpleShape.OBJECT_TYPE_COMBO_BOX);
- shape.anchor = anchor;
- addShape(shape);
- return shape;
- }
-
- public HSSFComment createCellComment(ClientAnchor anchor) {
- return createComment((HSSFAnchor)anchor);
- }
-
- /**
- * Returns a list of all shapes contained by the patriarch.
- */
- public List<HSSFShape> getChildren()
- {
- return _shapes;
- }
-
- /**
- * add a shape to this drawing
- */
- @Internal
- public void addShape(HSSFShape shape){
- shape._patriarch = this;
- _shapes.add(shape);
- }
-
- /**
- * Total count of all children and their children's children.
- */
- public int countOfAllChildren() {
- int count = _shapes.size();
- for (Iterator<HSSFShape> iterator = _shapes.iterator(); iterator.hasNext();) {
- HSSFShape shape = iterator.next();
- count += shape.countOfAllChildren();
- }
- return count;
- }
- /**
- * Sets the coordinate space of this group. All children are constrained
- * to these coordinates.
- */
- public void setCoordinates(int x1, int y1, int x2, int y2){
- _x1 = x1;
- _y1 = y1;
- _x2 = x2;
- _y2 = y2;
- }
-
- /**
- * 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() {
- // TODO - support charts properly in usermodel
-
- // We're looking for a EscherOptRecord
- EscherOptRecord optRecord = (EscherOptRecord)
- _boundAggregate.findFirstWithId(EscherOptRecord.RECORD_ID);
- if(optRecord == null) {
- // No opt record, can't have chart
- return false;
- }
-
- for(Iterator<EscherProperty> it = optRecord.getEscherProperties().iterator(); it.hasNext();) {
- EscherProperty prop = it.next();
- if(prop.getPropertyNumber() == 896 && prop.isComplex()) {
- EscherComplexProperty cp = (EscherComplexProperty)prop;
- String str = StringUtil.getFromUnicodeLE(cp.getComplexData());
-
- if(str.equals("Chart 1\0")) {
- return true;
- }
- }
- }
-
- return false;
- }
-
- /**
- * The top left x coordinate of this group.
- */
- public int getX1()
- {
- return _x1;
- }
-
- /**
- * The top left y coordinate of this group.
- */
- public int getY1()
- {
- return _y1;
- }
-
- /**
- * The bottom right x coordinate of this group.
- */
- public int getX2()
- {
- return _x2;
- }
-
- /**
- * The bottom right y coordinate of this group.
- */
- public int getY2()
- {
- return _y2;
- }
-
- /**
- * Returns the aggregate escher record we're bound to
- */
- protected EscherAggregate _getBoundAggregate() {
- return _boundAggregate;
- }
-
- /**
- * 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");
- }
-
-
- 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);
- }
- }
- }
- }
|