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

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