KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > area > BookmarkData


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  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 /* $Id: BookmarkData.java 426576 2006-07-28 15:44:37Z jeremias $ */
19  
20 package org.apache.fop.area;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.HashMap JavaDoc;
25
26 import org.apache.fop.fo.pagination.bookmarks.BookmarkTree;
27 import org.apache.fop.fo.pagination.bookmarks.Bookmark;
28
29 /**
30  * An instance of this class is either a PDF bookmark-tree and
31  * its child bookmark-items, or a bookmark-item and the child
32  * child bookmark-items under it.
33  */

34 public class BookmarkData extends AbstractOffDocumentItem implements Resolvable {
35     private ArrayList JavaDoc subData = new ArrayList JavaDoc();
36
37     // bookmark-title for this fo:bookmark
38
private String JavaDoc bookmarkTitle = null;
39
40     // indicator of whether to initially display/hide child bookmarks of this object
41
private boolean bShow = true;
42
43     // ID Reference for this bookmark
44
private String JavaDoc idRef;
45
46     // PageViewport that the idRef item refers to
47
private PageViewport pageRef = null;
48
49     // unresolved idrefs by this bookmark and child bookmarks below it
50
private HashMap JavaDoc unresolvedIDRefs = new HashMap JavaDoc();
51
52     /**
53      * Create a new bookmark data object.
54      * This should only be called by the bookmark-tree item because
55      * it has no idref item that needs to be resolved.
56      *
57      * @param bookmarkTree fo:bookmark-tree for this document
58      */

59     public BookmarkData(BookmarkTree bookmarkTree) {
60         idRef = null;
61         whenToProcess = END_OF_DOC;
62         // top level defined in Rec to show all child bookmarks
63
bShow = true;
64         
65         for (int count = 0; count < bookmarkTree.getBookmarks().size(); count++) {
66             Bookmark bkmk = (Bookmark)(bookmarkTree.getBookmarks()).get(count);
67             addSubData(createBookmarkData(bkmk));
68         }
69     }
70
71     /**
72      * Create a new pdf bookmark data object.
73      * This is used by the bookmark-items to create a data object
74      * with a idref. During processing, this idref will be
75      * subsequently resolved to a particular PageViewport.
76      *
77      * @param bookmark the fo:bookmark object
78      */

79     public BookmarkData(Bookmark bookmark) {
80         bookmarkTitle = bookmark.getBookmarkTitle();
81         bShow = bookmark.showChildItems();
82         this.idRef = bookmark.getInternalDestination();
83         unresolvedIDRefs.put(idRef, this);
84     }
85
86     /**
87      * Get the idref for this bookmark-item
88      *
89      * @return the idref for the bookmark-item
90      */

91     public String JavaDoc getIDRef() {
92         return idRef;
93     }
94
95     /**
96      * Add the child bookmark data object.
97      * This adds a child bookmark in the bookmark hierarchy.
98      *
99      * @param sub the child bookmark data
100      */

101     public void addSubData(BookmarkData sub) {
102         subData.add(sub);
103         unresolvedIDRefs.put(sub.getIDRef(), sub);
104         String JavaDoc[] ids = sub.getIDRefs();
105         for (int count = 0; count < ids.length; count++) {
106             unresolvedIDRefs.put(ids[count], sub);
107         }
108     }
109
110     /**
111      * Get the title for this bookmark object.
112      *
113      * @return the bookmark title
114      */

115     public String JavaDoc getBookmarkTitle() {
116         return bookmarkTitle;
117     }
118
119     /**
120      * Indicator of whether to initially display child bookmarks.
121      *
122      * @return true to initially display child bookmarks, false otherwise
123      */

124     public boolean showChildItems() {
125         return bShow;
126     }
127
128     /**
129      * Get the size of child data objects.
130      *
131      * @return the number of child bookmark data
132      */

133     public int getCount() {
134         return subData.size();
135     }
136
137     /**
138      * Get the child data object.
139      *
140      * @param count the index to get
141      * @return the child bookmark data
142      */

143     public BookmarkData getSubData(int count) {
144         return (BookmarkData) subData.get(count);
145     }
146
147     /**
148      * Get the PageViewport object that this bookmark refers to
149      *
150      * @return the PageViewport that this bookmark points to
151      */

152     public PageViewport getPageViewport() {
153         return pageRef;
154     }
155
156     /**
157      * Check if this resolvable object has been resolved.
158      * A BookmarkData object is considered resolved once the idrefs for it
159      * and for all of its child bookmark-items have been resolved.
160      *
161      * @return true if this object has been resolved
162      */

163     public boolean isResolved() {
164         return unresolvedIDRefs == null || (unresolvedIDRefs.size() == 0);
165     }
166
167     /**
168      * @see org.apache.fop.area.Resolvable#getIDRefs()
169      */

170     public String JavaDoc[] getIDRefs() {
171         return (String JavaDoc[])unresolvedIDRefs.keySet().toArray(new String JavaDoc[] {});
172     }
173
174     /**
175      * Resolve this resolvable object.
176      * This resolves the idref of this object and if possible also
177      * resolves id references of child elements that have the same
178      * id reference.
179      *
180      * @see org.apache.fop.area.Resolvable#resolveIDRef(String, List)
181      * @todo check to make sure it works if multiple bookmark-items
182      * have the same idref
183      */

184     public void resolveIDRef(String JavaDoc id, List JavaDoc pages) {
185         if (!id.equals(idRef)) {
186             BookmarkData bd = (BookmarkData) unresolvedIDRefs.get(id);
187             if (bd != null) {
188                 bd.resolveIDRef(id, pages);
189                 unresolvedIDRefs.remove(id);
190             }
191         } else {
192             pageRef = (PageViewport) pages.get(0);
193             // TODO get rect area of id on page
194
unresolvedIDRefs.remove(idRef);
195         }
196     }
197
198     /**
199      * @see org.apache.fop.area.OffDocumentItem#getName()
200      */

201     public String JavaDoc getName() {
202         return "Bookmarks";
203     }
204
205     /**
206      * Create and return the bookmark data for this bookmark
207      * This creates a bookmark data with the destination
208      * and adds all the data from child bookmarks
209      *
210      * @param bookmark the Bookmark object for which a bookmark entry should be
211      * created
212      * @return the new bookmark data
213      */

214     private BookmarkData createBookmarkData(Bookmark bookmark) {
215         BookmarkData data = new BookmarkData(bookmark);
216         for (int count = 0; count < bookmark.getChildBookmarks().size(); count++) {
217             Bookmark bkmk = (Bookmark)(bookmark.getChildBookmarks()).get(count);
218             data.addSubData(createBookmarkData(bkmk));
219         }
220         return data;
221     }
222
223 }
224
225
Popular Tags