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.

ListItemContentLayoutManager.java 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. /* $Id$ */
  18. package org.apache.fop.layoutmgr.list;
  19. import java.util.Iterator;
  20. import java.util.LinkedList;
  21. import java.util.List;
  22. import org.apache.fop.area.Area;
  23. import org.apache.fop.area.Block;
  24. import org.apache.fop.fo.flow.AbstractListItemPart;
  25. import org.apache.fop.fo.flow.ListItemBody;
  26. import org.apache.fop.fo.flow.ListItemLabel;
  27. import org.apache.fop.layoutmgr.BlockLevelLayoutManager;
  28. import org.apache.fop.layoutmgr.BlockStackingLayoutManager;
  29. import org.apache.fop.layoutmgr.LayoutContext;
  30. import org.apache.fop.layoutmgr.LayoutManager;
  31. import org.apache.fop.layoutmgr.NonLeafPosition;
  32. import org.apache.fop.layoutmgr.Position;
  33. import org.apache.fop.layoutmgr.PositionIterator;
  34. import org.apache.fop.layoutmgr.TraitSetter;
  35. import org.apache.fop.layoutmgr.SpaceResolver.SpaceHandlingBreakPosition;
  36. /**
  37. * LayoutManager for a list-item-label or list-item-body FO.
  38. */
  39. public class ListItemContentLayoutManager extends BlockStackingLayoutManager {
  40. private Block curBlockArea;
  41. private int xoffset;
  42. private int itemIPD;
  43. private static class StackingIter extends PositionIterator {
  44. StackingIter(Iterator parentIter) {
  45. super(parentIter);
  46. }
  47. protected LayoutManager getLM(Object nextObj) {
  48. return ((Position) nextObj).getLM();
  49. }
  50. protected Position getPos(Object nextObj) {
  51. return ((Position) nextObj);
  52. }
  53. }
  54. /**
  55. * Create a new Cell layout manager.
  56. * @param node list-item-label node
  57. */
  58. public ListItemContentLayoutManager(ListItemLabel node) {
  59. super(node);
  60. }
  61. /**
  62. * Create a new Cell layout manager.
  63. * @param node list-item-body node
  64. */
  65. public ListItemContentLayoutManager(ListItemBody node) {
  66. super(node);
  67. }
  68. /**
  69. * Convenience method.
  70. * @return the ListBlock node
  71. */
  72. protected AbstractListItemPart getPartFO() {
  73. return (AbstractListItemPart)fobj;
  74. }
  75. /**
  76. * Set the x offset of this list item.
  77. * This offset is used to set the absolute position
  78. * of the list item within the parent block area.
  79. *
  80. * @param off the x offset
  81. */
  82. public void setXOffset(int off) {
  83. xoffset = off;
  84. }
  85. /** {@inheritDoc} */
  86. public LinkedList getChangedKnuthElements(List oldList, int alignment) {
  87. //log.debug(" ListItemContentLayoutManager.getChanged>");
  88. return super.getChangedKnuthElements(oldList, alignment);
  89. }
  90. /**
  91. * Add the areas for the break points.
  92. * The list item contains block stacking layout managers
  93. * that add block areas.
  94. *
  95. * @param parentIter the iterator of the break positions
  96. * @param layoutContext the layout context for adding the areas
  97. */
  98. public void addAreas(PositionIterator parentIter,
  99. LayoutContext layoutContext) {
  100. getParentArea(null);
  101. addId();
  102. LayoutManager childLM;
  103. LayoutContext lc = new LayoutContext(0);
  104. LayoutManager firstLM = null;
  105. LayoutManager lastLM = null;
  106. Position firstPos = null;
  107. Position lastPos = null;
  108. // "unwrap" the NonLeafPositions stored in parentIter
  109. // and put them in a new list;
  110. LinkedList positionList = new LinkedList();
  111. Position pos;
  112. while (parentIter.hasNext()) {
  113. pos = (Position)parentIter.next();
  114. if (pos == null) {
  115. continue;
  116. }
  117. if (pos.getIndex() >= 0) {
  118. if (firstPos == null) {
  119. firstPos = pos;
  120. }
  121. lastPos = pos;
  122. }
  123. if (pos instanceof NonLeafPosition) {
  124. // pos was created by a child of this ListBlockLM
  125. positionList.add(pos.getPosition());
  126. lastLM = pos.getPosition().getLM();
  127. if (firstLM == null) {
  128. firstLM = lastLM;
  129. }
  130. } else if (pos instanceof SpaceHandlingBreakPosition) {
  131. positionList.add(pos);
  132. } else {
  133. // pos was created by this ListBlockLM, so it must be ignored
  134. }
  135. }
  136. addMarkersToPage(true, isFirst(firstPos), isLast(lastPos));
  137. StackingIter childPosIter = new StackingIter(positionList.listIterator());
  138. while ((childLM = childPosIter.getNextChildLM()) != null) {
  139. // Add the block areas to Area
  140. lc.setFlags(LayoutContext.FIRST_AREA, childLM == firstLM);
  141. lc.setFlags(LayoutContext.LAST_AREA, childLM == lastLM);
  142. // set the space adjustment ratio
  143. lc.setSpaceAdjust(layoutContext.getSpaceAdjust());
  144. lc.setStackLimitBP(layoutContext.getStackLimitBP());
  145. childLM.addAreas(childPosIter, lc);
  146. }
  147. addMarkersToPage(false, isFirst(firstPos), isLast(lastPos));
  148. flush();
  149. curBlockArea = null;
  150. getPSLM().notifyEndOfLayout(fobj.getId());
  151. }
  152. /**
  153. * Return an Area which can contain the passed childArea. The childArea
  154. * may not yet have any content, but it has essential traits set.
  155. * In general, if the LayoutManager already has an Area it simply returns
  156. * it. Otherwise, it makes a new Area of the appropriate class.
  157. * It gets a parent area for its area by calling its parent LM.
  158. * Finally, based on the dimensions of the parent area, it initializes
  159. * its own area. This includes setting the content IPD and the maximum
  160. * BPD.
  161. *
  162. * @param childArea the child area to get the parent for
  163. * @return the parent area
  164. */
  165. public Area getParentArea(Area childArea) {
  166. if (curBlockArea == null) {
  167. curBlockArea = new Block();
  168. curBlockArea.setPositioning(Block.ABSOLUTE);
  169. // set position
  170. curBlockArea.setXOffset(xoffset);
  171. curBlockArea.setIPD(itemIPD);
  172. //curBlockArea.setHeight();
  173. TraitSetter.setProducerID(curBlockArea, getPartFO().getId());
  174. // Set up dimensions
  175. Area parentArea = parentLM.getParentArea(curBlockArea);
  176. int referenceIPD = parentArea.getIPD();
  177. curBlockArea.setIPD(referenceIPD);
  178. // Get reference IPD from parentArea
  179. setCurrentArea(curBlockArea); // ??? for generic operations
  180. }
  181. return curBlockArea;
  182. }
  183. /**
  184. * Add the child to the list item area.
  185. *
  186. * @param childArea the child to add to the cell
  187. */
  188. public void addChildArea(Area childArea) {
  189. if (curBlockArea != null) {
  190. curBlockArea.addBlock((Block) childArea);
  191. }
  192. }
  193. /** {@inheritDoc} */
  194. public boolean mustKeepTogether() {
  195. //TODO Keeps will have to be more sophisticated sooner or later
  196. return ((BlockLevelLayoutManager)getParent()).mustKeepTogether()
  197. || !getPartFO().getKeepTogether().getWithinPage().isAuto()
  198. || !getPartFO().getKeepTogether().getWithinColumn().isAuto();
  199. }
  200. }