PageRenderTime 55ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/pptx/src/org/apache/poi/xslf/usermodel/XSLFSlide.java

https://github.com/minstrelsy/POI-Android
Java | 246 lines | 145 code | 32 blank | 69 comment | 29 complexity | bd91cfe08b1a6b3395f8a88caf56a673 MD5 | raw file
  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 java.io.IOException;
  17. import java.util.concurrent.atomic.AtomicBoolean;
  18. import net.pbdavey.awt.Graphics2D;
  19. import org.apache.poi.POIXMLDocumentPart;
  20. import org.apache.poi.openxml4j.opc.PackagePart;
  21. import org.apache.poi.openxml4j.opc.PackageRelationship;
  22. import org.apache.poi.util.Beta;
  23. import org.apache.xmlbeans.XmlException;
  24. import org.openxmlformats.schemas.drawingml.x2006.main.CTBlip;
  25. import org.openxmlformats.schemas.drawingml.x2006.main.CTGroupShapeProperties;
  26. import org.openxmlformats.schemas.drawingml.x2006.main.CTGroupTransform2D;
  27. //import org.openxmlformats.schemas.drawingml.x2006.main.CTNonVisualDrawingProps;
  28. import org.openxmlformats.schemas.drawingml.x2006.main.CTPoint2D;
  29. import org.openxmlformats.schemas.drawingml.x2006.main.CTPositiveSize2D;
  30. import org.openxmlformats.schemas.presentationml.x2006.main.CTBackground;
  31. import org.openxmlformats.schemas.presentationml.x2006.main.CTCommonSlideData;
  32. import org.openxmlformats.schemas.presentationml.x2006.main.CTGroupShape;
  33. import org.openxmlformats.schemas.presentationml.x2006.main.CTGroupShapeNonVisual;
  34. import org.openxmlformats.schemas.presentationml.x2006.main.CTSlide;
  35. import org.openxmlformats.schemas.presentationml.x2006.main.SldDocument;
  36. import android.os.Handler;
  37. @Beta
  38. public final class XSLFSlide extends XSLFSheet {
  39. private final CTSlide _slide;
  40. private XSLFSlideLayout _layout;
  41. private XSLFComments _comments;
  42. private XSLFNotes _notes;
  43. /**
  44. * Create a new slide
  45. */
  46. XSLFSlide() {
  47. super();
  48. _slide = prototype();
  49. setCommonSlideData(_slide.getCSld());
  50. }
  51. /**
  52. * Construct a SpreadsheetML slide from a package part
  53. *
  54. * @param part the package part holding the slide data,
  55. * the content type must be <code>application/vnd.openxmlformats-officedocument.slide+xml</code>
  56. * @param rel the package relationship holding this slide,
  57. * the relationship type must be http://schemas.openxmlformats.org/officeDocument/2006/relationships/slide
  58. */
  59. XSLFSlide(PackagePart part, PackageRelationship rel) throws IOException, XmlException {
  60. super(part, rel);
  61. SldDocument doc =
  62. SldDocument.Factory.parse(getPackagePart().getInputStream());
  63. _slide = doc.getSld();
  64. setCommonSlideData(_slide.getCSld());
  65. }
  66. private static CTSlide prototype(){
  67. CTSlide ctSlide = CTSlide.Factory.newInstance();
  68. CTCommonSlideData cSld = ctSlide.addNewCSld();
  69. CTGroupShape spTree = cSld.addNewSpTree();
  70. CTGroupShapeNonVisual nvGrpSpPr = spTree.addNewNvGrpSpPr();
  71. // CTNonVisualDrawingProps cnvPr = nvGrpSpPr.addNewCNvPr();
  72. // cnvPr.setId(1);
  73. // cnvPr.setName("");
  74. // nvGrpSpPr.addNewCNvGrpSpPr();
  75. // nvGrpSpPr.addNewNvPr();
  76. //
  77. // CTGroupShapeProperties grpSpr = spTree.addNewGrpSpPr();
  78. // CTGroupTransform2D xfrm = grpSpr.addNewXfrm();
  79. // CTPoint2D off = xfrm.addNewOff();
  80. // off.setX(0);
  81. // off.setY(0);
  82. // CTPositiveSize2D ext = xfrm.addNewExt();
  83. // ext.setCx(0);
  84. // ext.setCy(0);
  85. // CTPoint2D choff = xfrm.addNewChOff();
  86. // choff.setX(0);
  87. // choff.setY(0);
  88. // CTPositiveSize2D chExt = xfrm.addNewChExt();
  89. // chExt.setCx(0);
  90. // chExt.setCy(0);
  91. // ctSlide.addNewClrMapOvr().addNewMasterClrMapping();
  92. return ctSlide;
  93. }
  94. @Override
  95. public CTSlide getXmlObject() {
  96. return _slide;
  97. }
  98. @Override
  99. protected String getRootElementName(){
  100. return "sld";
  101. }
  102. @Override
  103. public XSLFSlideLayout getMasterSheet(){
  104. return getSlideLayout();
  105. }
  106. public XSLFSlideLayout getSlideLayout(){
  107. if(_layout == null){
  108. for (POIXMLDocumentPart p : getRelations()) {
  109. if (p instanceof XSLFSlideLayout){
  110. _layout = (XSLFSlideLayout)p;
  111. }
  112. }
  113. }
  114. if(_layout == null) {
  115. throw new IllegalArgumentException("SlideLayout was not found for " + this.toString());
  116. }
  117. return _layout;
  118. }
  119. public XSLFSlideMaster getSlideMaster(){
  120. return getSlideLayout().getSlideMaster();
  121. }
  122. public XSLFComments getComments() {
  123. if(_comments == null) {
  124. for (POIXMLDocumentPart p : getRelations()) {
  125. if (p instanceof XSLFComments) {
  126. _comments = (XSLFComments)p;
  127. }
  128. }
  129. }
  130. if(_comments == null) {
  131. // This slide lacks comments
  132. // Not all have them, sorry...
  133. return null;
  134. }
  135. return _comments;
  136. }
  137. public XSLFNotes getNotes() {
  138. if(_notes == null) {
  139. for (POIXMLDocumentPart p : getRelations()) {
  140. if (p instanceof XSLFNotes){
  141. _notes = (XSLFNotes)p;
  142. }
  143. }
  144. }
  145. if(_notes == null) {
  146. // This slide lacks notes
  147. // Not all have them, sorry...
  148. return null;
  149. }
  150. return _notes;
  151. }
  152. /**
  153. *
  154. * @return title of this slide or empty string if title is not set
  155. */
  156. public String getTitle(){
  157. XSLFTextShape txt = getTextShapeByType(Placeholder.TITLE);
  158. return txt == null ? "" : txt.getText();
  159. }
  160. @Override
  161. public XSLFTheme getTheme(){
  162. return getSlideLayout().getSlideMaster().getTheme();
  163. }
  164. /**
  165. *
  166. * @return the information about background appearance of this slide
  167. */
  168. @Override
  169. public XSLFBackground getBackground() {
  170. CTBackground bg = _slide.getCSld().getBg();
  171. if(bg != null) {
  172. return new XSLFBackground(bg, this);
  173. } else {
  174. return getMasterSheet().getBackground();
  175. }
  176. }
  177. /**
  178. *
  179. * @return whether shapes on the master slide should be shown or not.
  180. */
  181. public boolean getFollowMasterGraphics(){
  182. return !_slide.isSetShowMasterSp() || _slide.getShowMasterSp();
  183. }
  184. /**
  185. *
  186. * @param value whether shapes on the master slide should be shown or not.
  187. */
  188. public void setFollowMasterGraphics(boolean value){
  189. _slide.setShowMasterSp(value);
  190. }
  191. @Override
  192. public void draw(Graphics2D graphics, AtomicBoolean isCanceled, Handler handler, int position){
  193. XSLFBackground bg = getBackground();
  194. if(bg != null) bg.draw(graphics);
  195. super.draw(graphics, isCanceled, handler, position);
  196. }
  197. @Override
  198. public XSLFSlide importContent(XSLFSheet src){
  199. super.importContent(src);
  200. XSLFBackground bgShape = getBackground();
  201. if(bgShape != null) {
  202. CTBackground bg = (CTBackground)bgShape.getXmlObject();
  203. if(bg.isSetBgPr() && bg.getBgPr().isSetBlipFill()){
  204. CTBlip blip = bg.getBgPr().getBlipFill().getBlip();
  205. String blipId = blip.getEmbed();
  206. String relId = importBlip(blipId, src.getPackagePart());
  207. blip.setEmbed(relId);
  208. }
  209. }
  210. return this;
  211. }
  212. }