KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > render > rtf > TableAttributesConverter


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: TableAttributesConverter.java 426576 2006-07-28 15:44:37Z jeremias $ */
19
20 package org.apache.fop.render.rtf;
21
22 import java.awt.Color JavaDoc;
23
24 import org.apache.fop.apps.FOPException;
25 import org.apache.fop.fo.Constants;
26 import org.apache.fop.fo.flow.Table;
27 import org.apache.fop.fo.flow.TableBody;
28 import org.apache.fop.fo.flow.TableCell;
29 import org.apache.fop.fo.flow.TableHeader;
30 import org.apache.fop.fo.flow.TableRow;
31 import org.apache.fop.fo.properties.CommonBorderPaddingBackground;
32 import org.apache.fop.render.rtf.rtflib.rtfdoc.ITableAttributes;
33 import org.apache.fop.render.rtf.rtflib.rtfdoc.RtfAttributes;
34
35 /**
36  * Contributor(s):
37  * @author Roberto Marra <roberto@link-u.com>
38  * @author Boris Poudérous <boris.pouderous@eads-telecom.com>
39  * @author Normand Massé
40  * @author Peter Herweg <pherweg@web.de>
41  *
42  * This class was originally developed for the JFOR project and
43  * is now integrated into FOP.
44 -----------------------------------------------------------------------------*/

45
46 /**
47  * Provides methods to convert the attributes to RtfAttributes.
48  */

49
50 public final class TableAttributesConverter {
51
52     //////////////////////////////////////////////////
53
// @@ Construction
54
//////////////////////////////////////////////////
55

56     /**
57      * Constructor is private, because it's just a utility class.
58      */

59     private TableAttributesConverter() {
60     }
61
62     //////////////////////////////////////////////////
63
// @@ Static converter methods
64
//////////////////////////////////////////////////
65
/**
66      * Converts table-only attributes to rtf attributes.
67      *
68      * @param attrs Given attributes
69      * @param defaultAttributes Default rtf attributes
70      *
71      * @return All valid rtf attributes together
72      *
73      * @throws ConverterException On convertion error
74      */

75     static RtfAttributes convertTableAttributes(Table fobj)
76             throws FOPException {
77         FOPRtfAttributes attrib = new FOPRtfAttributes();
78         attrib.setTwips(ITableAttributes.ATTR_ROW_LEFT_INDENT,
79                 fobj.getCommonMarginBlock().marginLeft);
80         return attrib;
81     }
82
83     /**
84      * Converts table-only attributes to rtf attributes.
85      *
86      * @param attrs Given attributes
87      * @param defaultAttributes Default rtf attributes
88      *
89      * @return All valid rtf attributes together
90      *
91      * @throws ConverterException On convertion error
92      */

93     static RtfAttributes convertTableBodyAttributes(TableBody fobj)
94             throws FOPException {
95         FOPRtfAttributes attrib = new FOPRtfAttributes();
96         return attrib;
97     }
98
99     /**
100      * Converts cell attributes to rtf attributes.
101      * @param fobj FObj whose properties are to be converted
102      *
103      * @return All valid rtf attributes together
104      *
105      * @throws ConverterException On conversion error
106      */

107     static RtfAttributes convertCellAttributes(TableCell fobj)
108     throws FOPException {
109
110         //Property p;
111
//RtfColorTable colorTable = RtfColorTable.getInstance();
112

113         FOPRtfAttributes attrib = new FOPRtfAttributes();
114
115         //boolean isBorderPresent = false;
116
CommonBorderPaddingBackground border = fobj.getCommonBorderPaddingBackground();
117
118         // Cell background color
119
Color JavaDoc color = border.backgroundColor;
120         if (color == null) {
121             //If there is no background-color specified for the cell,
122
//then try to read it from table-row or table-header.
123
CommonBorderPaddingBackground brd = null;
124             
125             if (fobj.getParent() instanceof TableRow) {
126                 TableRow parentRow = (TableRow)fobj.getParent();
127                 brd = parentRow.getCommonBorderPaddingBackground();
128                 color = brd.backgroundColor;
129             } else if (fobj.getParent() instanceof TableHeader) {
130                 TableHeader parentHeader = (TableHeader)fobj.getParent();
131                 brd = parentHeader.getCommonBorderPaddingBackground();
132                 color = brd.backgroundColor;
133             }
134             
135             if (color == null
136                     && fobj.getParent() != null
137                     && fobj.getParent().getParent() != null
138                     && fobj.getParent().getParent().getParent() instanceof Table) {
139
140                 Table table = (Table)fobj.getParent().getParent().getParent();
141                 brd = table.getCommonBorderPaddingBackground();
142                 color = brd.backgroundColor;
143             }
144             
145             
146         }
147         if ((color != null)
148                 && (color.getAlpha() != 0
149                         || color.getRed() != 0
150                         || color.getGreen() != 0
151                         || color.getBlue() != 0)) {
152             attrib.set(ITableAttributes.CELL_COLOR_BACKGROUND, color);
153         }
154
155         BorderAttributesConverter.makeBorder(border, CommonBorderPaddingBackground.BEFORE,
156                 attrib, ITableAttributes.CELL_BORDER_TOP);
157         BorderAttributesConverter.makeBorder(border, CommonBorderPaddingBackground.AFTER,
158                 attrib, ITableAttributes.CELL_BORDER_BOTTOM);
159         BorderAttributesConverter.makeBorder(border, CommonBorderPaddingBackground.START,
160                 attrib, ITableAttributes.CELL_BORDER_LEFT);
161         BorderAttributesConverter.makeBorder(border, CommonBorderPaddingBackground.END,
162                 attrib, ITableAttributes.CELL_BORDER_RIGHT);
163
164         int padding;
165         boolean reproduceMSWordBug = true;
166         //TODO Make this configurable
167
if (reproduceMSWordBug) {
168             //MS Word has a bug where padding left and top are exchanged
169
padding = border.getPaddingStart(false, null); // TODO do we need a real context here?
170
if (padding != 0) {
171                 attrib.setTwips(ITableAttributes.ATTR_CELL_PADDING_TOP, padding);
172                 attrib.set(ITableAttributes.ATTR_CELL_U_PADDING_TOP, 3 /*=twips*/);
173             }
174             padding = border.getPaddingBefore(false, null); // TODO do we need a real context here?
175
if (padding != 0) {
176                 attrib.setTwips(ITableAttributes.ATTR_CELL_PADDING_LEFT, padding);
177                 attrib.set(ITableAttributes.ATTR_CELL_U_PADDING_LEFT, 3 /*=twips*/);
178             }
179         } else {
180             padding = border.getPaddingStart(false, null); // TODO do we need a real context here?
181
if (padding != 0) {
182                 attrib.setTwips(ITableAttributes.ATTR_CELL_PADDING_LEFT, padding);
183                 attrib.set(ITableAttributes.ATTR_CELL_U_PADDING_LEFT, 3 /*=twips*/);
184             }
185             padding = border.getPaddingBefore(false, null); // TODO do we need a real context here?
186
if (padding != 0) {
187                 attrib.setTwips(ITableAttributes.ATTR_CELL_PADDING_TOP, padding);
188                 attrib.set(ITableAttributes.ATTR_CELL_U_PADDING_TOP, 3 /*=twips*/);
189             }
190         }
191         padding = border.getPaddingEnd(false, null); // TODO do we need a real context here?
192
if (padding != 0) {
193             attrib.setTwips(ITableAttributes.ATTR_CELL_PADDING_RIGHT, padding);
194             attrib.set(ITableAttributes.ATTR_CELL_U_PADDING_RIGHT, 3 /*=twips*/);
195         }
196         padding = border.getPaddingAfter(false, null); // TODO do we need a real context here?
197
if (padding != 0) {
198             attrib.setTwips(ITableAttributes.ATTR_CELL_PADDING_BOTTOM, padding);
199             attrib.set(ITableAttributes.ATTR_CELL_U_PADDING_BOTTOM, 3 /*=twips*/);
200         }
201         
202         int n = fobj.getNumberColumnsSpanned();
203         // Column spanning :
204
if (n > 1) {
205             attrib.set(ITableAttributes.COLUMN_SPAN, n);
206         }
207
208         return attrib;
209     }
210
211
212     /**
213      * Converts table and row attributes to rtf attributes.
214      *
215      * @param fobj FObj to be converted
216      * @param defaultAttributes Default rtf attributes
217      *
218      * @return All valid rtf attributes together
219      * @throws ConverterException On converion error
220      */

221     static RtfAttributes convertRowAttributes(TableRow fobj,
222             RtfAttributes rtfatts)
223     throws FOPException {
224
225         //Property p;
226
//RtfColorTable colorTable = RtfColorTable.getInstance();
227

228         RtfAttributes attrib = null;
229
230         if (rtfatts == null) {
231             attrib = new RtfAttributes();
232         } else {
233             attrib = rtfatts;
234         }
235
236         //String attrValue;
237
//boolean isBorderPresent = false;
238
//need to set a default width
239

240         //check for keep-together row attribute
241

242         if (fobj.getKeepTogether().getWithinPage().getEnum() == Constants.EN_ALWAYS) {
243             attrib.set(ITableAttributes.ROW_KEEP_TOGETHER);
244         }
245
246         //Check for keep-with-next row attribute.
247
if (fobj.getKeepWithNext().getWithinPage().getEnum() == Constants.EN_ALWAYS) {
248             attrib.set(ITableAttributes.ROW_KEEP_WITH_NEXT);
249         }
250
251         //Check for keep-with-previous row attribute.
252
if (fobj.getKeepWithPrevious().getWithinPage().getEnum() == Constants.EN_ALWAYS) {
253             attrib.set(ITableAttributes.ROW_KEEP_WITH_PREVIOUS);
254         }
255
256         //Check for height row attribute.
257
if (fobj.getHeight().getEnum() != Constants.EN_AUTO) {
258             attrib.set(ITableAttributes.ROW_HEIGHT, fobj.getHeight().getValue() / (1000 / 20));
259         }
260
261         /* to write a border to a side of a cell one must write the directional
262          * side (ie. left, right) and the inside value if one needs to be taken
263          * out ie if the cell lies on the edge of a table or not, the offending
264          * value will be taken out by RtfTableRow. This is because you can't
265          * say BORDER_TOP and BORDER_HORIZONTAL if the cell lies at the top of
266          * the table. Similarly using BORDER_BOTTOM and BORDER_HORIZONTAL will
267          * not work if the cell lies at th bottom of the table. The same rules
268          * apply for left right and vertical.
269
270          * Also, the border type must be written after every control word. Thus
271          * it is implemented that the border type is the value of the border
272          * place.
273          */

274         CommonBorderPaddingBackground border = fobj.getCommonBorderPaddingBackground();
275         BorderAttributesConverter.makeBorder(border, CommonBorderPaddingBackground.BEFORE,
276                 attrib, ITableAttributes.CELL_BORDER_TOP);
277         BorderAttributesConverter.makeBorder(border, CommonBorderPaddingBackground.AFTER,
278                 attrib, ITableAttributes.CELL_BORDER_BOTTOM);
279         BorderAttributesConverter.makeBorder(border, CommonBorderPaddingBackground.START,
280                 attrib, ITableAttributes.CELL_BORDER_LEFT);
281         BorderAttributesConverter.makeBorder(border, CommonBorderPaddingBackground.END,
282                 attrib, ITableAttributes.CELL_BORDER_RIGHT);
283
284 /*
285         ep = (EnumProperty)fobj.getProperty(Constants.PR_BORDER_TOP_STYLE);
286         if (ep != null && ep.getEnum() != Constants.EN_NONE) {
287             attrib.set(ITableAttributes.ROW_BORDER_TOP, "\\"
288                        + convertAttributetoRtf(ep.getEnum()));
289             attrib.set(ITableAttributes.ROW_BORDER_HORIZONTAL, "\\"
290                        + convertAttributetoRtf(ep.getEnum()));
291             isBorderPresent = true;
292         }
293         ep = (EnumProperty)fobj.getProperty(Constants.PR_BORDER_BOTTOM_STYLE);
294         if (ep != null && ep.getEnum() != Constants.EN_NONE) {
295             attrib.set(ITableAttributes.ROW_BORDER_BOTTOM, "\\"
296                        + convertAttributetoRtf(ep.getEnum()));
297             attrib.set(ITableAttributes.ROW_BORDER_HORIZONTAL, "\\"
298                        + convertAttributetoRtf(ep.getEnum()));
299             isBorderPresent = true;
300         }
301         ep = (EnumProperty)fobj.getProperty(Constants.PR_BORDER_LEFT_STYLE);
302         if (ep != null && ep.getEnum() != Constants.EN_NONE) {
303             attrib.set(ITableAttributes.ROW_BORDER_LEFT, "\\"
304                        + convertAttributetoRtf(ep.getEnum()));
305             attrib.set(ITableAttributes.ROW_BORDER_VERTICAL, "\\"
306                        + convertAttributetoRtf(ep.getEnum()));
307             isBorderPresent = true;
308         }
309         ep = (EnumProperty)fobj.getProperty(Constants.PR_BORDER_RIGHT_STYLE);
310         if (ep != null && ep.getEnum() != Constants.EN_NONE) {
311             attrib.set(ITableAttributes.ROW_BORDER_RIGHT, "\\"
312                        + convertAttributetoRtf(ep.getEnum()));
313             attrib.set(ITableAttributes.ROW_BORDER_VERTICAL, "\\"
314                        + convertAttributetoRtf(ep.getEnum()));
315             isBorderPresent = true;
316         }
317
318         //Currently there is only one border width supported in each cell.
319         p = fobj.getProperty(Constants.PR_BORDER_LEFT_WIDTH);
320         if(p == null) {
321             p = fobj.getProperty(Constants.PR_BORDER_RIGHT_WIDTH);
322         }
323         if(p == null) {
324             p = fobj.getProperty(Constants.PR_BORDER_TOP_WIDTH);
325         }
326         if(p == null) {
327             p = fobj.getProperty(Constants.PR_BORDER_BOTTOM_WIDTH);
328         }
329         if (p != null) {
330             LengthProperty lengthprop = (LengthProperty)p;
331
332             Float f = new Float(lengthprop.getLength().getValue() / 1000f);
333             String sValue = f.toString() + "pt";
334
335             attrib.set(BorderAttributesConverter.BORDER_WIDTH,
336                        (int)FoUnitsConverter.getInstance().convertToTwips(sValue));
337         } else if (isBorderPresent) {
338             //if not defined, set default border width
339             //note 20 twips = 1 point
340             attrib.set(BorderAttributesConverter.BORDER_WIDTH,
341                        (int)FoUnitsConverter.getInstance().convertToTwips("1pt"));
342         }
343 */

344         return attrib;
345     }
346
347 }
348
Popular Tags