KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > render > rtf > rtflib > rtfdoc > RtfTableRow


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: RtfTableRow.java 426576 2006-07-28 15:44:37Z jeremias $ */
19
20 package org.apache.fop.render.rtf.rtflib.rtfdoc;
21
22 /*
23  * This file is part of the RTF library of the FOP project, which was originally
24  * created by Bertrand Delacretaz <bdelacretaz@codeconsult.ch> and by other
25  * contributors to the jfor project (www.jfor.org), who agreed to donate jfor to
26  * the FOP project.
27  */

28
29 import java.io.IOException JavaDoc;
30 import java.io.Writer JavaDoc;
31 import java.util.Iterator JavaDoc;
32
33 /** Container for RtfTableCell elements
34  * @author Bertrand Delacretaz bdelacretaz@codeconsult.ch
35  * @author Andreas Putz a.putz@skynamics.com
36  * @author Roberto Marra roberto@link-u.com
37  */

38
39 public class RtfTableRow extends RtfContainer implements ITableAttributes {
40     private RtfTableCell cell;
41 // private RtfExtraRowSet extraRowSet;
42
private int id;
43     private int highestCell = 0;
44
45
46     /** Create an RTF element as a child of given container */
47     RtfTableRow(RtfTable parent, Writer JavaDoc w, int idNum) throws IOException JavaDoc {
48         super(parent, w);
49         id = idNum;
50     }
51
52     /** Create an RTF element as a child of given container */
53     RtfTableRow(RtfTable parent, Writer JavaDoc w, RtfAttributes attrs, int idNum) throws IOException JavaDoc {
54         super(parent, w, attrs);
55         id = idNum;
56     }
57
58     /**
59      * Close current cell if any and start a new one
60      * @param cellWidth width of new cell
61      * @return new RtfTableCell
62      * @throws IOException for I/O problems
63      */

64     public RtfTableCell newTableCell(int cellWidth) throws IOException JavaDoc {
65         highestCell++;
66         cell = new RtfTableCell(this, writer, cellWidth, highestCell);
67         return cell;
68     }
69
70     /**
71      * Close current cell if any and start a new one
72      * @param attrs attributes of new cell
73      * @param cellWidth width of new cell
74      * @return new RtfTableCell
75      * @throws IOException for I/O problems
76      */

77     public RtfTableCell newTableCell(int cellWidth, RtfAttributes attrs) throws IOException JavaDoc {
78         highestCell++;
79         cell = new RtfTableCell(this, writer, cellWidth, attrs, highestCell);
80         return cell;
81     }
82
83     /**
84      * Added by Boris POUDEROUS on 07/02/2002
85      * in order to add an empty cell that is merged with the cell above.
86      * This cell is placed before or after the nested table.
87      * @param attrs attributes of new cell
88      * @param cellWidth width of new cell
89      * @return new RtfTableCell
90      * @throws IOException for I/O problems
91      */

92     public RtfTableCell newTableCellMergedVertically(int cellWidth,
93            RtfAttributes attrs) throws IOException JavaDoc {
94         highestCell++;
95         cell = new RtfTableCell (this, writer, cellWidth, attrs, highestCell);
96         cell.setVMerge(RtfTableCell.MERGE_WITH_PREVIOUS);
97         return cell;
98     }
99
100     /**
101      * Added by Boris POUDEROUS on 07/02/2002
102      * in order to add an empty cell that is merged with the previous cell.
103      * @param attrs attributes of new cell
104      * @param cellWidth width of new cell
105      * @return new RtfTableCell
106      * @throws IOException for I/O problems
107      */

108     public RtfTableCell newTableCellMergedHorizontally (int cellWidth,
109            RtfAttributes attrs) throws IOException JavaDoc {
110         highestCell++;
111         // Added by Normand Masse
112
// Inherit attributes from base cell for merge
113
RtfAttributes wAttributes = null;
114         if (attrs != null) {
115             wAttributes = (RtfAttributes)attrs.clone();
116         }
117
118         cell = new RtfTableCell(this, writer, cellWidth, wAttributes, highestCell);
119         cell.setHMerge(RtfTableCell.MERGE_WITH_PREVIOUS);
120         return cell;
121     }
122
123     /**
124      * @throws IOException for I/O problems
125      */

126     protected void writeRtfPrefix() throws IOException JavaDoc {
127         newLine();
128         writeGroupMark(true);
129     }
130
131     /**
132      * Overridden to write trowd and cell definitions before writing our cells
133      * @throws IOException for I/O problems
134      */

135     protected void writeRtfContent() throws IOException JavaDoc {
136
137         if (getTable().isNestedTable()) {
138             //nested table
139
writeControlWord("intbl");
140             writeControlWord("itap2");
141         } else {
142             //normal (not nested) table
143
writeRowAndCellsDefintions();
144         }
145         // now children can write themselves, we have the correct RTF prefix code
146
super.writeRtfContent();
147     }
148     
149     /**
150      *
151      * @throws IOException In case of a IO-problem
152      */

153     public void writeRowAndCellsDefintions() throws IOException JavaDoc {
154         // render the row and cells definitions
155
writeControlWord("trowd");
156         
157         if (!getTable().isNestedTable()) {
158             writeControlWord("itap0");
159         }
160
161         //check for keep-together
162
if (attrib != null && attrib.isSet(ITableAttributes.ROW_KEEP_TOGETHER)) {
163             writeControlWord(ROW_KEEP_TOGETHER);
164         }
165
166         writePaddingAttributes();
167
168         final RtfTable parentTable = (RtfTable) parent;
169         adjustBorderProperties(parentTable);
170
171         writeAttributes(attrib, new String JavaDoc[]{ITableAttributes.ATTR_HEADER});
172         writeAttributes(attrib, ITableAttributes.ROW_BORDER);
173         writeAttributes(attrib, ITableAttributes.CELL_BORDER);
174         writeAttributes(attrib, IBorderAttributes.BORDERS);
175
176         if (attrib.isSet(ITableAttributes.ROW_HEIGHT)) {
177             writeOneAttribute(
178                     ITableAttributes.ROW_HEIGHT,
179                     attrib.getValue(ITableAttributes.ROW_HEIGHT));
180         }
181
182         // write X positions of our cells
183
int xPos = 0;
184         
185         final Object JavaDoc leftIndent = attrib.getValue(ITableAttributes.ATTR_ROW_LEFT_INDENT);
186         if (leftIndent != null) {
187             xPos = ((Integer JavaDoc)leftIndent).intValue();
188         }
189         
190         RtfAttributes tableBorderAttributes = getTable().getBorderAttributes();
191         
192         int index = 0;
193         for (Iterator JavaDoc it = getChildren().iterator(); it.hasNext();) {
194             final RtfElement e = (RtfElement)it.next();
195             if (e instanceof RtfTableCell) {
196                 
197                 RtfTableCell rtfcell = (RtfTableCell)e;
198                 
199                 // Adjust the cell's display attributes so the table's/row's borders
200
// are drawn properly.
201

202                 if (tableBorderAttributes != null) {
203                     // get border attributes from table
204
if (index == 0) {
205                         String JavaDoc border = ITableAttributes.CELL_BORDER_LEFT;
206                         if (!rtfcell.getRtfAttributes().isSet(border)) {
207                             rtfcell.getRtfAttributes().set(border,
208                                 (RtfAttributes) tableBorderAttributes.getValue(border));
209                         }
210                     }
211
212                     if (index == this.getChildCount() - 1) {
213                         String JavaDoc border = ITableAttributes.CELL_BORDER_RIGHT;
214                         if (!rtfcell.getRtfAttributes().isSet(border)) {
215                             rtfcell.getRtfAttributes().set(border,
216                                 (RtfAttributes) tableBorderAttributes.getValue(border));
217                         }
218                     }
219
220                     if (isFirstRow()) {
221                         String JavaDoc border = ITableAttributes.CELL_BORDER_TOP;
222                         if (!rtfcell.getRtfAttributes().isSet(border)) {
223                             rtfcell.getRtfAttributes().set(border,
224                                 (RtfAttributes) tableBorderAttributes.getValue(border));
225                         }
226                     }
227
228                     if ((parentTable != null) && (parentTable.isHighestRow(id))) {
229                         String JavaDoc border = ITableAttributes.CELL_BORDER_BOTTOM;
230                         if (!rtfcell.getRtfAttributes().isSet(border)) {
231                             rtfcell.getRtfAttributes().set(border,
232                                 (RtfAttributes) tableBorderAttributes.getValue(border));
233                         }
234                     }
235                 }
236                 
237                 // get border attributes from row
238
if (index == 0) {
239                     if (!rtfcell.getRtfAttributes().isSet(ITableAttributes.CELL_BORDER_LEFT)) {
240                         rtfcell.getRtfAttributes().set(ITableAttributes.CELL_BORDER_LEFT,
241                             (String JavaDoc) attrib.getValue(ITableAttributes.ROW_BORDER_LEFT));
242                     }
243                 }
244
245                 if (index == this.getChildCount() - 1) {
246                     if (!rtfcell.getRtfAttributes().isSet(ITableAttributes.CELL_BORDER_RIGHT)) {
247                         rtfcell.getRtfAttributes().set(ITableAttributes.CELL_BORDER_RIGHT,
248                             (String JavaDoc) attrib.getValue(ITableAttributes.ROW_BORDER_RIGHT));
249                     }
250                 }
251
252                 if (isFirstRow()) {
253                     if (!rtfcell.getRtfAttributes().isSet(ITableAttributes.CELL_BORDER_TOP)) {
254                         rtfcell.getRtfAttributes().set(ITableAttributes.CELL_BORDER_TOP,
255                             (String JavaDoc) attrib.getValue(ITableAttributes.ROW_BORDER_TOP));
256                     }
257                 }
258
259                 if ((parentTable != null) && (parentTable.isHighestRow(id))) {
260                     if (!rtfcell.getRtfAttributes().isSet(ITableAttributes.CELL_BORDER_BOTTOM)) {
261                         rtfcell.getRtfAttributes().set(ITableAttributes.CELL_BORDER_BOTTOM,
262                             (String JavaDoc) attrib.getValue(ITableAttributes.ROW_BORDER_BOTTOM));
263                     }
264                 }
265
266                 // write cell's definition
267
xPos = rtfcell.writeCellDef(xPos);
268             }
269           index++; // Added by Boris POUDEROUS on 2002/07/02
270
}
271         
272         newLine();
273     }
274
275     private void adjustBorderProperties(RtfTable parentTable) {
276         // if we have attributes, manipulate border properties
277
if (attrib != null && parentTable != null) {
278
279             //if table is only one row long
280
if (isFirstRow() && parentTable.isHighestRow(id)) {
281                 attrib.unset(ITableAttributes.ROW_BORDER_HORIZONTAL);
282             //or if row is the first row
283
} else if (isFirstRow()) {
284                 attrib.unset(ITableAttributes.ROW_BORDER_BOTTOM);
285             //or if row is the last row
286
} else if (parentTable.isHighestRow(id)) {
287                 attrib.unset(ITableAttributes.ROW_BORDER_TOP);
288             //else the row is an inside row
289
} else {
290                 attrib.unset(ITableAttributes.ROW_BORDER_BOTTOM);
291                 attrib.unset(ITableAttributes.ROW_BORDER_TOP);
292             }
293         }
294     }
295
296     /**
297      * Overridden to write RTF suffix code, what comes after our children
298      * @throws IOException for I/O problems
299      */

300     protected void writeRtfSuffix() throws IOException JavaDoc {
301         if (getTable().isNestedTable()) {
302             //nested table
303
writeGroupMark(true);
304             writeStarControlWord("nesttableprops");
305             writeRowAndCellsDefintions();
306             writeControlWordNS("nestrow");
307             writeGroupMark(false);
308
309             writeGroupMark(true);
310             writeControlWord("nonesttables");
311             writeControlWord("par");
312             writeGroupMark(false);
313         } else {
314             writeControlWord("row");
315         }
316
317         writeGroupMark(false);
318     }
319
320 // RtfExtraRowSet getExtraRowSet() {
321
// return extraRowSet;
322
// }
323

324     private void writePaddingAttributes()
325     throws IOException JavaDoc {
326         // Row padding attributes generated in the converter package
327
// use RTF 1.6 definitions - try to compute a reasonable RTF 1.5 value
328
// out of them if present
329
// how to do vertical padding with RTF 1.5?
330
if (attrib != null && !attrib.isSet(ATTR_RTF_15_TRGAPH)) {
331             int gaph = -1;
332             try {
333                 // set (RTF 1.5) gaph to the average of the (RTF 1.6) left and right padding values
334
final Integer JavaDoc leftPadStr = (Integer JavaDoc)attrib.getValue(ATTR_ROW_PADDING_LEFT);
335                 if (leftPadStr != null) {
336                     gaph = leftPadStr.intValue();
337                 }
338                 final Integer JavaDoc rightPadStr = (Integer JavaDoc)attrib.getValue(ATTR_ROW_PADDING_RIGHT);
339                 if (rightPadStr != null) {
340                     gaph = (gaph + rightPadStr.intValue()) / 2;
341                 }
342             } catch (Exception JavaDoc e) {
343                 final String JavaDoc msg = "RtfTableRow.writePaddingAttributes: " + e.toString();
344 // getRtfFile().getLog().logWarning(msg);
345
}
346             if (gaph >= 0) {
347                 attrib.set(ATTR_RTF_15_TRGAPH, gaph);
348             }
349         }
350
351         // write all padding attributes
352
writeAttributes(attrib, ATTRIB_ROW_PADDING);
353     }
354
355     /**
356      * @return true if the row is the first in the table
357      */

358     public boolean isFirstRow() {
359         return (id == 1);
360     }
361
362     /**
363      * @param cellId cell id to check
364      * @return true if the cell is the highest cell
365      */

366     public boolean isHighestCell(int cellId) {
367         return (highestCell == cellId) ? true : false;
368     }
369     
370     /**
371      *
372      * @return Parent table of the row.
373      */

374     public RtfTable getTable() {
375         RtfElement e = this;
376         while (e.parent != null) {
377             if (e.parent instanceof RtfTable) {
378                 return (RtfTable) e.parent;
379             }
380
381             e = e.parent;
382         }
383
384         return null;
385     }
386 }
387
Popular Tags