Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

HSSFPatriarch.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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.*;
  20. import org.apache.poi.hssf.model.DrawingManager2;
  21. import org.apache.poi.hssf.record.EscherAggregate;
  22. import org.apache.poi.ss.usermodel.Chart;
  23. import org.apache.poi.util.StringUtil;
  24. import org.apache.poi.util.Internal;
  25. import org.apache.poi.ss.usermodel.Drawing;
  26. import org.apache.poi.ss.usermodel.ClientAnchor;
  27. /**
  28. * The patriarch is the toplevel container for shapes in a sheet. It does
  29. * little other than act as a container for other shapes and groups.
  30. *
  31. * @author Glen Stampoultzis (glens at apache.org)
  32. */
  33. public final class HSSFPatriarch implements HSSFShapeContainer, Drawing {
  34. private final List<HSSFShape> _shapes = new ArrayList<HSSFShape>();
  35. private int _x1 = 0;
  36. private int _y1 = 0 ;
  37. private int _x2 = 1023;
  38. private int _y2 = 255;
  39. /**
  40. * The EscherAggregate we have been bound to.
  41. * (This will handle writing us out into records,
  42. * and building up our shapes from the records)
  43. */
  44. private EscherAggregate _boundAggregate;
  45. final HSSFSheet _sheet; // TODO make private
  46. /**
  47. * Creates the patriarch.
  48. *
  49. * @param sheet the sheet this patriarch is stored in.
  50. */
  51. HSSFPatriarch(HSSFSheet sheet, EscherAggregate boundAggregate){
  52. _sheet = sheet;
  53. _boundAggregate = boundAggregate;
  54. }
  55. /**
  56. * Creates a new group record stored under this patriarch.
  57. *
  58. * @param anchor the client anchor describes how this group is attached
  59. * to the sheet.
  60. * @return the newly created group.
  61. */
  62. public HSSFShapeGroup createGroup(HSSFClientAnchor anchor)
  63. {
  64. HSSFShapeGroup group = new HSSFShapeGroup(null, anchor);
  65. group.anchor = anchor;
  66. addShape(group);
  67. return group;
  68. }
  69. /**
  70. * Creates a simple shape. This includes such shapes as lines, rectangles,
  71. * and ovals.
  72. *
  73. * @param anchor the client anchor describes how this group is attached
  74. * to the sheet.
  75. * @return the newly created shape.
  76. */
  77. public HSSFSimpleShape createSimpleShape(HSSFClientAnchor anchor)
  78. {
  79. HSSFSimpleShape shape = new HSSFSimpleShape(null, anchor);
  80. shape.anchor = anchor;
  81. addShape(shape);
  82. //open existing file
  83. onCreate(shape);
  84. return shape;
  85. }
  86. /**
  87. * Creates a picture.
  88. *
  89. * @param anchor the client anchor describes how this group is attached
  90. * to the sheet.
  91. * @return the newly created shape.
  92. */
  93. public HSSFPicture createPicture(HSSFClientAnchor anchor, int pictureIndex)
  94. {
  95. HSSFPicture shape = new HSSFPicture(null, anchor);
  96. shape.setPictureIndex( pictureIndex );
  97. shape.anchor = anchor;
  98. addShape(shape);
  99. //open existing file
  100. onCreate(shape);
  101. EscherBSERecord bse = _sheet.getWorkbook().getWorkbook().getBSERecord(pictureIndex);
  102. bse.setRef(bse.getRef() + 1);
  103. return shape;
  104. }
  105. public HSSFPicture createPicture(ClientAnchor anchor, int pictureIndex)
  106. {
  107. return createPicture((HSSFClientAnchor)anchor, pictureIndex);
  108. }
  109. /**
  110. * Creates a polygon
  111. *
  112. * @param anchor the client anchor describes how this group is attached
  113. * to the sheet.
  114. * @return the newly created shape.
  115. */
  116. public HSSFPolygon createPolygon(HSSFClientAnchor anchor)
  117. {
  118. HSSFPolygon shape = new HSSFPolygon(null, anchor);
  119. shape.anchor = anchor;
  120. addShape(shape);
  121. return shape;
  122. }
  123. /**
  124. * Constructs a textbox under the patriarch.
  125. *
  126. * @param anchor the client anchor describes how this group is attached
  127. * to the sheet.
  128. * @return the newly created textbox.
  129. */
  130. public HSSFTextbox createTextbox(HSSFClientAnchor anchor)
  131. {
  132. HSSFTextbox shape = new HSSFTextbox(null, anchor);
  133. shape.anchor = anchor;
  134. addShape(shape);
  135. onCreate(shape);
  136. return shape;
  137. }
  138. /**
  139. * Constructs a cell comment.
  140. *
  141. * @param anchor the client anchor describes how this comment is attached
  142. * to the sheet.
  143. * @return the newly created comment.
  144. */
  145. public HSSFComment createComment(HSSFAnchor anchor)
  146. {
  147. HSSFComment shape = new HSSFComment(null, anchor);
  148. shape.anchor = anchor;
  149. addShape(shape);
  150. return shape;
  151. }
  152. /**
  153. * YK: used to create autofilters
  154. *
  155. * @see org.apache.poi.hssf.usermodel.HSSFSheet#setAutoFilter(org.apache.poi.ss.util.CellRangeAddress)
  156. */
  157. HSSFSimpleShape createComboBox(HSSFAnchor anchor)
  158. {
  159. HSSFSimpleShape shape = new HSSFSimpleShape(null, anchor);
  160. shape.setShapeType(HSSFSimpleShape.OBJECT_TYPE_COMBO_BOX);
  161. shape.anchor = anchor;
  162. addShape(shape);
  163. return shape;
  164. }
  165. public HSSFComment createCellComment(ClientAnchor anchor) {
  166. return createComment((HSSFAnchor)anchor);
  167. }
  168. /**
  169. * Returns a list of all shapes contained by the patriarch.
  170. */
  171. public List<HSSFShape> getChildren()
  172. {
  173. return _shapes;
  174. }
  175. /**
  176. * add a shape to this drawing
  177. */
  178. @Internal
  179. public void addShape(HSSFShape shape){
  180. shape._patriarch = this;
  181. _shapes.add(shape);
  182. }
  183. private void onCreate(HSSFShape shape){
  184. if(_boundAggregate.getPatriarch() == null){
  185. EscherContainerRecord spgrContainer =
  186. _boundAggregate.getEscherContainer().getChildContainers().get(0);
  187. EscherContainerRecord spContainer = shape.getEscherContainer();
  188. int shapeId = newShapeId();
  189. shape.setShapeId(shapeId);
  190. spgrContainer.addChildRecord(spContainer);
  191. shape.afterInsert(this);
  192. }
  193. }
  194. /**
  195. * Total count of all children and their children's children.
  196. */
  197. public int countOfAllChildren() {
  198. int count = _shapes.size();
  199. for (Iterator<HSSFShape> iterator = _shapes.iterator(); iterator.hasNext();) {
  200. HSSFShape shape = iterator.next();
  201. count += shape.countOfAllChildren();
  202. }
  203. return count;
  204. }
  205. /**
  206. * Sets the coordinate space of this group. All children are constrained
  207. * to these coordinates.
  208. */
  209. public void setCoordinates(int x1, int y1, int x2, int y2){
  210. _x1 = x1;
  211. _y1 = y1;
  212. _x2 = x2;
  213. _y2 = y2;
  214. }
  215. int newShapeId() {
  216. if (_boundAggregate.getEscherContainer() == null){
  217. throw new IllegalStateException("We can use this method for only existing files");
  218. }
  219. DrawingManager2 dm = _boundAggregate.getDrawingManager();
  220. EscherDgRecord dg =
  221. _boundAggregate.getEscherContainer().getChildById(EscherDgRecord.RECORD_ID);
  222. short drawingGroupId = dg.getDrawingGroupId();
  223. return dm.allocateShapeId(drawingGroupId, dg);
  224. }
  225. /**
  226. * Does this HSSFPatriarch contain a chart?
  227. * (Technically a reference to a chart, since they
  228. * get stored in a different block of records)
  229. * FIXME - detect chart in all cases (only seems
  230. * to work on some charts so far)
  231. */
  232. public boolean containsChart() {
  233. // TODO - support charts properly in usermodel
  234. // We're looking for a EscherOptRecord
  235. EscherOptRecord optRecord = (EscherOptRecord)
  236. _boundAggregate.findFirstWithId(EscherOptRecord.RECORD_ID);
  237. if(optRecord == null) {
  238. // No opt record, can't have chart
  239. return false;
  240. }
  241. for(Iterator<EscherProperty> it = optRecord.getEscherProperties().iterator(); it.hasNext();) {
  242. EscherProperty prop = it.next();
  243. if(prop.getPropertyNumber() == 896 && prop.isComplex()) {
  244. EscherComplexProperty cp = (EscherComplexProperty)prop;
  245. String str = StringUtil.getFromUnicodeLE(cp.getComplexData());
  246. if(str.equals("Chart 1\0")) {
  247. return true;
  248. }
  249. }
  250. }
  251. return false;
  252. }
  253. /**
  254. * The top left x coordinate of this group.
  255. */
  256. public int getX1()
  257. {
  258. return _x1;
  259. }
  260. /**
  261. * The top left y coordinate of this group.
  262. */
  263. public int getY1()
  264. {
  265. return _y1;
  266. }
  267. /**
  268. * The bottom right x coordinate of this group.
  269. */
  270. public int getX2()
  271. {
  272. return _x2;
  273. }
  274. /**
  275. * The bottom right y coordinate of this group.
  276. */
  277. public int getY2()
  278. {
  279. return _y2;
  280. }
  281. /**
  282. * Returns the aggregate escher record we're bound to
  283. */
  284. protected EscherAggregate _getBoundAggregate() {
  285. return _boundAggregate;
  286. }
  287. /**
  288. * Creates a new client anchor and sets the top-left and bottom-right
  289. * coordinates of the anchor.
  290. *
  291. * @param dx1 the x coordinate in EMU within the first cell.
  292. * @param dy1 the y coordinate in EMU within the first cell.
  293. * @param dx2 the x coordinate in EMU within the second cell.
  294. * @param dy2 the y coordinate in EMU within the second cell.
  295. * @param col1 the column (0 based) of the first cell.
  296. * @param row1 the row (0 based) of the first cell.
  297. * @param col2 the column (0 based) of the second cell.
  298. * @param row2 the row (0 based) of the second cell.
  299. * @return the newly created client anchor
  300. */
  301. public HSSFClientAnchor createAnchor(int dx1, int dy1, int dx2, int dy2, int col1, int row1, int col2, int row2){
  302. return new HSSFClientAnchor(dx1, dy1, dx2, dy2, (short)col1, row1, (short)col2, row2);
  303. }
  304. public Chart createChart(ClientAnchor anchor) {
  305. throw new RuntimeException("NotImplemented");
  306. }
  307. void buildShapeTree(){
  308. EscherContainerRecord dgContainer = _boundAggregate.getEscherContainer();
  309. EscherContainerRecord spgrConrainer = dgContainer.getChildContainers().get(0);
  310. List<EscherContainerRecord> spgrChildren = spgrConrainer.getChildContainers();
  311. for(int i = 0; i < spgrChildren.size(); i++){
  312. EscherContainerRecord spContainer = spgrChildren.get(i);
  313. if (i == 0){
  314. EscherSpgrRecord spgr = (EscherSpgrRecord)spContainer.getChildById(EscherSpgrRecord.RECORD_ID);
  315. setCoordinates(
  316. spgr.getRectX1(), spgr.getRectY1(),
  317. spgr.getRectX2(), spgr.getRectY2()
  318. );
  319. } else {
  320. HSSFShapeFactory.createShapeTree(spContainer, _boundAggregate, this);
  321. }
  322. }
  323. }
  324. }