KickJava   Java API By Example, From Geeks To Geeks.

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


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: Area.java 433679 2006-08-22 15:49:26Z cbowditch $ */
19
20 package org.apache.fop.area;
21
22 import java.io.Serializable JavaDoc;
23 import java.util.Map JavaDoc;
24
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.apache.fop.traits.BorderProps;
28
29 // If the area appears more than once in the output
30
// or if the area has external data it is cached
31
// to keep track of it and to minimize rendered output
32
// renderers can render the output once and display it
33
// for every occurence
34
// this should also extend to all outputs (including PDFGraphics2D)
35
// and all types of renderers
36

37 /**
38  * Base object for all areas.
39  */

40 public class Area extends AreaTreeObject implements Serializable JavaDoc {
41     // stacking directions
42
/**
43      * Stacking left to right
44      */

45     public static final int LR = 0;
46
47     /**
48      * Stacking right to left
49      */

50     public static final int RL = 1;
51
52     /**
53      * Stacking top to bottom
54      */

55     public static final int TB = 2;
56
57     /**
58      * Stacking bottom to top
59      */

60     public static final int BT = 3;
61
62     // orientations for reference areas
63
/**
64      * Normal orientation
65      */

66     public static final int ORIENT_0 = 0;
67
68     /**
69      * Rotated 90 degrees clockwise
70      */

71     public static final int ORIENT_90 = 1;
72
73     /**
74      * Rotate 180 degrees
75      */

76     public static final int ORIENT_180 = 2;
77
78     /**
79      * Rotated 270 degrees clockwise
80      */

81     public static final int ORIENT_270 = 3;
82
83     // area class values
84

85     /**
86      * Normal class
87      */

88     public static final int CLASS_NORMAL = 0;
89
90     /**
91      * Fixed position class
92      */

93     public static final int CLASS_FIXED = 1;
94
95     /**
96      * Absolute position class
97      */

98     public static final int CLASS_ABSOLUTE = 2;
99
100     /**
101      * Before float class
102      */

103     public static final int CLASS_BEFORE_FLOAT = 3;
104
105     /**
106      * Footnote class
107      */

108     public static final int CLASS_FOOTNOTE = 4;
109
110     /**
111      * Side float class
112      */

113     public static final int CLASS_SIDE_FLOAT = 5;
114
115     // IMPORTANT: make sure this is the maximum + 1
116
/**
117      * Maximum class count
118      */

119     public static final int CLASS_MAX = CLASS_SIDE_FLOAT + 1;
120
121     private int areaClass = CLASS_NORMAL;
122     /** the area's inline-progression-dimension */
123     protected int ipd;
124     /** the area's block-progression-dimension */
125     protected int bpd;
126
127     /**
128      * Traits for this area stored in a HashMap
129      */

130     protected Map JavaDoc props = null;
131
132     /**
133      * logging instance
134      */

135     protected static Log log = LogFactory.getLog(Area.class);
136
137
138     /**
139      * Get the area class of this area.
140      *
141      * @return the area class
142      */

143     public int getAreaClass() {
144         return areaClass;
145     }
146
147     /**
148      * Set the area class of this area.
149      *
150      * @param areaClass the area class
151      */

152     public void setAreaClass(int areaClass) {
153         this.areaClass = areaClass;
154     }
155
156     /**
157      * Set the inline progression dimension of content rectangle
158      * for this area.
159      *
160      * @param i the new inline progression dimension
161      * @see <a HREF="http://www.w3.org/TR/xsl/slice4.html#area-common">ipd</a>
162      */

163     public void setIPD(int i) {
164         ipd = i;
165     }
166
167     /**
168      * Get the inline progression dimension of the content rectangle
169      * for this area.
170      *
171      * @return the inline progression dimension
172      * @see <a HREF="http://www.w3.org/TR/xsl/slice4.html#area-common">ipd</a>
173      */

174     public int getIPD() {
175         return ipd;
176     }
177
178     /**
179      * Set the block progression dimension of the content rectangle
180      * for this area.
181      *
182      * @param b the new block progression dimension
183      * @see <a HREF="http://www.w3.org/TR/xsl/slice4.html#area-common">bpd</a>
184      */

185     public void setBPD(int b) {
186         bpd = b;
187     }
188
189     /**
190      * Get the block progression dimension of the content rectangle
191      * for this area.
192      *
193      * @return the block progression dimension
194      * @see <a HREF="http://www.w3.org/TR/xsl/slice4.html#area-common">bpd</a>
195      */

196     public int getBPD() {
197         return bpd;
198     }
199
200     /**
201      * Get the allocation inline progression dimension of this area.
202      * This adds the content, borders and the padding to find the
203      * total allocated IPD.
204      *
205      * @return the total IPD allocation for this area
206      */

207     public int getAllocIPD() {
208         return getBorderAndPaddingWidthStart() + getIPD() + getBorderAndPaddingWidthEnd();
209     }
210
211     /**
212      * Get the allocation block progression dimension of this area.
213      * This adds the content, borders, padding and spaces to find the
214      * total allocated BPD.
215      *
216      * @return the total BPD allocation for this area
217      */

218     public int getAllocBPD() {
219         return getSpaceBefore() + getBorderAndPaddingWidthBefore() + getBPD()
220                 + getBorderAndPaddingWidthAfter() + getSpaceAfter();
221     }
222
223     /**
224      * Return the sum of region border- and padding-before
225      *
226      * @return width in millipoints
227      */

228     public int getBorderAndPaddingWidthBefore() {
229         int margin = 0;
230         BorderProps bps = (BorderProps) getTrait(Trait.BORDER_BEFORE);
231         if (bps != null) {
232             margin = bps.width;
233         }
234         
235         Integer JavaDoc padWidth = (Integer JavaDoc) getTrait(Trait.PADDING_BEFORE);
236         if (padWidth != null) {
237             margin += padWidth.intValue();
238         }
239
240         return margin;
241     }
242     
243     /**
244      * Return the sum of region border- and padding-after
245      *
246      * @return width in millipoints
247      */

248     public int getBorderAndPaddingWidthAfter() {
249         int margin = 0;
250         
251         BorderProps bps = (BorderProps) getTrait(Trait.BORDER_AFTER);
252         if (bps != null) {
253             margin = bps.width;
254         }
255         
256         Integer JavaDoc padWidth = (Integer JavaDoc) getTrait(Trait.PADDING_AFTER);
257         if (padWidth != null) {
258             margin += padWidth.intValue();
259         }
260
261         return margin;
262     }
263
264     /**
265      * Return the sum of region border- and padding-start
266      *
267      * @return width in millipoints
268      */

269     public int getBorderAndPaddingWidthStart() {
270         int margin = 0;
271         BorderProps bps = (BorderProps) getTrait(Trait.BORDER_START);
272         if (bps != null) {
273             margin = bps.width;
274         }
275         
276         Integer JavaDoc padWidth = (Integer JavaDoc) getTrait(Trait.PADDING_START);
277         if (padWidth != null) {
278             margin += padWidth.intValue();
279         }
280
281         return margin;
282     }
283
284     /**
285      * Return the sum of region border- and padding-end
286      *
287      * @return width in millipoints
288      */

289     public int getBorderAndPaddingWidthEnd() {
290         int margin = 0;
291         BorderProps bps = (BorderProps) getTrait(Trait.BORDER_END);
292         if (bps != null) {
293             margin = bps.width;
294         }
295         
296         Integer JavaDoc padWidth = (Integer JavaDoc) getTrait(Trait.PADDING_END);
297         if (padWidth != null) {
298             margin += padWidth.intValue();
299         }
300
301         return margin;
302     }
303
304     /**
305      * Returns the space before
306      *
307      * @return width in millipoints
308      */

309     public int getSpaceBefore() {
310         int margin = 0;
311         Integer JavaDoc space = (Integer JavaDoc) getTrait(Trait.SPACE_BEFORE);
312         if (space != null) {
313             margin = space.intValue();
314         }
315         return margin;
316     }
317     
318     /**
319      * Returns the space after
320      *
321      * @return width in millipoints
322      */

323     public int getSpaceAfter() {
324         int margin = 0;
325         Integer JavaDoc space = (Integer JavaDoc) getTrait(Trait.SPACE_AFTER);
326         if (space != null) {
327             margin = space.intValue();
328         }
329         return margin;
330     }
331     
332     /**
333      * Returns the space start
334      *
335      * @return width in millipoints
336      */

337     public int getSpaceStart() {
338         int margin = 0;
339         Integer JavaDoc space = (Integer JavaDoc) getTrait(Trait.SPACE_START);
340         if (space != null) {
341             margin = space.intValue();
342         }
343         return margin;
344     }
345     
346     /**
347      * Returns the space end
348      *
349      * @return width in millipoints
350      */

351     public int getSpaceEnd() {
352         int margin = 0;
353         Integer JavaDoc space = (Integer JavaDoc) getTrait(Trait.SPACE_END);
354         if (space != null) {
355             margin = space.intValue();
356         }
357         return margin;
358     }
359     
360     /**
361      * Add a child to this area.
362      * The default is to do nothing. Subclasses must override
363      * to do something if they can have child areas.
364      *
365      * @param child the child area to add
366      */

367     public void addChildArea(Area child) {
368     }
369
370     /**
371      * Add a trait property to this area.
372      *
373      * @param prop the Trait to add
374      */

375     public void addTrait(Trait prop) {
376         if (props == null) {
377             props = new java.util.HashMap JavaDoc(20);
378         }
379         props.put(prop.getPropType(), prop.getData());
380     }
381
382     /**
383      * Add a trait to this area.
384      *
385      * @param traitCode the trait key
386      * @param prop the value of the trait
387      */

388     public void addTrait(Object JavaDoc traitCode, Object JavaDoc prop) {
389         if (props == null) {
390             props = new java.util.HashMap JavaDoc(20);
391         }
392         props.put(traitCode, prop);
393     }
394
395     /**
396      * Get the map of all traits on this area.
397      *
398      * @return the map of traits
399      */

400     public Map JavaDoc getTraits() {
401         return this.props;
402     }
403
404     /** @return true if the area has traits */
405     public boolean hasTraits() {
406         return (this.props != null);
407     }
408     
409     /**
410      * Get a trait from this area.
411      *
412      * @param oTraitCode the trait key
413      * @return the trait value
414      */

415     public Object JavaDoc getTrait(Object JavaDoc oTraitCode) {
416         return (props != null ? props.get(oTraitCode) : null);
417     }
418     
419     /**
420      * Checks whether a certain trait is set on this area.
421      * @param oTraitCode the trait key
422      * @return true if the trait is set
423      */

424     public boolean hasTrait(Object JavaDoc oTraitCode) {
425         return (getTrait(oTraitCode) != null);
426     }
427     
428     /**
429      * Get a boolean trait from this area.
430      * @param oTraitCode the trait key
431      * @return the trait value
432      */

433     public boolean getBooleanTrait(Object JavaDoc oTraitCode) {
434         final Object JavaDoc obj = getTrait(oTraitCode);
435         if (obj instanceof Boolean JavaDoc) {
436             return ((Boolean JavaDoc)obj).booleanValue();
437         } else {
438             return false;
439         }
440     }
441
442     /**
443      * Get a trait from this area as an integer.
444      *
445      * @param oTraitCode the trait key
446      * @return the trait value
447      */

448     public int getTraitAsInteger(Object JavaDoc oTraitCode) {
449         final Object JavaDoc obj = getTrait(oTraitCode);
450         if (obj instanceof Integer JavaDoc) {
451             return ((Integer JavaDoc)obj).intValue();
452         } else {
453             throw new IllegalArgumentException JavaDoc("Trait "
454                     + oTraitCode.getClass().getName()
455                     + " could not be converted to an integer");
456         }
457     }
458     
459     /**
460      * @see java.lang.Object#toString()
461      * @return ipd and bpd of area
462      * */

463     public String JavaDoc toString() {
464         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(super.toString());
465         sb.append(" {ipd=").append(Integer.toString(getIPD()));
466         sb.append(", bpd=").append(Integer.toString(getBPD()));
467         sb.append("}");
468         return sb.toString();
469     }
470 }
471
472
Popular Tags