Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

HSSFShapeGroup.java 15KB

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