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.

XSLFSlideLayout.java 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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.apache.poi.POIXMLTypeLoader.DEFAULT_XML_OPTIONS;
  17. import java.io.IOException;
  18. import org.apache.poi.POIXMLDocumentPart;
  19. import org.apache.poi.openxml4j.opc.PackagePart;
  20. import org.apache.poi.sl.usermodel.MasterSheet;
  21. import org.apache.poi.sl.usermodel.Placeholder;
  22. import org.apache.poi.util.Beta;
  23. import org.apache.poi.util.Internal;
  24. import org.apache.xmlbeans.XmlException;
  25. import org.openxmlformats.schemas.presentationml.x2006.main.CTBackground;
  26. import org.openxmlformats.schemas.presentationml.x2006.main.CTSlideLayout;
  27. import org.openxmlformats.schemas.presentationml.x2006.main.SldLayoutDocument;
  28. @Beta
  29. public class XSLFSlideLayout extends XSLFSheet
  30. implements MasterSheet<XSLFShape,XSLFTextParagraph> {
  31. private final CTSlideLayout _layout;
  32. private XSLFSlideMaster _master;
  33. /**
  34. * @since POI 3.14-Beta1
  35. */
  36. public XSLFSlideLayout(PackagePart part) throws IOException, XmlException {
  37. super(part);
  38. SldLayoutDocument doc =
  39. SldLayoutDocument.Factory.parse(getPackagePart().getInputStream(), DEFAULT_XML_OPTIONS);
  40. _layout = doc.getSldLayout();
  41. }
  42. public String getName() {
  43. return _layout.getCSld().getName();
  44. }
  45. /**
  46. * While developing only!
  47. */
  48. @Internal
  49. public CTSlideLayout getXmlObject() {
  50. return _layout;
  51. }
  52. @Override
  53. protected String getRootElementName() {
  54. return "sldLayout";
  55. }
  56. /**
  57. * Slide master object associated with this layout.
  58. *
  59. * @return slide master. Never null.
  60. * @throws IllegalStateException if slide master was not found
  61. */
  62. public XSLFSlideMaster getSlideMaster() {
  63. if (_master == null) {
  64. for (POIXMLDocumentPart p : getRelations()) {
  65. if (p instanceof XSLFSlideMaster) {
  66. _master = (XSLFSlideMaster) p;
  67. }
  68. }
  69. }
  70. if (_master == null) {
  71. throw new IllegalStateException("SlideMaster was not found for " + this);
  72. }
  73. return _master;
  74. }
  75. @Override
  76. public XSLFSlideMaster getMasterSheet() {
  77. return getSlideMaster();
  78. }
  79. @Override
  80. public XSLFTheme getTheme() {
  81. return getSlideMaster().getTheme();
  82. }
  83. @Override
  84. public boolean getFollowMasterGraphics() {
  85. return _layout.getShowMasterSp();
  86. }
  87. /**
  88. * Render this sheet into the supplied graphics object
  89. */
  90. @Override
  91. protected boolean canDraw(XSLFShape shape) {
  92. return !(shape instanceof XSLFSimpleShape) || !shape.isPlaceholder();
  93. }
  94. @Override
  95. public XSLFBackground getBackground() {
  96. CTBackground bg = _layout.getCSld().getBg();
  97. if(bg != null) {
  98. return new XSLFBackground(bg, this);
  99. } else {
  100. return getMasterSheet().getBackground();
  101. }
  102. }
  103. /**
  104. * Copy placeholders from this layout to the destination slide
  105. *
  106. * @param slide destination slide
  107. */
  108. public void copyLayout(XSLFSlide slide) {
  109. for (XSLFShape sh : getShapes()) {
  110. if (sh instanceof XSLFTextShape) {
  111. XSLFTextShape tsh = (XSLFTextShape) sh;
  112. Placeholder ph = tsh.getTextType();
  113. if (ph == null) continue;
  114. switch (ph) {
  115. // these are special and not copied by default
  116. case DATETIME:
  117. case SLIDE_NUMBER:
  118. case FOOTER:
  119. break;
  120. default:
  121. slide.getSpTree().addNewSp().set(tsh.getXmlObject().copy());
  122. }
  123. }
  124. }
  125. }
  126. /**
  127. *
  128. * @return type of this layout
  129. */
  130. public SlideLayout getType(){
  131. int ordinal = _layout.getType().intValue() - 1;
  132. return SlideLayout.values()[ordinal];
  133. }
  134. }