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.7KB

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