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.

XWPFTableRow.java 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.xwpf.usermodel;
  16. import java.math.BigInteger;
  17. import java.util.ArrayList;
  18. import java.util.List;
  19. import org.apache.poi.util.Internal;
  20. import org.apache.xmlbeans.XmlCursor;
  21. import org.apache.xmlbeans.XmlObject;
  22. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTHeight;
  23. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTOnOff;
  24. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRow;
  25. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSdtCell;
  26. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTc;
  27. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTrPr;
  28. import org.openxmlformats.schemas.wordprocessingml.x2006.main.STOnOff;
  29. /**
  30. * A row within an {@link XWPFTable}. Rows mostly just have
  31. * sizings and stylings, the interesting content lives inside
  32. * the child {@link XWPFTableCell}s
  33. */
  34. public class XWPFTableRow {
  35. private CTRow ctRow;
  36. private XWPFTable table;
  37. private List<XWPFTableCell> tableCells;
  38. public XWPFTableRow(CTRow row, XWPFTable table) {
  39. this.table = table;
  40. this.ctRow = row;
  41. getTableCells();
  42. }
  43. @Internal
  44. public CTRow getCtRow() {
  45. return ctRow;
  46. }
  47. /**
  48. * create a new XWPFTableCell and add it to the tableCell-list of this tableRow
  49. * @return the newly created XWPFTableCell
  50. */
  51. public XWPFTableCell createCell() {
  52. XWPFTableCell tableCell = new XWPFTableCell(ctRow.addNewTc(), this, table.getBody());
  53. tableCells.add(tableCell);
  54. return tableCell;
  55. }
  56. public XWPFTableCell getCell(int pos) {
  57. if (pos >= 0 && pos < ctRow.sizeOfTcArray()) {
  58. return getTableCells().get(pos);
  59. }
  60. return null;
  61. }
  62. public void removeCell(int pos) {
  63. if (pos >= 0 && pos < ctRow.sizeOfTcArray()) {
  64. tableCells.remove(pos);
  65. }
  66. }
  67. /**
  68. * adds a new TableCell at the end of this tableRow
  69. */
  70. public XWPFTableCell addNewTableCell(){
  71. CTTc cell = ctRow.addNewTc();
  72. XWPFTableCell tableCell = new XWPFTableCell(cell, this, table.getBody());
  73. tableCells.add(tableCell);
  74. return tableCell;
  75. }
  76. /**
  77. * This element specifies the height of the current table row within the
  78. * current table. This height shall be used to determine the resulting
  79. * height of the table row, which may be absolute or relative (depending on
  80. * its attribute values). If omitted, then the table row shall automatically
  81. * resize its height to the height required by its contents (the equivalent
  82. * of an hRule value of auto).
  83. *
  84. * @param height
  85. */
  86. public void setHeight(int height) {
  87. CTTrPr properties = getTrPr();
  88. CTHeight h = properties.sizeOfTrHeightArray() == 0 ? properties.addNewTrHeight() : properties.getTrHeightArray(0);
  89. h.setVal(new BigInteger("" + height));
  90. }
  91. /**
  92. * This element specifies the height of the current table row within the
  93. * current table. This height shall be used to determine the resulting
  94. * height of the table row, which may be absolute or relative (depending on
  95. * its attribute values). If omitted, then the table row shall automatically
  96. * resize its height to the height required by its contents (the equivalent
  97. * of an hRule value of auto).
  98. *
  99. * @return height
  100. */
  101. public int getHeight() {
  102. CTTrPr properties = getTrPr();
  103. return properties.sizeOfTrHeightArray() == 0 ? 0 : properties.getTrHeightArray(0).getVal().intValue();
  104. }
  105. private CTTrPr getTrPr() {
  106. return (ctRow.isSetTrPr()) ? ctRow.getTrPr() : ctRow.addNewTrPr();
  107. }
  108. public XWPFTable getTable(){
  109. return table;
  110. }
  111. /**
  112. * create and return a list of all XWPFTableCell
  113. * who belongs to this row
  114. * @return a list of {@link XWPFTableCell}
  115. */
  116. public List<ICell> getTableICells(){
  117. List<ICell> cells = new ArrayList<ICell>();
  118. //Can't use ctRow.getTcList because that only gets table cells
  119. //Can't use ctRow.getSdtList because that only gets sdts that are at cell level
  120. XmlCursor cursor = ctRow.newCursor();
  121. cursor.selectPath("./*");
  122. while (cursor.toNextSelection()) {
  123. XmlObject o = cursor.getObject();
  124. if (o instanceof CTTc){
  125. cells.add(new XWPFTableCell((CTTc)o, this, table.getBody()));
  126. } else if (o instanceof CTSdtCell) {
  127. cells.add(new XWPFSDTCell((CTSdtCell)o, this, table.getBody()));
  128. }
  129. }
  130. return cells;
  131. }
  132. /**
  133. * create and return a list of all XWPFTableCell
  134. * who belongs to this row
  135. * @return a list of {@link XWPFTableCell}
  136. */
  137. @SuppressWarnings("deprecation")
  138. public List<XWPFTableCell> getTableCells(){
  139. if(tableCells == null){
  140. List<XWPFTableCell> cells = new ArrayList<XWPFTableCell>();
  141. for (CTTc tableCell : ctRow.getTcArray()) {
  142. cells.add(new XWPFTableCell(tableCell, this, table.getBody()));
  143. }
  144. //TODO: it is possible to have an SDT that contains a cell in within a row
  145. //need to modify this code so that it pulls out SDT wrappers around cells, too.
  146. this.tableCells = cells;
  147. }
  148. return tableCells;
  149. }
  150. /**
  151. * returns the XWPFTableCell which belongs to the CTTC cell
  152. * if there is no XWPFTableCell which belongs to the parameter CTTc cell null will be returned
  153. */
  154. public XWPFTableCell getTableCell(CTTc cell) {
  155. for(int i=0; i<tableCells.size(); i++){
  156. if (tableCells.get(i).getCTTc() == cell)
  157. return tableCells.get(i);
  158. }
  159. return null;
  160. }
  161. /**
  162. * This attribute controls whether to allow table rows to split across pages.
  163. * The logic for this attribute is a little unusual: a true value means
  164. * DON'T allow rows to split, false means allow rows to split.
  165. * @param split - if true, don't allow rows to be split. If false, allow
  166. * rows to be split.
  167. */
  168. public void setCantSplitRow(boolean split) {
  169. CTTrPr trpr = getTrPr();
  170. CTOnOff onoff = trpr.addNewCantSplit();
  171. onoff.setVal(split ? STOnOff.ON : STOnOff.OFF);
  172. }
  173. /**
  174. * Return true if the "can't split row" value is true. The logic for this
  175. * attribute is a little unusual: a TRUE value means DON'T allow rows to
  176. * split, FALSE means allow rows to split.
  177. * @return true if rows can't be split, false otherwise.
  178. */
  179. public boolean isCantSplitRow() {
  180. boolean isCant = false;
  181. CTTrPr trpr = getTrPr();
  182. if (trpr.sizeOfCantSplitArray() > 0) {
  183. CTOnOff onoff = trpr.getCantSplitArray(0);
  184. isCant = onoff.getVal().equals(STOnOff.ON);
  185. }
  186. return isCant;
  187. }
  188. /**
  189. * This attribute controls whether to repeat a table's header row at the top
  190. * of a table split across pages.
  191. * @param repeat - if TRUE, repeat header row at the top of each page of table;
  192. * if FALSE, don't repeat header row.
  193. */
  194. public void setRepeatHeader(boolean repeat) {
  195. CTTrPr trpr = getTrPr();
  196. CTOnOff onoff = trpr.addNewTblHeader();
  197. onoff.setVal(repeat ? STOnOff.ON : STOnOff.OFF);
  198. }
  199. /**
  200. * Return true if a table's header row should be repeated at the top of a
  201. * table split across pages.
  202. * @return true if table's header row should be repeated at the top of each
  203. * page of table, false otherwise.
  204. */
  205. public boolean isRepeatHeader() {
  206. boolean repeat = false;
  207. CTTrPr trpr = getTrPr();
  208. if (trpr.sizeOfTblHeaderArray() > 0) {
  209. CTOnOff rpt = trpr.getTblHeaderArray(0);
  210. repeat = rpt.getVal().equals(STOnOff.ON);
  211. }
  212. return repeat;
  213. }
  214. }