001: /**
002: * Copyright (c) 2005, www.pdfbox.org
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions are met:
007: *
008: * 1. Redistributions of source code must retain the above copyright notice,
009: * this list of conditions and the following disclaimer.
010: * 2. Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: * 3. Neither the name of pdfbox; nor the names of its
014: * contributors may be used to endorse or promote products derived from this
015: * software without specific prior written permission.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
018: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
019: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
020: * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
021: * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
022: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
023: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
024: * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
025: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
026: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027: *
028: * http://www.pdfbox.org
029: *
030: */package org.pdfbox.pdmodel.interactive.pagenavigation;
031:
032: import org.pdfbox.cos.COSArray;
033: import org.pdfbox.cos.COSBase;
034: import org.pdfbox.cos.COSDictionary;
035: import org.pdfbox.cos.COSName;
036:
037: import org.pdfbox.pdmodel.PDPage;
038: import org.pdfbox.pdmodel.common.COSObjectable;
039: import org.pdfbox.pdmodel.common.PDRectangle;
040:
041: /**
042: * This a single bead in a thread in a PDF document.
043: *
044: * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a>
045: * @version $Revision: 1.4 $
046: */
047: public class PDThreadBead implements COSObjectable {
048:
049: private COSDictionary bead;
050:
051: /**
052: * Constructor that is used for a preexisting dictionary.
053: *
054: * @param b The underlying dictionary.
055: */
056: public PDThreadBead(COSDictionary b) {
057: bead = b;
058: }
059:
060: /**
061: * Default constructor.
062: *
063: */
064: public PDThreadBead() {
065: bead = new COSDictionary();
066: bead.setName("Type", "Bead");
067: setNextBead(this );
068: setPreviousBead(this );
069: }
070:
071: /**
072: * This will get the underlying dictionary that this object wraps.
073: *
074: * @return The underlying info dictionary.
075: */
076: public COSDictionary getDictionary() {
077: return bead;
078: }
079:
080: /**
081: * Convert this standard java object to a COS object.
082: *
083: * @return The cos object that matches this Java object.
084: */
085: public COSBase getCOSObject() {
086: return bead;
087: }
088:
089: /**
090: * This will get the thread that this bead is part of. This is only required
091: * for the first bead in a thread, so other beads 'may' return null.
092: *
093: * @return The thread that this bead is part of.
094: */
095: public PDThread getThread() {
096: PDThread retval = null;
097: COSDictionary dic = (COSDictionary) bead
098: .getDictionaryObject("T");
099: if (dic != null) {
100: retval = new PDThread(dic);
101: }
102: return retval;
103: }
104:
105: /**
106: * Set the thread that this bead is part of. This is only required for the
107: * first bead in a thread. Note: This property is set for you by the PDThread.setFirstBead() method.
108: *
109: * @param thread The thread that this bead is part of.
110: */
111: public void setThread(PDThread thread) {
112: bead.setItem("T", thread);
113: }
114:
115: /**
116: * This will get the next bead. If this bead is the last bead in the list then this
117: * will return the first bead.
118: *
119: * @return The next bead in the list or the first bead if this is the last bead.
120: */
121: public PDThreadBead getNextBead() {
122: return new PDThreadBead((COSDictionary) bead
123: .getDictionaryObject("N"));
124: }
125:
126: /**
127: * Set the next bead in the thread.
128: *
129: * @param next The next bead.
130: */
131: protected void setNextBead(PDThreadBead next) {
132: bead.setItem("N", next);
133: }
134:
135: /**
136: * This will get the previous bead. If this bead is the first bead in the list then this
137: * will return the last bead.
138: *
139: * @return The previous bead in the list or the last bead if this is the first bead.
140: */
141: public PDThreadBead getPreviousBead() {
142: return new PDThreadBead((COSDictionary) bead
143: .getDictionaryObject("V"));
144: }
145:
146: /**
147: * Set the previous bead in the thread.
148: *
149: * @param previous The previous bead.
150: */
151: protected void setPreviousBead(PDThreadBead previous) {
152: bead.setItem("V", previous);
153: }
154:
155: /**
156: * Append a bead after this bead. This will correctly set the next/previous beads in the
157: * linked list.
158: *
159: * @param append The bead to insert.
160: */
161: public void appendBead(PDThreadBead append) {
162: PDThreadBead nextBead = getNextBead();
163: nextBead.setPreviousBead(append);
164: append.setNextBead(nextBead);
165: setNextBead(append);
166: append.setPreviousBead(this );
167: }
168:
169: /**
170: * Get the page that this bead is part of.
171: *
172: * @return The page that this bead is part of.
173: */
174: public PDPage getPage() {
175: PDPage page = null;
176: COSDictionary dic = (COSDictionary) bead
177: .getDictionaryObject("P");
178: if (dic != null) {
179: page = new PDPage(dic);
180: }
181: return page;
182: }
183:
184: /**
185: * Set the page that this bead is part of. This is a required property and must be
186: * set when creating a new bead. The PDPage object also has a list of beads in the natural
187: * reading order. It is recommended that you add this object to that list as well.
188: *
189: * @param page The page that this bead is on.
190: */
191: public void setPage(PDPage page) {
192: bead.setItem("P", page);
193: }
194:
195: /**
196: * The rectangle on the page that this bead is part of.
197: *
198: * @return The part of the page that this bead covers.
199: */
200: public PDRectangle getRectangle() {
201: PDRectangle rect = null;
202: COSArray array = (COSArray) bead.getDictionaryObject(COSName.R);
203: if (array != null) {
204: rect = new PDRectangle(array);
205: }
206: return rect;
207: }
208:
209: /**
210: * Set the rectangle on the page that this bead covers.
211: *
212: * @param rect The portion of the page that this bead covers.
213: */
214: public void setRectangle(PDRectangle rect) {
215: bead.setItem(COSName.R, rect);
216: }
217: }
|