001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /* $Id: AbstractBaseLayoutManager.java 513970 2007-03-02 21:39:02Z adelmelle $ */
019:
020: package org.apache.fop.layoutmgr;
021:
022: import org.apache.commons.logging.Log;
023: import org.apache.commons.logging.LogFactory;
024: import org.apache.fop.datatypes.LengthBase;
025: import org.apache.fop.datatypes.PercentBaseContext;
026: import org.apache.fop.fo.FObj;
027:
028: /**
029: * The base class for nearly all LayoutManagers.
030: * Provides the functionality for merging the {@link LayoutManager}
031: * and the {@link org.apache.fop.datatypes.PercentBaseContext} interfaces
032: * into a common base calls for all higher LayoutManagers.
033: */
034: public abstract class AbstractBaseLayoutManager implements
035: LayoutManager, PercentBaseContext {
036:
037: /** Indicator if this LM generates reference areas */
038: protected boolean generatesReferenceArea = false;
039: /** Indicator if this LM generates block areas */
040: protected boolean generatesBlockArea = false;
041: /** The formatting object for this LM */
042: protected FObj fobj = null;
043:
044: /**
045: * logging instance
046: */
047: private static Log log = LogFactory
048: .getLog(AbstractBaseLayoutManager.class);
049:
050: /**
051: * Abstract base layout manager.
052: */
053: public AbstractBaseLayoutManager() {
054: }
055:
056: /**
057: * Abstract base layout manager.
058: *
059: * @param fo the formatting object for this layout manager
060: */
061: public AbstractBaseLayoutManager(FObj fo) {
062: fobj = fo;
063: setGeneratesReferenceArea(fo.generatesReferenceAreas());
064: if (getGeneratesReferenceArea()) {
065: setGeneratesBlockArea(true);
066: }
067: }
068:
069: // --------- Property Resolution related functions --------- //
070:
071: /**
072: * @see org.apache.fop.datatypes.PercentBaseContext#getBaseLength(int, FObj)
073: */
074: public int getBaseLength(int lengthBase, FObj fobj) {
075: if (fobj == this .fobj) {
076: switch (lengthBase) {
077: case LengthBase.CONTAINING_BLOCK_WIDTH:
078: return getAncestorBlockAreaIPD();
079: case LengthBase.CONTAINING_BLOCK_HEIGHT:
080: return getAncestorBlockAreaBPD();
081: case LengthBase.PARENT_AREA_WIDTH:
082: return getParentAreaIPD();
083: case LengthBase.CONTAINING_REFAREA_WIDTH:
084: return getReferenceAreaIPD();
085: default:
086: log.error(new Exception(
087: "Unknown base type for LengthBase:"
088: + lengthBase));
089: return 0;
090: }
091: } else {
092: LayoutManager lm = getParent();
093: while (lm != null && fobj != lm.getFObj()) {
094: lm = lm.getParent();
095: }
096: if (lm != null) {
097: return lm.getBaseLength(lengthBase, fobj);
098: }
099: }
100: log.error("Cannot find LM to handle given FO for LengthBase. ("
101: + fobj.getContextInfo() + ")");
102: return 0;
103: }
104:
105: /**
106: * Find the first ancestor area that is a block area
107: * and returns its IPD.
108: * @return the ipd of the ancestor block area
109: */
110: protected int getAncestorBlockAreaIPD() {
111: LayoutManager lm = getParent();
112: while (lm != null) {
113: if (lm.getGeneratesBlockArea()
114: && !lm.getGeneratesLineArea()) {
115: return lm.getContentAreaIPD();
116: }
117: lm = lm.getParent();
118: }
119: if (lm == null) {
120: log.error("No parent LM found");
121: }
122: return 0;
123: }
124:
125: /**
126: * Find the first ancestor area that is a block area
127: * and returns its BPD.
128: * @return the bpd of the ancestor block area
129: */
130: protected int getAncestorBlockAreaBPD() {
131: LayoutManager lm = getParent();
132: while (lm != null) {
133: if (lm.getGeneratesBlockArea()
134: && !lm.getGeneratesLineArea()) {
135: return lm.getContentAreaBPD();
136: }
137: lm = lm.getParent();
138: }
139: if (lm == null) {
140: log.error("No parent LM found");
141: }
142: return 0;
143: }
144:
145: /**
146: * Find the parent area and returns its IPD.
147: * @return the ipd of the parent area
148: */
149: protected int getParentAreaIPD() {
150: LayoutManager lm = getParent();
151: if (lm != null) {
152: return lm.getContentAreaIPD();
153: }
154: log.error("No parent LM found");
155: return 0;
156: }
157:
158: /**
159: * Find the parent area and returns its BPD.
160: * @return the bpd of the parent area
161: */
162: protected int getParentAreaBPD() {
163: LayoutManager lm = getParent();
164: if (lm != null) {
165: return lm.getContentAreaBPD();
166: }
167: log.error("No parent LM found");
168: return 0;
169: }
170:
171: /**
172: * Find the first ancestor area that is a reference area
173: * and returns its IPD.
174: * @return the ipd of the ancestor reference area
175: */
176: public int getReferenceAreaIPD() {
177: LayoutManager lm = getParent();
178: while (lm != null) {
179: if (lm.getGeneratesReferenceArea()) {
180: return lm.getContentAreaIPD();
181: }
182: lm = lm.getParent();
183: }
184: if (lm == null) {
185: log.error("No parent LM found");
186: }
187: return 0;
188: }
189:
190: /**
191: * Find the first ancestor area that is a reference area
192: * and returns its BPD.
193: * @return the bpd of the ancestor reference area
194: */
195: protected int getReferenceAreaBPD() {
196: LayoutManager lm = getParent();
197: while (lm != null) {
198: if (lm.getGeneratesReferenceArea()) {
199: return lm.getContentAreaBPD();
200: }
201: lm = lm.getParent();
202: }
203: if (lm == null) {
204: log.error("No parent LM found");
205: }
206: return 0;
207: }
208:
209: /**
210: * Returns the IPD of the content area
211: * @return the IPD of the content area
212: * @see LayoutManager#getContentAreaIPD
213: */
214: public int getContentAreaIPD() {
215: log
216: .error("getContentAreaIPD called when it should have been overwritten");
217: return 0;
218: }
219:
220: /**
221: * Returns the BPD of the content area
222: * @return the BPD of the content area
223: * @see LayoutManager#getContentAreaBPD
224: */
225: public int getContentAreaBPD() {
226: log
227: .error("getContentAreaBPD called when it should have been overwritten");
228: return 0;
229: }
230:
231: /**
232: * @see LayoutManager#getGeneratesReferenceArea
233: */
234: public boolean getGeneratesReferenceArea() {
235: return generatesReferenceArea;
236: }
237:
238: /**
239: * Lets implementing LM set the flag indicating if they
240: * generate reference areas.
241: * @param generatesReferenceArea if true the areas generates by this LM are
242: * reference areas.
243: */
244: protected void setGeneratesReferenceArea(
245: boolean generatesReferenceArea) {
246: this .generatesReferenceArea = generatesReferenceArea;
247: }
248:
249: /**
250: * @see LayoutManager#getGeneratesBlockArea
251: */
252: public boolean getGeneratesBlockArea() {
253: return generatesBlockArea;
254: }
255:
256: /**
257: * Lets implementing LM set the flag indicating if they
258: * generate block areas.
259: * @param generatesBlockArea if true the areas generates by this LM are block areas.
260: */
261: protected void setGeneratesBlockArea(boolean generatesBlockArea) {
262: this .generatesBlockArea = generatesBlockArea;
263: }
264:
265: /**
266: * @see org.apache.fop.layoutmgr.LayoutManager#getGeneratesLineArea
267: */
268: public boolean getGeneratesLineArea() {
269: return false;
270: }
271:
272: /**
273: * @see org.apache.fop.layoutmgr.LayoutManager#getFObj
274: */
275: public FObj getFObj() {
276: return fobj;
277: }
278:
279: }
|