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.

ListItemLayoutManager.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. /*
  2. * $Id: ListItemLayoutManager.java,v 1.12 2003/03/07 07:58:52 jeremias Exp $
  3. * ============================================================================
  4. * The Apache Software License, Version 1.1
  5. * ============================================================================
  6. *
  7. * Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without modifica-
  10. * tion, are permitted provided that the following conditions are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright notice,
  13. * this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. *
  19. * 3. The end-user documentation included with the redistribution, if any, must
  20. * include the following acknowledgment: "This product includes software
  21. * developed by the Apache Software Foundation (http://www.apache.org/)."
  22. * Alternately, this acknowledgment may appear in the software itself, if
  23. * and wherever such third-party acknowledgments normally appear.
  24. *
  25. * 4. The names "FOP" and "Apache Software Foundation" must not be used to
  26. * endorse or promote products derived from this software without prior
  27. * written permission. For written permission, please contact
  28. * apache@apache.org.
  29. *
  30. * 5. Products derived from this software may not be called "Apache", nor may
  31. * "Apache" appear in their name, without prior written permission of the
  32. * Apache Software Foundation.
  33. *
  34. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
  35. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  36. * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  37. * APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  38. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
  39. * DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  40. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  41. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  42. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  43. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  44. * ============================================================================
  45. *
  46. * This software consists of voluntary contributions made by many individuals
  47. * on behalf of the Apache Software Foundation and was originally created by
  48. * James Tauber <jtauber@jtauber.com>. For more information on the Apache
  49. * Software Foundation, please see <http://www.apache.org/>.
  50. */
  51. package org.apache.fop.layoutmgr.list;
  52. import org.apache.fop.fo.PropertyManager;
  53. import org.apache.fop.layoutmgr.BlockStackingLayoutManager;
  54. import org.apache.fop.layoutmgr.LayoutProcessor;
  55. import org.apache.fop.layoutmgr.LeafPosition;
  56. import org.apache.fop.layoutmgr.BreakPoss;
  57. import org.apache.fop.layoutmgr.LayoutContext;
  58. import org.apache.fop.layoutmgr.PositionIterator;
  59. import org.apache.fop.layoutmgr.BreakPossPosIter;
  60. import org.apache.fop.layoutmgr.Position;
  61. import org.apache.fop.area.Area;
  62. import org.apache.fop.area.Block;
  63. import org.apache.fop.layoutmgr.MinOptMax;
  64. import org.apache.fop.fo.properties.CommonBorderAndPadding;
  65. import org.apache.fop.fo.properties.CommonBackground;
  66. import java.util.Iterator;
  67. import java.util.ArrayList;
  68. import java.util.List;
  69. import org.apache.fop.apps.*;
  70. /**
  71. * LayoutManager for a list-item FO.
  72. * The list item contains a list item label and a list item body.
  73. */
  74. public class ListItemLayoutManager extends BlockStackingLayoutManager {
  75. private Item label;
  76. private Item body;
  77. private Block curBlockArea = null;
  78. private List cellList = null;
  79. private int listItemHeight;
  80. private CommonBorderAndPadding borderProps = null;
  81. private CommonBackground backgroundProps;
  82. private class ItemPosition extends LeafPosition {
  83. protected List cellBreaks;
  84. protected ItemPosition(LayoutProcessor lm, int pos, List l) {
  85. super(lm, pos);
  86. cellBreaks = l;
  87. }
  88. }
  89. /**
  90. * Create a new list item layout manager.
  91. *
  92. */
  93. public ListItemLayoutManager() {
  94. }
  95. /**
  96. * @see org.apache.fop.layoutmgr.AbstractLayoutManager#initProperties(PropertyManager)
  97. */
  98. protected void initProperties(PropertyManager propMgr) {
  99. borderProps = propMgr.getBorderAndPadding();
  100. backgroundProps = propMgr.getBackgroundProps();
  101. }
  102. /**
  103. * Sets the label of the list item
  104. * @param item the label item
  105. */
  106. public void setLabel(Item item) {
  107. label = item;
  108. label.setParent(this);
  109. }
  110. /**
  111. * Sets the body of the list item
  112. * @param item the body item
  113. */
  114. public void setBody(Item item) {
  115. body = item;
  116. body.setParent(this);
  117. }
  118. /**
  119. * Get the next break possibility.
  120. *
  121. * @param context the layout context for getting breaks
  122. * @return the next break possibility
  123. */
  124. public BreakPoss getNextBreakPoss(LayoutContext context) {
  125. // currently active LM
  126. Item curLM;
  127. label.setUserAgent(getUserAgent());
  128. body.setUserAgent(getUserAgent());
  129. BreakPoss lastPos = null;
  130. List breakList = new ArrayList();
  131. int min = 0;
  132. int opt = 0;
  133. int max = 0;
  134. int stage = 0;
  135. boolean over = false;
  136. while (true) {
  137. if (stage == 0) {
  138. curLM = label;
  139. } else if (stage == 1) {
  140. curLM = body;
  141. } else {
  142. break;
  143. }
  144. List childBreaks = new ArrayList();
  145. MinOptMax stackSize = new MinOptMax();
  146. // Set up a LayoutContext
  147. // the ipd is from the current column
  148. int ipd = context.getRefIPD();
  149. BreakPoss bp;
  150. LayoutContext childLC = new LayoutContext(0);
  151. childLC.setStackLimit(
  152. MinOptMax.subtract(context.getStackLimit(),
  153. stackSize));
  154. if (stage == 0) {
  155. childLC.setRefIPD(24000);
  156. } else if (stage == 1) {
  157. childLC.setRefIPD(context.getRefIPD() - 24000);
  158. }
  159. stage++;
  160. while (!curLM.isFinished()) {
  161. if ((bp = curLM.getNextBreakPoss(childLC)) != null) {
  162. if (stackSize.opt + bp.getStackingSize().opt > context.getStackLimit().max) {
  163. // reset to last break
  164. if (lastPos != null) {
  165. LayoutProcessor lm = lastPos.getLayoutManager();
  166. lm.resetPosition(lastPos.getPosition());
  167. if (lm != curLM) {
  168. curLM.resetPosition(null);
  169. }
  170. } else {
  171. curLM.resetPosition(null);
  172. }
  173. over = true;
  174. break;
  175. } else {
  176. lastPos = bp;
  177. }
  178. stackSize.add(bp.getStackingSize());
  179. childBreaks.add(bp);
  180. if (bp.nextBreakOverflows()) {
  181. over = true;
  182. break;
  183. }
  184. childLC.setStackLimit(MinOptMax.subtract(
  185. context.getStackLimit(), stackSize));
  186. }
  187. }
  188. // the min is the maximum min of the label and body
  189. if (stackSize.min > min) {
  190. min = stackSize.min;
  191. }
  192. // the optimum is the minimum of all optimums
  193. if (stackSize.opt > opt) {
  194. opt = stackSize.opt;
  195. }
  196. // the maximum is the largest maximum
  197. if (stackSize.max > max) {
  198. max = stackSize.max;
  199. }
  200. breakList.add(childBreaks);
  201. }
  202. listItemHeight = opt;
  203. MinOptMax itemSize = new MinOptMax(min, opt, max);
  204. if (label.isFinished() && body.isFinished()) {
  205. setFinished(true);
  206. }
  207. ItemPosition rp = new ItemPosition(this, breakList.size() - 1, breakList);
  208. BreakPoss breakPoss = new BreakPoss(rp);
  209. if (over) {
  210. breakPoss.setFlag(BreakPoss.NEXT_OVERFLOWS, true);
  211. }
  212. breakPoss.setStackingSize(itemSize);
  213. return breakPoss;
  214. }
  215. /**
  216. * Add the areas for the break points.
  217. * This sets the offset of each cell as it is added.
  218. *
  219. * @param parentIter the position iterator
  220. * @param layoutContext the layout context for adding areas
  221. */
  222. public void addAreas(PositionIterator parentIter,
  223. LayoutContext layoutContext) {
  224. getParentArea(null);
  225. addID();
  226. Item childLM;
  227. LayoutContext lc = new LayoutContext(0);
  228. while (parentIter.hasNext()) {
  229. ItemPosition lfp = (ItemPosition) parentIter.next();
  230. // Add the block areas to Area
  231. for (Iterator iter = lfp.cellBreaks.iterator(); iter.hasNext();) {
  232. List cellsbr = (List)iter.next();
  233. PositionIterator breakPosIter;
  234. breakPosIter = new BreakPossPosIter(cellsbr, 0, cellsbr.size());
  235. while ((childLM = (Item)breakPosIter.getNextChildLM()) != null) {
  236. if (childLM == body) {
  237. childLM.setXOffset(24000);
  238. }
  239. childLM.addAreas(breakPosIter, lc);
  240. }
  241. }
  242. }
  243. curBlockArea.setHeight(listItemHeight);
  244. flush();
  245. curBlockArea = null;
  246. }
  247. /**
  248. * Get the height of the list item after adjusting.
  249. * Should only be called after adding the list item areas.
  250. *
  251. * @return the height of this list item after adjustment
  252. */
  253. public int getListItemHeight() {
  254. return listItemHeight;
  255. }
  256. /**
  257. * Return an Area which can contain the passed childArea. The childArea
  258. * may not yet have any content, but it has essential traits set.
  259. * In general, if the LayoutManager already has an Area it simply returns
  260. * it. Otherwise, it makes a new Area of the appropriate class.
  261. * It gets a parent area for its area by calling its parent LM.
  262. * Finally, based on the dimensions of the parent area, it initializes
  263. * its own area. This includes setting the content IPD and the maximum
  264. * BPD.
  265. *
  266. * @param childArea the child area
  267. * @return the parent are for the child
  268. */
  269. public Area getParentArea(Area childArea) {
  270. if (curBlockArea == null) {
  271. curBlockArea = new Block();
  272. // Set up dimensions
  273. Area parentArea = parentLM.getParentArea(curBlockArea);
  274. int referenceIPD = parentArea.getIPD();
  275. curBlockArea.setIPD(referenceIPD);
  276. curBlockArea.setWidth(referenceIPD);
  277. // Get reference IPD from parentArea
  278. setCurrentArea(curBlockArea); // ??? for generic operations
  279. }
  280. return curBlockArea;
  281. }
  282. /**
  283. * Add the child.
  284. * Rows return the areas returned by the child elements.
  285. * This simply adds the area to the parent layout manager.
  286. *
  287. * @param childArea the child area
  288. */
  289. public void addChild(Area childArea) {
  290. if (curBlockArea != null) {
  291. curBlockArea.addBlock((Block) childArea);
  292. }
  293. }
  294. /**
  295. * Reset the position of this layout manager.
  296. *
  297. * @param resetPos the position to reset to
  298. */
  299. public void resetPosition(Position resetPos) {
  300. if (resetPos == null) {
  301. reset(null);
  302. }
  303. }
  304. }