KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > layoutmgr > list > ListItemContentLayoutManager


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
18 /* $Id: ListItemContentLayoutManager.java 428011 2006-08-02 14:49:00Z jeremias $ */
19  
20 package org.apache.fop.layoutmgr.list;
21
22 import org.apache.fop.fo.flow.AbstractListItemPart;
23 import org.apache.fop.fo.flow.ListItemBody;
24 import org.apache.fop.fo.flow.ListItemLabel;
25 import org.apache.fop.layoutmgr.BlockLevelLayoutManager;
26 import org.apache.fop.layoutmgr.BlockStackingLayoutManager;
27 import org.apache.fop.layoutmgr.LayoutManager;
28 import org.apache.fop.layoutmgr.LayoutContext;
29 import org.apache.fop.layoutmgr.PositionIterator;
30 import org.apache.fop.layoutmgr.Position;
31 import org.apache.fop.layoutmgr.NonLeafPosition;
32 import org.apache.fop.layoutmgr.TraitSetter;
33 import org.apache.fop.layoutmgr.SpaceResolver.SpaceHandlingBreakPosition;
34 import org.apache.fop.area.Area;
35 import org.apache.fop.area.Block;
36
37 import java.util.Iterator JavaDoc;
38 import java.util.List JavaDoc;
39 import java.util.LinkedList JavaDoc;
40
41 /**
42  * LayoutManager for a list-item-label or list-item-body FO.
43  */

44 public class ListItemContentLayoutManager extends BlockStackingLayoutManager {
45
46     private Block curBlockArea;
47
48     private int xoffset;
49     private int itemIPD;
50
51     private static class StackingIter extends PositionIterator {
52         StackingIter(Iterator parentIter) {
53             super(parentIter);
54         }
55
56         protected LayoutManager getLM(Object JavaDoc nextObj) {
57             return ((Position) nextObj).getLM();
58         }
59
60         protected Position getPos(Object JavaDoc nextObj) {
61             return ((Position) nextObj);
62         }
63     }
64
65     /**
66      * Create a new Cell layout manager.
67      * @param node list-item-label node
68      */

69     public ListItemContentLayoutManager(ListItemLabel node) {
70         super(node);
71     }
72
73     /**
74      * Create a new Cell layout manager.
75      * @param node list-item-body node
76      */

77     public ListItemContentLayoutManager(ListItemBody node) {
78         super(node);
79     }
80
81     /**
82      * Convenience method.
83      * @return the ListBlock node
84      */

85     protected AbstractListItemPart getPartFO() {
86         return (AbstractListItemPart)fobj;
87     }
88     
89     /**
90      * Set the x offset of this list item.
91      * This offset is used to set the absolute position
92      * of the list item within the parent block area.
93      *
94      * @param off the x offset
95      */

96     public void setXOffset(int off) {
97         xoffset = off;
98     }
99
100     /** @see org.apache.fop.layoutmgr.LayoutManager#getChangedKnuthElements(java.util.List, int) */
101     public LinkedList JavaDoc getChangedKnuthElements(List JavaDoc oldList, int alignment) {
102         //log.debug(" ListItemContentLayoutManager.getChanged>");
103
return super.getChangedKnuthElements(oldList, alignment);
104     }
105
106     /**
107      * Add the areas for the break points.
108      * The list item contains block stacking layout managers
109      * that add block areas.
110      *
111      * @param parentIter the iterator of the break positions
112      * @param layoutContext the layout context for adding the areas
113      */

114     public void addAreas(PositionIterator parentIter,
115                          LayoutContext layoutContext) {
116         getParentArea(null);
117         
118         getPSLM().addIDToPage(getPartFO().getId());
119
120         LayoutManager childLM = null;
121         LayoutContext lc = new LayoutContext(0);
122         LayoutManager firstLM = null;
123         LayoutManager lastLM = null;
124         Position firstPos = null;
125         Position lastPos = null;
126
127         // "unwrap" the NonLeafPositions stored in parentIter
128
// and put them in a new list;
129
LinkedList JavaDoc positionList = new LinkedList JavaDoc();
130         Position pos;
131         while (parentIter.hasNext()) {
132             pos = (Position)parentIter.next();
133             if (pos == null) {
134                 continue;
135             }
136             if (pos.getIndex() >= 0) {
137                 if (firstPos == null) {
138                     firstPos = pos;
139                 }
140                 lastPos = pos;
141             }
142             if (pos instanceof NonLeafPosition) {
143                 // pos was created by a child of this ListBlockLM
144
positionList.add(((NonLeafPosition) pos).getPosition());
145                 lastLM = ((NonLeafPosition) pos).getPosition().getLM();
146                 if (firstLM == null) {
147                     firstLM = lastLM;
148                 }
149             } else if (pos instanceof SpaceHandlingBreakPosition) {
150                 positionList.add(pos);
151             } else {
152                 // pos was created by this ListBlockLM, so it must be ignored
153
}
154         }
155
156         if (markers != null) {
157             getCurrentPV().addMarkers(markers, true, isFirst(firstPos), isLast(lastPos));
158         }
159         
160         StackingIter childPosIter = new StackingIter(positionList.listIterator());
161         while ((childLM = childPosIter.getNextChildLM()) != null) {
162             // Add the block areas to Area
163
lc.setFlags(LayoutContext.FIRST_AREA, childLM == firstLM);
164             lc.setFlags(LayoutContext.LAST_AREA, childLM == lastLM);
165             // set the space adjustment ratio
166
lc.setSpaceAdjust(layoutContext.getSpaceAdjust());
167             lc.setStackLimit(layoutContext.getStackLimit());
168             childLM.addAreas(childPosIter, lc);
169         }
170
171         if (markers != null) {
172             getCurrentPV().addMarkers(markers, false, isFirst(firstPos), isLast(lastPos));
173         }
174
175         flush();
176
177         curBlockArea = null;
178         
179         getPSLM().notifyEndOfLayout(((AbstractListItemPart)getFObj()).getId());
180     }
181
182     /**
183      * Return an Area which can contain the passed childArea. The childArea
184      * may not yet have any content, but it has essential traits set.
185      * In general, if the LayoutManager already has an Area it simply returns
186      * it. Otherwise, it makes a new Area of the appropriate class.
187      * It gets a parent area for its area by calling its parent LM.
188      * Finally, based on the dimensions of the parent area, it initializes
189      * its own area. This includes setting the content IPD and the maximum
190      * BPD.
191      *
192      * @param childArea the child area to get the parent for
193      * @return the parent area
194      */

195     public Area getParentArea(Area childArea) {
196         if (curBlockArea == null) {
197             curBlockArea = new Block();
198             curBlockArea.setPositioning(Block.ABSOLUTE);
199             // set position
200
curBlockArea.setXOffset(xoffset);
201             curBlockArea.setIPD(itemIPD);
202             //curBlockArea.setHeight();
203

204             TraitSetter.setProducerID(curBlockArea, getPartFO().getId());
205             
206             // Set up dimensions
207
Area parentArea = parentLM.getParentArea(curBlockArea);
208             int referenceIPD = parentArea.getIPD();
209             curBlockArea.setIPD(referenceIPD);
210             // Get reference IPD from parentArea
211
setCurrentArea(curBlockArea); // ??? for generic operations
212
}
213         return curBlockArea;
214     }
215
216     /**
217      * Add the child to the list item area.
218      *
219      * @param childArea the child to add to the cell
220      */

221     public void addChildArea(Area childArea) {
222         if (curBlockArea != null) {
223             curBlockArea.addBlock((Block) childArea);
224         }
225     }
226
227     /**
228      * Reset the position of the layout.
229      *
230      * @param resetPos the position to reset to
231      */

232     public void resetPosition(Position resetPos) {
233         if (resetPos == null) {
234             reset(null);
235         } else {
236             setFinished(false);
237             //reset(resetPos);
238
}
239     }
240     
241     /** @see org.apache.fop.layoutmgr.BlockLevelLayoutManager#mustKeepTogether() */
242     public boolean mustKeepTogether() {
243         //TODO Keeps will have to be more sophisticated sooner or later
244
return ((BlockLevelLayoutManager)getParent()).mustKeepTogether()
245                 || !getPartFO().getKeepTogether().getWithinPage().isAuto()
246                 || !getPartFO().getKeepTogether().getWithinColumn().isAuto();
247     }
248
249 }
250
251
Popular Tags