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.7KB

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