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

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