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
;
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();
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
;
53 * Creates the patriarch.
55 * @param sheet the sheet this patriarch is stored in.
57 HSSFPatriarch(HSSFSheet sheet
, EscherAggregate boundAggregate
)
59 this.boundAggregate
= boundAggregate
;
64 * Creates a new group record stored under this patriarch.
66 * @param anchor the client anchor describes how this group is attached
68 * @return the newly created group.
70 public HSSFShapeGroup
createGroup(HSSFClientAnchor anchor
)
72 HSSFShapeGroup group
= new HSSFShapeGroup(null, anchor
);
73 group
.anchor
= anchor
;
79 * Creates a simple shape. This includes such shapes as lines, rectangles,
82 * @param anchor the client anchor describes how this group is attached
84 * @return the newly created shape.
86 public HSSFSimpleShape
createSimpleShape(HSSFClientAnchor anchor
)
88 HSSFSimpleShape shape
= new HSSFSimpleShape(null, anchor
);
89 shape
.anchor
= anchor
;
97 * @param anchor the client anchor describes how this group is attached
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;
115 * @param anchor the client anchor describes how this group is attached
117 * @return the newly created shape.
119 public HSSFPolygon
createPolygon(HSSFClientAnchor anchor
)
121 HSSFPolygon shape
= new HSSFPolygon(null, anchor
);
122 shape
.anchor
= anchor
;
128 * Constructs a textbox under the patriarch.
130 * @param anchor the client anchor describes how this group is attached
132 * @return the newly created textbox.
134 public HSSFTextbox
createTextbox(HSSFClientAnchor anchor
)
136 HSSFTextbox shape
= new HSSFTextbox(null, anchor
);
137 shape
.anchor
= anchor
;
143 * Constructs a cell comment.
145 * @param anchor the client anchor describes how this comment is attached
147 * @return the newly created comment.
149 public HSSFComment
createComment(HSSFAnchor anchor
)
151 HSSFComment shape
= new HSSFComment(null, anchor
);
152 shape
.anchor
= anchor
;
158 * Returns a list of all shapes contained by the patriarch.
160 public List
getChildren()
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();
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
)
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
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")) {
224 * The top left x coordinate of this group.
232 * The top left y coordinate of this group.
240 * The bottom right x coordinate of this group.
248 * The bottom right y coordinate of this group.
256 * Returns the aggregate escher record we're bound to
258 protected EscherAggregate
_getBoundAggregate() {
259 return boundAggregate
;