KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > area > Span


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: Span.java 426576 2006-07-28 15:44:37Z jeremias $ */
19  
20 package org.apache.fop.area;
21
22 import java.util.List JavaDoc;
23
24 /**
25  * The span-reference-area.
26  * This is a block-area with 0 border and padding that is stacked
27  * within the main-reference-area
28  * This object holds one or more normal-flow-reference-area children
29  * based on the column-count trait in effect for this span.
30  * See fo:region-body definition in the XSL Rec for more information.
31  */

32 public class Span extends Area {
33     // the list of flow reference areas in this span area
34
private List JavaDoc flowAreas;
35     private int colCount;
36     private int colGap;
37     private int colWidth; // width for each normal flow, calculated value
38
private int curFlowIdx; // n-f-r-a currently being processed, zero-based
39

40     /**
41      * Create a span area with the number of columns for this span area.
42      *
43      * @param colCount the number of columns in the span
44      * @param colGap the column gap between each column
45      * @param ipd the total ipd of the span
46      */

47     public Span(int colCount, int colGap, int ipd) {
48         addTrait(Trait.IS_REFERENCE_AREA, Boolean.TRUE);
49         this.colCount = colCount;
50         this.colGap = colGap;
51         this.ipd = ipd;
52         curFlowIdx = 0;
53         createNormalFlows();
54     }
55
56     /**
57      * Create the normal flows for this Span
58      */

59     private void createNormalFlows() {
60         flowAreas = new java.util.ArrayList JavaDoc(colCount);
61         colWidth = (ipd - ((colCount - 1) * colGap)) / colCount;
62
63         for (int i = 0; i < colCount; i++) {
64             NormalFlow newFlow = new NormalFlow(colWidth);
65             flowAreas.add(newFlow);
66         }
67     }
68
69     /**
70      * Get the column count for this span area.
71      *
72      * @return the number of columns defined for this span area
73      */

74     public int getColumnCount() {
75         return colCount;
76     }
77
78     /**
79      * Get the width of a single column within this Span
80      *
81      * @return the width of a single column
82      */

83     public int getColumnWidth() {
84         return colWidth;
85     }
86
87     /**
88      * Get the height of this span area.
89      *
90      * @return the height of this span area
91      */

92     public int getHeight() {
93         return getBPD();
94     }
95
96
97     /**
98      * Get the normal flow area for a particular column.
99      *
100      * @param colRequested the zero-based column number of the flow
101      * @return the flow area for the requested column
102      */

103     public NormalFlow getNormalFlow(int colRequested) {
104         if (colRequested >= 0 && colRequested < colCount) {
105             return (NormalFlow) flowAreas.get(colRequested);
106         } else { // internal error
107
throw new IllegalArgumentException JavaDoc("Invalid column number "
108                     + colRequested + " requested; only 0-" + (colCount - 1)
109                     + " available.");
110         }
111     }
112     
113     /**
114      * Get the NormalFlow area currently being processed
115      *
116      * @return the current NormalFlow
117      */

118     public NormalFlow getCurrentFlow() {
119         return getNormalFlow(curFlowIdx);
120     }
121     
122     /** @return the index of the current normal flow */
123     public int getCurrentFlowIndex() {
124         return curFlowIdx;
125     }
126     
127     /**
128      * Indicate to the Span that the next column is being
129      * processed.
130      *
131      * @return the new NormalFlow (in the next column)
132      */

133     public NormalFlow moveToNextFlow() {
134         if (hasMoreFlows()) {
135             curFlowIdx++;
136             return getNormalFlow(curFlowIdx);
137         } else {
138             throw new IllegalStateException JavaDoc("(Internal error.) No more flows left in span.");
139         }
140     }
141     
142     /**
143      * Indicates if the Span has unprocessed flows.
144      *
145      * @return true if Span can increment to the next flow,
146      * false otherwise.
147      */

148     public boolean hasMoreFlows() {
149         return (curFlowIdx < colCount - 1);
150     }
151     
152     /**
153      * Called to notify the span that all its flows have been fully generated so it can update
154      * its own BPD extent.
155      */

156     public void notifyFlowsFinished() {
157         int maxFlowBPD = Integer.MIN_VALUE;
158         for (int i = 0; i < colCount; i++) {
159             maxFlowBPD = Math.max(maxFlowBPD, getNormalFlow(i).getAllocBPD());
160         }
161         bpd = maxFlowBPD;
162     }
163     
164     /**
165      * Indicates whether any child areas have been added to this span area.
166      *
167      * This is achieved by looping through each flow.
168      * @return true if no child areas have been added yet.
169      */

170     public boolean isEmpty() {
171         int areaCount = 0;
172         for (int i = 0; i < getColumnCount(); i++) {
173             NormalFlow flow = getNormalFlow(i);
174             if (flow != null) {
175                 if (flow.getChildAreas() != null) {
176                     areaCount += flow.getChildAreas().size();
177                 }
178             }
179         }
180         return (areaCount == 0);
181     }
182     
183     /** @see java.lang.Object#toString() */
184     public String JavaDoc toString() {
185         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(super.toString());
186         if (colCount > 1) {
187             sb.append(" {colCount=").append(colCount);
188             sb.append(", colWidth=").append(colWidth);
189             sb.append(", curFlowIdx=").append(this.curFlowIdx);
190             sb.append("}");
191         }
192         return sb.toString();
193     }
194
195 }
196
197
Popular Tags