You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BookmarkData.java 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*
  2. * Copyright 1999-2004 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /* $Id$ */
  17. package org.apache.fop.area.extensions;
  18. import org.apache.fop.area.PageViewport;
  19. import org.apache.fop.area.Resolvable;
  20. import org.apache.fop.area.TreeExt;
  21. import org.apache.fop.area.AreaTreeModel;
  22. import java.util.ArrayList;
  23. import java.util.List;
  24. import java.util.HashMap;
  25. /**
  26. * This class holds the PDF bookmark extension data.
  27. * This implements Resolvable and TreeExt so that it can be
  28. * added to the area tree as a resolvable tree extension.
  29. */
  30. public class BookmarkData implements Resolvable, TreeExt {
  31. private ArrayList subData = new ArrayList();
  32. private HashMap idRefs = new HashMap();
  33. // area tree model for the top level object to activate when resolved
  34. private AreaTreeModel areaTreeModel = null;
  35. private String idRef;
  36. private PageViewport pageRef = null;
  37. private String label = null;
  38. /**
  39. * Create a new bookmark data object.
  40. * This should only be call by the top level element as the
  41. * id reference will be null.
  42. */
  43. public BookmarkData() {
  44. idRef = null;
  45. }
  46. /**
  47. * Create a new pdf bookmark data object.
  48. * This is used by the outlines to create a data object
  49. * with a id reference. The id reference is to be resolved.
  50. *
  51. * @param id the id reference
  52. */
  53. public BookmarkData(String id) {
  54. idRef = id;
  55. idRefs.put(idRef, this);
  56. }
  57. /**
  58. * Set the area tree model
  59. * This should only be called for the top level element.
  60. * The area tree model is used once resolving is complete.
  61. *
  62. * @param atm the area tree model for the current document
  63. */
  64. public void setAreaTreeModel(AreaTreeModel atm) {
  65. areaTreeModel = atm;
  66. }
  67. /**
  68. * Get the id reference for this data.
  69. *
  70. * @return the id reference
  71. */
  72. public String getID() {
  73. return idRef;
  74. }
  75. /**
  76. * Add the child bookmark data object.
  77. * This adds a child bookmark in the bookmark hierarchy.
  78. *
  79. * @param sub the child bookmark data
  80. */
  81. public void addSubData(BookmarkData sub) {
  82. subData.add(sub);
  83. idRefs.put(sub.getID(), sub);
  84. String[] ids = sub.getIDs();
  85. for (int count = 0; count < ids.length; count++) {
  86. idRefs.put(ids[count], sub);
  87. }
  88. }
  89. /**
  90. * Set the label for this bookmark.
  91. *
  92. * @param l the string label
  93. */
  94. public void setLabel(String l) {
  95. label = l;
  96. }
  97. /**
  98. * Get the label for this bookmark object.
  99. *
  100. * @return the label string
  101. */
  102. public String getLabel() {
  103. return label;
  104. }
  105. /**
  106. * Get the size of child data objects.
  107. *
  108. * @return the number of child bookmark data
  109. */
  110. public int getCount() {
  111. return subData.size();
  112. }
  113. /**
  114. * Get the child data object.
  115. *
  116. * @param count the index to get
  117. * @return the child bookmark data
  118. */
  119. public BookmarkData getSubData(int count) {
  120. return (BookmarkData)subData.get(count);
  121. }
  122. /**
  123. * Get the page that this resolves to.
  124. *
  125. * @return the PageViewport that this extension resolves to
  126. */
  127. public PageViewport getPage() {
  128. return pageRef;
  129. }
  130. /**
  131. * Check if this resolvable object has been resolved.
  132. * Once the id reference is null then it has been resolved.
  133. *
  134. * @return true if this has been resolved
  135. */
  136. public boolean isResolved() {
  137. return idRefs == null;
  138. }
  139. /**
  140. * Get the id references held by this object.
  141. * Also includes all id references of all children.
  142. *
  143. * @return the array of id references
  144. */
  145. public String[] getIDs() {
  146. return (String[])idRefs.keySet().toArray(new String[] {});
  147. }
  148. /**
  149. * Resolve this resolvable object.
  150. * This resolves the id reference and if possible also
  151. * resolves id references of child elements that have the same
  152. * id reference.
  153. *
  154. * @param id the id reference being resolved
  155. * @param pages the list of pages the the id reference resolves to
  156. */
  157. public void resolve(String id, List pages) {
  158. // this method is buggy
  159. if (!id.equals(idRef)) {
  160. BookmarkData bd = (BookmarkData)idRefs.get(id);
  161. idRefs.remove(id);
  162. if (bd != null) {
  163. bd.resolve(id, pages);
  164. if (bd.isResolved()) {
  165. checkFinish();
  166. }
  167. } else if (idRef == null) {
  168. checkFinish();
  169. }
  170. } else {
  171. if (pages != null) {
  172. pageRef = (PageViewport)pages.get(0);
  173. }
  174. // TODO get rect area of id on page
  175. idRefs.remove(idRef);
  176. checkFinish();
  177. }
  178. }
  179. private void checkFinish() {
  180. if (idRefs.size() == 0) {
  181. idRefs = null;
  182. if (areaTreeModel != null) {
  183. areaTreeModel.handleExtension(this, TreeExt.AFTER_PAGE);
  184. }
  185. }
  186. }
  187. }