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.

XSLFConnectorShape.java 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /*
  2. * ====================================================================
  3. * Licensed to the Apache Software Foundation (ASF) under one or more
  4. * contributor license agreements. See the NOTICE file distributed with
  5. * this work for additional information regarding copyright ownership.
  6. * The ASF licenses this file to You under the Apache License, Version 2.0
  7. * (the "License"); you may not use this file except in compliance with
  8. * the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. * ====================================================================
  18. */
  19. package org.apache.poi.xslf.usermodel;
  20. import org.apache.poi.util.Beta;
  21. import org.openxmlformats.schemas.drawingml.x2006.main.CTLineProperties;
  22. import org.openxmlformats.schemas.drawingml.x2006.main.CTNonVisualDrawingProps;
  23. import org.openxmlformats.schemas.drawingml.x2006.main.CTPresetGeometry2D;
  24. import org.openxmlformats.schemas.drawingml.x2006.main.CTShapeProperties;
  25. import org.openxmlformats.schemas.drawingml.x2006.main.STShapeType;
  26. import org.openxmlformats.schemas.drawingml.x2006.main.CTLineEndProperties;
  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.STLineEndLength;
  30. import org.openxmlformats.schemas.presentationml.x2006.main.CTConnector;
  31. import org.openxmlformats.schemas.presentationml.x2006.main.CTConnectorNonVisual;
  32. import java.awt.*;
  33. import java.awt.geom.GeneralPath;
  34. import java.awt.geom.Rectangle2D;
  35. /**
  36. *
  37. * Specifies a connection shape.
  38. *
  39. * @author Yegor Kozlov
  40. */
  41. @Beta
  42. public class XSLFConnectorShape extends XSLFSimpleShape {
  43. /*package*/ XSLFConnectorShape(CTConnector shape, XSLFSheet sheet){
  44. super(shape, sheet);
  45. }
  46. /**
  47. * @param shapeId 1-based shapeId
  48. */
  49. static CTConnector prototype(int shapeId) {
  50. CTConnector ct = CTConnector.Factory.newInstance();
  51. CTConnectorNonVisual nvSpPr = ct.addNewNvCxnSpPr();
  52. CTNonVisualDrawingProps cnv = nvSpPr.addNewCNvPr();
  53. cnv.setName("Connector " + shapeId);
  54. cnv.setId(shapeId + 1);
  55. nvSpPr.addNewCNvCxnSpPr();
  56. nvSpPr.addNewNvPr();
  57. CTShapeProperties spPr = ct.addNewSpPr();
  58. CTPresetGeometry2D prst = spPr.addNewPrstGeom();
  59. prst.setPrst(STShapeType.LINE);
  60. prst.addNewAvLst();
  61. CTLineProperties ln = spPr.addNewLn();
  62. return ct;
  63. }
  64. /**
  65. * Specifies the line end decoration, such as a triangle or arrowhead.
  66. */
  67. public void setLineHeadDecoration(LineDecoration style){
  68. CTLineProperties ln = getSpPr().getLn();
  69. CTLineEndProperties lnEnd = ln.isSetHeadEnd() ? ln.getHeadEnd() : ln.addNewHeadEnd();
  70. if(style == null){
  71. if(lnEnd.isSetType()) lnEnd.unsetType();
  72. } else {
  73. lnEnd.setType(STLineEndType.Enum.forInt(style.ordinal() + 1));
  74. }
  75. }
  76. public LineDecoration getLineHeadDecoration(){
  77. CTLineProperties ln = getSpPr().getLn();
  78. if(!ln.isSetHeadEnd()) return LineDecoration.NONE;
  79. STLineEndType.Enum end = ln.getHeadEnd().getType();
  80. return end == null ? LineDecoration.NONE : LineDecoration.values()[end.intValue() - 1];
  81. }
  82. /**
  83. * specifies decorations which can be added to the head of a line.
  84. */
  85. public void setLineHeadWidth(LineEndWidth style){
  86. CTLineProperties ln = getSpPr().getLn();
  87. CTLineEndProperties lnEnd = ln.isSetHeadEnd() ? ln.getHeadEnd() : ln.addNewHeadEnd();
  88. if(style == null){
  89. if(lnEnd.isSetW()) lnEnd.unsetW();
  90. } else {
  91. lnEnd.setW(STLineEndWidth.Enum.forInt(style.ordinal() + 1));
  92. }
  93. }
  94. public LineEndWidth getLineHeadWidth(){
  95. CTLineProperties ln = getSpPr().getLn();
  96. if(!ln.isSetHeadEnd()) return null;
  97. STLineEndWidth.Enum w = ln.getHeadEnd().getW();
  98. return w == null ? null : LineEndWidth.values()[w.intValue() - 1];
  99. }
  100. /**
  101. * Specifies the line end width in relation to the line width.
  102. */
  103. public void setLineHeadLength(LineEndLength style){
  104. CTLineProperties ln = getSpPr().getLn();
  105. CTLineEndProperties lnEnd = ln.isSetHeadEnd() ? ln.getHeadEnd() : ln.addNewHeadEnd();
  106. if(style == null){
  107. if(lnEnd.isSetLen()) lnEnd.unsetLen();
  108. } else {
  109. lnEnd.setLen(STLineEndLength.Enum.forInt(style.ordinal() + 1));
  110. }
  111. }
  112. public LineEndLength getLineHeadLength(){
  113. CTLineProperties ln = getSpPr().getLn();
  114. if(!ln.isSetHeadEnd()) return null;
  115. STLineEndLength.Enum len = ln.getHeadEnd().getLen();
  116. return len == null ? null : LineEndLength.values()[len.intValue() - 1];
  117. }
  118. /**
  119. * Specifies the line end decoration, such as a triangle or arrowhead.
  120. */
  121. public void setLineTailDecoration(LineDecoration style){
  122. CTLineProperties ln = getSpPr().getLn();
  123. CTLineEndProperties lnEnd = ln.isSetTailEnd() ? ln.getTailEnd() : ln.addNewTailEnd();
  124. if(style == null){
  125. if(lnEnd.isSetType()) lnEnd.unsetType();
  126. } else {
  127. lnEnd.setType(STLineEndType.Enum.forInt(style.ordinal() + 1));
  128. }
  129. }
  130. public LineDecoration getLineTailDecoration(){
  131. CTLineProperties ln = getSpPr().getLn();
  132. if(!ln.isSetTailEnd()) return LineDecoration.NONE;
  133. STLineEndType.Enum end = ln.getTailEnd().getType();
  134. return end == null ? LineDecoration.NONE : LineDecoration.values()[end.intValue() - 1];
  135. }
  136. /**
  137. * specifies decorations which can be added to the tail of a line.
  138. */
  139. public void setLineTailWidth(LineEndWidth style){
  140. CTLineProperties ln = getSpPr().getLn();
  141. CTLineEndProperties lnEnd = ln.isSetTailEnd() ? ln.getTailEnd() : ln.addNewTailEnd();
  142. if(style == null){
  143. if(lnEnd.isSetW()) lnEnd.unsetW();
  144. } else {
  145. lnEnd.setW(STLineEndWidth.Enum.forInt(style.ordinal() + 1));
  146. }
  147. }
  148. public LineEndWidth getLineTailWidth(){
  149. CTLineProperties ln = getSpPr().getLn();
  150. if(!ln.isSetTailEnd()) return null;
  151. STLineEndWidth.Enum w = ln.getTailEnd().getW();
  152. return w == null ? null : LineEndWidth.values()[w.intValue() - 1];
  153. }
  154. /**
  155. * Specifies the line end width in relation to the line width.
  156. */
  157. public void setLineTailLength(LineEndLength style){
  158. CTLineProperties ln = getSpPr().getLn();
  159. CTLineEndProperties lnEnd = ln.isSetTailEnd() ? ln.getTailEnd() : ln.addNewTailEnd();
  160. if(style == null){
  161. if(lnEnd.isSetLen()) lnEnd.unsetLen();
  162. } else {
  163. lnEnd.setLen(STLineEndLength.Enum.forInt(style.ordinal() + 1));
  164. }
  165. }
  166. public LineEndLength getLineTailLength(){
  167. CTLineProperties ln = getSpPr().getLn();
  168. if(!ln.isSetTailEnd()) return null;
  169. STLineEndLength.Enum len = ln.getTailEnd().getLen();
  170. return len == null ? null : LineEndLength.values()[len.intValue() - 1];
  171. }
  172. @Override
  173. public void draw(Graphics2D graphics){
  174. java.awt.Shape outline = getOutline();
  175. // shadow
  176. XSLFShadow shadow = getShadow();
  177. if(shadow != null) shadow.draw(graphics);
  178. //border
  179. Color lineColor = getLineColor();
  180. if (lineColor != null){
  181. graphics.setColor(lineColor);
  182. applyStroke(graphics);
  183. graphics.draw(outline);
  184. }
  185. }
  186. @Override
  187. protected java.awt.Shape getOutline(){
  188. Rectangle2D anchor = getAnchor();
  189. double x1 = anchor.getX(),
  190. y1 = anchor.getY(),
  191. x2 = anchor.getX() + anchor.getWidth(),
  192. y2 = anchor.getY() + anchor.getHeight();
  193. GeneralPath line = new GeneralPath();
  194. line.moveTo((float)x1, (float)y1);
  195. line.lineTo((float)x2, (float)y2);
  196. return line;
  197. }
  198. }