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 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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.Collections;
  18. import java.util.Iterator;
  19. import java.util.List;
  20. import java.util.Spliterator;
  21. import org.apache.poi.ddf.DefaultEscherRecordFactory;
  22. import org.apache.poi.ddf.EscherBoolProperty;
  23. import org.apache.poi.ddf.EscherChildAnchorRecord;
  24. import org.apache.poi.ddf.EscherClientAnchorRecord;
  25. import org.apache.poi.ddf.EscherClientDataRecord;
  26. import org.apache.poi.ddf.EscherContainerRecord;
  27. import org.apache.poi.ddf.EscherOptRecord;
  28. import org.apache.poi.ddf.EscherPropertyTypes;
  29. import org.apache.poi.ddf.EscherRecord;
  30. import org.apache.poi.ddf.EscherRecordTypes;
  31. import org.apache.poi.ddf.EscherSpRecord;
  32. import org.apache.poi.ddf.EscherSpgrRecord;
  33. import org.apache.poi.hssf.record.CommonObjectDataSubRecord;
  34. import org.apache.poi.hssf.record.EndSubRecord;
  35. import org.apache.poi.hssf.record.EscherAggregate;
  36. import org.apache.poi.hssf.record.GroupMarkerSubRecord;
  37. import org.apache.poi.hssf.record.ObjRecord;
  38. /**
  39. * A shape group may contain other shapes. It was no actual form on the
  40. * sheet.
  41. */
  42. public class HSSFShapeGroup extends HSSFShape implements HSSFShapeContainer {
  43. private final List<HSSFShape> shapes = new ArrayList<>();
  44. private final EscherSpgrRecord _spgrRecord;
  45. public HSSFShapeGroup(EscherContainerRecord spgrContainer, ObjRecord objRecord) {
  46. super(spgrContainer, objRecord);
  47. // read internal and external coordinates from spgrContainer
  48. EscherContainerRecord spContainer = spgrContainer.getChildContainers().get(0);
  49. _spgrRecord = (EscherSpgrRecord) spContainer.getChild(0);
  50. for (EscherRecord ch : spContainer) {
  51. switch (EscherRecordTypes.forTypeID(ch.getRecordId())) {
  52. case CLIENT_ANCHOR:
  53. anchor = new HSSFClientAnchor((EscherClientAnchorRecord) ch);
  54. break;
  55. case CHILD_ANCHOR:
  56. anchor = new HSSFChildAnchor((EscherChildAnchorRecord) ch);
  57. break;
  58. case SPGR:
  59. default:
  60. break;
  61. }
  62. }
  63. }
  64. public HSSFShapeGroup(HSSFShape parent, HSSFAnchor anchor) {
  65. super(parent, anchor);
  66. _spgrRecord = ((EscherContainerRecord)getEscherContainer().getChild(0)).getChildById(EscherSpgrRecord.RECORD_ID);
  67. }
  68. @Override
  69. protected EscherContainerRecord createSpContainer() {
  70. EscherContainerRecord spgrContainer = new EscherContainerRecord();
  71. EscherContainerRecord spContainer = new EscherContainerRecord();
  72. EscherSpgrRecord spgr = new EscherSpgrRecord();
  73. EscherSpRecord sp = new EscherSpRecord();
  74. EscherOptRecord opt = new EscherOptRecord();
  75. EscherRecord anchor;
  76. EscherClientDataRecord clientData = new EscherClientDataRecord();
  77. spgrContainer.setRecordId(EscherContainerRecord.SPGR_CONTAINER);
  78. spgrContainer.setOptions((short) 0x000F);
  79. spContainer.setRecordId(EscherContainerRecord.SP_CONTAINER);
  80. spContainer.setOptions((short) 0x000F);
  81. spgr.setRecordId(EscherSpgrRecord.RECORD_ID);
  82. spgr.setOptions((short) 0x0001);
  83. spgr.setRectX1(0);
  84. spgr.setRectY1(0);
  85. spgr.setRectX2(1023);
  86. spgr.setRectY2(255);
  87. sp.setRecordId(EscherSpRecord.RECORD_ID);
  88. sp.setOptions((short) 0x0002);
  89. if (getAnchor() instanceof HSSFClientAnchor) {
  90. sp.setFlags(EscherSpRecord.FLAG_GROUP | EscherSpRecord.FLAG_HAVEANCHOR);
  91. } else {
  92. sp.setFlags(EscherSpRecord.FLAG_GROUP | EscherSpRecord.FLAG_HAVEANCHOR | EscherSpRecord.FLAG_CHILD);
  93. }
  94. opt.setRecordId(EscherOptRecord.RECORD_ID);
  95. opt.setOptions((short) 0x0023);
  96. opt.addEscherProperty(new EscherBoolProperty(EscherPropertyTypes.PROTECTION__LOCKAGAINSTGROUPING, 0x00040004));
  97. opt.addEscherProperty(new EscherBoolProperty(EscherPropertyTypes.GROUPSHAPE__FLAGS, 0x00080000));
  98. anchor = getAnchor().getEscherAnchor();
  99. clientData.setRecordId(EscherClientDataRecord.RECORD_ID);
  100. clientData.setOptions((short) 0x0000);
  101. spgrContainer.addChildRecord(spContainer);
  102. spContainer.addChildRecord(spgr);
  103. spContainer.addChildRecord(sp);
  104. spContainer.addChildRecord(opt);
  105. spContainer.addChildRecord(anchor);
  106. spContainer.addChildRecord(clientData);
  107. return spgrContainer;
  108. }
  109. @Override
  110. protected ObjRecord createObjRecord() {
  111. ObjRecord obj = new ObjRecord();
  112. CommonObjectDataSubRecord cmo = new CommonObjectDataSubRecord();
  113. cmo.setObjectType(CommonObjectDataSubRecord.OBJECT_TYPE_GROUP);
  114. cmo.setLocked(true);
  115. cmo.setPrintable(true);
  116. cmo.setAutofill(true);
  117. cmo.setAutoline(true);
  118. GroupMarkerSubRecord gmo = new GroupMarkerSubRecord();
  119. EndSubRecord end = new EndSubRecord();
  120. obj.addSubRecord(cmo);
  121. obj.addSubRecord(gmo);
  122. obj.addSubRecord(end);
  123. return obj;
  124. }
  125. @Override
  126. protected void afterRemove(HSSFPatriarch patriarch) {
  127. patriarch.getBoundAggregate().removeShapeToObjRecord(getEscherContainer().getChildContainers().get(0)
  128. .getChildById(EscherClientDataRecord.RECORD_ID));
  129. EscherContainerRecord cont = getEscherContainer();
  130. HSSFPatriarch pat = getPatriarch();
  131. for (HSSFShape shape : shapes) {
  132. if (cont.removeChildRecord(shape.getEscherContainer())){
  133. shape.afterRemove(pat);
  134. }
  135. }
  136. shapes.clear();
  137. }
  138. private void onCreate(HSSFShape shape){
  139. if(getPatriarch() != null){
  140. EscherContainerRecord spContainer = shape.getEscherContainer();
  141. int shapeId = getPatriarch().newShapeId();
  142. shape.setShapeId(shapeId);
  143. getEscherContainer().addChildRecord(spContainer);
  144. shape.afterInsert(getPatriarch());
  145. EscherSpRecord sp;
  146. if (shape instanceof HSSFShapeGroup){
  147. sp = shape.getEscherContainer().getChildContainers().get(0).getChildById(EscherSpRecord.RECORD_ID);
  148. } else {
  149. sp = shape.getEscherContainer().getChildById(EscherSpRecord.RECORD_ID);
  150. }
  151. sp.setFlags(sp.getFlags() | EscherSpRecord.FLAG_CHILD);
  152. }
  153. }
  154. /**
  155. * Create another group under this group.
  156. *
  157. * @param anchor the position of the new group.
  158. * @return the group
  159. */
  160. public HSSFShapeGroup createGroup(HSSFChildAnchor anchor) {
  161. HSSFShapeGroup group = new HSSFShapeGroup(this, anchor);
  162. group.setParent(this);
  163. group.setAnchor(anchor);
  164. shapes.add(group);
  165. onCreate(group);
  166. return group;
  167. }
  168. @Override
  169. public void addShape(HSSFShape shape) {
  170. shape.setPatriarch(this.getPatriarch());
  171. shape.setParent(this);
  172. shapes.add(shape);
  173. }
  174. /**
  175. * Create a new simple shape under this group.
  176. *
  177. * @param anchor the position of the shape.
  178. * @return the shape
  179. */
  180. public HSSFSimpleShape createShape(HSSFChildAnchor anchor) {
  181. HSSFSimpleShape shape = new HSSFSimpleShape(this, anchor);
  182. shape.setParent(this);
  183. shape.setAnchor(anchor);
  184. shapes.add(shape);
  185. onCreate(shape);
  186. EscherSpRecord sp = shape.getEscherContainer().getChildById(EscherSpRecord.RECORD_ID);
  187. if (shape.getAnchor().isHorizontallyFlipped()){
  188. sp.setFlags(sp.getFlags() | EscherSpRecord.FLAG_FLIPHORIZ);
  189. }
  190. if (shape.getAnchor().isVerticallyFlipped()){
  191. sp.setFlags(sp.getFlags() | EscherSpRecord.FLAG_FLIPVERT);
  192. }
  193. return shape;
  194. }
  195. /**
  196. * Create a new textbox under this group.
  197. *
  198. * @param anchor the position of the shape.
  199. * @return the textbox
  200. */
  201. public HSSFTextbox createTextbox(HSSFChildAnchor anchor) {
  202. HSSFTextbox shape = new HSSFTextbox(this, anchor);
  203. shape.setParent(this);
  204. shape.setAnchor(anchor);
  205. shapes.add(shape);
  206. onCreate(shape);
  207. return shape;
  208. }
  209. /**
  210. * Creates a polygon
  211. *
  212. * @param anchor the client anchor describes how this group is attached
  213. * to the sheet.
  214. * @return the newly created shape.
  215. */
  216. public HSSFPolygon createPolygon(HSSFChildAnchor anchor) {
  217. HSSFPolygon shape = new HSSFPolygon(this, anchor);
  218. shape.setParent(this);
  219. shape.setAnchor(anchor);
  220. shapes.add(shape);
  221. onCreate(shape);
  222. return shape;
  223. }
  224. /**
  225. * Creates a picture.
  226. *
  227. * @param anchor the client anchor describes how this group is attached
  228. * to the sheet.
  229. * @return the newly created shape.
  230. */
  231. public HSSFPicture createPicture(HSSFChildAnchor anchor, int pictureIndex) {
  232. HSSFPicture shape = new HSSFPicture(this, anchor);
  233. shape.setParent(this);
  234. shape.setAnchor(anchor);
  235. shape.setPictureIndex(pictureIndex);
  236. shapes.add(shape);
  237. onCreate(shape);
  238. EscherSpRecord sp = shape.getEscherContainer().getChildById(EscherSpRecord.RECORD_ID);
  239. if (shape.getAnchor().isHorizontallyFlipped()){
  240. sp.setFlags(sp.getFlags() | EscherSpRecord.FLAG_FLIPHORIZ);
  241. }
  242. if (shape.getAnchor().isVerticallyFlipped()){
  243. sp.setFlags(sp.getFlags() | EscherSpRecord.FLAG_FLIPVERT);
  244. }
  245. return shape;
  246. }
  247. /**
  248. * Return all children contained by this shape.
  249. */
  250. @Override
  251. public List<HSSFShape> getChildren() {
  252. return Collections.unmodifiableList(shapes);
  253. }
  254. /**
  255. * Sets the coordinate space of this group. All children are constrained
  256. * to these coordinates.
  257. */
  258. @Override
  259. public void setCoordinates(int x1, int y1, int x2, int y2) {
  260. _spgrRecord.setRectX1(x1);
  261. _spgrRecord.setRectX2(x2);
  262. _spgrRecord.setRectY1(y1);
  263. _spgrRecord.setRectY2(y2);
  264. }
  265. @Override
  266. public void clear() {
  267. ArrayList <HSSFShape> copy = new ArrayList<>(shapes);
  268. for (HSSFShape shape: copy){
  269. removeShape(shape);
  270. }
  271. }
  272. /**
  273. * The top left x coordinate of this group.
  274. */
  275. @Override
  276. public int getX1() {
  277. return _spgrRecord.getRectX1();
  278. }
  279. /**
  280. * The top left y coordinate of this group.
  281. */
  282. @Override
  283. public int getY1() {
  284. return _spgrRecord.getRectY1();
  285. }
  286. /**
  287. * The bottom right x coordinate of this group.
  288. */
  289. @Override
  290. public int getX2() {
  291. return _spgrRecord.getRectX2();
  292. }
  293. /**
  294. * The bottom right y coordinate of this group.
  295. */
  296. @Override
  297. public int getY2() {
  298. return _spgrRecord.getRectY2();
  299. }
  300. /**
  301. * Count of all children and their childrens children.
  302. */
  303. @Override
  304. public int countOfAllChildren() {
  305. int count = shapes.size();
  306. for (HSSFShape shape : shapes) {
  307. count += shape.countOfAllChildren();
  308. }
  309. return count;
  310. }
  311. @Override
  312. void afterInsert(HSSFPatriarch patriarch){
  313. EscherAggregate agg = patriarch.getBoundAggregate();
  314. EscherContainerRecord containerRecord = getEscherContainer().getChildById(EscherContainerRecord.SP_CONTAINER);
  315. agg.associateShapeToObjRecord(containerRecord.getChildById(EscherClientDataRecord.RECORD_ID), getObjRecord());
  316. }
  317. @Override
  318. void setShapeId(int shapeId){
  319. EscherContainerRecord containerRecord = getEscherContainer().getChildById(EscherContainerRecord.SP_CONTAINER);
  320. EscherSpRecord spRecord = containerRecord.getChildById(EscherSpRecord.RECORD_ID);
  321. spRecord.setShapeId(shapeId);
  322. CommonObjectDataSubRecord cod = (CommonObjectDataSubRecord) getObjRecord().getSubRecords().get(0);
  323. cod.setObjectId((short) (shapeId % 1024));
  324. }
  325. @Override
  326. int getShapeId(){
  327. EscherContainerRecord containerRecord = getEscherContainer().getChildById(EscherContainerRecord.SP_CONTAINER);
  328. return ((EscherSpRecord)containerRecord.getChildById(EscherSpRecord.RECORD_ID)).getShapeId();
  329. }
  330. @Override
  331. protected HSSFShape cloneShape() {
  332. throw new IllegalStateException("Use method cloneShape(HSSFPatriarch patriarch)");
  333. }
  334. protected HSSFShape cloneShape(HSSFPatriarch patriarch) {
  335. EscherContainerRecord spgrContainer = new EscherContainerRecord();
  336. spgrContainer.setRecordId(EscherContainerRecord.SPGR_CONTAINER);
  337. spgrContainer.setOptions((short) 0x000F);
  338. EscherContainerRecord spContainer = new EscherContainerRecord();
  339. EscherContainerRecord cont = getEscherContainer().getChildById(EscherContainerRecord.SP_CONTAINER);
  340. byte [] inSp = cont.serialize();
  341. spContainer.fillFields(inSp, 0, new DefaultEscherRecordFactory());
  342. spgrContainer.addChildRecord(spContainer);
  343. ObjRecord obj = null;
  344. if (null != getObjRecord()){
  345. obj = (ObjRecord) getObjRecord().cloneViaReserialise();
  346. }
  347. HSSFShapeGroup group = new HSSFShapeGroup(spgrContainer, obj);
  348. group.setPatriarch(patriarch);
  349. for (HSSFShape shape: getChildren()){
  350. HSSFShape newShape;
  351. if (shape instanceof HSSFShapeGroup){
  352. newShape = ((HSSFShapeGroup)shape).cloneShape(patriarch);
  353. } else {
  354. newShape = shape.cloneShape();
  355. }
  356. group.addShape(newShape);
  357. group.onCreate(newShape);
  358. }
  359. return group;
  360. }
  361. @Override
  362. public boolean removeShape(HSSFShape shape) {
  363. boolean isRemoved = getEscherContainer().removeChildRecord(shape.getEscherContainer());
  364. if (isRemoved){
  365. shape.afterRemove(getPatriarch());
  366. shapes.remove(shape);
  367. }
  368. return isRemoved;
  369. }
  370. @Override
  371. public Iterator<HSSFShape> iterator() {
  372. return shapes.iterator();
  373. }
  374. /**
  375. * @since POI 5.2.0
  376. */
  377. @Override
  378. public Spliterator<HSSFShape> spliterator() {
  379. return shapes.spliterator();
  380. }
  381. }