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.

HSSFShapeGroup.java 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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 org.apache.poi.ddf.EscherChildAnchorRecord;
  17. import org.apache.poi.ddf.EscherClientAnchorRecord;
  18. import org.apache.poi.ddf.EscherContainerRecord;
  19. import org.apache.poi.ddf.EscherRecord;
  20. import org.apache.poi.ddf.EscherSpgrRecord;
  21. import org.apache.poi.hssf.model.TextboxShape;
  22. import org.apache.poi.hssf.record.EscherAggregate;
  23. import org.apache.poi.hssf.record.ObjRecord;
  24. import java.util.ArrayList;
  25. import java.util.List;
  26. import java.util.Iterator;
  27. /**
  28. * A shape group may contain other shapes. It was no actual form on the
  29. * sheet.
  30. *
  31. * @author Glen Stampoultzis (glens at apache.org)
  32. */
  33. public class HSSFShapeGroup
  34. extends HSSFShape
  35. implements HSSFShapeContainer
  36. {
  37. List<HSSFShape> shapes = new ArrayList<HSSFShape>();
  38. private EscherSpgrRecord _spgrRecord;
  39. public HSSFShapeGroup(EscherContainerRecord spgrContainer, ObjRecord objRecord) {
  40. super(spgrContainer, objRecord);
  41. // read internal and external coordinates from spgrContainer
  42. EscherContainerRecord spContainer = spgrContainer.getChildContainers().get(0);
  43. _spgrRecord = (EscherSpgrRecord) spContainer.getChild(0);
  44. for(EscherRecord ch : spContainer.getChildRecords()){
  45. switch(ch.getRecordId()) {
  46. case EscherSpgrRecord.RECORD_ID:
  47. break;
  48. case EscherClientAnchorRecord.RECORD_ID:
  49. anchor = new HSSFClientAnchor((EscherClientAnchorRecord)ch);
  50. break;
  51. case EscherChildAnchorRecord.RECORD_ID:
  52. anchor = new HSSFChildAnchor((EscherChildAnchorRecord)ch);
  53. break;
  54. }
  55. }
  56. }
  57. public HSSFShapeGroup( HSSFShape parent, HSSFAnchor anchor )
  58. {
  59. super( parent, anchor );
  60. _spgrRecord = new EscherSpgrRecord();
  61. _spgrRecord.setRectX1(0);
  62. _spgrRecord.setRectX2(1023);
  63. _spgrRecord.setRectY1(0);
  64. _spgrRecord.setRectY2(255);
  65. }
  66. @Override
  67. protected EscherContainerRecord createSpContainer() {
  68. return null; //To change body of implemented methods use File | Settings | File Templates.
  69. }
  70. @Override
  71. protected ObjRecord createObjRecord() {
  72. return null; //To change body of implemented methods use File | Settings | File Templates.
  73. }
  74. /**
  75. * Create another group under this group.
  76. * @param anchor the position of the new group.
  77. * @return the group
  78. */
  79. public HSSFShapeGroup createGroup(HSSFChildAnchor anchor)
  80. {
  81. HSSFShapeGroup group = new HSSFShapeGroup(this, anchor);
  82. group.anchor = anchor;
  83. shapes.add(group);
  84. return group;
  85. }
  86. public void addShape(HSSFShape shape){
  87. shape._patriarch = this._patriarch;
  88. shapes.add(shape);
  89. }
  90. public void addTextBox(TextboxShape textboxShape){
  91. // HSSFTextbox shape = new HSSFTextbox(this, textboxShape.geanchor);
  92. // shapes.add(textboxShape);
  93. }
  94. /**
  95. * Create a new simple shape under this group.
  96. * @param anchor the position of the shape.
  97. * @return the shape
  98. */
  99. public HSSFSimpleShape createShape(HSSFChildAnchor anchor)
  100. {
  101. HSSFSimpleShape shape = new HSSFSimpleShape(this, anchor);
  102. shape.anchor = anchor;
  103. shapes.add(shape);
  104. return shape;
  105. }
  106. /**
  107. * Create a new textbox under this group.
  108. * @param anchor the position of the shape.
  109. * @return the textbox
  110. */
  111. public HSSFTextbox createTextbox(HSSFChildAnchor anchor)
  112. {
  113. HSSFTextbox shape = new HSSFTextbox(this, anchor);
  114. shape.anchor = anchor;
  115. shapes.add(shape);
  116. return shape;
  117. }
  118. /**
  119. * Creates a polygon
  120. *
  121. * @param anchor the client anchor describes how this group is attached
  122. * to the sheet.
  123. * @return the newly created shape.
  124. */
  125. public HSSFPolygon createPolygon(HSSFChildAnchor anchor)
  126. {
  127. HSSFPolygon shape = new HSSFPolygon(this, anchor);
  128. shape.anchor = anchor;
  129. shapes.add(shape);
  130. return shape;
  131. }
  132. /**
  133. * Creates a picture.
  134. *
  135. * @param anchor the client anchor describes how this group is attached
  136. * to the sheet.
  137. * @return the newly created shape.
  138. */
  139. public HSSFPicture createPicture(HSSFChildAnchor anchor, int pictureIndex)
  140. {
  141. HSSFPicture shape = new HSSFPicture(this, anchor);
  142. shape.anchor = anchor;
  143. shape.setPictureIndex( pictureIndex );
  144. shapes.add(shape);
  145. return shape;
  146. }
  147. /**
  148. * Return all children contained by this shape.
  149. */
  150. public List<HSSFShape> getChildren()
  151. {
  152. return shapes;
  153. }
  154. /**
  155. * Sets the coordinate space of this group. All children are constrained
  156. * to these coordinates.
  157. */
  158. public void setCoordinates( int x1, int y1, int x2, int y2 )
  159. {
  160. _spgrRecord.setRectX1(x1);
  161. _spgrRecord.setRectX2(x2);
  162. _spgrRecord.setRectY1(y1);
  163. _spgrRecord.setRectY2(y2);
  164. }
  165. /**
  166. * The top left x coordinate of this group.
  167. */
  168. public int getX1()
  169. {
  170. return _spgrRecord.getRectX1();
  171. }
  172. /**
  173. * The top left y coordinate of this group.
  174. */
  175. public int getY1()
  176. {
  177. return _spgrRecord.getRectY1();
  178. }
  179. /**
  180. * The bottom right x coordinate of this group.
  181. */
  182. public int getX2()
  183. {
  184. return _spgrRecord.getRectX2();
  185. }
  186. /**
  187. * The bottom right y coordinate of this group.
  188. */
  189. public int getY2()
  190. {
  191. return _spgrRecord.getRectY2();
  192. }
  193. /**
  194. * Count of all children and their childrens children.
  195. */
  196. public int countOfAllChildren()
  197. {
  198. int count = shapes.size();
  199. for ( Iterator iterator = shapes.iterator(); iterator.hasNext(); )
  200. {
  201. HSSFShape shape = (HSSFShape) iterator.next();
  202. count += shape.countOfAllChildren();
  203. }
  204. return count;
  205. }
  206. }