KickJava   Java API By Example, From Geeks To Geeks.

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


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: AbstractBaseLayoutManager.java 453310 2006-10-05 18:44:15Z spepping $ */
19
20 package org.apache.fop.layoutmgr;
21
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24 import org.apache.fop.datatypes.LengthBase;
25 import org.apache.fop.datatypes.PercentBaseContext;
26 import org.apache.fop.fo.FObj;
27
28 /**
29  * The base class for nearly all LayoutManagers.
30  * Provides the functionality for merging the {@link LayoutManager}
31  * and the {@link org.apache.fop.datatypes.PercentBaseContext} interfaces
32  * into a common base calls for all higher LayoutManagers.
33  */

34 public abstract class AbstractBaseLayoutManager
35     implements LayoutManager, PercentBaseContext {
36     
37     /** Indicator if this LM generates reference areas */
38     protected boolean generatesReferenceArea = false;
39     /** Indicator if this LM generates block areas */
40     protected boolean generatesBlockArea = false;
41     /** The formatting object for this LM */
42     protected FObj fobj = null;
43
44     /**
45      * logging instance
46      */

47     private static Log log = LogFactory.getLog(AbstractBaseLayoutManager.class);
48
49     /**
50      * Abstract base layout manager.
51      */

52     public AbstractBaseLayoutManager() {
53     }
54
55     /**
56      * Abstract base layout manager.
57      *
58      * @param fo the formatting object for this layout manager
59      */

60     public AbstractBaseLayoutManager(FObj fo) {
61         fobj = fo;
62         setGeneratesReferenceArea(fo.generatesReferenceAreas());
63         if (getGeneratesReferenceArea()) {
64             setGeneratesBlockArea(true);
65         }
66     }
67
68     // --------- Property Resolution related functions --------- //
69

70     /**
71      * @see org.apache.fop.datatypes.PercentBaseContext#getBaseLength(int, FObj)
72      */

73     public int getBaseLength(int lengthBase, FObj fobj) {
74         if (fobj == getFObj()) {
75             switch (lengthBase) {
76             case LengthBase.CONTAINING_BLOCK_WIDTH:
77                 return getAncestorBlockAreaIPD();
78             case LengthBase.CONTAINING_BLOCK_HEIGHT:
79                 return getAncestorBlockAreaBPD();
80             case LengthBase.PARENT_AREA_WIDTH:
81                 return getParentAreaIPD();
82             case LengthBase.CONTAINING_REFAREA_WIDTH:
83                 return getReferenceAreaIPD();
84             default:
85                 log.error(new Exception JavaDoc("Unknown base type for LengthBase:" + lengthBase));
86                 return 0;
87             }
88         } else {
89             LayoutManager lm = getParent();
90             while (lm != null && fobj != lm.getFObj()) {
91                 lm = lm.getParent();
92             }
93             if (lm != null) {
94                 return lm.getBaseLength(lengthBase, fobj);
95             }
96         }
97         log.error("Cannot find LM to handle given FO for LengthBase.");
98         return 0;
99     }
100
101     /**
102      * Find the first ancestor area that is a block area
103      * and returns its IPD.
104      * @return the ipd of the ancestor block area
105      */

106     protected int getAncestorBlockAreaIPD() {
107         LayoutManager lm = getParent();
108         while (lm != null) {
109             if (lm.getGeneratesBlockArea() && !lm.getGeneratesLineArea()) {
110                 return lm.getContentAreaIPD();
111             }
112             lm = lm.getParent();
113         }
114         if (lm == null) {
115             log.error("No parent LM found");
116         }
117         return 0;
118     }
119
120     /**
121      * Find the first ancestor area that is a block area
122      * and returns its BPD.
123      * @return the bpd of the ancestor block area
124      */

125     protected int getAncestorBlockAreaBPD() {
126         LayoutManager lm = getParent();
127         while (lm != null) {
128             if (lm.getGeneratesBlockArea() && !lm.getGeneratesLineArea()) {
129                 return lm.getContentAreaBPD();
130             }
131             lm = lm.getParent();
132         }
133         if (lm == null) {
134             log.error("No parent LM found");
135         }
136         return 0;
137     }
138
139     /**
140      * Find the parent area and returns its IPD.
141      * @return the ipd of the parent area
142      */

143     protected int getParentAreaIPD() {
144         LayoutManager lm = getParent();
145         if (lm != null) {
146             return lm.getContentAreaIPD();
147         }
148         log.error("No parent LM found");
149         return 0;
150     }
151
152     /**
153      * Find the parent area and returns its BPD.
154      * @return the bpd of the parent area
155      */

156     protected int getParentAreaBPD() {
157         LayoutManager lm = getParent();
158         if (lm != null) {
159             return lm.getContentAreaBPD();
160         }
161         log.error("No parent LM found");
162         return 0;
163     }
164
165     /**
166      * Find the first ancestor area that is a reference area
167      * and returns its IPD.
168      * @return the ipd of the ancestor reference area
169      */

170     public int getReferenceAreaIPD() {
171         LayoutManager lm = getParent();
172         while (lm != null) {
173             if (lm.getGeneratesReferenceArea()) {
174                 return lm.getContentAreaIPD();
175             }
176             lm = lm.getParent();
177         }
178         if (lm == null) {
179             log.error("No parent LM found");
180         }
181         return 0;
182     }
183
184     /**
185      * Find the first ancestor area that is a reference area
186      * and returns its BPD.
187      * @return the bpd of the ancestor reference area
188      */

189     protected int getReferenceAreaBPD() {
190         LayoutManager lm = getParent();
191         while (lm != null) {
192             if (lm.getGeneratesReferenceArea()) {
193                 return lm.getContentAreaBPD();
194             }
195             lm = lm.getParent();
196         }
197         if (lm == null) {
198             log.error("No parent LM found");
199         }
200         return 0;
201     }
202
203     /**
204      * Returns the IPD of the content area
205      * @return the IPD of the content area
206      * @see LayoutManager#getContentAreaIPD
207      */

208     public int getContentAreaIPD() {
209         log.error("getContentAreaIPD called when it should have been overwritten");
210         return 0;
211     }
212    
213     /**
214      * Returns the BPD of the content area
215      * @return the BPD of the content area
216      * @see LayoutManager#getContentAreaBPD
217      */

218     public int getContentAreaBPD() {
219         log.error("getContentAreaBPD called when it should have been overwritten");
220         return 0;
221     }
222     
223     /**
224      * @see LayoutManager#getGeneratesReferenceArea
225      */

226     public boolean getGeneratesReferenceArea() {
227         return generatesReferenceArea;
228     }
229    
230     /**
231      * Lets implementing LM set the flag indicating if they
232      * generate reference areas.
233      * @param generatesReferenceArea if true the areas generates by this LM are
234      * reference areas.
235      */

236     protected void setGeneratesReferenceArea(boolean generatesReferenceArea) {
237         this.generatesReferenceArea = generatesReferenceArea;
238     }
239    
240     /**
241      * @see LayoutManager#getGeneratesBlockArea
242      */

243     public boolean getGeneratesBlockArea() {
244         return generatesBlockArea;
245     }
246    
247     /**
248      * Lets implementing LM set the flag indicating if they
249      * generate block areas.
250      * @param generatesBlockArea if true the areas generates by this LM are block areas.
251      */

252     protected void setGeneratesBlockArea(boolean generatesBlockArea) {
253         this.generatesBlockArea = generatesBlockArea;
254     }
255    
256     /**
257      * @see org.apache.fop.layoutmgr.LayoutManager#getGeneratesLineArea
258      */

259     public boolean getGeneratesLineArea() {
260         return false;
261     }
262     
263     /**
264      * @see org.apache.fop.layoutmgr.LayoutManager#getFObj
265      */

266     public FObj getFObj() {
267         return fobj;
268     }
269    
270 }
271
Popular Tags