KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pdfbox > pdmodel > interactive > pagenavigation > PDThreadBead


1 /**
2  * Copyright (c) 2005, www.pdfbox.org
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  * 3. Neither the name of pdfbox; nor the names of its
14  * contributors may be used to endorse or promote products derived from this
15  * software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
24  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  * http://www.pdfbox.org
29  *
30  */

31 package org.pdfbox.pdmodel.interactive.pagenavigation;
32
33 import org.pdfbox.cos.COSArray;
34 import org.pdfbox.cos.COSBase;
35 import org.pdfbox.cos.COSDictionary;
36 import org.pdfbox.cos.COSName;
37
38 import org.pdfbox.pdmodel.PDPage;
39 import org.pdfbox.pdmodel.common.COSObjectable;
40 import org.pdfbox.pdmodel.common.PDRectangle;
41
42 /**
43  * This a single bead in a thread in a PDF document.
44  *
45  * @author <a HREF="mailto:ben@benlitchfield.com">Ben Litchfield</a>
46  * @version $Revision: 1.4 $
47  */

48 public class PDThreadBead implements COSObjectable
49 {
50     
51     
52     private COSDictionary bead;
53
54     /**
55      * Constructor that is used for a preexisting dictionary.
56      *
57      * @param b The underlying dictionary.
58      */

59     public PDThreadBead( COSDictionary b )
60     {
61         bead = b;
62     }
63     
64     /**
65      * Default constructor.
66      *
67      */

68     public PDThreadBead()
69     {
70         bead = new COSDictionary();
71         bead.setName( "Type", "Bead" );
72         setNextBead( this );
73         setPreviousBead( this );
74     }
75
76     /**
77      * This will get the underlying dictionary that this object wraps.
78      *
79      * @return The underlying info dictionary.
80      */

81     public COSDictionary getDictionary()
82     {
83         return bead;
84     }
85     
86     /**
87      * Convert this standard java object to a COS object.
88      *
89      * @return The cos object that matches this Java object.
90      */

91     public COSBase getCOSObject()
92     {
93         return bead;
94     }
95     
96     /**
97      * This will get the thread that this bead is part of. This is only required
98      * for the first bead in a thread, so other beads 'may' return null.
99      *
100      * @return The thread that this bead is part of.
101      */

102     public PDThread getThread()
103     {
104         PDThread retval = null;
105         COSDictionary dic = (COSDictionary)bead.getDictionaryObject( "T" );
106         if( dic != null )
107         {
108             retval = new PDThread( dic );
109         }
110         return retval;
111     }
112     
113     /**
114      * Set the thread that this bead is part of. This is only required for the
115      * first bead in a thread. Note: This property is set for you by the PDThread.setFirstBead() method.
116      *
117      * @param thread The thread that this bead is part of.
118      */

119     public void setThread( PDThread thread )
120     {
121         bead.setItem( "T", thread );
122     }
123     
124     /**
125      * This will get the next bead. If this bead is the last bead in the list then this
126      * will return the first bead.
127      *
128      * @return The next bead in the list or the first bead if this is the last bead.
129      */

130     public PDThreadBead getNextBead()
131     {
132         return new PDThreadBead( (COSDictionary) bead.getDictionaryObject( "N" ) );
133     }
134     
135     /**
136      * Set the next bead in the thread.
137      *
138      * @param next The next bead.
139      */

140     protected void setNextBead( PDThreadBead next )
141     {
142         bead.setItem( "N", next );
143     }
144     
145     /**
146      * This will get the previous bead. If this bead is the first bead in the list then this
147      * will return the last bead.
148      *
149      * @return The previous bead in the list or the last bead if this is the first bead.
150      */

151     public PDThreadBead getPreviousBead()
152     {
153         return new PDThreadBead( (COSDictionary) bead.getDictionaryObject( "V" ) );
154     }
155     
156     /**
157      * Set the previous bead in the thread.
158      *
159      * @param previous The previous bead.
160      */

161     protected void setPreviousBead( PDThreadBead previous )
162     {
163         bead.setItem( "V", previous );
164     }
165     
166     /**
167      * Append a bead after this bead. This will correctly set the next/previous beads in the
168      * linked list.
169      *
170      * @param append The bead to insert.
171      */

172     public void appendBead( PDThreadBead append )
173     {
174         PDThreadBead nextBead = getNextBead();
175         nextBead.setPreviousBead( append );
176         append.setNextBead( nextBead );
177         setNextBead( append );
178         append.setPreviousBead( this );
179     }
180     
181     /**
182      * Get the page that this bead is part of.
183      *
184      * @return The page that this bead is part of.
185      */

186     public PDPage getPage()
187     {
188         PDPage page = null;
189         COSDictionary dic = (COSDictionary)bead.getDictionaryObject( "P" );
190         if( dic != null )
191         {
192             page = new PDPage( dic );
193         }
194         return page;
195     }
196     
197     /**
198      * Set the page that this bead is part of. This is a required property and must be
199      * set when creating a new bead. The PDPage object also has a list of beads in the natural
200      * reading order. It is recommended that you add this object to that list as well.
201      *
202      * @param page The page that this bead is on.
203      */

204     public void setPage( PDPage page )
205     {
206         bead.setItem( "P", page );
207     }
208     
209     /**
210      * The rectangle on the page that this bead is part of.
211      *
212      * @return The part of the page that this bead covers.
213      */

214     public PDRectangle getRectangle()
215     {
216         PDRectangle rect = null;
217         COSArray array = (COSArray)bead.getDictionaryObject( COSName.R );
218         if( array != null )
219         {
220             rect = new PDRectangle( array );
221         }
222         return rect;
223     }
224     
225     /**
226      * Set the rectangle on the page that this bead covers.
227      *
228      * @param rect The portion of the page that this bead covers.
229      */

230     public void setRectangle( PDRectangle rect )
231     {
232         bead.setItem( COSName.R, rect );
233     }
234 }
Popular Tags