KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > fo > properties > CommonBorderPaddingBackground


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: CommonBorderPaddingBackground.java 478928 2006-11-24 17:32:48Z vhennebert $ */
19
20 package org.apache.fop.fo.properties;
21
22 import java.awt.Color JavaDoc;
23
24 import org.apache.fop.apps.FOUserAgent;
25 import org.apache.fop.datatypes.Length;
26 import org.apache.fop.datatypes.PercentBaseContext;
27 import org.apache.fop.fo.Constants;
28 import org.apache.fop.fo.FObj;
29 import org.apache.fop.fo.PropertyList;
30 import org.apache.fop.fo.expr.PropertyException;
31 import org.apache.fop.image.FopImage;
32 import org.apache.fop.image.ImageFactory;
33
34 /**
35  * Stores all common border and padding properties.
36  * See Sec. 7.7 of the XSL-FO Standard.
37  */

38 public class CommonBorderPaddingBackground implements Cloneable JavaDoc {
39     /**
40      * The "background-attachment" property.
41      */

42     public int backgroundAttachment;
43
44     /**
45      * The "background-color" property.
46      */

47     public Color JavaDoc backgroundColor;
48
49     /**
50      * The "background-image" property.
51      */

52     public String JavaDoc backgroundImage;
53
54     /**
55      * The "background-repeat" property.
56      */

57     public int backgroundRepeat;
58
59     /**
60      * The "background-position-horizontal" property.
61      */

62     public Length backgroundPositionHorizontal;
63
64     /**
65      * The "background-position-vertical" property.
66      */

67     public Length backgroundPositionVertical;
68     
69     
70     private FopImage fopimage;
71     
72     
73     /** the "before" edge */
74     public static final int BEFORE = 0;
75     /** the "after" edge */
76     public static final int AFTER = 1;
77     /** the "start" edge */
78     public static final int START = 2;
79     /** the "end" edge */
80     public static final int END = 3;
81     
82     public static class BorderInfo implements Cloneable JavaDoc {
83         private int mStyle; // Enum for border style
84
private Color JavaDoc mColor; // Border color
85
private CondLengthProperty mWidth;
86
87         BorderInfo(int style, CondLengthProperty width, Color JavaDoc color) {
88             mStyle = style;
89             mWidth = width;
90             mColor = color;
91         }
92         
93         public int getStyle() {
94             return this.mStyle;
95         }
96         
97         public Color JavaDoc getColor() {
98             return this.mColor;
99         }
100         
101         public CondLengthProperty getWidth() {
102             return this.mWidth;
103         }
104
105         public int getRetainedWidth() {
106             if ((mStyle == Constants.EN_NONE)
107                     || (mStyle == Constants.EN_HIDDEN)) {
108                 return 0;
109             } else {
110                 return mWidth.getLengthValue();
111             }
112         }
113         
114         /** @see java.lang.Object#toString() */
115         public String JavaDoc toString() {
116             StringBuffer JavaDoc sb = new StringBuffer JavaDoc("BorderInfo");
117             sb.append(" {");
118             sb.append(mStyle);
119             sb.append(", ");
120             sb.append(mColor);
121             sb.append(", ");
122             sb.append(mWidth);
123             sb.append("}");
124             return sb.toString();
125         }
126     }
127
128     private BorderInfo[] borderInfo = new BorderInfo[4];
129     private CondLengthProperty[] padding = new CondLengthProperty[4];
130
131     /**
132      * Construct a CommonBorderPaddingBackground object.
133      */

134     public CommonBorderPaddingBackground() {
135         
136     }
137     
138     /**
139      * Construct a CommonBorderPaddingBackground object.
140      * @param pList The PropertyList to get properties from.
141      * @param fobj The FO to create this instance for.
142      * @throws PropertyException if there's an error while binding the properties
143      */

144     public CommonBorderPaddingBackground(PropertyList pList, FObj fobj) throws PropertyException {
145         
146         backgroundAttachment = pList.get(Constants.PR_BACKGROUND_ATTACHMENT).getEnum();
147         backgroundColor = pList.get(Constants.PR_BACKGROUND_COLOR).getColor(
148                 fobj == null ? null : fobj.getUserAgent());
149         if (backgroundColor.getAlpha() == 0) {
150             backgroundColor = null;
151         }
152
153         backgroundImage = pList.get(Constants.PR_BACKGROUND_IMAGE).getString();
154         if (backgroundImage == null || "none".equals(backgroundImage)) {
155             backgroundImage = null;
156         } else {
157             backgroundRepeat = pList.get(Constants.PR_BACKGROUND_REPEAT).getEnum();
158             backgroundPositionHorizontal = pList.get(
159                     Constants.PR_BACKGROUND_POSITION_HORIZONTAL).getLength();
160             backgroundPositionVertical = pList.get(
161                     Constants.PR_BACKGROUND_POSITION_VERTICAL).getLength();
162             
163             //Additional processing: preload image
164
String JavaDoc url = ImageFactory.getURL(backgroundImage);
165             FOUserAgent userAgent = fobj.getUserAgent();
166             ImageFactory fact = userAgent.getFactory().getImageFactory();
167             fopimage = fact.getImage(url, userAgent);
168             if (fopimage == null) {
169                 fobj.getLogger().error("Background image not available: " + backgroundImage);
170             } else {
171                 // load dimensions
172
if (!fopimage.load(FopImage.DIMENSIONS)) {
173                     fobj.getLogger().error("Cannot read background image dimensions: "
174                             + backgroundImage);
175                 }
176             }
177             //TODO Report to caller so he can decide to throw an exception
178
}
179
180         initBorderInfo(pList, BEFORE,
181                 Constants.PR_BORDER_BEFORE_COLOR,
182                 Constants.PR_BORDER_BEFORE_STYLE,
183                 Constants.PR_BORDER_BEFORE_WIDTH,
184                 Constants.PR_PADDING_BEFORE);
185         initBorderInfo(pList, AFTER,
186                 Constants.PR_BORDER_AFTER_COLOR,
187                 Constants.PR_BORDER_AFTER_STYLE,
188                 Constants.PR_BORDER_AFTER_WIDTH,
189                 Constants.PR_PADDING_AFTER);
190         initBorderInfo(pList, START,
191                 Constants.PR_BORDER_START_COLOR,
192                 Constants.PR_BORDER_START_STYLE,
193                 Constants.PR_BORDER_START_WIDTH,
194                 Constants.PR_PADDING_START);
195         initBorderInfo(pList, END,
196                 Constants.PR_BORDER_END_COLOR,
197                 Constants.PR_BORDER_END_STYLE,
198                 Constants.PR_BORDER_END_WIDTH,
199                 Constants.PR_PADDING_END);
200
201     }
202
203     private void initBorderInfo(PropertyList pList, int side,
204                     int colorProp, int styleProp, int widthProp, int paddingProp)
205                 throws PropertyException {
206         padding[side] = pList.get(paddingProp).getCondLength();
207         // If style = none, force width to 0, don't get Color (spec 7.7.20)
208
int style = pList.get(styleProp).getEnum();
209         if (style != Constants.EN_NONE) {
210             FOUserAgent ua = (pList == null)
211                     ? null
212                     : (pList.getFObj() == null ? null : pList.getFObj().getUserAgent());
213             setBorderInfo(new BorderInfo(style,
214                 pList.get(widthProp).getCondLength(),
215                 pList.get(colorProp).getColor(ua)), side);
216         }
217     }
218     
219     /**
220      * Sets a border.
221      * @param info the border information
222      * @param side the side to apply the info to
223      */

224     public void setBorderInfo(BorderInfo info, int side) {
225         this.borderInfo[side] = info;
226     }
227     
228     /**
229      * @param side the side to retrieve
230      * @return the border info for a side
231      */

232     public BorderInfo getBorderInfo(int side) {
233         return this.borderInfo[side];
234     }
235     
236     /**
237      * Set padding.
238      * @param source the padding info to copy from
239      */

240     public void setPadding(CommonBorderPaddingBackground source) {
241         this.padding = source.padding;
242     }
243     
244     /**
245      * @return the background image as a preloaded FopImage, null if there is
246      * no background image.
247      */

248     public FopImage getFopImage() {
249         return this.fopimage;
250     }
251     
252     /**
253      * @param bDiscard indicates whether the .conditionality component should be
254      * considered (start of a reference-area)
255      */

256     public int getBorderStartWidth(boolean bDiscard) {
257         return getBorderWidth(START, bDiscard);
258     }
259
260     /**
261      * @param bDiscard indicates whether the .conditionality component should be
262      * considered (end of a reference-area)
263      */

264     public int getBorderEndWidth(boolean bDiscard) {
265         return getBorderWidth(END, bDiscard);
266     }
267
268     /**
269      * @param bDiscard indicates whether the .conditionality component should be
270      * considered (start of a reference-area)
271      */

272     public int getBorderBeforeWidth(boolean bDiscard) {
273         return getBorderWidth(BEFORE, bDiscard);
274     }
275
276     /**
277      * @param bDiscard indicates whether the .conditionality component should be
278      * considered (end of a reference-area)
279      */

280     public int getBorderAfterWidth(boolean bDiscard) {
281         return getBorderWidth(AFTER, bDiscard);
282     }
283
284     public int getPaddingStart(boolean bDiscard, PercentBaseContext context) {
285         return getPadding(START, bDiscard, context);
286     }
287
288     public int getPaddingEnd(boolean bDiscard, PercentBaseContext context) {
289         return getPadding(END, bDiscard, context);
290     }
291
292     public int getPaddingBefore(boolean bDiscard, PercentBaseContext context) {
293         return getPadding(BEFORE, bDiscard, context);
294     }
295
296     public int getPaddingAfter(boolean bDiscard, PercentBaseContext context) {
297         return getPadding(AFTER, bDiscard, context);
298     }
299
300     public int getBorderWidth(int side, boolean bDiscard) {
301         if ((borderInfo[side] == null)
302                 || (borderInfo[side].mStyle == Constants.EN_NONE)
303                 || (borderInfo[side].mStyle == Constants.EN_HIDDEN)
304                 || (bDiscard && borderInfo[side].mWidth.isDiscard())) {
305             return 0;
306         } else {
307             return borderInfo[side].mWidth.getLengthValue();
308         }
309     }
310
311     public Color JavaDoc getBorderColor(int side) {
312         if (borderInfo[side] != null) {
313             return borderInfo[side].getColor();
314         } else {
315             return null;
316         }
317     }
318
319     public int getBorderStyle(int side) {
320         if (borderInfo[side] != null) {
321             return borderInfo[side].mStyle;
322         } else {
323             return Constants.EN_NONE;
324         }
325     }
326
327     public int getPadding(int side, boolean bDiscard, PercentBaseContext context) {
328         if ((padding[side] == null) || (bDiscard && padding[side].isDiscard())) {
329             return 0;
330         } else {
331             return padding[side].getLengthValue(context);
332         }
333     }
334     
335     /**
336      * Returns the CondLengthProperty for the padding on one side.
337      * @param side the side
338      * @return the requested CondLengthProperty
339      */

340     public CondLengthProperty getPaddingLengthProperty(int side) {
341         return padding[side];
342     }
343
344     /**
345      * Return all the border and padding width in the inline progression
346      * dimension.
347      * @param bDiscard the discard flag.
348      * @param context for percentage evaluation.
349      * @return all the padding and border width.
350      */

351     public int getIPPaddingAndBorder(boolean bDiscard, PercentBaseContext context) {
352         return getPaddingStart(bDiscard, context)
353             + getPaddingEnd(bDiscard, context)
354             + getBorderStartWidth(bDiscard)
355             + getBorderEndWidth(bDiscard);
356     }
357     
358     /**
359      * Return all the border and padding height in the block progression
360      * dimension.
361      * @param bDiscard the discard flag.
362      * @param context for percentage evaluation
363      * @return all the padding and border height.
364      */

365     public int getBPPaddingAndBorder(boolean bDiscard, PercentBaseContext context) {
366         return getPaddingBefore(bDiscard, context) + getPaddingAfter(bDiscard, context)
367                + getBorderBeforeWidth(bDiscard) + getBorderAfterWidth(bDiscard);
368     }
369
370     /** @see java.lang.Object#toString() */
371     public String JavaDoc toString() {
372         return "CommonBordersAndPadding (Before, After, Start, End):\n"
373             + "Borders: (" + getBorderBeforeWidth(false) + ", " + getBorderAfterWidth(false) + ", "
374             + getBorderStartWidth(false) + ", " + getBorderEndWidth(false) + ")\n"
375             + "Border Colors: (" + getBorderColor(BEFORE) + ", " + getBorderColor(AFTER) + ", "
376             + getBorderColor(START) + ", " + getBorderColor(END) + ")\n"
377             + "Padding: (" + getPaddingBefore(false, null) + ", " + getPaddingAfter(false, null)
378             + ", " + getPaddingStart(false, null) + ", " + getPaddingEnd(false, null) + ")\n";
379     }
380
381     /**
382      * @return true if there is any kind of background to be painted
383      */

384     public boolean hasBackground() {
385         return ((backgroundColor != null || getFopImage() != null));
386     }
387
388     /** @return true if border is non-zero. */
389     public boolean hasBorder() {
390         return ((getBorderBeforeWidth(false) + getBorderAfterWidth(false)
391                 + getBorderStartWidth(false) + getBorderEndWidth(false)) > 0);
392     }
393
394     /**
395      * @param context for percentage based evaluation.
396      * @return true if padding is non-zero.
397      */

398     public boolean hasPadding(PercentBaseContext context) {
399         return ((getPaddingBefore(false, context) + getPaddingAfter(false, context)
400                 + getPaddingStart(false, context) + getPaddingEnd(false, context)) > 0);
401     }
402     
403     /** @return true if there are any borders defined. */
404     public boolean hasBorderInfo() {
405         return (borderInfo[BEFORE] != null || borderInfo[AFTER] != null
406                 || borderInfo[START] != null || borderInfo[END] != null);
407     }
408 }
409
Popular Tags