You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RtfTable.java 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. /* $Id$ */
  18. package org.apache.fop.render.rtf.rtflib.rtfdoc;
  19. /*
  20. * This file is part of the RTF library of the FOP project, which was originally
  21. * created by Bertrand Delacretaz <bdelacretaz@codeconsult.ch> and by other
  22. * contributors to the jfor project (www.jfor.org), who agreed to donate jfor to
  23. * the FOP project.
  24. */
  25. import java.io.IOException;
  26. import java.io.Writer;
  27. import org.apache.fop.apps.FOPException;
  28. /**
  29. * <p>Container for RtfRow elements.</p>
  30. *
  31. * <p>This work was authored by Bertrand Delacretaz (bdelacretaz@codeconsult.ch).</p>
  32. */
  33. public class RtfTable extends RtfContainer {
  34. private RtfTableRow row;
  35. private int highestRow;
  36. private Boolean isNestedTable;
  37. private RtfAttributes borderAttributes;
  38. /** Added by Boris Poudérous on 07/22/2002 in order to process
  39. * number-columns-spanned attribute */
  40. private ITableColumnsInfo tableContext;
  41. /** Shows the table depth necessary for nested tables */
  42. private int nestedTableDepth;
  43. /** Create an RTF element as a child of given container */
  44. RtfTable(IRtfTableContainer parent, Writer w, ITableColumnsInfo tc)
  45. throws IOException {
  46. super((RtfContainer)parent, w);
  47. // Line added by Boris Poudérous on 07/22/2002
  48. tableContext = tc;
  49. }
  50. /** Create an RTF element as a child of given container
  51. * Modified by Boris Poudérous in order to process 'number-columns-spanned' attribute
  52. */
  53. RtfTable(IRtfTableContainer parent, Writer w, RtfAttributes attrs,
  54. ITableColumnsInfo tc) throws IOException {
  55. super((RtfContainer)parent, w, attrs);
  56. // Line added by Boris Poudérous on 07/22/2002
  57. tableContext = tc;
  58. }
  59. /**
  60. * Close current row if any and start a new one
  61. * @return new RtfTableRow
  62. * @throws IOException for I/O problems
  63. */
  64. public RtfTableRow newTableRow() throws IOException {
  65. if (row != null) {
  66. row.close();
  67. }
  68. highestRow++;
  69. row = new RtfTableRow(this, writer, attrib, highestRow);
  70. return row;
  71. }
  72. /**
  73. * Close current row if any and start a new one
  74. * @param attrs attributs of new RtfTableRow
  75. * @return new RtfTableRow
  76. * @throws IOException for I/O problems
  77. * @throws FOPException if attributes cannot be cloned
  78. */
  79. public RtfTableRow newTableRow(RtfAttributes attrs) throws IOException, FOPException {
  80. RtfAttributes attr = null;
  81. if (attrib != null) {
  82. try {
  83. attr = (RtfAttributes) attrib.clone();
  84. } catch (CloneNotSupportedException e) {
  85. throw new FOPException(e);
  86. }
  87. attr.set(attrs);
  88. } else {
  89. attr = attrs;
  90. }
  91. if (row != null) {
  92. row.close();
  93. }
  94. highestRow++;
  95. row = new RtfTableRow(this, writer, attr, highestRow);
  96. return row;
  97. }
  98. /**
  99. * Overridden to write RTF prefix code, what comes before our children
  100. * @throws IOException for I/O problems
  101. */
  102. protected void writeRtfPrefix() throws IOException {
  103. if (isNestedTable()) {
  104. writeControlWordNS("pard");
  105. }
  106. writeGroupMark(true);
  107. }
  108. /**
  109. * Overridden to write RTF suffix code, what comes after our children
  110. * @throws IOException for I/O problems
  111. */
  112. protected void writeRtfSuffix() throws IOException {
  113. writeGroupMark(false);
  114. if (isNestedTable()) {
  115. getRow().writeRowAndCellsDefintions();
  116. }
  117. }
  118. /**
  119. *
  120. * @param id row to check (??)
  121. * @return true if id is the highestRow
  122. */
  123. public boolean isHighestRow(int id) {
  124. return (highestRow == id) ? true : false;
  125. }
  126. /**
  127. * Added by Boris Poudérous on 07/22/2002
  128. * @return ITableColumnsInfo for this table
  129. */
  130. public ITableColumnsInfo getITableColumnsInfo() {
  131. return this.tableContext;
  132. }
  133. private RtfAttributes headerAttribs;
  134. /**
  135. * Added by Normand Masse
  136. * Support for table-header attributes (used instead of table attributes)
  137. * @param attrs attributes to be set
  138. */
  139. public void setHeaderAttribs(RtfAttributes attrs) {
  140. headerAttribs = attrs;
  141. }
  142. /**
  143. *
  144. * @return RtfAttributes of Header
  145. */
  146. public RtfAttributes getHeaderAttribs() {
  147. return headerAttribs;
  148. }
  149. /**
  150. * Added by Normand Masse
  151. * @return the table-header attributes if they are present, otherwise the
  152. * parent's attributes are returned normally.
  153. */
  154. public RtfAttributes getRtfAttributes() {
  155. if (headerAttribs != null) {
  156. return headerAttribs;
  157. }
  158. return super.getRtfAttributes();
  159. }
  160. /** @return true if the the table is a nested table */
  161. public boolean isNestedTable() {
  162. if (isNestedTable == null) {
  163. RtfElement e = this;
  164. while (e.parent != null) {
  165. if (e.parent instanceof RtfTableCell) {
  166. isNestedTable = Boolean.TRUE;
  167. return true;
  168. }
  169. e = e.parent;
  170. }
  171. isNestedTable = Boolean.FALSE;
  172. } else {
  173. return isNestedTable;
  174. }
  175. return false;
  176. }
  177. /**
  178. *
  179. * @return Parent row table (for nested tables only)
  180. */
  181. public RtfTableRow getRow() {
  182. RtfElement e = this;
  183. while (e.parent != null) {
  184. if (e.parent instanceof RtfTableRow) {
  185. return (RtfTableRow) e.parent;
  186. }
  187. e = e.parent;
  188. }
  189. return null;
  190. }
  191. /**
  192. * Sets the nested table depth.
  193. * @param nestedTableDepth the nested table depth
  194. */
  195. public void setNestedTableDepth(int nestedTableDepth) {
  196. this.nestedTableDepth = nestedTableDepth;
  197. }
  198. /**
  199. * Returns the nested table depth.
  200. * @return the nested table depth
  201. */
  202. public int getNestedTableDepth() {
  203. return this.nestedTableDepth;
  204. }
  205. /**
  206. * Sets the RtfAttributes for the borders of the table.
  207. * @param attributes Border attributes of the table.
  208. */
  209. public void setBorderAttributes(RtfAttributes attributes) {
  210. borderAttributes = attributes;
  211. }
  212. /**
  213. * Returns the RtfAttributes for the borders of the table.
  214. * @return Border attributes of the table.
  215. */
  216. public RtfAttributes getBorderAttributes() {
  217. return borderAttributes;
  218. }
  219. }