PageRenderTime 39ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/ppt/scratchpad/src/org/apache/poi/hslf/model/Slide.java

https://github.com/minstrelsy/POI-Android
Java | 513 lines | 289 code | 56 blank | 168 comment | 62 complexity | 346ffc55e0b7a05381ade3b094d03d17 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.hslf.model;
  16. import java.util.Vector;
  17. import java.util.concurrent.atomic.AtomicBoolean;
  18. import net.pbdavey.awt.Graphics2D;
  19. import org.apache.poi.ddf.EscherContainerRecord;
  20. import org.apache.poi.ddf.EscherDgRecord;
  21. import org.apache.poi.ddf.EscherDggRecord;
  22. import org.apache.poi.ddf.EscherSpRecord;
  23. import org.apache.poi.hslf.record.ColorSchemeAtom;
  24. import org.apache.poi.hslf.record.Comment2000;
  25. import org.apache.poi.hslf.record.HeadersFootersContainer;
  26. import org.apache.poi.hslf.record.Record;
  27. import org.apache.poi.hslf.record.RecordContainer;
  28. import org.apache.poi.hslf.record.RecordTypes;
  29. import org.apache.poi.hslf.record.SlideAtom;
  30. import org.apache.poi.hslf.record.TextHeaderAtom;
  31. import org.apache.poi.hslf.record.SlideListWithText.SlideAtomsSet;
  32. import android.os.Handler;
  33. import android.os.Message;
  34. import android.util.Log;
  35. /**
  36. * This class represents a slide in a PowerPoint Document. It allows
  37. * access to the text within, and the layout. For now, it only does
  38. * the text side of things though
  39. *
  40. * @author Nick Burch
  41. * @author Yegor Kozlov
  42. */
  43. public final class Slide extends Sheet
  44. {
  45. private int _slideNo;
  46. private SlideAtomsSet _atomSet;
  47. private TextRun[] _runs;
  48. private Notes _notes; // usermodel needs to set this
  49. /**
  50. * Constructs a Slide from the Slide record, and the SlideAtomsSet
  51. * containing the text.
  52. * Initialises TextRuns, to provide easier access to the text
  53. *
  54. * @param slide the Slide record we're based on
  55. * @param notes the Notes sheet attached to us
  56. * @param atomSet the SlideAtomsSet to get the text from
  57. */
  58. public Slide(org.apache.poi.hslf.record.Slide slide, Notes notes, SlideAtomsSet atomSet, int slideIdentifier, int slideNumber) {
  59. super(slide, slideIdentifier);
  60. _notes = notes;
  61. _atomSet = atomSet;
  62. _slideNo = slideNumber;
  63. // Grab the TextRuns from the PPDrawing
  64. TextRun[] _otherRuns = findTextRuns(getPPDrawing());
  65. // For the text coming in from the SlideAtomsSet:
  66. // Build up TextRuns from pairs of TextHeaderAtom and
  67. // one of TextBytesAtom or TextCharsAtom
  68. Vector textRuns = new Vector();
  69. if(_atomSet != null) {
  70. findTextRuns(_atomSet.getSlideRecords(),textRuns);
  71. } else {
  72. // No text on the slide, must just be pictures
  73. }
  74. // Build an array, more useful than a vector
  75. _runs = new TextRun[textRuns.size()+_otherRuns.length];
  76. // Grab text from SlideListWithTexts entries
  77. int i=0;
  78. for(i=0; i<textRuns.size(); i++) {
  79. _runs[i] = (TextRun)textRuns.get(i);
  80. _runs[i].setSheet(this);
  81. }
  82. // Grab text from slide's PPDrawing
  83. for(int k=0; k<_otherRuns.length; i++, k++) {
  84. _runs[i] = _otherRuns[k];
  85. _runs[i].setSheet(this);
  86. _runs[i].setIndex(-1); // runs found in PPDrawing are not linked with SlideListWithTexts
  87. }
  88. }
  89. /**
  90. * Create a new Slide instance
  91. * @param sheetNumber The internal number of the sheet, as used by PersistPtrHolder
  92. * @param slideNumber The user facing number of the sheet
  93. */
  94. public Slide(int sheetNumber, int sheetRefId, int slideNumber){
  95. super(new org.apache.poi.hslf.record.Slide(), sheetNumber);
  96. _slideNo = slideNumber;
  97. getSheetContainer().setSheetId(sheetRefId);
  98. }
  99. /**
  100. * Sets the Notes that are associated with this. Updates the
  101. * references in the records to point to the new ID
  102. */
  103. public void setNotes(Notes notes) {
  104. _notes = notes;
  105. // Update the Slide Atom's ID of where to point to
  106. SlideAtom sa = getSlideRecord().getSlideAtom();
  107. if(notes == null) {
  108. // Set to 0
  109. sa.setNotesID(0);
  110. } else {
  111. // Set to the value from the notes' sheet id
  112. sa.setNotesID(notes._getSheetNumber());
  113. }
  114. }
  115. /**
  116. * Changes the Slide's (external facing) page number.
  117. * @see org.apache.poi.hslf.usermodel.SlideShow#reorderSlide(int, int)
  118. */
  119. public void setSlideNumber(int newSlideNumber) {
  120. _slideNo = newSlideNumber;
  121. }
  122. /**
  123. * Called by SlideShow ater a new slide is created.
  124. * <p>
  125. * For Slide we need to do the following:
  126. * <li> set id of the drawing group.
  127. * <li> set shapeId for the container descriptor and background
  128. * </p>
  129. */
  130. public void onCreate(){
  131. //initialize drawing group id
  132. EscherDggRecord dgg = getSlideShow().getDocumentRecord().getPPDrawingGroup().getEscherDggRecord();
  133. EscherContainerRecord dgContainer = (EscherContainerRecord)getSheetContainer().getPPDrawing().getEscherRecords()[0];
  134. EscherDgRecord dg = (EscherDgRecord) Shape.getEscherChild(dgContainer, EscherDgRecord.RECORD_ID);
  135. int dgId = dgg.getMaxDrawingGroupId() + 1;
  136. dg.setOptions((short)(dgId << 4));
  137. dgg.setDrawingsSaved(dgg.getDrawingsSaved() + 1);
  138. dgg.setMaxDrawingGroupId(dgId);
  139. for (EscherContainerRecord c : dgContainer.getChildContainers()) {
  140. EscherSpRecord spr = null;
  141. switch(c.getRecordId()){
  142. case EscherContainerRecord.SPGR_CONTAINER:
  143. EscherContainerRecord dc = (EscherContainerRecord)c.getChild(0);
  144. spr = dc.getChildById(EscherSpRecord.RECORD_ID);
  145. break;
  146. case EscherContainerRecord.SP_CONTAINER:
  147. spr = c.getChildById(EscherSpRecord.RECORD_ID);
  148. break;
  149. }
  150. if(spr != null) spr.setShapeId(allocateShapeId());
  151. }
  152. //PPT doen't increment the number of saved shapes for group descriptor and background
  153. dg.setNumShapes(1);
  154. }
  155. /**
  156. * Create a <code>TextBox</code> object that represents the slide's title.
  157. *
  158. * @return <code>TextBox</code> object that represents the slide's title.
  159. */
  160. public TextBox addTitle() {
  161. Placeholder pl = new Placeholder();
  162. pl.setShapeType(ShapeTypes.Rectangle);
  163. pl.getTextRun().setRunType(TextHeaderAtom.TITLE_TYPE);
  164. pl.setText("Click to edit title");
  165. pl.setAnchor(new and.awt.Rectangle(54, 48, 612, 90));
  166. addShape(pl);
  167. return pl;
  168. }
  169. // Complex Accesser methods follow
  170. /**
  171. * Return title of this slide or <code>null</code> if the slide does not have title.
  172. * <p>
  173. * The title is a run of text of type <code>TextHeaderAtom.CENTER_TITLE_TYPE</code> or
  174. * <code>TextHeaderAtom.TITLE_TYPE</code>
  175. * </p>
  176. *
  177. * @see TextHeaderAtom
  178. *
  179. * @return title of this slide
  180. */
  181. public String getTitle(){
  182. TextRun[] txt = getTextRuns();
  183. for (int i = 0; i < txt.length; i++) {
  184. int type = txt[i].getRunType();
  185. if (type == TextHeaderAtom.CENTER_TITLE_TYPE ||
  186. type == TextHeaderAtom.TITLE_TYPE ){
  187. String title = txt[i].getText();
  188. return title;
  189. }
  190. }
  191. return null;
  192. }
  193. // Simple Accesser methods follow
  194. /**
  195. * Returns an array of all the TextRuns found
  196. */
  197. public TextRun[] getTextRuns() { return _runs; }
  198. /**
  199. * Returns the (public facing) page number of this slide
  200. */
  201. public int getSlideNumber() { return _slideNo; }
  202. /**
  203. * Returns the underlying slide record
  204. */
  205. public org.apache.poi.hslf.record.Slide getSlideRecord() {
  206. return (org.apache.poi.hslf.record.Slide)getSheetContainer();
  207. }
  208. /**
  209. * Returns the Notes Sheet for this slide, or null if there isn't one
  210. */
  211. public Notes getNotesSheet() { return _notes; }
  212. /**
  213. * @return set of records inside <code>SlideListWithtext</code> container
  214. * which hold text data for this slide (typically for placeholders).
  215. */
  216. protected SlideAtomsSet getSlideAtomsSet() { return _atomSet; }
  217. /**
  218. * Returns master sheet associated with this slide.
  219. * It can be either SlideMaster or TitleMaster objects.
  220. *
  221. * @return the master sheet associated with this slide.
  222. */
  223. public MasterSheet getMasterSheet(){
  224. SlideMaster[] master = getSlideShow().getSlidesMasters();
  225. SlideAtom sa = getSlideRecord().getSlideAtom();
  226. int masterId = sa.getMasterID();
  227. MasterSheet sheet = null;
  228. for (int i = 0; i < master.length; i++) {
  229. if (masterId == master[i]._getSheetNumber()) {
  230. sheet = master[i];
  231. break;
  232. }
  233. }
  234. if (sheet == null){
  235. TitleMaster[] titleMaster = getSlideShow().getTitleMasters();
  236. if(titleMaster != null) for (int i = 0; i < titleMaster.length; i++) {
  237. if (masterId == titleMaster[i]._getSheetNumber()) {
  238. sheet = titleMaster[i];
  239. break;
  240. }
  241. }
  242. }
  243. return sheet;
  244. }
  245. /**
  246. * Change Master of this slide.
  247. */
  248. public void setMasterSheet(MasterSheet master){
  249. SlideAtom sa = getSlideRecord().getSlideAtom();
  250. int sheetNo = master._getSheetNumber();
  251. sa.setMasterID(sheetNo);
  252. }
  253. /**
  254. * Sets whether this slide follows master background
  255. *
  256. * @param flag <code>true</code> if the slide follows master,
  257. * <code>false</code> otherwise
  258. */
  259. public void setFollowMasterBackground(boolean flag){
  260. SlideAtom sa = getSlideRecord().getSlideAtom();
  261. sa.setFollowMasterBackground(flag);
  262. }
  263. /**
  264. * Whether this slide follows master sheet background
  265. *
  266. * @return <code>true</code> if the slide follows master background,
  267. * <code>false</code> otherwise
  268. */
  269. public boolean getFollowMasterBackground(){
  270. SlideAtom sa = getSlideRecord().getSlideAtom();
  271. return sa.getFollowMasterBackground();
  272. }
  273. /**
  274. * Sets whether this slide draws master sheet objects
  275. *
  276. * @param flag <code>true</code> if the slide draws master sheet objects,
  277. * <code>false</code> otherwise
  278. */
  279. public void setFollowMasterObjects(boolean flag){
  280. SlideAtom sa = getSlideRecord().getSlideAtom();
  281. sa.setFollowMasterObjects(flag);
  282. }
  283. /**
  284. * Whether this slide follows master color scheme
  285. *
  286. * @return <code>true</code> if the slide follows master color scheme,
  287. * <code>false</code> otherwise
  288. */
  289. public boolean getFollowMasterScheme(){
  290. SlideAtom sa = getSlideRecord().getSlideAtom();
  291. return sa.getFollowMasterScheme();
  292. }
  293. /**
  294. * Sets whether this slide draws master color scheme
  295. *
  296. * @param flag <code>true</code> if the slide draws master color scheme,
  297. * <code>false</code> otherwise
  298. */
  299. public void setFollowMasterScheme(boolean flag){
  300. SlideAtom sa = getSlideRecord().getSlideAtom();
  301. sa.setFollowMasterScheme(flag);
  302. }
  303. /**
  304. * Whether this slide draws master sheet objects
  305. *
  306. * @return <code>true</code> if the slide draws master sheet objects,
  307. * <code>false</code> otherwise
  308. */
  309. public boolean getFollowMasterObjects(){
  310. SlideAtom sa = getSlideRecord().getSlideAtom();
  311. return sa.getFollowMasterObjects();
  312. }
  313. /**
  314. * Background for this slide.
  315. */
  316. public Background getBackground() {
  317. if(getFollowMasterBackground()) {
  318. return getMasterSheet().getBackground();
  319. }
  320. return super.getBackground();
  321. }
  322. /**
  323. * Color scheme for this slide.
  324. */
  325. public ColorSchemeAtom getColorScheme() {
  326. if(getFollowMasterScheme()){
  327. return getMasterSheet().getColorScheme();
  328. }
  329. return super.getColorScheme();
  330. }
  331. /**
  332. * Get the comment(s) for this slide.
  333. * Note - for now, only works on PPT 2000 and
  334. * PPT 2003 files. Doesn't work for PPT 97
  335. * ones, as they do their comments oddly.
  336. */
  337. public Comment[] getComments() {
  338. // If there are any, they're in
  339. // ProgTags -> ProgBinaryTag -> BinaryTagData
  340. RecordContainer progTags = (RecordContainer)
  341. getSheetContainer().findFirstOfType(
  342. RecordTypes.ProgTags.typeID
  343. );
  344. if(progTags != null) {
  345. RecordContainer progBinaryTag = (RecordContainer)
  346. progTags.findFirstOfType(
  347. RecordTypes.ProgBinaryTag.typeID
  348. );
  349. if(progBinaryTag != null) {
  350. RecordContainer binaryTags = (RecordContainer)
  351. progBinaryTag.findFirstOfType(
  352. RecordTypes.BinaryTagData.typeID
  353. );
  354. if(binaryTags != null) {
  355. // This is where they'll be
  356. int count = 0;
  357. for(int i=0; i<binaryTags.getChildRecords().length; i++) {
  358. if(binaryTags.getChildRecords()[i] instanceof Comment2000) {
  359. count++;
  360. }
  361. }
  362. // Now build
  363. Comment[] comments = new Comment[count];
  364. count = 0;
  365. for(int i=0; i<binaryTags.getChildRecords().length; i++) {
  366. if(binaryTags.getChildRecords()[i] instanceof Comment2000) {
  367. comments[i] = new Comment(
  368. (Comment2000)binaryTags.getChildRecords()[i]
  369. );
  370. count++;
  371. }
  372. }
  373. return comments;
  374. }
  375. }
  376. }
  377. // None found
  378. return new Comment[0];
  379. }
  380. public void draw(Graphics2D graphics, AtomicBoolean isCanceled, Handler handler, int position){
  381. if (isCanceled.get()) {
  382. Log.d("Slide", "Thread.Canceled");
  383. return;
  384. }
  385. MasterSheet master = getMasterSheet();
  386. Background bg = getBackground();
  387. if(bg != null)bg.draw(graphics);
  388. if (isCanceled.get()) {
  389. Log.d("Slide", "Thread.Canceled");
  390. return;
  391. }
  392. if(getFollowMasterObjects()){
  393. Shape[] sh = master.getShapes();
  394. for (int i = 0; i < sh.length; i++) {
  395. if (isCanceled.get()) {
  396. Log.d("Slide", "Thread.Canceled");
  397. return;
  398. }
  399. if(MasterSheet.isPlaceholder(sh[i])) continue;
  400. sh[i].draw(graphics);
  401. }
  402. }
  403. if (isCanceled.get()) {
  404. Log.d("Slide", "Thread.Canceled");
  405. return;
  406. }
  407. Shape[] sh = getShapes();
  408. for (int i = 0; i < sh.length; i++) {
  409. if (isCanceled.get()) {
  410. Log.d("Slide", "Thread.Canceled");
  411. return;
  412. } else {
  413. handler.sendMessage(Message.obtain(handler, 1, i, sh.length, Integer.valueOf(position)));
  414. }
  415. System.out.println("shape: " + sh[i].getClass().getName());
  416. sh[i].draw(graphics);
  417. }
  418. }
  419. /**
  420. * Header / Footer settings for this slide.
  421. *
  422. * @return Header / Footer settings for this slide
  423. */
  424. public HeadersFooters getHeadersFooters(){
  425. HeadersFootersContainer hdd = null;
  426. Record[] ch = getSheetContainer().getChildRecords();
  427. boolean ppt2007 = false;
  428. for (int i = 0; i < ch.length; i++) {
  429. if(ch[i] instanceof HeadersFootersContainer){
  430. hdd = (HeadersFootersContainer)ch[i];
  431. } else if (ch[i].getRecordType() == RecordTypes.RoundTripContentMasterId.typeID){
  432. ppt2007 = true;
  433. }
  434. }
  435. boolean newRecord = false;
  436. if(hdd == null && !ppt2007) {
  437. return getSlideShow().getSlideHeadersFooters();
  438. }
  439. if(hdd == null) {
  440. hdd = new HeadersFootersContainer(HeadersFootersContainer.SlideHeadersFootersContainer);
  441. newRecord = true;
  442. }
  443. return new HeadersFooters(hdd, this, newRecord, ppt2007);
  444. }
  445. protected void onAddTextShape(TextShape shape) {
  446. TextRun run = shape.getTextRun();
  447. if(_runs == null) _runs = new TextRun[]{run};
  448. else {
  449. TextRun[] tmp = new TextRun[_runs.length + 1];
  450. System.arraycopy(_runs, 0, tmp, 0, _runs.length);
  451. tmp[tmp.length-1] = run;
  452. _runs = tmp;
  453. }
  454. }
  455. }