KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > layoutmgr > AreaAdditionUtil


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: AreaAdditionUtil.java 426576 2006-07-28 15:44:37Z jeremias $ */
19
20 package org.apache.fop.layoutmgr;
21
22 import java.util.Iterator JavaDoc;
23 import java.util.LinkedList JavaDoc;
24
25 import org.apache.fop.layoutmgr.SpaceResolver.SpaceHandlingBreakPosition;
26
27 /**
28  * Utility class which provides common code for the addAreas stage.
29  */

30 public class AreaAdditionUtil {
31
32     private static class StackingIter extends PositionIterator {
33         StackingIter(Iterator JavaDoc parentIter) {
34             super(parentIter);
35         }
36
37         protected LayoutManager getLM(Object JavaDoc nextObj) {
38             return ((Position) nextObj).getLM();
39         }
40
41         protected Position getPos(Object JavaDoc nextObj) {
42             return ((Position) nextObj);
43         }
44     }
45
46     /**
47      * Creates the child areas for the given layout manager.
48      * @param bslm the BlockStackingLayoutManager instance for which "addAreas" is performed.
49      * @param parentIter the position iterator
50      * @param layoutContext the layout context
51      */

52     public static void addAreas(BlockStackingLayoutManager bslm,
53             PositionIterator parentIter, LayoutContext layoutContext) {
54         LayoutManager childLM = null;
55         LayoutContext lc = new LayoutContext(0);
56         LayoutManager firstLM = null;
57         LayoutManager lastLM = null;
58         Position firstPos = null;
59         Position lastPos = null;
60
61         // "unwrap" the NonLeafPositions stored in parentIter
62
// and put them in a new list;
63
LinkedList JavaDoc positionList = new LinkedList JavaDoc();
64         Position pos;
65         while (parentIter.hasNext()) {
66             pos = (Position)parentIter.next();
67             if (pos == null) {
68                 continue;
69             }
70             if (pos.getIndex() >= 0) {
71                 if (firstPos == null) {
72                     firstPos = pos;
73                 }
74                 lastPos = pos;
75             }
76             if (pos instanceof NonLeafPosition) {
77                 // pos was created by a child of this FlowLM
78
positionList.add(((NonLeafPosition) pos).getPosition());
79                 lastLM = ((NonLeafPosition) pos).getPosition().getLM();
80                 if (firstLM == null) {
81                     firstLM = lastLM;
82                 }
83             } else if (pos instanceof SpaceHandlingBreakPosition) {
84                 positionList.add(pos);
85             } else {
86                 // pos was created by this LM, so it must be ignored
87
}
88         }
89         if (firstPos == null) {
90             return; //Nothing to do, return early
91
//TODO This is a hack to avoid an NPE in the code block below.
92
//If there's no firstPos/lastPos there's currently no way to
93
//correctly determine first and last conditions. The Iterator
94
//doesn't give us that info.
95
}
96         
97         if (bslm != null && bslm.markers != null) {
98             bslm.getCurrentPV().addMarkers(bslm.markers, true,
99                     bslm.isFirst(firstPos), bslm.isLast(lastPos));
100         }
101
102         StackingIter childPosIter = new StackingIter(positionList.listIterator());
103         while ((childLM = childPosIter.getNextChildLM()) != null) {
104             // Add the block areas to Area
105
lc.setFlags(LayoutContext.FIRST_AREA, childLM == firstLM);
106             lc.setFlags(LayoutContext.LAST_AREA, childLM == lastLM);
107             // set the space adjustment ratio
108
lc.setSpaceAdjust(layoutContext.getSpaceAdjust());
109             // set space before for the first LM, in order to implement
110
// display-align = center or after
111
lc.setSpaceBefore((childLM == firstLM ? layoutContext.getSpaceBefore() : 0));
112             // set space after for each LM, in order to implement
113
// display-align = distribute
114
lc.setSpaceAfter(layoutContext.getSpaceAfter());
115             lc.setStackLimit(layoutContext.getStackLimit());
116             childLM.addAreas(childPosIter, lc);
117         }
118         if (bslm != null && bslm.markers != null) {
119             bslm.getCurrentPV().addMarkers(bslm.markers, false,
120                     bslm.isFirst(firstPos), bslm.isLast(lastPos));
121         }
122         
123     }
124     
125 }
126
Popular Tags