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.

TestXSLFConnectorShape.java 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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.xslf.usermodel;
  16. import static org.junit.Assert.assertEquals;
  17. import static org.junit.Assert.assertFalse;
  18. import java.awt.Color;
  19. import java.awt.geom.Rectangle2D;
  20. import org.apache.poi.sl.usermodel.LineDecoration.DecorationShape;
  21. import org.apache.poi.sl.usermodel.LineDecoration.DecorationSize;
  22. import org.apache.poi.sl.usermodel.ShapeType;
  23. import org.junit.Test;
  24. import org.openxmlformats.schemas.drawingml.x2006.main.CTConnection;
  25. import org.openxmlformats.schemas.drawingml.x2006.main.CTNonVisualConnectorProperties;
  26. import org.openxmlformats.schemas.drawingml.x2006.main.STLineEndLength;
  27. import org.openxmlformats.schemas.drawingml.x2006.main.STLineEndType;
  28. import org.openxmlformats.schemas.drawingml.x2006.main.STLineEndWidth;
  29. import org.openxmlformats.schemas.drawingml.x2006.main.STShapeType;
  30. import org.openxmlformats.schemas.presentationml.x2006.main.CTConnector;
  31. /**
  32. * @author Yegor Kozlov
  33. */
  34. public class TestXSLFConnectorShape {
  35. @Test
  36. public void testLineDecorations() {
  37. XMLSlideShow ppt = new XMLSlideShow();
  38. XSLFSlide slide = ppt.createSlide();
  39. XSLFConnectorShape shape = slide.createConnector();
  40. assertEquals(1, slide.getShapes().size());
  41. assertFalse(shape.getSpPr().getLn().isSetHeadEnd());
  42. assertFalse(shape.getSpPr().getLn().isSetTailEnd());
  43. // line decorations
  44. assertEquals(DecorationShape.NONE, shape.getLineHeadDecoration());
  45. assertEquals(DecorationShape.NONE, shape.getLineTailDecoration());
  46. shape.setLineHeadDecoration(null);
  47. shape.setLineTailDecoration(null);
  48. assertEquals(DecorationShape.NONE, shape.getLineHeadDecoration());
  49. assertEquals(DecorationShape.NONE, shape.getLineTailDecoration());
  50. assertFalse(shape.getSpPr().getLn().getHeadEnd().isSetType());
  51. assertFalse(shape.getSpPr().getLn().getTailEnd().isSetType());
  52. shape.setLineHeadDecoration(DecorationShape.ARROW);
  53. shape.setLineTailDecoration(DecorationShape.DIAMOND);
  54. assertEquals(DecorationShape.ARROW, shape.getLineHeadDecoration());
  55. assertEquals(DecorationShape.DIAMOND, shape.getLineTailDecoration());
  56. assertEquals(STLineEndType.ARROW, shape.getSpPr().getLn().getHeadEnd().getType());
  57. assertEquals(STLineEndType.DIAMOND, shape.getSpPr().getLn().getTailEnd().getType());
  58. shape.setLineHeadDecoration(DecorationShape.DIAMOND);
  59. shape.setLineTailDecoration(DecorationShape.ARROW);
  60. assertEquals(DecorationShape.DIAMOND, shape.getLineHeadDecoration());
  61. assertEquals(DecorationShape.ARROW, shape.getLineTailDecoration());
  62. assertEquals(STLineEndType.DIAMOND, shape.getSpPr().getLn().getHeadEnd().getType());
  63. assertEquals(STLineEndType.ARROW, shape.getSpPr().getLn().getTailEnd().getType());
  64. // line end width
  65. assertEquals(DecorationSize.MEDIUM, shape.getLineHeadWidth());
  66. assertEquals(DecorationSize.MEDIUM, shape.getLineTailWidth());
  67. shape.setLineHeadWidth(null);
  68. shape.setLineHeadWidth(null);
  69. assertEquals(DecorationSize.MEDIUM, shape.getLineHeadWidth());
  70. assertEquals(DecorationSize.MEDIUM, shape.getLineTailWidth());
  71. assertFalse(shape.getSpPr().getLn().getHeadEnd().isSetW());
  72. assertFalse(shape.getSpPr().getLn().getTailEnd().isSetW());
  73. shape.setLineHeadWidth(DecorationSize.LARGE);
  74. shape.setLineTailWidth(DecorationSize.MEDIUM);
  75. assertEquals(DecorationSize.LARGE, shape.getLineHeadWidth());
  76. assertEquals(DecorationSize.MEDIUM, shape.getLineTailWidth());
  77. assertEquals(STLineEndWidth.LG, shape.getSpPr().getLn().getHeadEnd().getW());
  78. assertEquals(STLineEndWidth.MED, shape.getSpPr().getLn().getTailEnd().getW());
  79. shape.setLineHeadWidth(DecorationSize.MEDIUM);
  80. shape.setLineTailWidth(DecorationSize.LARGE);
  81. assertEquals(DecorationSize.MEDIUM, shape.getLineHeadWidth());
  82. assertEquals(DecorationSize.LARGE, shape.getLineTailWidth());
  83. assertEquals(STLineEndWidth.MED, shape.getSpPr().getLn().getHeadEnd().getW());
  84. assertEquals(STLineEndWidth.LG, shape.getSpPr().getLn().getTailEnd().getW());
  85. // line end length
  86. assertEquals(DecorationSize.MEDIUM, shape.getLineHeadLength());
  87. assertEquals(DecorationSize.MEDIUM, shape.getLineTailLength());
  88. shape.setLineHeadLength(null);
  89. shape.setLineTailLength(null);
  90. assertEquals(DecorationSize.MEDIUM, shape.getLineHeadLength());
  91. assertEquals(DecorationSize.MEDIUM, shape.getLineTailLength());
  92. assertFalse(shape.getSpPr().getLn().getHeadEnd().isSetLen());
  93. assertFalse(shape.getSpPr().getLn().getTailEnd().isSetLen());
  94. shape.setLineHeadLength(DecorationSize.LARGE);
  95. shape.setLineTailLength(DecorationSize.MEDIUM);
  96. assertEquals(DecorationSize.LARGE, shape.getLineHeadLength());
  97. assertEquals(DecorationSize.MEDIUM, shape.getLineTailLength());
  98. assertEquals(STLineEndLength.LG, shape.getSpPr().getLn().getHeadEnd().getLen());
  99. assertEquals(STLineEndLength.MED, shape.getSpPr().getLn().getTailEnd().getLen());
  100. shape.setLineHeadLength(DecorationSize.MEDIUM);
  101. shape.setLineTailLength(DecorationSize.LARGE);
  102. assertEquals(DecorationSize.MEDIUM, shape.getLineHeadLength());
  103. assertEquals(DecorationSize.LARGE, shape.getLineTailLength());
  104. assertEquals(STLineEndLength.MED, shape.getSpPr().getLn().getHeadEnd().getLen());
  105. assertEquals(STLineEndLength.LG, shape.getSpPr().getLn().getTailEnd().getLen());
  106. }
  107. @Test
  108. public void testAddConnector(){
  109. XMLSlideShow pptx = new XMLSlideShow();
  110. XSLFSlide slide = pptx.createSlide();
  111. XSLFAutoShape rect1 = slide.createAutoShape();
  112. rect1.setShapeType(ShapeType.RECT);
  113. rect1.setAnchor(new Rectangle2D.Double(100, 100, 100, 100));
  114. rect1.setFillColor(Color.blue);
  115. XSLFAutoShape rect2 = slide.createAutoShape();
  116. rect2.setShapeType(ShapeType.RECT);
  117. rect2.setAnchor(new Rectangle2D.Double(300, 300, 100, 100));
  118. rect2.setFillColor(Color.red);
  119. XSLFConnectorShape connector1 = slide.createConnector();
  120. connector1.setAnchor(new Rectangle2D.Double(200, 150, 100, 200));
  121. CTConnector ctConnector = (CTConnector)connector1.getXmlObject();
  122. ctConnector.getSpPr().getPrstGeom().setPrst(STShapeType.BENT_CONNECTOR_3);
  123. CTNonVisualConnectorProperties cx = ctConnector.getNvCxnSpPr().getCNvCxnSpPr();
  124. // connection start
  125. CTConnection stCxn = cx.addNewStCxn();
  126. stCxn.setId(rect1.getShapeId());
  127. // side of the rectangle to attach the connector: left=1, bottom=2,right=3, top=4
  128. stCxn.setIdx(2);
  129. CTConnection end = cx.addNewEndCxn();
  130. end.setId(rect2.getShapeId());
  131. // side of the rectangle to attach the connector: left=1, bottom=2,right=3, top=4
  132. end.setIdx(3);
  133. }
  134. }