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: RtfTable.java 426576 2006-07-28 15:44:37Z jeremias $ */
019:
020: package org.apache.fop.render.rtf.rtflib.rtfdoc;
021:
022: /*
023: * This file is part of the RTF library of the FOP project, which was originally
024: * created by Bertrand Delacretaz <bdelacretaz@codeconsult.ch> and by other
025: * contributors to the jfor project (www.jfor.org), who agreed to donate jfor to
026: * the FOP project.
027: */
028:
029: import java.io.Writer;
030: import java.io.IOException;
031:
032: /** Container for RtfRow elements
033: * @author Bertrand Delacretaz bdelacretaz@codeconsult.ch
034: */
035:
036: public class RtfTable extends RtfContainer {
037: private RtfTableRow row;
038: private int highestRow = 0;
039: private Boolean isNestedTable = null;
040: private RtfAttributes borderAttributes = null;
041:
042: /** Added by Boris Poudérous on 07/22/2002 in order to process
043: * number-columns-spanned attribute */
044: private ITableColumnsInfo tableContext;
045:
046: /** Create an RTF element as a child of given container */
047: RtfTable(IRtfTableContainer parent, Writer w, ITableColumnsInfo tc)
048: throws IOException {
049: super ((RtfContainer) parent, w);
050: // Line added by Boris Poudérous on 07/22/2002
051: tableContext = tc;
052: }
053:
054: /** Create an RTF element as a child of given container
055: * Modified by Boris Poudérous in order to process 'number-columns-spanned' attribute
056: */
057: RtfTable(IRtfTableContainer parent, Writer w, RtfAttributes attrs,
058: ITableColumnsInfo tc) throws IOException {
059: super ((RtfContainer) parent, w, attrs);
060: // Line added by Boris Poudérous on 07/22/2002
061: tableContext = tc;
062: }
063:
064: /**
065: * Close current row if any and start a new one
066: * @return new RtfTableRow
067: * @throws IOException for I/O problems
068: */
069: public RtfTableRow newTableRow() throws IOException {
070: if (row != null) {
071: row.close();
072: }
073:
074: highestRow++;
075: row = new RtfTableRow(this , writer, attrib, highestRow);
076: return row;
077: }
078:
079: /**
080: * Close current row if any and start a new one
081: * @param attrs attributs of new RtfTableRow
082: * @return new RtfTableRow
083: * @throws IOException for I/O problems
084: */
085: public RtfTableRow newTableRow(RtfAttributes attrs)
086: throws IOException {
087: RtfAttributes attr = null;
088: if (attrib != null) {
089: attr = (RtfAttributes) attrib.clone();
090: attr.set(attrs);
091: } else {
092: attr = attrs;
093: }
094: if (row != null) {
095: row.close();
096: }
097: highestRow++;
098:
099: row = new RtfTableRow(this , writer, attr, highestRow);
100: return row;
101: }
102:
103: /**
104: * Overridden to write RTF prefix code, what comes before our children
105: * @throws IOException for I/O problems
106: */
107: protected void writeRtfPrefix() throws IOException {
108: if (isNestedTable()) {
109: writeControlWordNS("pard");
110: }
111:
112: writeGroupMark(true);
113: }
114:
115: /**
116: * Overridden to write RTF suffix code, what comes after our children
117: * @throws IOException for I/O problems
118: */
119: protected void writeRtfSuffix() throws IOException {
120: writeGroupMark(false);
121:
122: if (isNestedTable()) {
123: getRow().writeRowAndCellsDefintions();
124: }
125: }
126:
127: /**
128: *
129: * @param id row to check (??)
130: * @return true if id is the highestRow
131: */
132: public boolean isHighestRow(int id) {
133: return (highestRow == id) ? true : false;
134: }
135:
136: /**
137: * Added by Boris Poudérous on 07/22/2002
138: * @return ITableColumnsInfo for this table
139: */
140: public ITableColumnsInfo getITableColumnsInfo() {
141: return this .tableContext;
142: }
143:
144: private RtfAttributes headerAttribs = null;
145:
146: /**
147: * Added by Normand Masse
148: * Support for table-header attributes (used instead of table attributes)
149: * @param attrs attributes to be set
150: */
151: public void setHeaderAttribs(RtfAttributes attrs) {
152: headerAttribs = attrs;
153: }
154:
155: /**
156: *
157: * @return RtfAttributes of Header
158: */
159: public RtfAttributes getHeaderAttribs() {
160: return headerAttribs;
161: }
162:
163: /**
164: * Added by Normand Masse
165: * @return the table-header attributes if they are present, otherwise the
166: * parent's attributes are returned normally.
167: */
168: public RtfAttributes getRtfAttributes() {
169: if (headerAttribs != null) {
170: return headerAttribs;
171: }
172:
173: return super .getRtfAttributes();
174: }
175:
176: /** @return true if the the table is a nested table */
177: public boolean isNestedTable() {
178: if (isNestedTable == null) {
179: RtfElement e = this ;
180: while (e.parent != null) {
181: if (e.parent instanceof RtfTableCell) {
182: isNestedTable = Boolean.TRUE;
183: return true;
184: }
185:
186: e = e.parent;
187: }
188:
189: isNestedTable = Boolean.FALSE;
190: } else {
191: return isNestedTable.booleanValue();
192: }
193:
194: return false;
195: }
196:
197: /**
198: *
199: * @return Parent row table (for nested tables only)
200: */
201: public RtfTableRow getRow() {
202: RtfElement e = this ;
203: while (e.parent != null) {
204: if (e.parent instanceof RtfTableRow) {
205: return (RtfTableRow) e.parent;
206: }
207:
208: e = e.parent;
209: }
210:
211: return null;
212: }
213:
214: /**
215: * Sets the RtfAttributes for the borders of the table.
216: * @param attributes Border attributes of the table.
217: */
218: public void setBorderAttributes(RtfAttributes attributes) {
219: borderAttributes = attributes;
220: }
221:
222: /**
223: * Returns the RtfAttributes for the borders of the table.
224: * @return Border attributes of the table.
225: */
226: public RtfAttributes getBorderAttributes() {
227: return borderAttributes;
228: }
229: }
|