Inspired by bug #44958 - Record level support for Data Tables. (No formula parser...
[poi.git] / src / java / org / apache / poi / hssf / usermodel / HSSFPatriarch.java
blob9b0d7070680d212a14198937b1880e2416bcd4a3
1 /* ====================================================================
2 Licensed to the Apache Software Foundation (ASF) under one or more
3 contributor license agreements. See the NOTICE file distributed with
4 this work for additional information regarding copyright ownership.
5 The ASF licenses this file to You under the Apache License, Version 2.0
6 (the "License"); you may not use this file except in compliance with
7 the License. You may obtain a copy of the License at
9 http://www.apache.org/licenses/LICENSE-2.0
11 Unless required by applicable law or agreed to in writing, software
12 distributed under the License is distributed on an "AS IS" BASIS,
13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 See the License for the specific language governing permissions and
15 limitations under the License.
16 ==================================================================== */
18 package org.apache.poi.hssf.usermodel;
20 import java.util.ArrayList;
21 import java.util.Iterator;
22 import java.util.List;
24 import org.apache.poi.ddf.EscherComplexProperty;
25 import org.apache.poi.ddf.EscherOptRecord;
26 import org.apache.poi.ddf.EscherProperty;
27 import org.apache.poi.hssf.record.EscherAggregate;
28 import org.apache.poi.util.StringUtil;
30 /**
31 * The patriarch is the toplevel container for shapes in a sheet. It does
32 * little other than act as a container for other shapes and groups.
34 * @author Glen Stampoultzis (glens at apache.org)
36 public final class HSSFPatriarch implements HSSFShapeContainer
38 List shapes = new ArrayList();
39 HSSFSheet sheet;
40 int x1 = 0;
41 int y1 = 0 ;
42 int x2 = 1023;
43 int y2 = 255;
45 /**
46 * The EscherAggregate we have been bound to.
47 * (This will handle writing us out into records,
48 * and building up our shapes from the records)
50 private EscherAggregate boundAggregate;
52 /**
53 * Creates the patriarch.
55 * @param sheet the sheet this patriarch is stored in.
57 HSSFPatriarch(HSSFSheet sheet, EscherAggregate boundAggregate)
59 this.boundAggregate = boundAggregate;
60 this.sheet = sheet;
63 /**
64 * Creates a new group record stored under this patriarch.
66 * @param anchor the client anchor describes how this group is attached
67 * to the sheet.
68 * @return the newly created group.
70 public HSSFShapeGroup createGroup(HSSFClientAnchor anchor)
72 HSSFShapeGroup group = new HSSFShapeGroup(null, anchor);
73 group.anchor = anchor;
74 shapes.add(group);
75 return group;
78 /**
79 * Creates a simple shape. This includes such shapes as lines, rectangles,
80 * and ovals.
82 * @param anchor the client anchor describes how this group is attached
83 * to the sheet.
84 * @return the newly created shape.
86 public HSSFSimpleShape createSimpleShape(HSSFClientAnchor anchor)
88 HSSFSimpleShape shape = new HSSFSimpleShape(null, anchor);
89 shape.anchor = anchor;
90 shapes.add(shape);
91 return shape;
94 /**
95 * Creates a picture.
97 * @param anchor the client anchor describes how this group is attached
98 * to the sheet.
99 * @return the newly created shape.
101 public HSSFPicture createPicture(HSSFClientAnchor anchor, int pictureIndex)
103 HSSFPicture shape = new HSSFPicture(null, anchor);
104 shape.setPictureIndex( pictureIndex );
105 shape.anchor = anchor;
106 shape.patriarch = this;
107 shapes.add(shape);
108 return shape;
113 * Creates a polygon
115 * @param anchor the client anchor describes how this group is attached
116 * to the sheet.
117 * @return the newly created shape.
119 public HSSFPolygon createPolygon(HSSFClientAnchor anchor)
121 HSSFPolygon shape = new HSSFPolygon(null, anchor);
122 shape.anchor = anchor;
123 shapes.add(shape);
124 return shape;
128 * Constructs a textbox under the patriarch.
130 * @param anchor the client anchor describes how this group is attached
131 * to the sheet.
132 * @return the newly created textbox.
134 public HSSFTextbox createTextbox(HSSFClientAnchor anchor)
136 HSSFTextbox shape = new HSSFTextbox(null, anchor);
137 shape.anchor = anchor;
138 shapes.add(shape);
139 return shape;
143 * Constructs a cell comment.
145 * @param anchor the client anchor describes how this comment is attached
146 * to the sheet.
147 * @return the newly created comment.
149 public HSSFComment createComment(HSSFAnchor anchor)
151 HSSFComment shape = new HSSFComment(null, anchor);
152 shape.anchor = anchor;
153 shapes.add(shape);
154 return shape;
158 * Returns a list of all shapes contained by the patriarch.
160 public List getChildren()
162 return shapes;
166 * Total count of all children and their children's children.
168 public int countOfAllChildren()
170 int count = shapes.size();
171 for ( Iterator iterator = shapes.iterator(); iterator.hasNext(); )
173 HSSFShape shape = (HSSFShape) iterator.next();
174 count += shape.countOfAllChildren();
176 return count;
179 * Sets the coordinate space of this group. All children are contrained
180 * to these coordinates.
182 public void setCoordinates( int x1, int y1, int x2, int y2 )
184 this.x1 = x1;
185 this.y1 = y1;
186 this.x2 = x2;
187 this.y2 = y2;
191 * Does this HSSFPatriarch contain a chart?
192 * (Technically a reference to a chart, since they
193 * get stored in a different block of records)
194 * FIXME - detect chart in all cases (only seems
195 * to work on some charts so far)
197 public boolean containsChart() {
198 // TODO - support charts properly in usermodel
200 // We're looking for a EscherOptRecord
201 EscherOptRecord optRecord = (EscherOptRecord)
202 boundAggregate.findFirstWithId(EscherOptRecord.RECORD_ID);
203 if(optRecord == null) {
204 // No opt record, can't have chart
205 return false;
208 for(Iterator it = optRecord.getEscherProperties().iterator(); it.hasNext();) {
209 EscherProperty prop = (EscherProperty)it.next();
210 if(prop.getPropertyNumber() == 896 && prop.isComplex()) {
211 EscherComplexProperty cp = (EscherComplexProperty)prop;
212 String str = StringUtil.getFromUnicodeLE(cp.getComplexData());
213 //System.err.println(str);
214 if(str.equals("Chart 1\0")) {
215 return true;
220 return false;
224 * The top left x coordinate of this group.
226 public int getX1()
228 return x1;
232 * The top left y coordinate of this group.
234 public int getY1()
236 return y1;
240 * The bottom right x coordinate of this group.
242 public int getX2()
244 return x2;
248 * The bottom right y coordinate of this group.
250 public int getY2()
252 return y2;
256 * Returns the aggregate escher record we're bound to
258 protected EscherAggregate _getBoundAggregate() {
259 return boundAggregate;