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.

XSSFShape.java 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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.xssf.usermodel;
  16. import org.apache.poi.ss.usermodel.Shape;
  17. import org.apache.poi.util.Units;
  18. import org.openxmlformats.schemas.drawingml.x2006.main.CTLineProperties;
  19. import org.openxmlformats.schemas.drawingml.x2006.main.CTNoFillProperties;
  20. import org.openxmlformats.schemas.drawingml.x2006.main.CTPresetLineDashProperties;
  21. import org.openxmlformats.schemas.drawingml.x2006.main.CTSRgbColor;
  22. import org.openxmlformats.schemas.drawingml.x2006.main.CTShapeProperties;
  23. import org.openxmlformats.schemas.drawingml.x2006.main.CTSolidColorFillProperties;
  24. import org.openxmlformats.schemas.drawingml.x2006.main.STPresetLineDashVal;
  25. /**
  26. * Represents a shape in a SpreadsheetML drawing.
  27. */
  28. public abstract class XSSFShape implements Shape {
  29. /**
  30. * Parent drawing
  31. */
  32. protected XSSFDrawing drawing;
  33. /**
  34. * The parent shape, always not-null for shapes in groups
  35. */
  36. protected XSSFShapeGroup parent;
  37. /**
  38. * anchor that is used by this shape
  39. */
  40. protected XSSFAnchor anchor;
  41. /**
  42. * Return the drawing that owns this shape
  43. *
  44. * @return the parent drawing that owns this shape
  45. */
  46. public XSSFDrawing getDrawing(){
  47. return drawing;
  48. }
  49. @Override
  50. public XSSFShapeGroup getParent()
  51. {
  52. return parent;
  53. }
  54. /**
  55. * @return the anchor that is used by this shape.
  56. */
  57. @Override
  58. public XSSFAnchor getAnchor()
  59. {
  60. return anchor;
  61. }
  62. /**
  63. * Returns xml bean with shape properties.
  64. *
  65. * @return xml bean with shape properties.
  66. */
  67. protected abstract CTShapeProperties getShapeProperties();
  68. @Override
  69. public boolean isNoFill() {
  70. return getShapeProperties().isSetNoFill();
  71. }
  72. @Override
  73. public void setNoFill(boolean noFill) {
  74. CTShapeProperties props = getShapeProperties();
  75. //unset solid and pattern fills if they are set
  76. if (props.isSetPattFill()) props.unsetPattFill();
  77. if (props.isSetSolidFill()) props.unsetSolidFill();
  78. props.setNoFill(CTNoFillProperties.Factory.newInstance());
  79. }
  80. @Override
  81. public void setFillColor(int red, int green, int blue) {
  82. CTShapeProperties props = getShapeProperties();
  83. CTSolidColorFillProperties fill = props.isSetSolidFill() ? props.getSolidFill() : props.addNewSolidFill();
  84. CTSRgbColor rgb = CTSRgbColor.Factory.newInstance();
  85. rgb.setVal(new byte[]{(byte)red, (byte)green, (byte)blue});
  86. fill.setSrgbClr(rgb);
  87. }
  88. @Override
  89. public void setLineStyleColor( int red, int green, int blue ) {
  90. CTShapeProperties props = getShapeProperties();
  91. CTLineProperties ln = props.isSetLn() ? props.getLn() : props.addNewLn();
  92. CTSolidColorFillProperties fill = ln.isSetSolidFill() ? ln.getSolidFill() : ln.addNewSolidFill();
  93. CTSRgbColor rgb = CTSRgbColor.Factory.newInstance();
  94. rgb.setVal(new byte[]{(byte)red, (byte)green, (byte)blue});
  95. fill.setSrgbClr(rgb);
  96. }
  97. /**
  98. * Specifies the width to be used for the underline stroke.
  99. *
  100. * @param lineWidth width in points
  101. */
  102. public void setLineWidth( double lineWidth ) {
  103. CTShapeProperties props = getShapeProperties();
  104. CTLineProperties ln = props.isSetLn() ? props.getLn() : props.addNewLn();
  105. ln.setW((int)(lineWidth*Units.EMU_PER_POINT));
  106. }
  107. /**
  108. * Sets the line style.
  109. */
  110. public void setLineStyle( int lineStyle ) {
  111. CTShapeProperties props = getShapeProperties();
  112. CTLineProperties ln = props.isSetLn() ? props.getLn() : props.addNewLn();
  113. CTPresetLineDashProperties dashStyle = CTPresetLineDashProperties.Factory.newInstance();
  114. dashStyle.setVal(STPresetLineDashVal.Enum.forInt(lineStyle+1));
  115. ln.setPrstDash(dashStyle);
  116. }
  117. }