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.

RtfTableRow.java 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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 java.util.Iterator;
  28. import org.apache.fop.apps.FOPException;
  29. /**
  30. * <p>Container for RtfTableCell elements.</p>
  31. *
  32. * <p>This work was authored by Bertrand Delacretaz (bdelacretaz@codeconsult.ch),
  33. * Andreas Putz (a.putz@skynamics.com), and
  34. * Roberto Marra (roberto@link-u.com).</p>
  35. */
  36. public class RtfTableRow extends RtfContainer implements ITableAttributes {
  37. private RtfTableCell cell;
  38. // private RtfExtraRowSet extraRowSet;
  39. private int id;
  40. private int highestCell;
  41. /** Create an RTF element as a child of given container */
  42. RtfTableRow(RtfTable parent, Writer w, int idNum) throws IOException {
  43. super(parent, w);
  44. id = idNum;
  45. }
  46. /** Create an RTF element as a child of given container */
  47. RtfTableRow(RtfTable parent, Writer w, RtfAttributes attrs, int idNum) throws IOException {
  48. super(parent, w, attrs);
  49. id = idNum;
  50. }
  51. /**
  52. * Close current cell if any and start a new one
  53. * @param cellWidth width of new cell
  54. * @return new RtfTableCell
  55. * @throws IOException for I/O problems
  56. */
  57. public RtfTableCell newTableCell(int cellWidth) throws IOException {
  58. highestCell++;
  59. cell = new RtfTableCell(this, writer, cellWidth, highestCell);
  60. return cell;
  61. }
  62. /**
  63. * Close current cell if any and start a new one
  64. * @param attrs attributes of new cell
  65. * @param cellWidth width of new cell
  66. * @return new RtfTableCell
  67. * @throws IOException for I/O problems
  68. */
  69. public RtfTableCell newTableCell(int cellWidth, RtfAttributes attrs) throws IOException {
  70. highestCell++;
  71. cell = new RtfTableCell(this, writer, cellWidth, attrs, highestCell);
  72. return cell;
  73. }
  74. /**
  75. * Added by Boris POUDEROUS on 07/02/2002
  76. * in order to add an empty cell that is merged with the cell above.
  77. * This cell is placed before or after the nested table.
  78. * @param attrs attributes of new cell
  79. * @param cellWidth width of new cell
  80. * @return new RtfTableCell
  81. * @throws IOException for I/O problems
  82. */
  83. public RtfTableCell newTableCellMergedVertically(int cellWidth,
  84. RtfAttributes attrs) throws IOException {
  85. highestCell++;
  86. cell = new RtfTableCell(this, writer, cellWidth, attrs, highestCell);
  87. cell.setVMerge(RtfTableCell.MERGE_WITH_PREVIOUS);
  88. return cell;
  89. }
  90. /**
  91. * Added by Boris POUDEROUS on 07/02/2002
  92. * in order to add an empty cell that is merged with the previous cell.
  93. * @param attrs attributes of new cell
  94. * @param cellWidth width of new cell
  95. * @return new RtfTableCell
  96. * @throws IOException for I/O problems
  97. * @throws FOPException if attributes cannot be cloned
  98. */
  99. public RtfTableCell newTableCellMergedHorizontally(int cellWidth,
  100. RtfAttributes attrs) throws IOException, FOPException {
  101. highestCell++;
  102. // Added by Normand Masse
  103. // Inherit attributes from base cell for merge
  104. RtfAttributes wAttributes = null;
  105. if (attrs != null) {
  106. try {
  107. wAttributes = (RtfAttributes)attrs.clone();
  108. } catch (CloneNotSupportedException e) {
  109. throw new FOPException(e);
  110. }
  111. }
  112. cell = new RtfTableCell(this, writer, cellWidth, wAttributes, highestCell);
  113. cell.setHMerge(RtfTableCell.MERGE_WITH_PREVIOUS);
  114. return cell;
  115. }
  116. /**
  117. * @throws IOException for I/O problems
  118. */
  119. protected void writeRtfPrefix() throws IOException {
  120. newLine();
  121. writeGroupMark(true);
  122. }
  123. /**
  124. * Overridden to write trowd and cell definitions before writing our cells
  125. * @throws IOException for I/O problems
  126. */
  127. protected void writeRtfContent() throws IOException {
  128. if (getTable().isNestedTable()) {
  129. //nested table
  130. writeControlWord("intbl");
  131. //itap is the depth (level) of the current nested table
  132. writeControlWord("itap" + getTable().getNestedTableDepth());
  133. } else {
  134. //normal (not nested) table
  135. writeRowAndCellsDefintions();
  136. }
  137. // now children can write themselves, we have the correct RTF prefix code
  138. super.writeRtfContent();
  139. }
  140. /**
  141. *
  142. * @throws IOException In case of a IO-problem
  143. */
  144. public void writeRowAndCellsDefintions() throws IOException {
  145. // render the row and cells definitions
  146. writeControlWord("trowd");
  147. if (!getTable().isNestedTable()) {
  148. writeControlWord("itap0");
  149. }
  150. //check for keep-together
  151. if (attrib.isSet(ITableAttributes.ROW_KEEP_TOGETHER)) {
  152. writeControlWord(ROW_KEEP_TOGETHER);
  153. }
  154. writePaddingAttributes();
  155. final RtfTable parentTable = (RtfTable) parent;
  156. adjustBorderProperties(parentTable);
  157. writeAttributes(attrib, new String[]{ITableAttributes.ATTR_HEADER});
  158. writeAttributes(attrib, ITableAttributes.ROW_BORDER);
  159. writeAttributes(attrib, ITableAttributes.CELL_BORDER);
  160. writeAttributes(attrib, IBorderAttributes.BORDERS);
  161. if (attrib.isSet(ITableAttributes.ROW_HEIGHT)) {
  162. writeOneAttribute(
  163. ITableAttributes.ROW_HEIGHT,
  164. attrib.getValue(ITableAttributes.ROW_HEIGHT));
  165. }
  166. // write X positions of our cells
  167. int xPos = 0;
  168. final Object leftIndent = attrib.getValue(ITableAttributes.ATTR_ROW_LEFT_INDENT);
  169. if (leftIndent != null) {
  170. xPos = (Integer) leftIndent;
  171. }
  172. RtfAttributes tableBorderAttributes = getTable().getBorderAttributes();
  173. int index = 0;
  174. for (Iterator it = getChildren().iterator(); it.hasNext();) {
  175. final RtfElement e = (RtfElement)it.next();
  176. if (e instanceof RtfTableCell) {
  177. RtfTableCell rtfcell = (RtfTableCell)e;
  178. // Adjust the cell's display attributes so the table's/row's borders
  179. // are drawn properly.
  180. if (tableBorderAttributes != null) {
  181. // get border attributes from table
  182. if (index == 0) {
  183. String border = ITableAttributes.CELL_BORDER_LEFT;
  184. if (!rtfcell.getRtfAttributes().isSet(border)) {
  185. rtfcell.getRtfAttributes().set(border,
  186. (RtfAttributes) tableBorderAttributes.getValue(border));
  187. }
  188. }
  189. if (index == this.getChildCount() - 1) {
  190. String border = ITableAttributes.CELL_BORDER_RIGHT;
  191. if (!rtfcell.getRtfAttributes().isSet(border)) {
  192. rtfcell.getRtfAttributes().set(border,
  193. (RtfAttributes) tableBorderAttributes.getValue(border));
  194. }
  195. }
  196. if (isFirstRow()) {
  197. String border = ITableAttributes.CELL_BORDER_TOP;
  198. if (!rtfcell.getRtfAttributes().isSet(border)) {
  199. rtfcell.getRtfAttributes().set(border,
  200. (RtfAttributes) tableBorderAttributes.getValue(border));
  201. }
  202. }
  203. if ((parentTable != null) && (parentTable.isHighestRow(id))) {
  204. String border = ITableAttributes.CELL_BORDER_BOTTOM;
  205. if (!rtfcell.getRtfAttributes().isSet(border)) {
  206. rtfcell.getRtfAttributes().set(border,
  207. (RtfAttributes) tableBorderAttributes.getValue(border));
  208. }
  209. }
  210. }
  211. // get border attributes from row
  212. if (index == 0) {
  213. if (!rtfcell.getRtfAttributes().isSet(ITableAttributes.CELL_BORDER_LEFT)) {
  214. rtfcell.getRtfAttributes().set(ITableAttributes.CELL_BORDER_LEFT,
  215. (String) attrib.getValue(ITableAttributes.ROW_BORDER_LEFT));
  216. }
  217. }
  218. if (index == this.getChildCount() - 1) {
  219. if (!rtfcell.getRtfAttributes().isSet(ITableAttributes.CELL_BORDER_RIGHT)) {
  220. rtfcell.getRtfAttributes().set(ITableAttributes.CELL_BORDER_RIGHT,
  221. (String) attrib.getValue(ITableAttributes.ROW_BORDER_RIGHT));
  222. }
  223. }
  224. if (isFirstRow()) {
  225. if (!rtfcell.getRtfAttributes().isSet(ITableAttributes.CELL_BORDER_TOP)) {
  226. rtfcell.getRtfAttributes().set(ITableAttributes.CELL_BORDER_TOP,
  227. (String) attrib.getValue(ITableAttributes.ROW_BORDER_TOP));
  228. }
  229. }
  230. if ((parentTable != null) && (parentTable.isHighestRow(id))) {
  231. if (!rtfcell.getRtfAttributes().isSet(ITableAttributes.CELL_BORDER_BOTTOM)) {
  232. rtfcell.getRtfAttributes().set(ITableAttributes.CELL_BORDER_BOTTOM,
  233. (String) attrib.getValue(ITableAttributes.ROW_BORDER_BOTTOM));
  234. }
  235. }
  236. // write cell's definition
  237. xPos = rtfcell.writeCellDef(xPos);
  238. }
  239. index++; // Added by Boris POUDEROUS on 2002/07/02
  240. }
  241. newLine();
  242. }
  243. private void adjustBorderProperties(RtfTable parentTable) {
  244. // if we have attributes, manipulate border properties
  245. if (attrib != null && parentTable != null) {
  246. //if table is only one row long
  247. if (isFirstRow() && parentTable.isHighestRow(id)) {
  248. attrib.unset(ITableAttributes.ROW_BORDER_HORIZONTAL);
  249. //or if row is the first row
  250. } else if (isFirstRow()) {
  251. attrib.unset(ITableAttributes.ROW_BORDER_BOTTOM);
  252. //or if row is the last row
  253. } else if (parentTable.isHighestRow(id)) {
  254. attrib.unset(ITableAttributes.ROW_BORDER_TOP);
  255. //else the row is an inside row
  256. } else {
  257. attrib.unset(ITableAttributes.ROW_BORDER_BOTTOM);
  258. attrib.unset(ITableAttributes.ROW_BORDER_TOP);
  259. }
  260. }
  261. }
  262. /**
  263. * Overridden to write RTF suffix code, what comes after our children
  264. * @throws IOException for I/O problems
  265. */
  266. protected void writeRtfSuffix() throws IOException {
  267. if (getTable().isNestedTable()) {
  268. //nested table
  269. writeGroupMark(true);
  270. writeStarControlWord("nesttableprops");
  271. writeRowAndCellsDefintions();
  272. writeControlWordNS("nestrow");
  273. writeGroupMark(false);
  274. writeGroupMark(true);
  275. writeControlWord("nonesttables");
  276. writeControlWord("par");
  277. writeGroupMark(false);
  278. } else {
  279. writeControlWord("row");
  280. }
  281. writeGroupMark(false);
  282. }
  283. // RtfExtraRowSet getExtraRowSet() {
  284. // return extraRowSet;
  285. // }
  286. private void writePaddingAttributes()
  287. throws IOException {
  288. // Row padding attributes generated in the converter package
  289. // use RTF 1.6 definitions - try to compute a reasonable RTF 1.5 value
  290. // out of them if present
  291. // how to do vertical padding with RTF 1.5?
  292. if (attrib != null && !attrib.isSet(ATTR_RTF_15_TRGAPH)) {
  293. int gaph = -1;
  294. try {
  295. // set (RTF 1.5) gaph to the average of the (RTF 1.6) left and right padding values
  296. final Integer leftPadStr = (Integer)attrib.getValue(ATTR_ROW_PADDING_LEFT);
  297. if (leftPadStr != null) {
  298. gaph = leftPadStr;
  299. }
  300. final Integer rightPadStr = (Integer)attrib.getValue(ATTR_ROW_PADDING_RIGHT);
  301. if (rightPadStr != null) {
  302. gaph = (gaph + rightPadStr) / 2;
  303. }
  304. } catch (Exception e) {
  305. final String msg = "RtfTableRow.writePaddingAttributes: " + e.toString();
  306. // getRtfFile().getLog().logWarning(msg);
  307. }
  308. if (gaph >= 0) {
  309. attrib.set(ATTR_RTF_15_TRGAPH, gaph);
  310. }
  311. }
  312. // write all padding attributes
  313. writeAttributes(attrib, ATTRIB_ROW_PADDING);
  314. }
  315. /**
  316. * @return true if the row is the first in the table
  317. */
  318. public boolean isFirstRow() {
  319. return (id == 1);
  320. }
  321. /**
  322. * @param cellId cell id to check
  323. * @return true if the cell is the highest cell
  324. */
  325. public boolean isHighestCell(int cellId) {
  326. return (highestCell == cellId) ? true : false;
  327. }
  328. /**
  329. *
  330. * @return Parent table of the row.
  331. */
  332. public RtfTable getTable() {
  333. RtfElement e = this;
  334. while (e.parent != null) {
  335. if (e.parent instanceof RtfTable) {
  336. return (RtfTable) e.parent;
  337. }
  338. e = e.parent;
  339. }
  340. return null;
  341. }
  342. }