You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

HSSFPatriarch.java 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.hssf.usermodel;
  16. import java.util.ArrayList;
  17. import java.util.Iterator;
  18. import java.util.List;
  19. import org.apache.poi.ddf.EscherComplexProperty;
  20. import org.apache.poi.ddf.EscherOptRecord;
  21. import org.apache.poi.ddf.EscherProperty;
  22. import org.apache.poi.hssf.record.EscherAggregate;
  23. import org.apache.poi.util.StringUtil;
  24. import org.apache.poi.ss.usermodel.Drawing;
  25. import org.apache.poi.ss.usermodel.ClientAnchor;
  26. /**
  27. * The patriarch is the toplevel container for shapes in a sheet. It does
  28. * little other than act as a container for other shapes and groups.
  29. *
  30. * @author Glen Stampoultzis (glens at apache.org)
  31. */
  32. public final class HSSFPatriarch implements HSSFShapeContainer, Drawing {
  33. private final List<HSSFShape> _shapes = new ArrayList<HSSFShape>();
  34. private int _x1 = 0;
  35. private int _y1 = 0 ;
  36. private int _x2 = 1023;
  37. private int _y2 = 255;
  38. /**
  39. * The EscherAggregate we have been bound to.
  40. * (This will handle writing us out into records,
  41. * and building up our shapes from the records)
  42. */
  43. private EscherAggregate _boundAggregate;
  44. final HSSFSheet _sheet; // TODO make private
  45. /**
  46. * Creates the patriarch.
  47. *
  48. * @param sheet the sheet this patriarch is stored in.
  49. */
  50. HSSFPatriarch(HSSFSheet sheet, EscherAggregate boundAggregate){
  51. _sheet = sheet;
  52. _boundAggregate = boundAggregate;
  53. }
  54. /**
  55. * Creates a new group record stored under this patriarch.
  56. *
  57. * @param anchor the client anchor describes how this group is attached
  58. * to the sheet.
  59. * @return the newly created group.
  60. */
  61. public HSSFShapeGroup createGroup(HSSFClientAnchor anchor)
  62. {
  63. HSSFShapeGroup group = new HSSFShapeGroup(null, anchor);
  64. group.anchor = anchor;
  65. _shapes.add(group);
  66. return group;
  67. }
  68. /**
  69. * Creates a simple shape. This includes such shapes as lines, rectangles,
  70. * and ovals.
  71. *
  72. * @param anchor the client anchor describes how this group is attached
  73. * to the sheet.
  74. * @return the newly created shape.
  75. */
  76. public HSSFSimpleShape createSimpleShape(HSSFClientAnchor anchor)
  77. {
  78. HSSFSimpleShape shape = new HSSFSimpleShape(null, anchor);
  79. shape.anchor = anchor;
  80. _shapes.add(shape);
  81. return shape;
  82. }
  83. /**
  84. * Creates a picture.
  85. *
  86. * @param anchor the client anchor describes how this group is attached
  87. * to the sheet.
  88. * @return the newly created shape.
  89. */
  90. public HSSFPicture createPicture(HSSFClientAnchor anchor, int pictureIndex)
  91. {
  92. HSSFPicture shape = new HSSFPicture(null, anchor);
  93. shape.setPictureIndex( pictureIndex );
  94. shape.anchor = anchor;
  95. shape._patriarch = this;
  96. _shapes.add(shape);
  97. return shape;
  98. }
  99. public HSSFPicture createPicture(ClientAnchor anchor, int pictureIndex)
  100. {
  101. return createPicture((HSSFClientAnchor)anchor, pictureIndex);
  102. }
  103. /**
  104. * Creates a polygon
  105. *
  106. * @param anchor the client anchor describes how this group is attached
  107. * to the sheet.
  108. * @return the newly created shape.
  109. */
  110. public HSSFPolygon createPolygon(HSSFClientAnchor anchor)
  111. {
  112. HSSFPolygon shape = new HSSFPolygon(null, anchor);
  113. shape.anchor = anchor;
  114. _shapes.add(shape);
  115. return shape;
  116. }
  117. /**
  118. * Constructs a textbox under the patriarch.
  119. *
  120. * @param anchor the client anchor describes how this group is attached
  121. * to the sheet.
  122. * @return the newly created textbox.
  123. */
  124. public HSSFTextbox createTextbox(HSSFClientAnchor anchor)
  125. {
  126. HSSFTextbox shape = new HSSFTextbox(null, anchor);
  127. shape.anchor = anchor;
  128. _shapes.add(shape);
  129. return shape;
  130. }
  131. /**
  132. * Constructs a cell comment.
  133. *
  134. * @param anchor the client anchor describes how this comment is attached
  135. * to the sheet.
  136. * @return the newly created comment.
  137. */
  138. public HSSFComment createComment(HSSFAnchor anchor)
  139. {
  140. HSSFComment shape = new HSSFComment(null, anchor);
  141. shape.anchor = anchor;
  142. _shapes.add(shape);
  143. return shape;
  144. }
  145. @Override
  146. public HSSFComment createCellComment(ClientAnchor anchor){
  147. return createComment((HSSFAnchor)anchor);
  148. }
  149. /**
  150. * Returns a list of all shapes contained by the patriarch.
  151. */
  152. public List<HSSFShape> getChildren()
  153. {
  154. return _shapes;
  155. }
  156. /**
  157. * Total count of all children and their children's children.
  158. */
  159. public int countOfAllChildren() {
  160. int count = _shapes.size();
  161. for (Iterator<HSSFShape> iterator = _shapes.iterator(); iterator.hasNext();) {
  162. HSSFShape shape = iterator.next();
  163. count += shape.countOfAllChildren();
  164. }
  165. return count;
  166. }
  167. /**
  168. * Sets the coordinate space of this group. All children are constrained
  169. * to these coordinates.
  170. */
  171. public void setCoordinates(int x1, int y1, int x2, int y2){
  172. _x1 = x1;
  173. _y1 = y1;
  174. _x2 = x2;
  175. _y2 = y2;
  176. }
  177. /**
  178. * Does this HSSFPatriarch contain a chart?
  179. * (Technically a reference to a chart, since they
  180. * get stored in a different block of records)
  181. * FIXME - detect chart in all cases (only seems
  182. * to work on some charts so far)
  183. */
  184. public boolean containsChart() {
  185. // TODO - support charts properly in usermodel
  186. // We're looking for a EscherOptRecord
  187. EscherOptRecord optRecord = (EscherOptRecord)
  188. _boundAggregate.findFirstWithId(EscherOptRecord.RECORD_ID);
  189. if(optRecord == null) {
  190. // No opt record, can't have chart
  191. return false;
  192. }
  193. for(Iterator<EscherProperty> it = optRecord.getEscherProperties().iterator(); it.hasNext();) {
  194. EscherProperty prop = it.next();
  195. if(prop.getPropertyNumber() == 896 && prop.isComplex()) {
  196. EscherComplexProperty cp = (EscherComplexProperty)prop;
  197. String str = StringUtil.getFromUnicodeLE(cp.getComplexData());
  198. if(str.equals("Chart 1\0")) {
  199. return true;
  200. }
  201. }
  202. }
  203. return false;
  204. }
  205. /**
  206. * The top left x coordinate of this group.
  207. */
  208. public int getX1()
  209. {
  210. return _x1;
  211. }
  212. /**
  213. * The top left y coordinate of this group.
  214. */
  215. public int getY1()
  216. {
  217. return _y1;
  218. }
  219. /**
  220. * The bottom right x coordinate of this group.
  221. */
  222. public int getX2()
  223. {
  224. return _x2;
  225. }
  226. /**
  227. * The bottom right y coordinate of this group.
  228. */
  229. public int getY2()
  230. {
  231. return _y2;
  232. }
  233. /**
  234. * Returns the aggregate escher record we're bound to
  235. */
  236. protected EscherAggregate _getBoundAggregate() {
  237. return _boundAggregate;
  238. }
  239. }