001: /* ====================================================================
002: Licensed to the Apache Software Foundation (ASF) under one or more
003: contributor license agreements. See the NOTICE file distributed with
004: this work for additional information regarding copyright ownership.
005: The ASF licenses this file to You under the Apache License, Version 2.0
006: (the "License"); you may not use this file except in compliance with
007: the License. You may obtain a copy of the License at
008:
009: http://www.apache.org/licenses/LICENSE-2.0
010:
011: Unless required by applicable law or agreed to in writing, software
012: distributed under the License is distributed on an "AS IS" BASIS,
013: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: See the License for the specific language governing permissions and
015: limitations under the License.
016: ==================================================================== */
017:
018: package org.apache.poi.hslf.model;
019:
020: import java.util.Vector;
021: import java.util.List;
022: import java.util.Iterator;
023: import java.util.ArrayList;
024:
025: import org.apache.poi.hslf.record.PPDrawing;
026: import org.apache.poi.hslf.record.SlideAtom;
027: import org.apache.poi.hslf.record.TextHeaderAtom;
028: import org.apache.poi.hslf.record.ColorSchemeAtom;
029: import org.apache.poi.hslf.record.SlideListWithText.SlideAtomsSet;
030: import org.apache.poi.ddf.EscherContainerRecord;
031: import org.apache.poi.ddf.EscherRecord;
032:
033: /**
034: * This class represents a slide in a PowerPoint Document. It allows
035: * access to the text within, and the layout. For now, it only does
036: * the text side of things though
037: *
038: * @author Nick Burch
039: * @author Yegor Kozlov
040: */
041:
042: public class Slide extends Sheet {
043: private int _slideNo;
044: private SlideAtomsSet _atomSet;
045: private TextRun[] _runs;
046: private Notes _notes; // usermodel needs to set this
047:
048: /**
049: * Constructs a Slide from the Slide record, and the SlideAtomsSet
050: * containing the text.
051: * Initialises TextRuns, to provide easier access to the text
052: *
053: * @param slide the Slide record we're based on
054: * @param notes the Notes sheet attached to us
055: * @param atomSet the SlideAtomsSet to get the text from
056: */
057: public Slide(org.apache.poi.hslf.record.Slide slide, Notes notes,
058: SlideAtomsSet atomSet, int slideIdentifier, int slideNumber) {
059: super (slide, slideIdentifier);
060:
061: _notes = notes;
062: _atomSet = atomSet;
063: _slideNo = slideNumber;
064:
065: // Grab the TextRuns from the PPDrawing
066: TextRun[] _otherRuns = findTextRuns(getPPDrawing());
067:
068: // For the text coming in from the SlideAtomsSet:
069: // Build up TextRuns from pairs of TextHeaderAtom and
070: // one of TextBytesAtom or TextCharsAtom
071: Vector textRuns = new Vector();
072: if (_atomSet != null) {
073: findTextRuns(_atomSet.getSlideRecords(), textRuns);
074: } else {
075: // No text on the slide, must just be pictures
076: }
077:
078: // Build an array, more useful than a vector
079: _runs = new TextRun[textRuns.size() + _otherRuns.length];
080: // Grab text from SlideListWithTexts entries
081: int i = 0;
082: for (i = 0; i < textRuns.size(); i++) {
083: _runs[i] = (TextRun) textRuns.get(i);
084: _runs[i].setSheet(this );
085: }
086: // Grab text from slide's PPDrawing
087: for (int k = 0; k < _otherRuns.length; i++, k++) {
088: _runs[i] = _otherRuns[k];
089: _runs[i].setSheet(this );
090: }
091: }
092:
093: /**
094: * Create a new Slide instance
095: * @param sheetNumber The internal number of the sheet, as used by PersistPtrHolder
096: * @param slideNumber The user facing number of the sheet
097: */
098: public Slide(int sheetNumber, int sheetRefId, int slideNumber) {
099: super (new org.apache.poi.hslf.record.Slide(), sheetNumber);
100: _slideNo = slideNumber;
101: getSheetContainer().setSheetId(sheetRefId);
102: }
103:
104: /**
105: * Sets the Notes that are associated with this. Updates the
106: * references in the records to point to the new ID
107: */
108: public void setNotes(Notes notes) {
109: _notes = notes;
110:
111: // Update the Slide Atom's ID of where to point to
112: SlideAtom sa = getSlideRecord().getSlideAtom();
113:
114: if (notes == null) {
115: // Set to 0
116: sa.setNotesID(0);
117: } else {
118: // Set to the value from the notes' sheet id
119: sa.setNotesID(notes._getSheetNumber());
120: }
121: }
122:
123: /**
124: * Changes the Slide's (external facing) page number.
125: * @see org.apache.poi.hslf.usermodel.SlideShow#reorderSlide(int, int)
126: */
127: public void setSlideNumber(int newSlideNumber) {
128: _slideNo = newSlideNumber;
129: }
130:
131: /**
132: * Create a <code>TextBox</code> object that represents the slide's title.
133: *
134: * @return <code>TextBox</code> object that represents the slide's title.
135: */
136: public TextBox addTitle() {
137: Placeholder pl = new Placeholder();
138: pl.setShapeType(ShapeTypes.Rectangle);
139: pl.getTextRun().setRunType(TextHeaderAtom.TITLE_TYPE);
140: pl.setText("Click to edit title");
141: pl.setAnchor(new java.awt.Rectangle(54, 48, 612, 90));
142: addShape(pl);
143: return pl;
144: }
145:
146: // Complex Accesser methods follow
147:
148: /**
149: * Return title of this slide or <code>null</code> if the slide does not have title.
150: * <p>
151: * The title is a run of text of type <code>TextHeaderAtom.CENTER_TITLE_TYPE</code> or
152: * <code>TextHeaderAtom.TITLE_TYPE</code>
153: * </p>
154: *
155: * @see TextHeaderAtom
156: *
157: * @return title of this slide
158: */
159: public String getTitle() {
160: TextRun[] txt = getTextRuns();
161: for (int i = 0; i < txt.length; i++) {
162: int type = txt[i].getRunType();
163: if (type == TextHeaderAtom.CENTER_TITLE_TYPE
164: || type == TextHeaderAtom.TITLE_TYPE) {
165: String title = txt[i].getText();
166: return title;
167: }
168: }
169: return null;
170: }
171:
172: // Simple Accesser methods follow
173:
174: /**
175: * Returns an array of all the TextRuns found
176: */
177: public TextRun[] getTextRuns() {
178: return _runs;
179: }
180:
181: /**
182: * Returns the (public facing) page number of this slide
183: */
184: public int getSlideNumber() {
185: return _slideNo;
186: }
187:
188: /**
189: * Returns the underlying slide record
190: */
191: public org.apache.poi.hslf.record.Slide getSlideRecord() {
192: return (org.apache.poi.hslf.record.Slide) getSheetContainer();
193: }
194:
195: /**
196: * Returns the Notes Sheet for this slide, or null if there isn't one
197: */
198: public Notes getNotesSheet() {
199: return _notes;
200: }
201:
202: /**
203: * @return set of records inside <code>SlideListWithtext</code> container
204: * which hold text data for this slide (typically for placeholders).
205: */
206: protected SlideAtomsSet getSlideAtomsSet() {
207: return _atomSet;
208: }
209:
210: /**
211: * Returns master sheet associated with this slide.
212: * It can be either SlideMaster or TitleMaster objects.
213: *
214: * @return the master sheet associated with this slide.
215: */
216: public MasterSheet getMasterSheet() {
217: SlideMaster[] master = getSlideShow().getSlidesMasters();
218: SlideAtom sa = getSlideRecord().getSlideAtom();
219: int masterId = sa.getMasterID();
220: MasterSheet sheet = null;
221: for (int i = 0; i < master.length; i++) {
222: if (masterId == master[i]._getSheetNumber()) {
223: sheet = master[i];
224: break;
225: }
226: }
227: if (sheet == null) {
228: TitleMaster[] titleMaster = getSlideShow()
229: .getTitleMasters();
230: if (titleMaster != null)
231: for (int i = 0; i < titleMaster.length; i++) {
232: if (masterId == titleMaster[i]._getSheetNumber()) {
233: sheet = titleMaster[i];
234: break;
235: }
236: }
237: }
238: return sheet;
239: }
240:
241: /**
242: * Change Master of this slide.
243: */
244: public void setMasterSheet(MasterSheet master) {
245: SlideAtom sa = getSlideRecord().getSlideAtom();
246: int sheetNo = master._getSheetNumber();
247: sa.setMasterID(sheetNo);
248: }
249:
250: /**
251: * Sets whether this slide follows master background
252: *
253: * @param flag <code>true</code> if the slide follows master,
254: * <code>false</code> otherwise
255: */
256: public void setFollowMasterBackground(boolean flag) {
257: SlideAtom sa = getSlideRecord().getSlideAtom();
258: sa.setFollowMasterBackground(flag);
259: }
260:
261: /**
262: * Whether this slide follows master sheet background
263: *
264: * @return <code>true</code> if the slide follows master background,
265: * <code>false</code> otherwise
266: */
267: public boolean getFollowMasterBackground() {
268: SlideAtom sa = getSlideRecord().getSlideAtom();
269: return sa.getFollowMasterBackground();
270: }
271: }
|