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.

AbstractLayoutManager.java 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. /*
  2. * Copyright 1999-2006 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.layoutmgr;
  18. import org.apache.fop.fo.FObj;
  19. import org.apache.fop.fo.FONode;
  20. import org.apache.fop.area.Area;
  21. import org.apache.fop.area.PageViewport;
  22. import org.apache.fop.fo.Constants;
  23. import org.apache.fop.fo.flow.RetrieveMarker;
  24. import java.util.LinkedList;
  25. import java.util.List;
  26. import java.util.ArrayList;
  27. import java.util.ListIterator;
  28. import java.util.Map;
  29. /**
  30. * The base class for most LayoutManagers.
  31. */
  32. public abstract class AbstractLayoutManager extends AbstractBaseLayoutManager
  33. implements Constants {
  34. /** Parent LayoutManager for this LayoutManager */
  35. protected LayoutManager parentLM = null;
  36. /** List of child LayoutManagers */
  37. protected List childLMs = null;
  38. /** Iterator for child LayoutManagers */
  39. protected ListIterator fobjIter = null;
  40. /** Marker map for markers related to this LayoutManager */
  41. protected Map markers = null;
  42. /** True if this LayoutManager has handled all of its content. */
  43. private boolean bFinished = false;
  44. /** child LM and child LM iterator during getNextKnuthElement phase */
  45. protected LayoutManager curChildLM = null;
  46. /** child LM and child LM iterator during getNextKnuthElement phase */
  47. protected ListIterator childLMiter = null;
  48. private int lastGeneratedPosition = -1;
  49. private int smallestPosNumberChecked = Integer.MAX_VALUE;
  50. /**
  51. * Abstract layout manager.
  52. */
  53. public AbstractLayoutManager() {
  54. }
  55. /**
  56. * Abstract layout manager.
  57. *
  58. * @param fo the formatting object for this layout manager
  59. */
  60. public AbstractLayoutManager(FObj fo) {
  61. super(fo);
  62. if (fo == null) {
  63. throw new IllegalStateException("Null formatting object found.");
  64. }
  65. markers = fo.getMarkers();
  66. fobjIter = fo.getChildNodes();
  67. childLMiter = new LMiter(this);
  68. }
  69. /** @see LayoutManager#setParent(LayoutManager) */
  70. public void setParent(LayoutManager lm) {
  71. this.parentLM = lm;
  72. }
  73. /** @see LayoutManager#getParent */
  74. public LayoutManager getParent() {
  75. return this.parentLM;
  76. }
  77. /** @see LayoutManager#initialize */
  78. public void initialize() {
  79. // Empty
  80. }
  81. /**
  82. * Return currently active child LayoutManager or null if
  83. * all children have finished layout.
  84. * Note: child must implement LayoutManager! If it doesn't, skip it
  85. * and print a warning.
  86. * @return the current child LayoutManager
  87. */
  88. protected LayoutManager getChildLM() {
  89. if (curChildLM != null && !curChildLM.isFinished()) {
  90. return curChildLM;
  91. }
  92. while (childLMiter.hasNext()) {
  93. curChildLM = (LayoutManager) childLMiter.next();
  94. curChildLM.initialize();
  95. return curChildLM;
  96. }
  97. return null;
  98. }
  99. /**
  100. * Return indication if getChildLM will return another LM.
  101. * @return true if another child LM is still available
  102. */
  103. protected boolean hasNextChildLM() {
  104. return childLMiter.hasNext();
  105. }
  106. /**
  107. * Reset the layoutmanager "iterator" so that it will start
  108. * with the passed Position's generating LM
  109. * on the next call to getChildLM.
  110. * @param pos a Position returned by a child layout manager
  111. * representing a potential break decision.
  112. * If pos is null, then back up to the first child LM.
  113. */
  114. protected void reset(org.apache.fop.layoutmgr.Position pos) {
  115. //if (lm == null) return;
  116. LayoutManager lm = (pos != null) ? pos.getLM() : null;
  117. if (curChildLM != lm) {
  118. // ASSERT curChildLM == (LayoutManager)childLMiter.previous()
  119. if (childLMiter.hasPrevious() && curChildLM
  120. != (LayoutManager) childLMiter.previous()) {
  121. //log.error("LMiter problem!");
  122. }
  123. while (curChildLM != lm && childLMiter.hasPrevious()) {
  124. curChildLM.resetPosition(null);
  125. curChildLM = (LayoutManager) childLMiter.previous();
  126. }
  127. // Otherwise next returns same object
  128. childLMiter.next();
  129. }
  130. if (curChildLM != null) {
  131. curChildLM.resetPosition(pos);
  132. }
  133. if (isFinished()) {
  134. setFinished(false);
  135. }
  136. }
  137. /** @see LayoutManager#resetPosition(Position) */
  138. public void resetPosition(Position resetPos) {
  139. // if (resetPos == null) {
  140. // reset(null);
  141. // }
  142. }
  143. /**
  144. * Tell whether this LayoutManager has handled all of its content.
  145. * @return True if there are no more break possibilities,
  146. * ie. the last one returned represents the end of the content.
  147. */
  148. public boolean isFinished() {
  149. return bFinished;
  150. }
  151. /**
  152. * Set the flag indicating the LayoutManager has handled all of its content.
  153. * @param fin the flag value to be set
  154. */
  155. public void setFinished(boolean fin) {
  156. bFinished = fin;
  157. }
  158. /**
  159. * @see org.apache.fop.layoutmgr.LayoutManager#addAreas(
  160. * org.apache.fop.layoutmgr.PositionIterator
  161. * , org.apache.fop.layoutmgr.LayoutContext)
  162. */
  163. public void addAreas(PositionIterator posIter, LayoutContext context) {
  164. }
  165. /**
  166. * @see org.apache.fop.layoutmgr.LayoutManager#getNextKnuthElements(LayoutContext, int)
  167. */
  168. public LinkedList getNextKnuthElements(LayoutContext context,
  169. int alignment) {
  170. log.warn("null implementation of getNextKnuthElements() called!");
  171. setFinished(true);
  172. return null;
  173. }
  174. /**
  175. * @see org.apache.fop.layoutmgr.LayoutManager#getChangedKnuthElements(List, int)
  176. */
  177. public LinkedList getChangedKnuthElements(List oldList,
  178. int alignment) {
  179. log.warn("null implementation of getChangeKnuthElement() called!");
  180. return null;
  181. }
  182. /**
  183. * Return an Area which can contain the passed childArea. The childArea
  184. * may not yet have any content, but it has essential traits set.
  185. * In general, if the LayoutManager already has an Area it simply returns
  186. * it. Otherwise, it makes a new Area of the appropriate class.
  187. * It gets a parent area for its area by calling its parent LM.
  188. * Finally, based on the dimensions of the parent area, it initializes
  189. * its own area. This includes setting the content IPD and the maximum
  190. * BPD.
  191. * @param childArea the child area for which the parent area is wanted
  192. * @return the parent area for the given child
  193. */
  194. public Area getParentArea(Area childArea) {
  195. return null;
  196. }
  197. /**
  198. * Add a child area to the current area. If this causes the maximum
  199. * dimension of the current area to be exceeded, the parent LM is called
  200. * to add it.
  201. * @param childArea the child area to be added
  202. */
  203. public void addChildArea(Area childArea) {
  204. }
  205. /**
  206. * Create the LM instances for the children of the
  207. * formatting object being handled by this LM.
  208. * @param size the requested number of child LMs
  209. * @return the list with the preloaded child LMs
  210. */
  211. protected List createChildLMs(int size) {
  212. if (fobjIter == null) {
  213. return null;
  214. }
  215. List newLMs = new ArrayList(size);
  216. while (fobjIter.hasNext() && newLMs.size() < size ) {
  217. Object theobj = fobjIter.next();
  218. if (theobj instanceof FONode) {
  219. FONode foNode = (FONode) theobj;
  220. if (foNode instanceof RetrieveMarker) {
  221. foNode = getPSLM().resolveRetrieveMarker(
  222. (RetrieveMarker) foNode);
  223. }
  224. if (foNode != null) {
  225. getPSLM().getLayoutManagerMaker().
  226. makeLayoutManagers(foNode, newLMs);
  227. }
  228. }
  229. }
  230. return newLMs;
  231. }
  232. /**
  233. * @see org.apache.fop.layoutmgr.PageSequenceLayoutManager#getPSLM
  234. */
  235. public PageSequenceLayoutManager getPSLM() {
  236. return parentLM.getPSLM();
  237. }
  238. /**
  239. * @see org.apache.fop.layoutmgr.PageSequenceLayoutManager#getCurrentPage
  240. */
  241. public Page getCurrentPage() {
  242. return getPSLM().getCurrentPage();
  243. }
  244. /** @return the current page viewport */
  245. public PageViewport getCurrentPV() {
  246. return getPSLM().getCurrentPage().getPageViewport();
  247. }
  248. /**
  249. * @see org.apache.fop.layoutmgr.LayoutManager#createNextChildLMs
  250. */
  251. public boolean createNextChildLMs(int pos) {
  252. List newLMs = createChildLMs(pos + 1 - childLMs.size());
  253. addChildLMs(newLMs);
  254. return pos < childLMs.size();
  255. }
  256. /**
  257. * @see org.apache.fop.layoutmgr.LayoutManager#getChildLMs
  258. */
  259. public List getChildLMs() {
  260. if (childLMs == null) {
  261. childLMs = new java.util.ArrayList(10);
  262. }
  263. return childLMs;
  264. }
  265. /**
  266. * @see org.apache.fop.layoutmgr.LayoutManager#addChildLM
  267. */
  268. public void addChildLM(LayoutManager lm) {
  269. if (lm == null) {
  270. return;
  271. }
  272. lm.setParent(this);
  273. if (childLMs == null) {
  274. childLMs = new java.util.ArrayList(10);
  275. }
  276. childLMs.add(lm);
  277. log.trace(this.getClass().getName()
  278. + ": Adding child LM " + lm.getClass().getName());
  279. }
  280. /**
  281. * @see org.apache.fop.layoutmgr.LayoutManager#addChildLMs
  282. */
  283. public void addChildLMs(List newLMs) {
  284. if (newLMs == null || newLMs.size() == 0) {
  285. return;
  286. }
  287. ListIterator iter = newLMs.listIterator();
  288. while (iter.hasNext()) {
  289. LayoutManager lm = (LayoutManager) iter.next();
  290. addChildLM(lm);
  291. }
  292. }
  293. /**
  294. * Adds a Position to the Position participating in the first|last determination by assigning
  295. * it a unique position index.
  296. * @param pos the Position
  297. * @return the same Position but with a position index
  298. */
  299. public Position notifyPos(Position pos) {
  300. if (pos.getIndex() >= 0) {
  301. throw new IllegalStateException("Position already got its index");
  302. }
  303. lastGeneratedPosition++;
  304. pos.setIndex(lastGeneratedPosition);
  305. return pos;
  306. }
  307. /**
  308. * Indicates whether the given Position is the first area-generating Position of this LM.
  309. * @param pos the Position (must be one with a position index)
  310. * @return True if it is the first Position
  311. */
  312. public boolean isFirst(Position pos) {
  313. //log.trace("isFirst() smallestPosNumberChecked=" + smallestPosNumberChecked + " " + pos);
  314. if (pos.getIndex() < 0) {
  315. throw new IllegalArgumentException("Only Positions with an index can be checked");
  316. }
  317. if (pos.getIndex() == this.smallestPosNumberChecked) {
  318. return true;
  319. } else if (pos.getIndex() < this.smallestPosNumberChecked) {
  320. this.smallestPosNumberChecked = pos.getIndex();
  321. return true;
  322. } else {
  323. return false;
  324. }
  325. }
  326. /**
  327. * Indicates whether the given Position is the last area-generating Position of this LM.
  328. * @param pos the Position (must be one with a position index)
  329. * @return True if it is the last Position
  330. */
  331. public boolean isLast(Position pos) {
  332. //log.trace("isLast() lastGenPos=" + lastGeneratedPosition + " " + pos);
  333. if (pos.getIndex() < 0) {
  334. throw new IllegalArgumentException("Only Positions with an index can be checked");
  335. }
  336. return (pos.getIndex() == this.lastGeneratedPosition
  337. && isFinished());
  338. }
  339. /**
  340. * Transfers foreign attributes from the formatting object to the area.
  341. * @param targetArea the area to set the attributes on
  342. */
  343. protected void transferForeignAttributes(Area targetArea) {
  344. Map atts = getFObj().getForeignAttributes();
  345. targetArea.setForeignAttributes(atts);
  346. }
  347. }