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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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.traits.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. /**
  70. * LayoutManager for a list-item FO.
  71. * The list item contains a list item label and a list item body.
  72. */
  73. public class ListItemLayoutManager extends BlockStackingLayoutManager {
  74. private Item label;
  75. private Item body;
  76. private Block curBlockArea = null;
  77. private List cellList = null;
  78. private int listItemHeight;
  79. private CommonBorderAndPadding borderProps = null;
  80. private CommonBackground backgroundProps;
  81. private class ItemPosition extends LeafPosition {
  82. protected List cellBreaks;
  83. protected ItemPosition(LayoutProcessor lm, int pos, List l) {
  84. super(lm, pos);
  85. cellBreaks = l;
  86. }
  87. }
  88. /**
  89. * Create a new list item layout manager.
  90. *
  91. */
  92. public ListItemLayoutManager() {
  93. }
  94. /**
  95. * @see org.apache.fop.layoutmgr.AbstractLayoutManager#initProperties(PropertyManager)
  96. */
  97. protected void initProperties(PropertyManager propMgr) {
  98. borderProps = propMgr.getBorderAndPadding();
  99. backgroundProps = propMgr.getBackgroundProps();
  100. }
  101. /**
  102. * Sets the label of the list item
  103. * @param item the label item
  104. */
  105. public void setLabel(Item item) {
  106. label = item;
  107. label.setParent(this);
  108. }
  109. /**
  110. * Sets the body of the list item
  111. * @param item the body item
  112. */
  113. public void setBody(Item item) {
  114. body = item;
  115. body.setParent(this);
  116. }
  117. /**
  118. * Get the next break possibility.
  119. *
  120. * @param context the layout context for getting breaks
  121. * @return the next break possibility
  122. */
  123. public BreakPoss getNextBreakPoss(LayoutContext context) {
  124. // currently active LM
  125. Item curLM;
  126. label.setUserAgent(getUserAgent());
  127. body.setUserAgent(getUserAgent());
  128. BreakPoss lastPos = null;
  129. List breakList = new ArrayList();
  130. int min = 0;
  131. int opt = 0;
  132. int max = 0;
  133. int stage = 0;
  134. boolean over = false;
  135. while (true) {
  136. if (stage == 0) {
  137. curLM = label;
  138. } else if (stage == 1) {
  139. curLM = body;
  140. } else {
  141. break;
  142. }
  143. List childBreaks = new ArrayList();
  144. MinOptMax stackSize = new MinOptMax();
  145. // Set up a LayoutContext
  146. // the ipd is from the current column
  147. int ipd = context.getRefIPD();
  148. BreakPoss bp;
  149. LayoutContext childLC = new LayoutContext(0);
  150. childLC.setStackLimit(
  151. MinOptMax.subtract(context.getStackLimit(),
  152. stackSize));
  153. if (stage == 0) {
  154. childLC.setRefIPD(24000);
  155. } else if (stage == 1) {
  156. childLC.setRefIPD(context.getRefIPD() - 24000);
  157. }
  158. stage++;
  159. while (!curLM.isFinished()) {
  160. if ((bp = curLM.getNextBreakPoss(childLC)) != null) {
  161. if (stackSize.opt + bp.getStackingSize().opt > context.getStackLimit().max) {
  162. // reset to last break
  163. if (lastPos != null) {
  164. LayoutProcessor lm = lastPos.getLayoutManager();
  165. lm.resetPosition(lastPos.getPosition());
  166. if (lm != curLM) {
  167. curLM.resetPosition(null);
  168. }
  169. } else {
  170. curLM.resetPosition(null);
  171. }
  172. over = true;
  173. break;
  174. } else {
  175. lastPos = bp;
  176. }
  177. stackSize.add(bp.getStackingSize());
  178. childBreaks.add(bp);
  179. if (bp.nextBreakOverflows()) {
  180. over = true;
  181. break;
  182. }
  183. childLC.setStackLimit(MinOptMax.subtract(
  184. context.getStackLimit(), stackSize));
  185. }
  186. }
  187. // the min is the maximum min of the label and body
  188. if (stackSize.min > min) {
  189. min = stackSize.min;
  190. }
  191. // the optimum is the minimum of all optimums
  192. if (stackSize.opt > opt) {
  193. opt = stackSize.opt;
  194. }
  195. // the maximum is the largest maximum
  196. if (stackSize.max > max) {
  197. max = stackSize.max;
  198. }
  199. breakList.add(childBreaks);
  200. }
  201. listItemHeight = opt;
  202. MinOptMax itemSize = new MinOptMax(min, opt, max);
  203. if (label.isFinished() && body.isFinished()) {
  204. setFinished(true);
  205. }
  206. ItemPosition rp = new ItemPosition(this, breakList.size() - 1, breakList);
  207. BreakPoss breakPoss = new BreakPoss(rp);
  208. if (over) {
  209. breakPoss.setFlag(BreakPoss.NEXT_OVERFLOWS, true);
  210. }
  211. breakPoss.setStackingSize(itemSize);
  212. return breakPoss;
  213. }
  214. /**
  215. * Add the areas for the break points.
  216. * This sets the offset of each cell as it is added.
  217. *
  218. * @param parentIter the position iterator
  219. * @param layoutContext the layout context for adding areas
  220. */
  221. public void addAreas(PositionIterator parentIter,
  222. LayoutContext layoutContext) {
  223. getParentArea(null);
  224. addID();
  225. Item childLM;
  226. LayoutContext lc = new LayoutContext(0);
  227. while (parentIter.hasNext()) {
  228. ItemPosition lfp = (ItemPosition) parentIter.next();
  229. // Add the block areas to Area
  230. for (Iterator iter = lfp.cellBreaks.iterator(); iter.hasNext();) {
  231. List cellsbr = (List)iter.next();
  232. PositionIterator breakPosIter;
  233. breakPosIter = new BreakPossPosIter(cellsbr, 0, cellsbr.size());
  234. while ((childLM = (Item)breakPosIter.getNextChildLM()) != null) {
  235. if (childLM == body) {
  236. childLM.setXOffset(24000);
  237. }
  238. childLM.addAreas(breakPosIter, lc);
  239. }
  240. }
  241. }
  242. curBlockArea.setHeight(listItemHeight);
  243. flush();
  244. curBlockArea = null;
  245. }
  246. /**
  247. * Get the height of the list item after adjusting.
  248. * Should only be called after adding the list item areas.
  249. *
  250. * @return the height of this list item after adjustment
  251. */
  252. public int getListItemHeight() {
  253. return listItemHeight;
  254. }
  255. /**
  256. * Return an Area which can contain the passed childArea. The childArea
  257. * may not yet have any content, but it has essential traits set.
  258. * In general, if the LayoutManager already has an Area it simply returns
  259. * it. Otherwise, it makes a new Area of the appropriate class.
  260. * It gets a parent area for its area by calling its parent LM.
  261. * Finally, based on the dimensions of the parent area, it initializes
  262. * its own area. This includes setting the content IPD and the maximum
  263. * BPD.
  264. *
  265. * @param childArea the child area
  266. * @return the parent are for the child
  267. */
  268. public Area getParentArea(Area childArea) {
  269. if (curBlockArea == null) {
  270. curBlockArea = new Block();
  271. // Set up dimensions
  272. Area parentArea = parentLM.getParentArea(curBlockArea);
  273. int referenceIPD = parentArea.getIPD();
  274. curBlockArea.setIPD(referenceIPD);
  275. curBlockArea.setWidth(referenceIPD);
  276. // Get reference IPD from parentArea
  277. setCurrentArea(curBlockArea); // ??? for generic operations
  278. }
  279. return curBlockArea;
  280. }
  281. /**
  282. * Add the child.
  283. * Rows return the areas returned by the child elements.
  284. * This simply adds the area to the parent layout manager.
  285. *
  286. * @param childArea the child area
  287. */
  288. public void addChild(Area childArea) {
  289. if (curBlockArea != null) {
  290. curBlockArea.addBlock((Block) childArea);
  291. }
  292. }
  293. /**
  294. * Reset the position of this layout manager.
  295. *
  296. * @param resetPos the position to reset to
  297. */
  298. public void resetPosition(Position resetPos) {
  299. if (resetPos == null) {
  300. reset(null);
  301. }
  302. }
  303. }